#format python """ReverseComplement using largely one reading """ import sys class Sequence: def __init__(self, seq='', name=''): self._seq = seq self._name = name def get_seq(self): return self._seq def get_name(self): return self._name def Read_fasta(filename): file = open(filename, 'r') sequence = '' seq_tot = [] # sequence and name is saved in list data name = [] while 1: line = file.readline() line = line.rstrip() if not line: break if line[0] == '>': name.append(line[1:]) line = '' seq_tot.append(sequence) sequence = '' # For another sequence reading sequence = sequence + line sequence.upper() seq_tot.append(sequence) del seq_tot[0] # At first time, delete first empty item return Sequence(seq_tot, name) def trDnaCompl(seqclass): DnaCompl = {'A':'T', 'T':'A', 'G':'C', 'C':'G'} trname = seqclass.get_name() seq = seqclass.get_seq() trseq = [] for sequence in seq: y = '' for back in range(len(sequence)-1, -1, -1): y = y + DnaCompl[sequence[back]] trseq.append(y) return Sequence(trseq, trname) def Write_fasta(seqclass, wfile): names = seqclass.get_name() seqs = seqclass.get_seq() file = open(wfile, 'w') i = 0 for name in names: file.write('>'); file.write(name) file.write('_reverse_complenent\n') seqseq = seqs[i] j = 0 while seqseq[j:j+1] != '': seqline = seqseq[j:j+60] file.write(seqline); file.write('\n') j = j + 60 i = i + 1 file.close def main(rfile, wfile): read = Read_fasta(rfile) # Reading function trdna = trDnaCompl(read) # Reverse Complement conversion function Write_fasta(trdna, wfile) # Writing script if __name__ == '__main__': rfile, wfile = sys.argv[1:3] main(rfile, wfile)