annotate translate_bed_sequences.py @ 0:57e586ee821e

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