annotate translate_bed_sequences.py @ 0:d328db400280 draft default tip

Uploaded
author jjohnson
date Wed, 05 Feb 2014 09:27:54 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
d328db400280 Uploaded
jjohnson
parents:
diff changeset
1 #!/usr/bin/env python
d328db400280 Uploaded
jjohnson
parents:
diff changeset
2 """
d328db400280 Uploaded
jjohnson
parents:
diff changeset
3 #
d328db400280 Uploaded
jjohnson
parents:
diff changeset
4 #------------------------------------------------------------------------------
d328db400280 Uploaded
jjohnson
parents:
diff changeset
5 # University of Minnesota
d328db400280 Uploaded
jjohnson
parents:
diff changeset
6 # Copyright 2014, Regents of the University of Minnesota
d328db400280 Uploaded
jjohnson
parents:
diff changeset
7 #------------------------------------------------------------------------------
d328db400280 Uploaded
jjohnson
parents:
diff changeset
8 # Author:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
9 #
d328db400280 Uploaded
jjohnson
parents:
diff changeset
10 # James E Johnson
d328db400280 Uploaded
jjohnson
parents:
diff changeset
11 #
d328db400280 Uploaded
jjohnson
parents:
diff changeset
12 #------------------------------------------------------------------------------
d328db400280 Uploaded
jjohnson
parents:
diff changeset
13 """
d328db400280 Uploaded
jjohnson
parents:
diff changeset
14
d328db400280 Uploaded
jjohnson
parents:
diff changeset
15 """
d328db400280 Uploaded
jjohnson
parents:
diff changeset
16 Input: BED file (12 column) + 13th sequence column appended by extract_genomic_dna
d328db400280 Uploaded
jjohnson
parents:
diff changeset
17 Output: Fasta of 3-frame translations of the spliced sequence
d328db400280 Uploaded
jjohnson
parents:
diff changeset
18
d328db400280 Uploaded
jjohnson
parents:
diff changeset
19 """
d328db400280 Uploaded
jjohnson
parents:
diff changeset
20
d328db400280 Uploaded
jjohnson
parents:
diff changeset
21 import sys,re,os.path
d328db400280 Uploaded
jjohnson
parents:
diff changeset
22 import optparse
d328db400280 Uploaded
jjohnson
parents:
diff changeset
23 from optparse import OptionParser
d328db400280 Uploaded
jjohnson
parents:
diff changeset
24 from Bio.Seq import reverse_complement, transcribe, back_transcribe, translate
d328db400280 Uploaded
jjohnson
parents:
diff changeset
25
d328db400280 Uploaded
jjohnson
parents:
diff changeset
26 class BedEntry( object ):
d328db400280 Uploaded
jjohnson
parents:
diff changeset
27 def __init__(self, line):
d328db400280 Uploaded
jjohnson
parents:
diff changeset
28 self.line = line
d328db400280 Uploaded
jjohnson
parents:
diff changeset
29 try:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
30 (chrom,chromStart,chromEnd,name,score,strand,thickStart,thickEnd,itemRgb,blockCount,blockSizes,blockStarts,seq) = line.split('\t')[0:13]
d328db400280 Uploaded
jjohnson
parents:
diff changeset
31 self.chrom = chrom
d328db400280 Uploaded
jjohnson
parents:
diff changeset
32 self.chromStart = int(chromStart)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
33 self.chromEnd = int(chromEnd)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
34 self.name = name
d328db400280 Uploaded
jjohnson
parents:
diff changeset
35 self.score = int(score)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
36 self.strand = strand
d328db400280 Uploaded
jjohnson
parents:
diff changeset
37 self.thickStart = int(thickStart)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
38 self.thickEnd = int(thickEnd)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
39 self.itemRgb = itemRgb
d328db400280 Uploaded
jjohnson
parents:
diff changeset
40 self.blockCount = int(blockCount)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
41 self.blockSizes = [int(x) for x in blockSizes.split(',')]
d328db400280 Uploaded
jjohnson
parents:
diff changeset
42 self.blockStarts = [int(x) for x in blockStarts.split(',')]
d328db400280 Uploaded
jjohnson
parents:
diff changeset
43 self.seq = seq
d328db400280 Uploaded
jjohnson
parents:
diff changeset
44 except Exception, e:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
45 print >> sys.stderr, "Unable to read Bed entry" % e
d328db400280 Uploaded
jjohnson
parents:
diff changeset
46 exit(1)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
47 def get_splice_junctions(self):
d328db400280 Uploaded
jjohnson
parents:
diff changeset
48 splice_juncs = []
d328db400280 Uploaded
jjohnson
parents:
diff changeset
49 for i in range(self.blockCount - 1):
d328db400280 Uploaded
jjohnson
parents:
diff changeset
50 splice_junc = "%s:%d_%d" % (self.chrom, self.chromStart + self.blockSizes[i], self.chromStart + self.blockStarts[i+1])
d328db400280 Uploaded
jjohnson
parents:
diff changeset
51 splice_juncs.append(splice_junc)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
52 return splice_juncs
d328db400280 Uploaded
jjohnson
parents:
diff changeset
53 def get_exon_seqs(self):
d328db400280 Uploaded
jjohnson
parents:
diff changeset
54 exons = []
d328db400280 Uploaded
jjohnson
parents:
diff changeset
55 for i in range(self.blockCount):
d328db400280 Uploaded
jjohnson
parents:
diff changeset
56 # splice_junc = "%s:%d_%d" % (self.chrom, self.chromStart + self.blockSizes[i], self.chromStart + self.blockStarts[i+1])
d328db400280 Uploaded
jjohnson
parents:
diff changeset
57 exons.append(self.seq[self.blockStarts[i]:self.blockStarts[i] + self.blockSizes[i]])
d328db400280 Uploaded
jjohnson
parents:
diff changeset
58 if self.strand == '-': #reverse complement
d328db400280 Uploaded
jjohnson
parents:
diff changeset
59 exons.reverse()
d328db400280 Uploaded
jjohnson
parents:
diff changeset
60 for i,s in enumerate(exons):
d328db400280 Uploaded
jjohnson
parents:
diff changeset
61 exons[i] = reverse_complement(s)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
62 return exons
d328db400280 Uploaded
jjohnson
parents:
diff changeset
63 def get_spliced_seq(self):
d328db400280 Uploaded
jjohnson
parents:
diff changeset
64 seq = ''.join(self.get_exon_seqs())
d328db400280 Uploaded
jjohnson
parents:
diff changeset
65 return seq
d328db400280 Uploaded
jjohnson
parents:
diff changeset
66 def get_translation(self,sequence=None):
d328db400280 Uploaded
jjohnson
parents:
diff changeset
67 translation = None
d328db400280 Uploaded
jjohnson
parents:
diff changeset
68 seq = sequence if sequence else self.get_spliced_seq()
d328db400280 Uploaded
jjohnson
parents:
diff changeset
69 if seq:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
70 seqlen = len(seq) / 3 * 3;
d328db400280 Uploaded
jjohnson
parents:
diff changeset
71 if seqlen >= 3:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
72 translation = translate(seq[:seqlen])
d328db400280 Uploaded
jjohnson
parents:
diff changeset
73 return translation
d328db400280 Uploaded
jjohnson
parents:
diff changeset
74 def get_translations(self):
d328db400280 Uploaded
jjohnson
parents:
diff changeset
75 translations = []
d328db400280 Uploaded
jjohnson
parents:
diff changeset
76 seq = self.get_spliced_seq()
d328db400280 Uploaded
jjohnson
parents:
diff changeset
77 if seq:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
78 for i in range(3):
d328db400280 Uploaded
jjohnson
parents:
diff changeset
79 translation = self.get_translation(sequence=seq[i:])
d328db400280 Uploaded
jjohnson
parents:
diff changeset
80 if translation:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
81 translations.append(translation)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
82 return translations
d328db400280 Uploaded
jjohnson
parents:
diff changeset
83 ## [[start,end,seq],[start,end,seq],[start,end,seq]]
d328db400280 Uploaded
jjohnson
parents:
diff changeset
84 ## filter: ignore translation if stop codon in first exon after ignore_left_bp
d328db400280 Uploaded
jjohnson
parents:
diff changeset
85 def get_filterd_translations(self,untrimmed=False,filtering=True,ignore_left_bp=0,ignore_right_bp=0):
d328db400280 Uploaded
jjohnson
parents:
diff changeset
86 translations = [None,None,None]
d328db400280 Uploaded
jjohnson
parents:
diff changeset
87 seq = self.get_spliced_seq()
d328db400280 Uploaded
jjohnson
parents:
diff changeset
88 ignore = (ignore_left_bp if self.strand == '+' else ignore_right_bp) / 3
d328db400280 Uploaded
jjohnson
parents:
diff changeset
89 block_sum = sum(self.blockSizes)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
90 exon_sizes = self.blockSizes
d328db400280 Uploaded
jjohnson
parents:
diff changeset
91 if self.strand == '-':
d328db400280 Uploaded
jjohnson
parents:
diff changeset
92 exon_sizes.reverse()
d328db400280 Uploaded
jjohnson
parents:
diff changeset
93 splice_sites = [sum(exon_sizes[:x]) / 3 for x in range(1,len(exon_sizes))]
d328db400280 Uploaded
jjohnson
parents:
diff changeset
94 junc = splice_sites[0] if len(splice_sites) > 0 else exon_sizes[0]
d328db400280 Uploaded
jjohnson
parents:
diff changeset
95 if seq:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
96 for i in range(3):
d328db400280 Uploaded
jjohnson
parents:
diff changeset
97 translation = self.get_translation(sequence=seq[i:])
d328db400280 Uploaded
jjohnson
parents:
diff changeset
98 if translation:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
99 tstart = 0
d328db400280 Uploaded
jjohnson
parents:
diff changeset
100 tstop = len(translation)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
101 if not untrimmed:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
102 tstart = translation.rfind('*',0,junc) + 1
d328db400280 Uploaded
jjohnson
parents:
diff changeset
103 stop = translation.find('*',junc)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
104 tstop = stop if stop >= 0 else len(translation)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
105 if filtering and tstart > ignore:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
106 continue
d328db400280 Uploaded
jjohnson
parents:
diff changeset
107 trimmed = translation[tstart:tstop]
d328db400280 Uploaded
jjohnson
parents:
diff changeset
108 #get genomic locations for start and end
d328db400280 Uploaded
jjohnson
parents:
diff changeset
109 offset = (block_sum - i) % 3
d328db400280 Uploaded
jjohnson
parents:
diff changeset
110 if self.strand == '+':
d328db400280 Uploaded
jjohnson
parents:
diff changeset
111 chromStart = self.chromStart + i + (tstart * 3)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
112 chromEnd = self.chromEnd - offset - (len(translation) - tstop) * 3
d328db400280 Uploaded
jjohnson
parents:
diff changeset
113 else:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
114 chromStart = self.chromStart + offset + (len(translation) - tstop) * 3
d328db400280 Uploaded
jjohnson
parents:
diff changeset
115 chromEnd = self.chromEnd - i - (tstart * 3)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
116 translations[i] = [chromStart,chromEnd,trimmed]
d328db400280 Uploaded
jjohnson
parents:
diff changeset
117 return translations
d328db400280 Uploaded
jjohnson
parents:
diff changeset
118 def get_seq_id(self,seqtype='unk:unk',reference='',frame=None):
d328db400280 Uploaded
jjohnson
parents:
diff changeset
119 ## Ensembl fasta ID format
d328db400280 Uploaded
jjohnson
parents:
diff changeset
120 # >ID SEQTYPE:STATUS LOCATION GENE TRANSCRIPT
d328db400280 Uploaded
jjohnson
parents:
diff changeset
121 # >ENSP00000328693 pep:splice chromosome:NCBI35:1:904515:910768:1 gene:ENSG00000158815:transcript:ENST00000328693 gene_biotype:protein_coding transcript_biotype:protein_coding
d328db400280 Uploaded
jjohnson
parents:
diff changeset
122 frame_name = ''
d328db400280 Uploaded
jjohnson
parents:
diff changeset
123 chromStart = self.chromStart
d328db400280 Uploaded
jjohnson
parents:
diff changeset
124 chromEnd = self.chromEnd
d328db400280 Uploaded
jjohnson
parents:
diff changeset
125 strand = 1 if self.strand == '+' else -1
d328db400280 Uploaded
jjohnson
parents:
diff changeset
126 if frame != None:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
127 block_sum = sum(self.blockSizes)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
128 offset = (block_sum - frame) % 3
d328db400280 Uploaded
jjohnson
parents:
diff changeset
129 frame_name = '_' + str(frame + 1)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
130 if self.strand == '+':
d328db400280 Uploaded
jjohnson
parents:
diff changeset
131 chromStart += frame
d328db400280 Uploaded
jjohnson
parents:
diff changeset
132 chromEnd -= offset
d328db400280 Uploaded
jjohnson
parents:
diff changeset
133 else:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
134 chromStart += offset
d328db400280 Uploaded
jjohnson
parents:
diff changeset
135 chromEnd -= frame
d328db400280 Uploaded
jjohnson
parents:
diff changeset
136 location = "chromosome:%s:%s:%s:%s:%s" % (reference,self.chrom,chromStart,chromEnd,strand)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
137 seq_id = "%s%s %s %s" % (self.name,frame_name,seqtype,location)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
138 return seq_id
d328db400280 Uploaded
jjohnson
parents:
diff changeset
139 def get_line(self, start_offset = 0, end_offset = 0):
d328db400280 Uploaded
jjohnson
parents:
diff changeset
140 if start_offset or end_offset:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
141 s_offset = start_offset if start_offset else 0
d328db400280 Uploaded
jjohnson
parents:
diff changeset
142 e_offset = end_offset if end_offset else 0
d328db400280 Uploaded
jjohnson
parents:
diff changeset
143 if s_offset > self.chromStart:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
144 s_offset = self.chromStart
d328db400280 Uploaded
jjohnson
parents:
diff changeset
145 chrStart = self.chromStart - s_offset
d328db400280 Uploaded
jjohnson
parents:
diff changeset
146 chrEnd = self.chromEnd + e_offset
d328db400280 Uploaded
jjohnson
parents:
diff changeset
147 blkSizes = self.blockSizes
d328db400280 Uploaded
jjohnson
parents:
diff changeset
148 blkSizes[0] += s_offset
d328db400280 Uploaded
jjohnson
parents:
diff changeset
149 blkSizes[-1] += e_offset
d328db400280 Uploaded
jjohnson
parents:
diff changeset
150 blkStarts = self.blockStarts
d328db400280 Uploaded
jjohnson
parents:
diff changeset
151 for i in range(1,self.blockCount):
d328db400280 Uploaded
jjohnson
parents:
diff changeset
152 blkStarts[i] += s_offset
d328db400280 Uploaded
jjohnson
parents:
diff changeset
153 items = [str(x) for x in [self.chrom,chrStart,chrEnd,self.name,self.score,self.strand,self.thickStart,self.thickEnd,self.itemRgb,self.blockCount,','.join([str(x) for x in blkSizes]),','.join([str(x) for x in blkStarts])]]
d328db400280 Uploaded
jjohnson
parents:
diff changeset
154 return '\t'.join(items) + '\n'
d328db400280 Uploaded
jjohnson
parents:
diff changeset
155 return self.line
d328db400280 Uploaded
jjohnson
parents:
diff changeset
156
d328db400280 Uploaded
jjohnson
parents:
diff changeset
157 def __main__():
d328db400280 Uploaded
jjohnson
parents:
diff changeset
158 #Parse Command Line
d328db400280 Uploaded
jjohnson
parents:
diff changeset
159 parser = optparse.OptionParser()
d328db400280 Uploaded
jjohnson
parents:
diff changeset
160 parser.add_option( '-i', '--input', dest='input', help='BED file (tophat junctions.bed) with sequence column added' )
d328db400280 Uploaded
jjohnson
parents:
diff changeset
161 parser.add_option( '-o', '--output', dest='output', help='Translations of spliced sequence')
d328db400280 Uploaded
jjohnson
parents:
diff changeset
162 parser.add_option( '-b', '--bed_format', dest='bed_format', action='store_true', default=False, help='Append translations to bed file instead of fasta' )
d328db400280 Uploaded
jjohnson
parents:
diff changeset
163 parser.add_option( '-S', '--seqtype', dest='seqtype', default='pep:splice', help='SEQTYPE:STATUS for fasta ID line' )
d328db400280 Uploaded
jjohnson
parents:
diff changeset
164 parser.add_option( '-R', '--reference', dest='reference', default=None, help='Genome Reference Name for fasta ID location ' )
d328db400280 Uploaded
jjohnson
parents:
diff changeset
165 parser.add_option( '-Q', '--score_name', dest='score_name', default=None, help='include in the fasta ID line score_name:score ' )
d328db400280 Uploaded
jjohnson
parents:
diff changeset
166 parser.add_option( '-l', '--leading_bp', dest='leading_bp', type='int', default=None, help='leading number of base pairs to ignore when filtering' )
d328db400280 Uploaded
jjohnson
parents:
diff changeset
167 parser.add_option( '-t', '--trailing_bp', dest='trailing_bp', type='int', default=None, help='trailing number of base pairs to ignore when filtering' )
d328db400280 Uploaded
jjohnson
parents:
diff changeset
168 parser.add_option( '-U', '--unfiltered', dest='filtering', action='store_false', default=True, help='Do NOT filterout translation with stop codon in the first exon' )
d328db400280 Uploaded
jjohnson
parents:
diff changeset
169 parser.add_option( '-u', '--untrimmed', dest='untrimmed', action='store_true', default=False, help='Do NOT trim from splice site to stop codon' )
d328db400280 Uploaded
jjohnson
parents:
diff changeset
170 parser.add_option( '-L', '--min_length', dest='min_length', type='int', default=None, help='Minimun length (to first stop codon)' )
d328db400280 Uploaded
jjohnson
parents:
diff changeset
171 parser.add_option( '-M', '--max_stop_codons', dest='max_stop_codons', type='int', default=None, help='Filter out translations with more than max_stop_codons' )
d328db400280 Uploaded
jjohnson
parents:
diff changeset
172 parser.add_option( '-d', '--debug', dest='debug', action='store_true', default=False, help='Turn on wrapper debugging to stdout' )
d328db400280 Uploaded
jjohnson
parents:
diff changeset
173 (options, args) = parser.parse_args()
d328db400280 Uploaded
jjohnson
parents:
diff changeset
174 # Input files
d328db400280 Uploaded
jjohnson
parents:
diff changeset
175 if options.input != None:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
176 try:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
177 inputPath = os.path.abspath(options.input)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
178 inputFile = open(inputPath, 'r')
d328db400280 Uploaded
jjohnson
parents:
diff changeset
179 except Exception, e:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
180 print >> sys.stderr, "failed: %s" % e
d328db400280 Uploaded
jjohnson
parents:
diff changeset
181 exit(2)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
182 else:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
183 inputFile = sys.stdin
d328db400280 Uploaded
jjohnson
parents:
diff changeset
184 # Output files
d328db400280 Uploaded
jjohnson
parents:
diff changeset
185 outFile = None
d328db400280 Uploaded
jjohnson
parents:
diff changeset
186 if options.output == None:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
187 #write to stdout
d328db400280 Uploaded
jjohnson
parents:
diff changeset
188 outFile = sys.stdout
d328db400280 Uploaded
jjohnson
parents:
diff changeset
189 else:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
190 try:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
191 outPath = os.path.abspath(options.output)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
192 outFile = open(outPath, 'w')
d328db400280 Uploaded
jjohnson
parents:
diff changeset
193 except Exception, e:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
194 print >> sys.stderr, "failed: %s" % e
d328db400280 Uploaded
jjohnson
parents:
diff changeset
195 exit(3)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
196 leading_bp = 0
d328db400280 Uploaded
jjohnson
parents:
diff changeset
197 trailing_bp = 0
d328db400280 Uploaded
jjohnson
parents:
diff changeset
198 if options.leading_bp:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
199 if options.leading_bp >= 0:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
200 leading_bp = options.leading_bp
d328db400280 Uploaded
jjohnson
parents:
diff changeset
201 else:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
202 print >> sys.stderr, "failed: leading_bp must be positive"
d328db400280 Uploaded
jjohnson
parents:
diff changeset
203 exit(5)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
204 if options.trailing_bp:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
205 if options.trailing_bp >= 0:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
206 trailing_bp = options.trailing_bp
d328db400280 Uploaded
jjohnson
parents:
diff changeset
207 else:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
208 print >> sys.stderr, "failed: trailing_bp must be positive"
d328db400280 Uploaded
jjohnson
parents:
diff changeset
209 exit(5)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
210 # Scan bed file
d328db400280 Uploaded
jjohnson
parents:
diff changeset
211 try:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
212 for i, line in enumerate( inputFile ):
d328db400280 Uploaded
jjohnson
parents:
diff changeset
213 if line.startswith('track'):
d328db400280 Uploaded
jjohnson
parents:
diff changeset
214 if outFile and options.bed_format:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
215 outFile.write(line)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
216 continue
d328db400280 Uploaded
jjohnson
parents:
diff changeset
217 entry = BedEntry(line)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
218 strand = 1 if entry.strand == '+' else -1
d328db400280 Uploaded
jjohnson
parents:
diff changeset
219 translations = entry.get_translations()
d328db400280 Uploaded
jjohnson
parents:
diff changeset
220 if options.debug:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
221 exon_seqs = entry.get_exon_seqs()
d328db400280 Uploaded
jjohnson
parents:
diff changeset
222 exon_sizes = [len(seq) for seq in exon_seqs]
d328db400280 Uploaded
jjohnson
parents:
diff changeset
223 splice_sites = [sum(exon_sizes[:x]) / 3 for x in range(1,len(exon_sizes))]
d328db400280 Uploaded
jjohnson
parents:
diff changeset
224 print >> sys.stderr, entry.name
d328db400280 Uploaded
jjohnson
parents:
diff changeset
225 print >> sys.stderr, line.rstrip('\r\n')
d328db400280 Uploaded
jjohnson
parents:
diff changeset
226 print >> sys.stderr, "exons: %s" % exon_seqs
d328db400280 Uploaded
jjohnson
parents:
diff changeset
227 print >> sys.stderr, "%s" % splice_sites
d328db400280 Uploaded
jjohnson
parents:
diff changeset
228 for i,translation in enumerate(translations):
d328db400280 Uploaded
jjohnson
parents:
diff changeset
229 print >> sys.stderr, "frame %d: %s" % (i+1,translation)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
230 print >> sys.stderr, "splice: %s" % (''.join(['^' if (((j*3)+i)/3) in splice_sites else '-' for j in range(len(translation))]))
d328db400280 Uploaded
jjohnson
parents:
diff changeset
231 print >> sys.stderr, ""
d328db400280 Uploaded
jjohnson
parents:
diff changeset
232 if options.bed_format:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
233 tx_entry = "%s\t%s\n" % (line.rstrip('\r\n'),'\t'.join(translations))
d328db400280 Uploaded
jjohnson
parents:
diff changeset
234 outFile.write(tx_entry)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
235 else:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
236 translations = entry.get_filterd_translations(untrimmed=options.untrimmed,filtering=options.filtering,ignore_left_bp=leading_bp,ignore_right_bp=trailing_bp)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
237 for i,tx in enumerate(translations):
d328db400280 Uploaded
jjohnson
parents:
diff changeset
238 if tx:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
239 (chromStart,chromEnd,translation) = tx
d328db400280 Uploaded
jjohnson
parents:
diff changeset
240 if options.min_length != None and len(translation) < options.min_length:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
241 continue
d328db400280 Uploaded
jjohnson
parents:
diff changeset
242 if options.max_stop_codons != None and translation.count('*') > options.max_stop_codons:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
243 continue
d328db400280 Uploaded
jjohnson
parents:
diff changeset
244 frame_name = '_%s' % (i + 1)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
245 location = "chromosome:%s:%s:%s:%s:%s" % (options.reference,entry.chrom,chromStart,chromEnd,strand)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
246 score = " %s:%s" % (options.score_name,entry.score) if options.score_name else ''
d328db400280 Uploaded
jjohnson
parents:
diff changeset
247 seq_id = "%s%s %s %s%s" % (entry.name,frame_name,options.seqtype,location, score)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
248 outFile.write(">%s\n" % seq_id)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
249 outFile.write(translation)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
250 outFile.write('\n')
d328db400280 Uploaded
jjohnson
parents:
diff changeset
251 except Exception, e:
d328db400280 Uploaded
jjohnson
parents:
diff changeset
252 print >> sys.stderr, "failed: Error reading %s - %s" % (options.input if options.input else 'stdin',e)
d328db400280 Uploaded
jjohnson
parents:
diff changeset
253
d328db400280 Uploaded
jjohnson
parents:
diff changeset
254 if __name__ == "__main__" : __main__()
d328db400280 Uploaded
jjohnson
parents:
diff changeset
255