1 """ReverseComplement using OOP iterator
   2 """
   3 
   4 import sys, string
   5 
   6 dictDnaComp = {'A':'T', 'T':'A', 'G':'C', 'C':'G'}
   7 
   8 class FastaRecord:
   9     def __init__(self, colwidth=60):
  10         self.title = ''
  11         self.sequence = ''
  12         self._colwidth = colwidth
  13  
  14     def __str__(self):
  15         s = []
  16         s.append('>%s' % self.title)
  17         i = 0
  18         while i < len(self.sequence):
  19             s.append(self.sequence[i:i+self._colwidth])
  20             i = i + self._colwidth
  21         return string.join(s, '\n')
  22 
  23     def cvtRevComp(self):
  24         newSeq = ''
  25         for index in range(len(self.sequence)-1, 0, -1):
  26             newSeq += dictDnaComp[self.sequence[index]]
  27         self.sequence = newSeq
  28 
  29 class FastaParser:
  30     def __init__(self, filename):
  31         self.file = open(filename, 'r')
  32         self._saved = []
  33     
  34     def next(self):
  35         lines = []
  36         while(1):
  37             line = self.file.readline()
  38             if not line:
  39                 break
  40             if lines and line[0]=='>':
  41                 self.saveLine(line)
  42                 break                            
  43             lines.append(line.strip())
  44         
  45         if not lines:
  46             return None
  47 
  48         record = FastaRecord()
  49         if lines[0][0] == '>':
  50             record.title = lines[0][1:]
  51             lines = lines[1:]
  52         else:
  53             record.title = self._saved[0][1:]
  54             self._saved = self._saved[1:]
  55         record.sequence = string.join(lines, '')
  56         return record
  57 
  58     def saveLine(self, line):
  59         self._saved += [line]
  60 
  61 def main(infilename, outfilename):
  62     try:
  63         outfile = open(outfilename, 'w')
  64     except:
  65         print "Error: unable open file"
  66         sys.exit()
  67     my = FastaParser(infilename)
  68     while 1:
  69         curRecord = my.next()
  70         if curRecord is None: break
  71         curRecord.cvtRevComp()
  72         outfile.write(str(curRecord))
  73 
  74 
  75 if __name__ == '__main__': 
  76     rfile, ofile = sys.argv[1:3]
  77     main(rfile, ofile)
web biohackers.net