Package Bio :: Package SeqIO :: Module FASTA
[hide private]
[frames] | no frames]

Source Code for Module Bio.SeqIO.FASTA

  1  # This code is part of the Biopython distribution and governed by its 
  2  # license.  Please see the LICENSE file that should have been included 
  3  # as part of this package. 
  4   
  5  import warnings 
  6  warnings.warn("Bio.SeqIO.FASTA is deprecated." \ 
  7                + " We hope the new code in Bio.SeqIO will be suitable for" \ 
  8                + " most users.  Please get in touch on the mailing lists if" \ 
  9                + " this (or its removal) causes any problems for you.", 
 10                DeprecationWarning) 
 11   
 12  import os, string 
 13  import Bio.Alphabet 
 14   
 15  from Bio.Seq import Seq 
 16  from Bio.SeqRecord import SeqRecord 
 17   
 18  # This can be made a lot faster by using infile.readlines! 
 19  # (Alas, that's a bit complicated to write.) 
 20   
21 -class FastaReader:
22 - def __init__(self, infile, alphabet = Bio.Alphabet.generic_alphabet):
23 self.infile = infile 24 self.alphabet = alphabet 25 26 # find the start of data 27 line = infile.readline() 28 while line and line[0] != ">": 29 line = infile.readline() 30 self._lookahead = line 31 32 self._n = 0
33
34 - def next(self):
35 self._n = self._n + 1 36 37 line = self._lookahead 38 if not line: 39 return None 40 41 # Like bioperl, I assume the first word is the name/id and the 42 # rest of the line (after the first whitespace) is a 43 # description. If there's only one word, it's the id. 44 x = string.split(line[1:].rstrip(), None, 1) 45 if len(x) == 1: 46 id = x[0] 47 desc = "" 48 else: 49 id, desc = x 50 51 lines = [] 52 line = self.infile.readline() 53 while line: 54 if line[0] == ">": 55 break 56 lines.append(line.rstrip()) 57 line = self.infile.readline() 58 59 self._lookahead = line 60 61 # Unlike bioperl, I assume whitespace is significant. 62 return SeqRecord(Seq(string.join(lines, ""), self.alphabet), 63 id = id, name = id, description = desc)
64
65 - def __getitem__(self, i):
66 # wrapper to the normal Python "for spam in list:" idiom 67 assert i == self._n # forward iteration only! 68 x = self.next() 69 if x is None: 70 raise IndexError, i 71 return x
72
73 -class FastaWriter:
74 - def __init__(self, outfile):
75 self.outfile = outfile
76
77 - def write(self, record):
78 id = record.id 79 assert os.linesep not in id 80 81 description = record.description 82 assert os.linesep not in description 83 84 self.outfile.write(">%s %s%s" % (id, description,os.linesep)) 85 86 data = record.seq.tostring() 87 for i in range(0, len(data), 60): 88 self.outfile.write(data[i:i+60] + os.linesep)
89 90
91 - def write_records(self, records):
92 # In general, can assume homogenous records... useful? 93 for record in records: 94 self.write(record)
95
96 - def close(self):
97 return self.outfile.close()
98
99 - def flush(self):
100 return self.outfile.flush()
101