1 """ReverseComplement using largely one reading 
   2 """
   3 
   4 import sys
   5 
   6 class Sequence:
   7     def __init__(self, seq='', name=''):
   8         self._seq = seq
   9         self._name = name
  10     def get_seq(self):
  11         return self._seq
  12     def get_name(self):
  13         return self._name
  14 
  15 
  16 def Read_fasta(filename):
  17     file = open(filename, 'r')
  18     sequence = ''
  19     seq_tot = []      # sequence and name is saved in list data
  20     name = []
  21     while 1:
  22         line = file.readline()
  23         line = line.rstrip()
  24         if not line:
  25             break
  26         if line[0] == '>':
  27             name.append(line[1:])
  28             line = ''
  29             seq_tot.append(sequence)
  30             sequence = ''              # For another sequence reading
  31         sequence = sequence + line
  32         sequence.upper()
  33     seq_tot.append(sequence)
  34     del seq_tot[0]                     # At first time, delete first empty item
  35     return Sequence(seq_tot, name)
  36 
  37 
  38 def trDnaCompl(seqclass):
  39     DnaCompl = {'A':'T', 'T':'A', 'G':'C', 'C':'G'}
  40     trname = seqclass.get_name()
  41     seq = seqclass.get_seq()
  42     trseq = []
  43     for sequence in seq:
  44         y = ''
  45         for back in range(len(sequence)-1, -1, -1):
  46             y = y + DnaCompl[sequence[back]]
  47         trseq.append(y)
  48     return Sequence(trseq, trname)
  49 
  50 
  51 def Write_fasta(seqclass, wfile):
  52     names = seqclass.get_name()
  53     seqs = seqclass.get_seq()
  54     file = open(wfile, 'w')
  55     i = 0
  56     for name in names:
  57         file.write('>'); file.write(name)
  58         file.write('_reverse_complenent\n')
  59         seqseq = seqs[i]
  60         j = 0
  61         while seqseq[j:j+1] != '':
  62             seqline = seqseq[j:j+60]
  63             file.write(seqline); file.write('\n')
  64             j = j + 60
  65             
  66         i = i + 1
  67     file.close
  68 
  69 
  70 def main(rfile, wfile):
  71     read = Read_fasta(rfile)       # Reading function
  72     trdna = trDnaCompl(read)       # Reverse Complement conversion function
  73     Write_fasta(trdna, wfile)      # Writing script
  74 
  75 
  76 if __name__ == '__main__':
  77     rfile, wfile = sys.argv[1:3]
  78     main(rfile, wfile)