home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / biology / esee109e.zip / RESTORE.PAS < prev   
Pascal/Delphi Source File  |  1991-10-01  |  9KB  |  238 lines

  1. Algorithm to read Esee Save-file
  2. 1. Determine file name/open it.
  3. 2. Abort if file size<=0 or I/O error
  4.  
  5. <all reads below done byte-wise with Pascal blockread function>
  6.  
  7. 3. Determine version:
  8.    first byte=#252 for version  1.05-1.09
  9.      21 seqs x 18kb
  10.    first byte=#253 version 2.0 (soon to be released)
  11.      46 seqs x 9.5kb
  12.    Abort if one of above not found.
  13. 4. Read next two bytes=number of sequences as integer
  14. 5. For i := 1 to number of sequences get records. Check after each one
  15.     to see if there's enough memory.
  16. 6. Each record <check for i/o error after each read,
  17.                  abort process if any error>:
  18.       seqlen: 2 bytes (integer)
  19.       sequence data: seqlen bytes
  20.                      end-of sequence character is ascii #1 (happy face)
  21.                      and it included in count
  22.       seqname:65 bytes (characters)
  23.       linesum:6 bytes (pascal real)
  24.       start  :6 bytes (pascal real)
  25.               Start is the position to start numbering on print-out,
  26.               for example a sequence in a promotor may begin with a
  27.               negative number. Make sure, when counting, to avoid
  28.               position 0 since by convention it is not used.
  29.       sequence type:1 byte (an upper case character)
  30.                       valid types: NPTA
  31.       numoff:1 byte (boolean) user may have set numbering off within Esee or
  32.              the program might have done (by default type A sequences have
  33.             numbering off when they are created)
  34.        reve:1 byte (boolean) has the user selected reverse (3'->5') orientation?
  35.             One scenario for this is the display of double stranded DNA. The lower
  36.             sequence is created by complementing, followed by reverse.
  37. +_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_
  38. Now here's a fragment of the actual code that I use.
  39. You might want to replace some of the tests of allowable sequence
  40.  length, memory use with limits relevant to BoxShade, rather than Esee
  41. +_+_+_++++_+_+_+++_+_+_+_+_+_+_+_+_
  42. ...
  43. ...
  44. (*Definitions borrowed from Main program*)
  45. (*in order to make the algorithm easier to understand*)
  46. (*I have no idea if you understand Pascal, you're probably
  47.   in the C generation  :-) *)
  48.  
  49. CONST
  50.   realseqlen=18500; (*soon to be released version 9900 *)
  51.   logo:char=#253;   (*#254 in next version, allow either *)
  52.   maxseqs=21;      (*46 in next version limit should be set by boxshade*)
  53. TYPE
  54.   seqarray=array(1..realseqlen) of char;
  55.   seqrec=record
  56.             seqlen:integer;  (*total seqlen ... others to follow*)
  57.             sarr:seqarray;
  58.             name:string[64];         linesum,start:real;
  59.             kind:char;               NUMOFF,reve:BOOLEAN
  60.          end; (*seqrec*)
  61.    seqpoint=^seqrec;
  62. VAR
  63.  stamp:byte;
  64.  spnt:array[1..nseqs] of seqpoint;
  65.  inname:string;
  66.  rfile:file; (*untyped file*)
  67.  nseq,ierr:integer;
  68. Procedure Flash; (*just a fancey prompt message*)
  69. begin end;
  70. Procedure Insrept; (*clear flashed message*)
  71. begin end;
  72. Procedure Emesg; (*flash an error message*)
  73. begin end;
  74.  
  75.  
  76.  (*a given sequence record can be addressed as follows:
  77.      spnt[1]^.sarr[1] is the first character of the first sequence *)
  78.  (* you can also use:
  79.       with spnt[i]^ do
  80.         begin
  81.            sarr[1]:=1;
  82.            kind:='P'
  83.        end;
  84.  *)
  85.  
  86. Procedure Restore;
  87. var rseq,rs,i:integer;                                            (*restore*)
  88.     needed:word; namelen:byte; logic:boolean;
  89.  
  90. Procedure Fadeout; (*tidy up if restore fails*)
  91. begin end;
  92.  
  93.  
  94.   procedure getrecord;
  95.   var j:byte; tempname:string[64];
  96.   begin (*getrecord*)
  97.       with spnt[rs]^ do
  98.       begin
  99.       (*$i-*)
  100.          ierr:=1; (*only a succesful read sets it to 0*)
  101.          fillchar(sarr,realseqlen,dot);
  102.          blockread(rfile,seqlen,2);
  103.          if ioresult<>0 then exit;
  104.          (*fix for long old save files*)
  105.          if seqlen>maxseqlen
  106.             then begin
  107.                      blockread(rfile,sarr,maxseqlen);
  108.                      sarr[maxseqlen]:=#1;
  109.                      blockread(rfile,temp,seqlen-maxseqlen);
  110. (*put overflow in temp to throw away*)
  111. (* say 18,000 - 9499 = 8500 so temp should be big enough*)
  112.                      seqlen:=maxseqlen
  113.                  end
  114.              else
  115.                  blockread(rfile,sarr,seqlen);
  116.          if ioresult<>0 then exit;
  117.          blockread(rfile,tempname,namelen); (*was 11 in older versions now is 65*)
  118.          name:=copy(tempname,1,namelen);
  119.          if ioresult<>0 then exit;
  120.          blockread(rfile,linesum,6);
  121.          if ioresult<>0 then exit;
  122.          blockread(rfile,start,6);
  123.          if ioresult<>0 then exit;
  124.          blockread(rfile,kind,1);
  125.           if namelen=11
  126.          then  case kind of
  127.                   'P':kind:='T';        'S':kind:='P';
  128.                   'A':kind:='A';       else kind:='N'
  129.                 end;
  130.          if ioresult<>0 then exit;
  131.          blockread(rfile,numoff,1);
  132.          if ioresult<>0 then exit;
  133.          blockread(rfile,reve,1);
  134.          if ioresult<>0 then exit;
  135.          ierr:=0
  136.       end
  137.       (*$i+*)
  138.   end;  (*getrecord*)
  139.  
  140.  
  141. BEGIN     (*restore*)
  142.      intry:=0; namelen:=65;
  143.       (*$i-*)
  144.     repeat (*until ioerr=0*)
  145. (*Get file name*)
  146.          flash;
  147.          write(output,'RESTORE ... What is the name of the Saved file?    ....   <RET> to abort');
  148.          gotoxy(1,2); write(output,'>'); clreol;  readln(iname);
  149.          if iname='' then begin insrept; exit end;
  150.  
  151. (*OPEN file HERE..........................v-INDICATES READ RECORDS=1 BYTE*)
  152.          assign(rfile,iname); reset(rfile,1);
  153.          ierr:=ioresult;    intry:=intry+1;
  154.          if ierr<>0 then begin
  155.                               mestr:='**  FILE NOT FOUND   **';
  156.                               emesg
  157.                           end
  158.       until (ierr=0) or (intry>2);
  159.  
  160. (*Get here if valid file found and successfully opened*)
  161.  (*$i+*)
  162.      if intry>2
  163.        then
  164.          begin
  165.  mestr:='** Look you''ve had three tries now and you haven''t hit one yet. I quit !!!';
  166.               emesg;  exit
  167.          end;
  168.        (*$i-*)
  169.        if sizeof(rfile)<=0 then exit;
  170. (*check for logo stamp in data file*)
  171.        blockread(rfile,stamp,1);
  172.        if ioresult<>0 then exit;
  173. {       if stamp<>logo then (*logo is currently char(254)*)}
  174.       if not (stamp in [logo,#253]) then
  175.          begin (*now always bailout, as advertised*)
  176.             close(rfile);
  177.             mestr:='WARNING: That file''s format is not correct! If possible, resave it immediately';
  178.             emesg; exit
  179.          end;
  180. (*Have to determine how many sequences before reading sequence records*)
  181.        blockread(rfile,rseq,2);
  182. (*Stuff here is to avoid trying to read more sequences than there
  183.   are facilities for 21 in early versions, 46 in next version.
  184.   It takes into account the number of sequences already in play (nseq)
  185. *)
  186.        if ioresult<>0 then exit;
  187.        if   ( (nseq+rseq) > maxseqs )
  188.        then
  189.         if (  (nseq>1) or  ( (nseq=1) and (spnt[1]^.seqlen>1)) )
  190.            then begin
  191.                    mestr:='There are too many sequences to restore that file';
  192.                    emesg; exit
  193.                 end;
  194.        rs:=0;
  195.  
  196. (*also check to see if there's one empty sequence-startup condition,
  197.   if so, get rid of it*)
  198.        if ((nseq=1) and (spnt[1]^.seqlen=1))
  199.        then begin dispose(spnt[nseq]); nseq:=0; rs:=1 end;
  200.  
  201. (*Is there enough memory for a all of the sequences?');
  202.        if  not enuffroom(rseq-1)
  203.         then
  204.           begin
  205.             mestr:='There''s not enough memory to restore that  file.';
  206.             fadeout; exit
  207.           end;
  208.        rs:=nseq;
  209.  
  210. (*Now read those sequece records*)
  211.        for i:=1 to rseq do
  212.          begin
  213.               if enuffroom(1)
  214.               then  begin
  215.                       inc(rs); new(spnt[rs]);
  216.                       getrecord (*set ierr=0 when successful*)
  217.                     end
  218.               else  begin
  219.                        mestr:='NOT enough memory .... ABORTING';
  220.                        fadeout;       exit
  221.                      end;
  222.               if ierr<>0 then
  223.                 begin
  224.                       mestr:='I/O error, is the file really a Saved file?     Operation Aborted.';
  225.                       fadeout;     exit
  226.                  end
  227.        end; (*end of reading*)
  228.        close(rfile);
  229.        if (ioresult<>0) then
  230.        begin
  231.           mestr:='I/O error on file close.  Command aborted.';
  232.           emesg; if nseq=0 then begin seqno:=1; create end; exit
  233.        end;
  234.        (*$i+*)
  235.  (*ETC*)
  236.  
  237. end;                                                             (*restore*)
  238.