annotate ensembl_cdna_translate.py @ 7:d59e3ce10e74 draft

Uploaded
author jjohnson
date Wed, 13 Dec 2017 11:15:34 -0500
parents b7f2f5e3390c
children 5c92d0be6514
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
1 #!/usr/bin/env python
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
2 """
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
3 #
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
4 #------------------------------------------------------------------------------
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
5 # University of Minnesota
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
6 # Copyright 2017, Regents of the University of Minnesota
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
7 #------------------------------------------------------------------------------
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
8 # Author:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
9 #
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
10 # James E Johnson
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
11 #
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
12 #------------------------------------------------------------------------------
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
13 """
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
14
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
15 import argparse
7
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
16 import re
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
17 import sys
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
18 from time import sleep
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
19
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
20 from Bio.Seq import translate
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
21
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
22 import requests
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
23
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
24 import digest
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
25
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
26
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
27 server = "https://rest.ensembl.org"
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
28 ext = "/info/assembly/homo_sapiens?"
7
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
29 max_region = 4000000
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
30
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
31
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
32 def ensembl_rest(ext, headers):
7
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
33 if True: print >> sys.stderr, "%s" % ext
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
34 r = requests.get(server+ext, headers=headers)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
35 if r.status_code == 429:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
36 print >> sys.stderr, "response headers: %s\n" % r.headers
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
37 if 'Retry-After' in r.headers:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
38 sleep(r.headers['Retry-After'])
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
39 r = requests.get(server+ext, headers=headers)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
40 if not r.ok:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
41 r.raise_for_status()
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
42 return r
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
43
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
44
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
45 def get_species():
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
46 results = dict()
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
47 ext = "/info/species"
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
48 req_header = {"Content-Type": "application/json"}
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
49 r = ensembl_rest(ext, req_header)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
50 for species in r.json()['species']:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
51 results[species['name']] = species
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
52 print >> sys.stdout,\
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
53 "%s\t%s\t%s\t%s\t%s"\
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
54 % (species['name'], species['common_name'],
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
55 species['display_name'],
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
56 species['strain'],
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
57 species['taxon_id'])
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
58 return results
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
59
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
60
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
61 def get_biotypes(species):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
62 biotypes = []
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
63 ext = "/info/biotypes/%s?" % species
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
64 req_header = {"Content-Type": "application/json"}
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
65 r = ensembl_rest(ext, req_header)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
66 for entry in r.json():
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
67 if 'biotype' in entry:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
68 biotypes.append(entry['biotype'])
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
69 return biotypes
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
70
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
71
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
72 def get_toplevel(species):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
73 coord_systems = dict()
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
74 ext = "/info/assembly/%s?" % species
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
75 req_header = {"Content-Type": "application/json"}
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
76 r = ensembl_rest(ext, req_header)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
77 toplevel = r.json()
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
78 for seq in toplevel['top_level_region']:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
79 if seq['coord_system'] not in coord_systems:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
80 coord_systems[seq['coord_system']] = dict()
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
81 coord_system = coord_systems[seq['coord_system']]
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
82 coord_system[seq['name']] = int(seq['length'])
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
83 return coord_systems
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
84
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
85
7
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
86 def get_transcripts_bed(species, refseq, start, length, strand='', params=None):
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
87 bed = []
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
88 param = params if params else ''
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
89 req_header = {"Content-Type": "text/x-bed"}
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
90 regions = range(start, length, max_region)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
91 if not regions or regions[-1] < length:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
92 regions.append(length)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
93 for end in regions[1:]:
7
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
94 ext = "/overlap/region/%s/%s:%d-%d%s?feature=transcript;%s"\
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
95 % (species, refseq, start, end, strand, param)
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
96 start = end + 1
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
97 r = ensembl_rest(ext, req_header)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
98 if r.text:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
99 bed += r.text.splitlines()
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
100 return bed
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
101
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
102
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
103 def get_seq(id, seqtype,params=None):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
104 param = params if params else ''
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
105 ext = "/sequence/id/%s?type=%s;%s" % (id, seqtype,param)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
106 req_header = {"Content-Type": "text/plain"}
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
107 r = ensembl_rest(ext, req_header)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
108 return r.text
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
109
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
110
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
111 def get_cdna(id,params=None):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
112 return get_seq(id, 'cdna',params=params)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
113
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
114
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
115 def get_cds(id,params=None):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
116 return get_seq(id, 'cds',params=params)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
117
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
118
7
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
119 def get_transcript_haplotypes(species,transcript):
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
120 ext = "/transcript_haplotypes/%s/%s?aligned_sequences=1" % (species,transcript)
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
121 req_header = {"Content-Type" : "application/json"}
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
122 r = ensembl_rest(ext, req_header)
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
123 decoded = r.json()
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
124
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
125
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
126 def bed_from_line(line):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
127 fields = line.rstrip('\r\n').split('\t')
7
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
128 if len(fields) < 12:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
129 return None
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
130 (chrom, chromStart, chromEnd, name, score, strand,
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
131 thickStart, thickEnd, itemRgb,
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
132 blockCount, blockSizes, blockStarts) = fields[0:12]
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
133 bed_entry = BedEntry(chrom=chrom, chromStart=chromStart, chromEnd=chromEnd,
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
134 name=name, score=score, strand=strand,
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
135 thickStart=thickStart, thickEnd=thickEnd,
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
136 itemRgb=itemRgb,
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
137 blockCount=blockCount,
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
138 blockSizes=blockSizes.rstrip(','),
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
139 blockStarts=blockStarts.rstrip(','))
7
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
140 if len(fields) == 20:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
141 bed_entry.second_name = fields[12]
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
142 bed_entry.cds_start_status = fields[13]
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
143 bed_entry.cds_end_status = fields[14]
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
144 bed_entry.exon_frames = fields[15]
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
145 bed_entry.biotype = fields[16]
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
146 bed_entry.gene_name = fields[17]
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
147 bed_entry.second_gene_name = fields[18]
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
148 bed_entry.gene_type = fields[19]
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
149 return bed_entry
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
150
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
151
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
152 class BedEntry(object):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
153 def __init__(self, chrom=None, chromStart=None, chromEnd=None,
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
154 name=None, score=None, strand=None,
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
155 thickStart=None, thickEnd=None, itemRgb=None,
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
156 blockCount=None, blockSizes=None, blockStarts=None):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
157 self.chrom = chrom
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
158 self.chromStart = int(chromStart)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
159 self.chromEnd = int(chromEnd)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
160 self.name = name
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
161 self.score = int(score) if score is not None else 0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
162 self.strand = '-' if str(strand).startswith('-') else '+'
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
163 self.thickStart = int(thickStart) if thickStart else self.chromStart
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
164 self.thickEnd = int(thickEnd) if thickEnd else self.chromEnd
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
165 self.itemRgb = str(itemRgb) if itemRgb is not None else r'100,100,100'
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
166 self.blockCount = int(blockCount)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
167 if isinstance(blockSizes, str) or isinstance(blockSizes, unicode):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
168 self.blockSizes = [int(x) for x in blockSizes.split(',')]
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
169 elif isinstance(blockSizes, list):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
170 self.blockSizes = [int(x) for x in blockSizes]
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
171 else:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
172 self.blockSizes = blockSizes
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
173 if isinstance(blockStarts, str) or isinstance(blockSizes, unicode):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
174 self.blockStarts = [int(x) for x in blockStarts.split(',')]
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
175 elif isinstance(blockStarts, list):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
176 self.blockStarts = [int(x) for x in blockStarts]
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
177 else:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
178 self.blockStarts = blockStarts
7
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
179 self.second_name = None
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
180 self.cds_start_status = None
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
181 self.cds_end_status = None
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
182 self.exon_frames = None
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
183 self.biotype = None
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
184 self.gene_name = None
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
185 self.second_gene_name = None
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
186 self.gene_type = None
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
187 self.seq = None
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
188 self.pep = None
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
189
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
190 def __str__(self):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
191 return '%s\t%d\t%d\t%s\t%d\t%s\t%d\t%d\t%s\t%d\t%s\t%s' % (
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
192 self.chrom, self.chromStart, self.chromEnd,
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
193 self.name, self.score, self.strand,
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
194 self.thickStart, self.thickEnd, str(self.itemRgb), self.blockCount,
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
195 ','.join([str(x) for x in self.blockSizes]),
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
196 ','.join([str(x) for x in self.blockStarts]))
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
197
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
198 # (start, end)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
199 def get_subrange(self, tstart, tstop, debug=False):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
200 chromStart = self.chromStart
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
201 chromEnd = self.chromEnd
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
202 if debug:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
203 print >> sys.stderr, "%s" % (str(self))
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
204 r = range(self.blockCount)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
205 if self.strand == '-':
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
206 r.reverse()
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
207 bStart = 0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
208 bEnd = 0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
209 for x in r:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
210 bEnd = bStart + self.blockSizes[x]
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
211 if bStart <= tstart < bEnd:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
212 if self.strand == '+':
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
213 chromStart = self.chromStart + self.blockStarts[x] +\
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
214 (tstart - bStart)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
215 else:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
216 chromEnd = self.chromStart + self.blockStarts[x] +\
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
217 self.blockSizes[x] - (tstart - bStart)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
218 if bStart <= tstop < bEnd:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
219 if self.strand == '+':
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
220 chromEnd = self.chromStart + self.blockStarts[x] +\
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
221 (tstop - bStart)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
222 else:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
223 chromStart = self.chromStart + self.blockStarts[x] +\
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
224 self.blockSizes[x] - (tstop - bStart)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
225 if debug:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
226 print >> sys.stderr,\
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
227 "%3d %s\t%d\t%d\t%d\t%d\t%d\t%d"\
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
228 % (x, self.strand, bStart, bEnd,
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
229 tstart, tstop, chromStart, chromEnd)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
230 bStart += self.blockSizes[x]
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
231 return(chromStart, chromEnd)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
232
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
233 # get the blocks for sub range
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
234 def get_blocks(self, chromStart, chromEnd):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
235 tblockCount = 0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
236 tblockSizes = []
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
237 tblockStarts = []
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
238 for x in range(self.blockCount):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
239 bStart = self.chromStart + self.blockStarts[x]
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
240 bEnd = bStart + self.blockSizes[x]
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
241 if bStart > chromEnd:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
242 break
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
243 if bEnd < chromStart:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
244 continue
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
245 cStart = max(chromStart, bStart)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
246 tblockStarts.append(cStart - chromStart)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
247 tblockSizes.append(min(chromEnd, bEnd) - cStart)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
248 tblockCount += 1
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
249 return (tblockCount, tblockSizes, tblockStarts)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
250
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
251 def trim(self, tstart, tstop, debug=False):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
252 (tchromStart, tchromEnd) =\
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
253 self.get_subrange(tstart, tstop, debug=debug)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
254 (tblockCount, tblockSizes, tblockStarts) =\
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
255 self.get_blocks(tchromStart, tchromEnd)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
256 tbed = BedEntry(
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
257 chrom=self.chrom, chromStart=tchromStart, chromEnd=tchromEnd,
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
258 name=self.name, score=self.score, strand=self.strand,
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
259 thickStart=tchromStart, thickEnd=tchromEnd, itemRgb=self.itemRgb,
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
260 blockCount=tblockCount,
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
261 blockSizes=tblockSizes, blockStarts=tblockStarts)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
262 if self.seq:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
263 ts = tchromStart-self.chromStart
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
264 te = tchromEnd - tchromStart + ts
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
265 tbed.seq = self.seq[ts:te]
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
266 return tbed
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
267
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
268
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
269 def __main__():
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
270 parser = argparse.ArgumentParser(
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
271 description='Retrieve Ensembl cDNAs and three frame translate')
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
272 parser.add_argument(
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
273 '-s', '--species', default='human',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
274 help='Ensembl Species to retrieve')
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
275 parser.add_argument(
7
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
276 '-R', '--regions', action='append', default=[],
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
277 help='Restrict Ensembl retrieval to regions e.g.: X,2:20000-25000,3:100-500+')
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
278 parser.add_argument(
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
279 '-B', '--biotypes', action='append', default=[],
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
280 help='Restrict Ensembl biotypes to retrieve')
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
281 parser.add_argument(
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
282 '-i', '--input', default=None,
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
283 help='Use BED instead of retrieving cDNA from ensembl (-) for stdin')
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
284 parser.add_argument(
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
285 '-t', '--transcripts', default=None,
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
286 help='Path to output cDNA transcripts.bed (-) for stdout')
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
287 parser.add_argument(
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
288 '-r', '--raw', action='store_true',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
289 help='Report transcript exacty as returned from Ensembl')
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
290 parser.add_argument(
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
291 '-f', '--fasta', default=None,
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
292 help='Path to output translations.fasta')
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
293 parser.add_argument(
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
294 '-b', '--bed', default=None,
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
295 help='Path to output translations.bed')
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
296 parser.add_argument(
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
297 '-m', '--min_length', type=int, default=7,
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
298 help='Minimum length of protein translation to report')
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
299 parser.add_argument(
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
300 '-e', '--enzyme', default=None,
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
301 help='Digest translation with enzyme')
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
302 parser.add_argument(
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
303 '-a', '--all', action='store_true',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
304 help='Include reference protein translations')
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
305 parser.add_argument('-v', '--verbose', action='store_true', help='Verbose')
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
306 parser.add_argument('-d', '--debug', action='store_true', help='Debug')
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
307 args = parser.parse_args()
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
308 # print >> sys.stderr, "args: %s" % args
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
309 species = args.species
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
310 input_rdr = None
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
311 if args.input is not None:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
312 input_rdr = open(args.input, 'r') if args.input != '-' else sys.stdin
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
313 tx_wtr = None
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
314 if args.transcripts is not None:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
315 tx_wtr = open(args.transcripts, 'w')\
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
316 if args.transcripts != '-' else sys.stdout
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
317 fa_wtr = open(args.fasta, 'w') if args.fasta is not None else None
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
318 bed_wtr = open(args.bed, 'w') if args.bed is not None else None
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
319
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
320 enzyme = digest.expasy_rules.get(args.enzyme,args.enzyme)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
321
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
322 # print >> sys.stderr, "args biotypes: %s" % args.biotypes
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
323 biotypea = ['biotype=%s' % bt.strip() for biotype in args.biotypes for bt in biotype.split(',')]
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
324 # print >> sys.stderr, "args biotypes: %s" % biotypea
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
325 biotypes = ';'.join(['biotype=%s' % bt.strip() for biotype in args.biotypes for bt in biotype.split(',') if bt.strip()])
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
326 # print >> sys.stderr, "biotypes: %s" % biotypes
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
327
7
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
328 selected_regions = dict() # chrom:(start,end)
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
329 region_pat = '^([^:]+)(?::(\d*)(?:-(\d+)([+-])?)?)?'
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
330 if args.regions:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
331 for entry in args.regions:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
332 if not entry:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
333 continue
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
334 regs = [x.strip() for x in entry.split(',') if x.strip()]
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
335 for reg in regs:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
336 m = re.match(region_pat,reg)
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
337 if m:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
338 (chrom,start,end,strand) = m.groups()
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
339 if chrom:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
340 if chrom not in selected_regions:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
341 selected_regions[chrom] = []
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
342 selected_regions[chrom].append([start,end,strand])
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
343
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
344 translations = dict() # start : end : seq
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
345
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
346 def unique_prot(tbed, seq):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
347 if tbed.chromStart not in translations:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
348 translations[tbed.chromStart] = dict()
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
349 translations[tbed.chromStart][tbed.chromEnd] = []
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
350 translations[tbed.chromStart][tbed.chromEnd].append(seq)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
351 elif tbed.chromEnd not in translations[tbed.chromStart]:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
352 translations[tbed.chromStart][tbed.chromEnd] = []
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
353 translations[tbed.chromStart][tbed.chromEnd].append(seq)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
354 elif seq not in translations[tbed.chromStart][tbed.chromEnd]:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
355 translations[tbed.chromStart][tbed.chromEnd].append(seq)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
356 else:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
357 return False
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
358 return True
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
359
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
360 def translate_bed(bed):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
361 translate_count = 0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
362 if any([fa_wtr, bed_wtr]):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
363 transcript_id = bed.name
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
364 refprot = None
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
365 if not args.all:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
366 try:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
367 cds = get_cds(transcript_id)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
368 if len(cds) % 3 != 0:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
369 cds = cds[:-(len(cds) % 3)]
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
370 refprot = translate(cds) if cds else None
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
371 except:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
372 refprot = None
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
373 cdna = get_cdna(transcript_id)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
374 cdna_len = len(cdna)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
375 for offset in range(3):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
376 seqend = cdna_len - (cdna_len - offset) % 3
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
377 aaseq = translate(cdna[offset:seqend])
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
378 aa_start = 0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
379 while aa_start < len(aaseq):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
380 aa_end = aaseq.find('*', aa_start)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
381 if aa_end < 0:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
382 aa_end = len(aaseq)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
383 prot = aaseq[aa_start:aa_end]
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
384 if enzyme and refprot:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
385 frags = digest._cleave(prot,enzyme)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
386 for frag in reversed(frags):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
387 if frag in refprot:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
388 prot = prot[:prot.rfind(frag)]
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
389 else:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
390 break
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
391 if len(prot) < args.min_length:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
392 pass
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
393 elif refprot and prot in refprot:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
394 pass
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
395 else:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
396 tstart = aa_start*3+offset
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
397 tend = aa_end*3+offset
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
398 prot_acc = "%s_%d_%d" % (transcript_id, tstart, tend)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
399 tbed = bed.trim(tstart, tend)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
400 if args.all or unique_prot(tbed, prot):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
401 translate_count += 1
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
402 tbed.name = prot_acc
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
403 bed_wtr.write("%s\t%s\n" % (str(tbed), prot))
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
404 bed_wtr.flush()
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
405 fa_id = ">%s\n" % (prot_acc)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
406 fa_wtr.write(fa_id)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
407 fa_wtr.write(prot)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
408 fa_wtr.write("\n")
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
409 fa_wtr.flush()
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
410 aa_start = aa_end + 1
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
411 return translate_count
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
412
7
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
413 def translate_region(species,ref,start,stop,strand):
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
414 translation_count = 0
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
415 regions = range(start, stop, max_region)
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
416 if not regions or regions[-1] < stop:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
417 regions.append(stop)
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
418 for end in regions[1:]:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
419 bedlines = get_transcripts_bed(species, ref, start, end, strand=strand, params=biotypes)
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
420 if args.verbose or args.debug:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
421 print >> sys.stderr,\
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
422 "%s\t%s\tstart: %d\tend: %d\tcDNA transcripts:%d"\
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
423 % (species, ref, start, end, len(bedlines))
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
424 # start, end, seq
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
425 for i, bedline in enumerate(bedlines):
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
426 try:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
427 bed = bed_from_line(bedline)\
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
428 if any([not args.raw, fa_wtr, bed_wtr])\
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
429 else None
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
430 if tx_wtr:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
431 tx_wtr.write(bedline if args.raw else str(bed))
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
432 tx_wtr.write("\n")
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
433 tx_wtr.flush()
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
434 if bed:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
435 translation_count += translate_bed(bed)
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
436 except Exception as e:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
437 print >> sys.stderr,\
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
438 "BED error (%s) : %s\n" % (e, bedline)
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
439 start = end + 1
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
440 return translation_count
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
441
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
442 if input_rdr:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
443 translation_count = 0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
444 for i, bedline in enumerate(input_rdr):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
445 try:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
446 bed = bed_from_line(bedline)
7
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
447 if bed is None:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
448 continue
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
449 if bed.biotype and biotypea and bed.biotype not in biotypea:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
450 continue
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
451 translation_count += translate_bed(bed)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
452 except:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
453 print >> sys.stderr, "BED format error: %s\n" % bedline
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
454 if args.debug or (args.verbose and any([fa_wtr, bed_wtr])):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
455 print >> sys.stderr,\
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
456 "%s\tcDNA translations:%d" % (species, translation_count)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
457 else:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
458 coord_systems = get_toplevel(species)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
459 if 'chromosome' in coord_systems:
7
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
460 ref_lengths = dict()
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
461 for ref in sorted(coord_systems['chromosome'].keys()):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
462 length = coord_systems['chromosome'][ref]
7
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
463 ref_lengths[ref] = length
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
464 if not any([tx_wtr, fa_wtr, bed_wtr]):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
465 print >> sys.stderr,\
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
466 "%s\t%s\tlength: %d" % (species, ref, length)
7
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
467 if selected_regions:
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
468 translation_count = 0
7
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
469 for ref in sorted(selected_regions.keys()):
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
470 if ref in ref_lengths:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
471 for reg in selected_regions[ref]:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
472 (_start,_stop,_strand) = reg
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
473 start = int(_start) if _start else 0
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
474 stop = int(_stop) if _stop else ref_lengths[ref]
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
475 strand = '' if not _strand else ':1' if _strand == '+' else ':-1'
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
476 translation_count += translate_region(species,ref,start,stop,strand)
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
477 else:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
478 strand = ''
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
479 start = 0
7
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
480 for ref in sorted(ref_lengths.keys()):
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
481 length = ref_lengths[ref]
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
482 translation_count = 0
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
483 if args.debug:
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
484 print >> sys.stderr,\
7
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
485 "Retrieving transcripts: %s\t%s\tlength: %d"\
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
486 % (species, ref, length)
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
487 translation_count += translate_region(species,ref,start,length,strand)
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
488 if args.debug or (args.verbose and any([fa_wtr, bed_wtr])):
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
489 print >> sys.stderr,\
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
490 "%s\t%s\tlength: %d\tcDNA translations:%d"\
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
491 % (species, ref, length, translation_count)
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
492
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
493
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
494 if __name__ == "__main__":
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
495 __main__()