Back to index

python-biopython  1.60
xbb_translations.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # Created: Wed Jun 21 15:53:22 2000
00003 # Last changed: Time-stamp: <00/12/02 15:56:27 thomas>
00004 # thomas@cbs.dtu.dk, http://www.cbs.dtu.dk/thomas
00005 # File: xbb_translations.py
00006 
00007 import sys
00008 import time
00009 
00010 sys.path.insert(0, '.')
00011 from Tkinter import *
00012 
00013 from Bio.Seq import reverse_complement, translate
00014 from Bio.SeqUtils import GC
00015 
00016 class xbb_translations:
00017     def __init__(self):
00018         ""
00019 
00020     def frame1(self, seq, translation_table = 1):
00021         return translate(seq, table=translation_table)
00022 
00023     def complement(self, seq):
00024         #TODO - use Seq methods instead of this hack:?
00025         return reverse_complement(seq)[::-1]
00026 
00027     def reverse(self, seq):
00028         return seq[::-1]
00029 
00030     def antiparallel(self, seq):
00031         return reverse_complement(seq)
00032     
00033     def frame(self, seq, frame, translation_table = 1):
00034         if not ((-3 <= frame <= -1) or (1 <= frame <= 3)):
00035             frame = 1
00036         if frame != 1 :
00037             raise NotImplementedError
00038             #TODO - Support the frame argument
00039             #The old code didn't, but I can guess from
00040             #the code the expected 1,2,3 for the forward
00041             #strands and -1,-2,-3 for the reverse.
00042         return translate(seq, table=translation_table)
00043 
00044     def header_nice(self, txt, seq):
00045         length = len(seq)
00046         if length > 20:
00047             short = '%s ... %s' % (seq[:10], seq[-10:])
00048         else:
00049             short = seq
00050             
00051         date = time.strftime('%y %b %d, %X', time.localtime(time.time()))
00052         res = '%s: %s, ' % (txt,date)
00053 
00054         for nt in ['a','t','g','c']:
00055             res += '%s:%d ' % (nt, seq.count(nt.upper()))
00056 
00057         res += '\nSequence: %s, %d nt, %0.2f %%GC\n' % (short.lower(),length, self.gc(seq))       
00058         res += '\n\n'
00059         return res
00060         
00061     def frame_nice(self, seq, frame, translation_table = 1):
00062         length = len(seq)
00063         protein = self.frame(seq, frame, translation_table)
00064         res = self.header_nice('Plus one frame translation',seq)
00065         for i in range(0,length,60):
00066             subseq = seq[i:i+60]
00067             p = i/3
00068             res += '%d/%d\n' % (i+1, i/3+1)
00069             res += '  '.join(map(None,protein[p:p+20])) + '\n'
00070             # seq
00071             res += subseq.lower() + '%5d %%\n' % int(self.gc(subseq))
00072 
00073         return res
00074     
00075     def gc(self, seq):
00076         """Returns a float between 0 and 100."""
00077         return GC(seq)
00078     
00079     def gcframe(self, seq, translation_table = 1):
00080         # always use uppercase nt-sequence !!
00081         comp = self.complement(seq)
00082         anti = self.reverse(comp)
00083         length = len(seq)
00084         frames = {}
00085         for i in range(0,3):
00086             frames[i+1]  = self.frame1(seq[i:], translation_table)
00087             frames[-(i+1)] = self.reverse(self.frame1(anti[i:], translation_table))
00088 
00089         res = self.header_nice('GCFrame', seq)
00090 
00091         for i in range(0,length,60):
00092             subseq = seq[i:i+60]
00093             csubseq = comp[i:i+60]
00094             p = i/3
00095             # + frames
00096             res += '%d/%d\n' % (i+1, i/3+1)
00097             res += '  ' + '  '.join(map(None,frames[3][p:p+20])) + '\n'
00098             res += ' ' + '  '.join(map(None,frames[2][p:p+20])) + '\n'
00099             res += '  '.join(map(None,frames[1][p:p+20])) + '\n'
00100             # seq
00101             res += subseq.lower() + '%5d %%\n' % int(self.gc(subseq))
00102             res += csubseq.lower() + '\n'
00103             # - frames
00104             res += '  '.join(map(None,frames[-2][p:p+20]))  +' \n'
00105             res += ' ' + '  '.join(map(None,frames[-1][p:p+20])) + '\n'
00106             res += '  ' + '  '.join(map(None,frames[-3][p:p+20])) + '\n\n'
00107             
00108             
00109         return res
00110         
00111 if __name__ == '__main__':
00112     #s = 'GCCCTTTCTTATTAGTGCTACCGCTAATAGGTAAATATGAAAAACCTTTG'
00113     s = 'ATTCCGGTTGATCCTGCCGGACCCGACCGCTATCGGGGTAGGGATAAGCCATGGGAGTCTTACACTCCCGGGTAAGGGAGTGTGGCGGACGGCTGAGTAACACGTGGCTAACCTACCCTCGGGACGGGGATAACCCCGGGAAACTGGGGATAATCCCCGATAGGGAAGGAGTCCTGGAATGGTTCCTTCCCTAAAGGGCTATAGGCTATTTCCCGTTTGTAGCCGCCCGAGGATGGGGCTACGGCCCATCAGGCTGTCGGTGGGGTAAAGGCCCACCGAACCTATAACGGGTAGGGGCCGTGGAAGCGGGAGCCTCCAGTTGGGCACTGAGACAAGGGCCCAGGCCCTACGGGGCGCACCAGGCGCGAAACGTCCCCAATGCGCGAAAGCGTGAGGGCGCTACCCCGAGTGCCTCCGCAAGGAGGCTTTTCCCCGCTCTAAAAAGGCGGGGGAATAAGCGGGGGGCAAGTCTGGTGTCAGCCGCCGCGGTAATACCAGCTCCGCGAGTGGTCGGGGTGATTACTGGGCCTAAAGCGCCTGTAGCCGGCCCACCAAGTCGCCCCTTAAAGTCCCCGGCTCAACCGGGGAACTGGGGGCGATACTGGTGGGCTAGGGGGCGGGAGAGGCGGGGGGTACTCCCGGAGTAGGGGCGAAATCCTTAGATACCGGGAGGACCACCAGTGGCGGAAGCGCCCCGCTA'
00114 
00115     test = xbb_translations()
00116 #     for i in range(0,4):
00117 #         print test.frame1(s[i:])
00118     #print s
00119     #print test.complement(s)
00120     print '============================================================'
00121     print test.gcframe(s)
00122     
00123 #     for i in Translate.unambiguous_dna_by_id.keys():
00124 #         print Translate.unambiguous_dna_by_id[i].table.names[0]
00125