#format python """ReverseComplement using OOP iterator """ import sys, string dictDnaComp = {'A':'T', 'T':'A', 'G':'C', 'C':'G'} class FastaRecord: def __init__(self, colwidth=60): self.title = '' self.sequence = '' self._colwidth = colwidth def __str__(self): s = [] s.append('>%s' % self.title) i = 0 while i < len(self.sequence): s.append(self.sequence[i:i+self._colwidth]) i = i + self._colwidth return string.join(s, '\n') def cvtRevComp(self): newSeq = '' for index in range(len(self.sequence)-1, 0, -1): newSeq += dictDnaComp[self.sequence[index]] self.sequence = newSeq class FastaParser: def __init__(self, filename): self.file = open(filename, 'r') self._saved = [] def next(self): lines = [] while(1): line = self.file.readline() if not line: break if lines and line[0]=='>': self.saveLine(line) break lines.append(line.strip()) if not lines: return None record = FastaRecord() if lines[0][0] == '>': record.title = lines[0][1:] lines = lines[1:] else: record.title = self._saved[0][1:] self._saved = self._saved[1:] record.sequence = string.join(lines, '') return record def saveLine(self, line): self._saved += [line] def main(infilename, outfilename): try: outfile = open(outfilename, 'w') except: print "Error: unable open file" sys.exit() my = FastaParser(infilename) while 1: curRecord = my.next() if curRecord is None: break curRecord.cvtRevComp() outfile.write(str(curRecord)) if __name__ == '__main__': rfile, ofile = sys.argv[1:3] main(rfile, ofile)