annotate translate_bed_sequences.py @ 5:c626a939eef7 draft default tip

Uploaded
author jjohnson
date Tue, 12 Jan 2016 14:38:03 -0500
parents 3b526a780849
children
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
5
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
22 import tempfile
0
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
23 import optparse
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
24 from optparse import OptionParser
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
25 from Bio.Seq import reverse_complement, transcribe, back_transcribe, translate
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
26
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
27 class BedEntry( object ):
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
28 def __init__(self, line):
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
29 self.line = line
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
30 try:
5
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
31 fields = line.rstrip('\r\n').split('\t')
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
32 (chrom,chromStart,chromEnd,name,score,strand,thickStart,thickEnd,itemRgb,blockCount,blockSizes,blockStarts) = fields[0:12]
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
33 seq = fields[12] if len(fields) > 12 else None
0
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
34 self.chrom = chrom
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
35 self.chromStart = int(chromStart)
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
36 self.chromEnd = int(chromEnd)
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
37 self.name = name
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
38 self.score = int(score)
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
39 self.strand = strand
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
40 self.thickStart = int(thickStart)
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
41 self.thickEnd = int(thickEnd)
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
42 self.itemRgb = itemRgb
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
43 self.blockCount = int(blockCount)
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
44 self.blockSizes = [int(x) for x in blockSizes.split(',')]
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
45 self.blockStarts = [int(x) for x in blockStarts.split(',')]
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
46 self.seq = seq
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
47 except Exception, e:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
48 print >> sys.stderr, "Unable to read Bed entry" % e
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
49 exit(1)
5
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
50 def __str__(self):
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
51 return '%s\t%d\t%d\t%s\t%d\t%s\t%d\t%d\t%s\t%d\t%s\t%s%s' % (
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
52 self.chrom, self.chromStart, self.chromEnd, self.name, self.score, self.strand, self.thickStart, self.thickEnd, self.itemRgb, self.blockCount,
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
53 ','.join([str(x) for x in self.blockSizes]),
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
54 ','.join([str(x) for x in self.blockStarts]),
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
55 '\t%s' % self.seq if self.seq else '')
0
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
56 def get_splice_junctions(self):
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
57 splice_juncs = []
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
58 for i in range(self.blockCount - 1):
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
59 splice_junc = "%s:%d_%d" % (self.chrom, self.chromStart + self.blockSizes[i], self.chromStart + self.blockStarts[i+1])
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
60 splice_juncs.append(splice_junc)
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
61 return splice_juncs
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
62 def get_exon_seqs(self):
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
63 exons = []
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
64 for i in range(self.blockCount):
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
65 # splice_junc = "%s:%d_%d" % (self.chrom, self.chromStart + self.blockSizes[i], self.chromStart + self.blockStarts[i+1])
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
66 exons.append(self.seq[self.blockStarts[i]:self.blockStarts[i] + self.blockSizes[i]])
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
67 if self.strand == '-': #reverse complement
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
68 exons.reverse()
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
69 for i,s in enumerate(exons):
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
70 exons[i] = reverse_complement(s)
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
71 return exons
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
72 def get_spliced_seq(self):
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
73 seq = ''.join(self.get_exon_seqs())
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
74 return seq
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
75 def get_translation(self,sequence=None):
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
76 translation = None
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
77 seq = sequence if sequence else self.get_spliced_seq()
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
78 if seq:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
79 seqlen = len(seq) / 3 * 3;
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
80 if seqlen >= 3:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
81 translation = translate(seq[:seqlen])
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
82 return translation
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
83 def get_translations(self):
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
84 translations = []
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
85 seq = self.get_spliced_seq()
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
86 if seq:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
87 for i in range(3):
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
88 translation = self.get_translation(sequence=seq[i:])
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
89 if translation:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
90 translations.append(translation)
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
91 return translations
5
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
92 ## (start,end)
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
93 def get_subrange(self,tstart,tstop):
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
94 chromStart = self.chromStart
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
95 chromEnd = self.chromEnd
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
96 r = range(self.blockCount)
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
97 if self.strand == '-':
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
98 r.reverse()
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
99 bStart = 0
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
100 for x in r:
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
101 bEnd = bStart + self.blockSizes[x]
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
102 if bStart <= tstart < bEnd:
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
103 if self.strand == '+':
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
104 chromStart = self.chromStart + self.blockStarts[x] + (tstart - bStart)
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
105 else:
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
106 chromEnd = self.chromStart + self.blockStarts[x] + (tstart - bStart)
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
107 if bStart <= tstop < bEnd:
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
108 if self.strand == '+':
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
109 chromEnd = self.chromStart + self.blockStarts[x] + (tstop - bStart)
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
110 else:
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
111 chromStart = self.chromStart + self.blockStarts[x] + self.blockSizes[x] - (tstop - bStart)
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
112 bStart += self.blockSizes[x]
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
113 return(chromStart,chromEnd)
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
114 #get the blocks for sub range
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
115 def get_blocks(self,chromStart,chromEnd):
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
116 tblockCount = 0
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
117 tblockSizes = []
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
118 tblockStarts = []
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
119 for x in range(self.blockCount):
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
120 bStart = self.chromStart + self.blockStarts[x]
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
121 bEnd = bStart + self.blockSizes[x]
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
122 if bStart > chromEnd:
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
123 break
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
124 if bEnd < chromStart:
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
125 continue
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
126 cStart = max(chromStart,bStart)
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
127 tblockStarts.append(cStart - chromStart)
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
128 tblockSizes.append(min(chromEnd,bEnd) - cStart)
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
129 tblockCount += 1
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
130 ## print >> sys.stderr, "tblockCount: %d tblockStarts: %s tblockSizes: %s" % (tblockCount,tblockStarts,tblockSizes)
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
131 return (tblockCount,tblockSizes,tblockStarts)
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
132 ## [(start,end,seq,blockCount,blockSizes,blockStarts),(start,end,seq,blockCount,blockSizes,blockStarts),(start,end,seq,blockCount,blockSizes,blockStarts)]
0
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
133 ## filter: ignore translation if stop codon in first exon after ignore_left_bp
5
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
134 def get_filterd_translations(self,untrimmed=False,filtering=True,ignore_left_bp=0,ignore_right_bp=0,debug=False):
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
135 translations = [None,None,None,None,None,None]
0
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
136 seq = self.get_spliced_seq()
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
137 ignore = (ignore_left_bp if self.strand == '+' else ignore_right_bp) / 3
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
138 block_sum = sum(self.blockSizes)
5
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
139 exon_sizes = [x for x in self.blockSizes]
0
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
140 if self.strand == '-':
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
141 exon_sizes.reverse()
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
142 splice_sites = [sum(exon_sizes[:x]) / 3 for x in range(1,len(exon_sizes))]
5
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
143 if debug:
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
144 print >> sys.stderr, "splice_sites: %s" % splice_sites
0
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
145 junc = splice_sites[0] if len(splice_sites) > 0 else exon_sizes[0]
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
146 if seq:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
147 for i in range(3):
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
148 translation = self.get_translation(sequence=seq[i:])
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
149 if translation:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
150 tstart = 0
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
151 tstop = len(translation)
5
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
152 offset = (block_sum - i) % 3
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
153 if debug:
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
154 print >> sys.stderr, "frame: %d\ttstart: %d tstop: %d offset: %d\t%s" % (i,tstart,tstop,offset,translation)
0
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
155 if not untrimmed:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
156 tstart = translation.rfind('*',0,junc) + 1
2
359addb9b9d4 Do not include the stop codon when filtering
Jim Johnson <jj@umn.edu>
parents: 0
diff changeset
157 stop = translation.find('*',junc)
359addb9b9d4 Do not include the stop codon when filtering
Jim Johnson <jj@umn.edu>
parents: 0
diff changeset
158 tstop = stop if stop >= 0 else len(translation)
5
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
159 offset = (block_sum - i) % 3
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
160 trimmed = translation[tstart:tstop]
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
161 if debug:
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
162 print >> sys.stderr, "frame: %d\ttstart: %d tstop: %d offset: %d\t%s" % (i,tstart,tstop,offset,trimmed)
0
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
163 if filtering and tstart > ignore:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
164 continue
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
165 #get genomic locations for start and end
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
166 if self.strand == '+':
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
167 chromStart = self.chromStart + i + (tstart * 3)
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
168 chromEnd = self.chromEnd - offset - (len(translation) - tstop) * 3
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
169 else:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
170 chromStart = self.chromStart + offset + (len(translation) - tstop) * 3
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
171 chromEnd = self.chromEnd - i - (tstart * 3)
5
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
172 #get the blocks for this translation
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
173 (tblockCount,tblockSizes,tblockStarts) = self.get_blocks(chromStart,chromEnd)
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
174 translations[i] = (chromStart,chromEnd,trimmed,tblockCount,tblockSizes,tblockStarts)
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
175 if debug:
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
176 print >> sys.stderr, "tblockCount: %d tblockStarts: %s tblockSizes: %s" % (tblockCount,tblockStarts,tblockSizes)
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
177 # translations[i] = (chromStart,chromEnd,trimmed,tblockCount,tblockSizes,tblockStarts)
0
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
178 return translations
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
179 def get_seq_id(self,seqtype='unk:unk',reference='',frame=None):
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
180 ## Ensembl fasta ID format
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
181 # >ID SEQTYPE:STATUS LOCATION GENE TRANSCRIPT
3
3b526a780849 Change default seqtype from pep:novel to pep:splice
Jim Johnson <jj@umn.edu>
parents: 2
diff changeset
182 # >ENSP00000328693 pep:splice chromosome:NCBI35:1:904515:910768:1 gene:ENSG00000158815:transcript:ENST00000328693 gene_biotype:protein_coding transcript_biotype:protein_coding
0
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
183 frame_name = ''
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
184 chromStart = self.chromStart
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
185 chromEnd = self.chromEnd
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
186 strand = 1 if self.strand == '+' else -1
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
187 if frame != None:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
188 block_sum = sum(self.blockSizes)
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
189 offset = (block_sum - frame) % 3
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
190 frame_name = '_' + str(frame + 1)
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
191 if self.strand == '+':
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
192 chromStart += frame
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
193 chromEnd -= offset
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
194 else:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
195 chromStart += offset
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
196 chromEnd -= frame
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
197 location = "chromosome:%s:%s:%s:%s:%s" % (reference,self.chrom,chromStart,chromEnd,strand)
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
198 seq_id = "%s%s %s %s" % (self.name,frame_name,seqtype,location)
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
199 return seq_id
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
200 def get_line(self, start_offset = 0, end_offset = 0):
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
201 if start_offset or end_offset:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
202 s_offset = start_offset if start_offset else 0
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
203 e_offset = end_offset if end_offset else 0
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
204 if s_offset > self.chromStart:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
205 s_offset = self.chromStart
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
206 chrStart = self.chromStart - s_offset
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
207 chrEnd = self.chromEnd + e_offset
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
208 blkSizes = self.blockSizes
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
209 blkSizes[0] += s_offset
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
210 blkSizes[-1] += e_offset
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
211 blkStarts = self.blockStarts
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
212 for i in range(1,self.blockCount):
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
213 blkStarts[i] += s_offset
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
214 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
215 return '\t'.join(items) + '\n'
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
216 return self.line
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
217
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
218 def __main__():
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
219 #Parse Command Line
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
220 parser = optparse.OptionParser()
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
221 parser.add_option( '-i', '--input', dest='input', help='BED file (tophat junctions.bed) with sequence column added' )
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
222 parser.add_option( '-o', '--output', dest='output', help='Translations of spliced sequence')
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
223 parser.add_option( '-b', '--bed_format', dest='bed_format', action='store_true', default=False, help='Append translations to bed file instead of fasta' )
5
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
224 parser.add_option( '-D', '--fa_db', dest='fa_db', default=None, help='Prefix DB identifier for fasta ID line, e.g. generic' )
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
225 parser.add_option( '-s', '--fa_sep', dest='fa_sep', default='|', help='fasta ID separator defaults to pipe char, e.g. generic|ProtID|description' )
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
226 parser.add_option( '-B', '--bed', dest='bed', default=None, help='Output a bed file with added 13th column having translation' )
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
227 parser.add_option( '-G', '--gff3', dest='gff', default=None, help='Output translations to a GFF3 file' )
3
3b526a780849 Change default seqtype from pep:novel to pep:splice
Jim Johnson <jj@umn.edu>
parents: 2
diff changeset
228 parser.add_option( '-S', '--seqtype', dest='seqtype', default='pep:splice', help='SEQTYPE:STATUS for fasta ID line' )
5
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
229 parser.add_option( '-P', '--id_prefix', dest='id_prefix', default='', help='prefix for the sequence ID' )
0
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
230 parser.add_option( '-R', '--reference', dest='reference', default=None, help='Genome Reference Name for fasta ID location ' )
5
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
231 parser.add_option( '-r', '--refsource', dest='refsource', default=None, help='Source for Genome Reference, e.g. Ensembl, UCSC, or NCBI' )
0
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
232 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
233 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
234 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
235 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
236 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
237 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
238 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
239 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
240 (options, args) = parser.parse_args()
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
241 # Input files
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
242 if options.input != None:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
243 try:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
244 inputPath = os.path.abspath(options.input)
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
245 inputFile = open(inputPath, 'r')
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
246 except Exception, e:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
247 print >> sys.stderr, "failed: %s" % e
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
248 exit(2)
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
249 else:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
250 inputFile = sys.stdin
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
251 # Output files
5
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
252 bed_fh = None
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
253 gff_fh = None
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
254 gff_fa_file = None
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
255 gff_fa = None
0
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
256 outFile = None
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
257 if options.output == None:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
258 #write to stdout
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
259 outFile = sys.stdout
5
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
260 if options.gff:
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
261 gff_fa_file = tempfile.NamedTemporaryFile(prefix='gff_fasta_',suffix=".fa",dir=os.getcwd()).name
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
262 gff_fa = open(gff_fa_file,'w')
0
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
263 else:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
264 try:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
265 outPath = os.path.abspath(options.output)
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
266 outFile = open(outPath, 'w')
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
267 except Exception, e:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
268 print >> sys.stderr, "failed: %s" % e
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
269 exit(3)
5
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
270 if options.gff:
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
271 gff_fa_file = outPath
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
272 if options.bed:
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
273 bed_fh = open(options.bed,'w')
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
274 bed_fh.write('track name="%s" description="%s" \n' % ('novel_junctioni_translations','test'))
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
275 if options.gff:
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
276 gff_fh = open(options.gff,'w')
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
277 gff_fh.write("##gff-version 3.2.1\n")
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
278 if options.reference:
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
279 gff_fh.write("##genome-build %s %s\n" % (options.refsource if options.refsource else 'unknown', options.reference))
0
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
280 leading_bp = 0
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
281 trailing_bp = 0
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
282 if options.leading_bp:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
283 if options.leading_bp >= 0:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
284 leading_bp = options.leading_bp
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
285 else:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
286 print >> sys.stderr, "failed: leading_bp must be positive"
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
287 exit(5)
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
288 if options.trailing_bp:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
289 if options.trailing_bp >= 0:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
290 trailing_bp = options.trailing_bp
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
291 else:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
292 print >> sys.stderr, "failed: trailing_bp must be positive"
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
293 exit(5)
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
294 # Scan bed file
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
295 try:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
296 for i, line in enumerate( inputFile ):
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
297 if line.startswith('track'):
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
298 if outFile and options.bed_format:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
299 outFile.write(line)
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
300 continue
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
301 entry = BedEntry(line)
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
302 strand = 1 if entry.strand == '+' else -1
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
303 translations = entry.get_translations()
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
304 if options.debug:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
305 exon_seqs = entry.get_exon_seqs()
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
306 exon_sizes = [len(seq) for seq in exon_seqs]
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
307 splice_sites = [sum(exon_sizes[:x]) / 3 for x in range(1,len(exon_sizes))]
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
308 print >> sys.stderr, entry.name
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
309 print >> sys.stderr, line.rstrip('\r\n')
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
310 print >> sys.stderr, "exons: %s" % exon_seqs
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
311 print >> sys.stderr, "%s" % splice_sites
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
312 for i,translation in enumerate(translations):
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
313 print >> sys.stderr, "frame %d: %s" % (i+1,translation)
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
314 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
315 print >> sys.stderr, ""
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
316 if options.bed_format:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
317 tx_entry = "%s\t%s\n" % (line.rstrip('\r\n'),'\t'.join(translations))
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
318 outFile.write(tx_entry)
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
319 else:
5
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
320 translations = entry.get_filterd_translations(untrimmed=options.untrimmed,filtering=options.filtering,ignore_left_bp=leading_bp,ignore_right_bp=trailing_bp,debug=options.debug)
0
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
321 for i,tx in enumerate(translations):
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
322 if tx:
5
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
323 (chromStart,chromEnd,translation,blockCount,blockSizes,blockStarts) = tx
0
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
324 if options.min_length != None and len(translation) < options.min_length:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
325 continue
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
326 if options.max_stop_codons != None and translation.count('*') > options.max_stop_codons:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
327 continue
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
328 frame_name = '_%s' % (i + 1)
5
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
329 pep_id = "%s%s%s" % (options.id_prefix,entry.name,frame_name)
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
330 if bed_fh:
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
331 bed_fh.write('%s\t%d\t%d\t%s\t%d\t%s\t%d\t%d\t%s\t%d\t%s\t%s\t%s\n' % (str(entry.chrom),chromStart,chromEnd,pep_id,entry.score,entry.strand,chromStart,chromEnd,entry.itemRgb,blockCount,','.join([str(x) for x in blockSizes]),','.join([str(x) for x in blockStarts]),translation))
0
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
332 location = "chromosome:%s:%s:%s:%s:%s" % (options.reference,entry.chrom,chromStart,chromEnd,strand)
5
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
333 if blockCount:
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
334 location += " blockCount:%d blockSizes:%s blockStarts:%s" % (blockCount,','.join([str(x) for x in blockSizes]),','.join([str(x) for x in blockStarts]))
0
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
335 score = " %s:%s" % (options.score_name,entry.score) if options.score_name else ''
5
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
336 seq_description = "%s %s%s" % (options.seqtype, location, score)
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
337 seq_id = "%s " % pep_id
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
338 if options.fa_db:
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
339 seq_id = "%s%s%s%s" % (options.fa_db,options.fa_sep,pep_id,options.fa_sep)
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
340 fa_id = "%s%s" % (seq_id,seq_description)
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
341 fa_entry = ">%s\n%s\n" % (fa_id,translation)
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
342 outFile.write(fa_entry)
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
343 if gff_fh:
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
344 if gff_fa:
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
345 gff_fa.write(fa_entry)
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
346 gff_fh.write("##sequence-region %s %d %d\n" % (entry.chrom,chromStart + 1,chromEnd - 1))
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
347 gff_fh.write("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%d\tID=%s\n" % (entry.chrom,'splice_junc','gene',chromStart + 1,chromEnd - 1,entry.score,entry.strand,0,pep_id))
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
348 for x in range(blockCount):
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
349 start = chromStart+blockStarts[x] + 1
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
350 end = start + blockSizes[x] - 1
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
351 phase = (3 - sum(blockSizes[:x]) % 3) % 3
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
352 gff_fh.write("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%d\tParent=%s;ID=%s_%d\n" % (entry.chrom,'splice_junc','CDS',start,end,entry.score,entry.strand,phase,pep_id,pep_id,x))
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
353 """
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
354 ##gff-version 3
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
355 ##sequence-region 19 1 287484
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
356 19 MassSpec peptide 282299 287484 10.0 - 0 ID=TEARLSFYSGHSSFGMYCMVFLALYVQ
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
357 19 MassSpec CDS 287474 287484 . - 0 Parent=TEARLSFYSGHSSFGMYCMVFLALYVQ;transcript_id=ENST00000269812
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
358 19 MassSpec CDS 282752 282809 . - 1 Parent=TEARLSFYSGHSSFGMYCMVFLALYVQ;transcript_id=ENST00000269812
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
359 19 MassSpec CDS 282299 282310 . - 0 Parent=TEARLSFYSGHSSFGMYCMVFLALYVQ;transcript_id=ENST00000269812
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
360 """
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
361 if bed_fh:
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
362 bed_fh.close()
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
363 if gff_fh:
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
364 if gff_fa:
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
365 gff_fa.close()
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
366 else:
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
367 outFile.close()
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
368 gff_fa = open(gff_fa_file,'r')
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
369 gff_fh.write("##FASTA\n")
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
370 for i, line in enumerate(gff_fa):
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
371 gff_fh.write(line)
c626a939eef7 Uploaded
jjohnson
parents: 3
diff changeset
372 gff_fh.close()
0
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
373 except Exception, e:
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
374 print >> sys.stderr, "failed: Error reading %s - %s" % (options.input if options.input else 'stdin',e)
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
375
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
376 if __name__ == "__main__" : __main__()
57e586ee821e Uploaded
jjohnson
parents:
diff changeset
377