annotate ensembl_cdna_translate.py @ 8:5c92d0be6514 draft

Uploaded
author jjohnson
date Thu, 14 Dec 2017 13:32:00 -0500
parents d59e3ce10e74
children
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
8
5c92d0be6514 Uploaded
jjohnson
parents: 7
diff changeset
24 from bedutil import BedEntry, bed_from_line
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
25 import digest
8
5c92d0be6514 Uploaded
jjohnson
parents: 7
diff changeset
26 from ensembl_rest import get_toplevel, get_transcripts_bed, get_cds, get_cdna, max_region
5c92d0be6514 Uploaded
jjohnson
parents: 7
diff changeset
27 from twobitreader import TwoBitFile
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
28
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
29
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
30 def __main__():
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
31 parser = argparse.ArgumentParser(
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
32 description='Retrieve Ensembl cDNAs and three frame translate')
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
33 parser.add_argument(
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
34 '-s', '--species', default='human',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
35 help='Ensembl Species to retrieve')
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
36 parser.add_argument(
7
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
37 '-R', '--regions', action='append', default=[],
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
38 help='Restrict Ensembl retrieval to regions e.g.: X,2:20000-25000,3:100-500+')
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
39 parser.add_argument(
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
40 '-B', '--biotypes', action='append', default=[],
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
41 help='Restrict Ensembl biotypes to retrieve')
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
42 parser.add_argument(
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
43 '-i', '--input', default=None,
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
44 help='Use BED instead of retrieving cDNA from ensembl (-) for stdin')
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
45 parser.add_argument(
8
5c92d0be6514 Uploaded
jjohnson
parents: 7
diff changeset
46 '-T', '--twobit', default=None,
5c92d0be6514 Uploaded
jjohnson
parents: 7
diff changeset
47 help='Genome reference sequence in 2bit format')
5c92d0be6514 Uploaded
jjohnson
parents: 7
diff changeset
48 parser.add_argument(
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
49 '-t', '--transcripts', default=None,
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
50 help='Path to output cDNA transcripts.bed (-) for stdout')
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
51 parser.add_argument(
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
52 '-r', '--raw', action='store_true',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
53 help='Report transcript exacty as returned from Ensembl')
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
54 parser.add_argument(
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
55 '-f', '--fasta', default=None,
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
56 help='Path to output translations.fasta')
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
57 parser.add_argument(
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
58 '-b', '--bed', default=None,
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
59 help='Path to output translations.bed')
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
60 parser.add_argument(
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
61 '-m', '--min_length', type=int, default=7,
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
62 help='Minimum length of protein translation to report')
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
63 parser.add_argument(
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
64 '-e', '--enzyme', default=None,
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
65 help='Digest translation with enzyme')
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
66 parser.add_argument(
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
67 '-a', '--all', action='store_true',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
68 help='Include reference protein translations')
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
69 parser.add_argument('-v', '--verbose', action='store_true', help='Verbose')
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
70 parser.add_argument('-d', '--debug', action='store_true', help='Debug')
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
71 args = parser.parse_args()
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
72 # print >> sys.stderr, "args: %s" % args
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
73 species = args.species
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
74 input_rdr = None
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
75 if args.input is not None:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
76 input_rdr = open(args.input, 'r') if args.input != '-' else sys.stdin
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
77 tx_wtr = None
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
78 if args.transcripts is not None:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
79 tx_wtr = open(args.transcripts, 'w')\
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
80 if args.transcripts != '-' else sys.stdout
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
81 fa_wtr = open(args.fasta, 'w') if args.fasta is not None else None
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
82 bed_wtr = open(args.bed, 'w') if args.bed is not None else None
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
83
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
84 enzyme = digest.expasy_rules.get(args.enzyme,args.enzyme)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
85
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
86 # print >> sys.stderr, "args biotypes: %s" % args.biotypes
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
87 biotypea = ['biotype=%s' % bt.strip() for biotype in args.biotypes for bt in biotype.split(',')]
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
88 # print >> sys.stderr, "args biotypes: %s" % biotypea
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
89 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
90 # print >> sys.stderr, "biotypes: %s" % biotypes
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
91
8
5c92d0be6514 Uploaded
jjohnson
parents: 7
diff changeset
92 twobit = TwoBitFile(args.twobit) if args.twobit else None
5c92d0be6514 Uploaded
jjohnson
parents: 7
diff changeset
93
7
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
94 selected_regions = dict() # chrom:(start,end)
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
95 region_pat = '^([^:]+)(?::(\d*)(?:-(\d+)([+-])?)?)?'
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
96 if args.regions:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
97 for entry in args.regions:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
98 if not entry:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
99 continue
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
100 regs = [x.strip() for x in entry.split(',') if x.strip()]
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
101 for reg in regs:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
102 m = re.match(region_pat,reg)
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
103 if m:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
104 (chrom,start,end,strand) = m.groups()
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
105 if chrom:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
106 if chrom not in selected_regions:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
107 selected_regions[chrom] = []
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
108 selected_regions[chrom].append([start,end,strand])
8
5c92d0be6514 Uploaded
jjohnson
parents: 7
diff changeset
109 if args.debug: print >> sys.stderr, "selected_regions: %s" % selected_regions
7
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
110
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
111 translations = dict() # start : end : seq
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
112
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
113 def unique_prot(tbed, seq):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
114 if tbed.chromStart not in translations:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
115 translations[tbed.chromStart] = dict()
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
116 translations[tbed.chromStart][tbed.chromEnd] = []
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
117 translations[tbed.chromStart][tbed.chromEnd].append(seq)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
118 elif tbed.chromEnd not in translations[tbed.chromStart]:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
119 translations[tbed.chromStart][tbed.chromEnd] = []
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
120 translations[tbed.chromStart][tbed.chromEnd].append(seq)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
121 elif seq not in translations[tbed.chromStart][tbed.chromEnd]:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
122 translations[tbed.chromStart][tbed.chromEnd].append(seq)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
123 else:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
124 return False
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
125 return True
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
126
8
5c92d0be6514 Uploaded
jjohnson
parents: 7
diff changeset
127 def get_sequence(chrom,start,end):
5c92d0be6514 Uploaded
jjohnson
parents: 7
diff changeset
128 if twobit:
5c92d0be6514 Uploaded
jjohnson
parents: 7
diff changeset
129 if chrom in twobit:
5c92d0be6514 Uploaded
jjohnson
parents: 7
diff changeset
130 return twobit[chrom][start:end]
5c92d0be6514 Uploaded
jjohnson
parents: 7
diff changeset
131 contig = chrom[3:] if chrom.startswith('chr') else 'chr%s' % chrom
5c92d0be6514 Uploaded
jjohnson
parents: 7
diff changeset
132 if contig in twobit:
5c92d0be6514 Uploaded
jjohnson
parents: 7
diff changeset
133 return twobit[contig][start:end]
5c92d0be6514 Uploaded
jjohnson
parents: 7
diff changeset
134 return None
5c92d0be6514 Uploaded
jjohnson
parents: 7
diff changeset
135
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
136 def translate_bed(bed):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
137 translate_count = 0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
138 if any([fa_wtr, bed_wtr]):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
139 transcript_id = bed.name
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
140 refprot = None
8
5c92d0be6514 Uploaded
jjohnson
parents: 7
diff changeset
141 if twobit:
5c92d0be6514 Uploaded
jjohnson
parents: 7
diff changeset
142 bed.seq = get_sequence(bed.chrom,bed.chromStart,bed.chromEnd)
5c92d0be6514 Uploaded
jjohnson
parents: 7
diff changeset
143 else:
5c92d0be6514 Uploaded
jjohnson
parents: 7
diff changeset
144 bed.cdna = get_cdna(transcript_id)
5c92d0be6514 Uploaded
jjohnson
parents: 7
diff changeset
145 cdna = bed.get_cdna()
5c92d0be6514 Uploaded
jjohnson
parents: 7
diff changeset
146 cdna_len = len(cdna)
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
147 if not args.all:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
148 try:
8
5c92d0be6514 Uploaded
jjohnson
parents: 7
diff changeset
149 cds = bed.get_cds()
5c92d0be6514 Uploaded
jjohnson
parents: 7
diff changeset
150 if cds is None:
5c92d0be6514 Uploaded
jjohnson
parents: 7
diff changeset
151 cds = get_cds(transcript_id)
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
152 if len(cds) % 3 != 0:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
153 cds = cds[:-(len(cds) % 3)]
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
154 refprot = translate(cds) if cds else None
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
155 except:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
156 refprot = None
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
157 for offset in range(3):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
158 seqend = cdna_len - (cdna_len - offset) % 3
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
159 aaseq = translate(cdna[offset:seqend])
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
160 aa_start = 0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
161 while aa_start < len(aaseq):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
162 aa_end = aaseq.find('*', aa_start)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
163 if aa_end < 0:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
164 aa_end = len(aaseq)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
165 prot = aaseq[aa_start:aa_end]
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
166 if enzyme and refprot:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
167 frags = digest._cleave(prot,enzyme)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
168 for frag in reversed(frags):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
169 if frag in refprot:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
170 prot = prot[:prot.rfind(frag)]
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
171 else:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
172 break
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
173 if len(prot) < args.min_length:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
174 pass
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
175 elif refprot and prot in refprot:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
176 pass
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
177 else:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
178 tstart = aa_start*3+offset
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
179 tend = aa_end*3+offset
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
180 prot_acc = "%s_%d_%d" % (transcript_id, tstart, tend)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
181 tbed = bed.trim(tstart, tend)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
182 if args.all or unique_prot(tbed, prot):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
183 translate_count += 1
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
184 tbed.name = prot_acc
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
185 bed_wtr.write("%s\t%s\n" % (str(tbed), prot))
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
186 bed_wtr.flush()
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
187 fa_id = ">%s\n" % (prot_acc)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
188 fa_wtr.write(fa_id)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
189 fa_wtr.write(prot)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
190 fa_wtr.write("\n")
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
191 fa_wtr.flush()
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
192 aa_start = aa_end + 1
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
193 return translate_count
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
194
7
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
195 def translate_region(species,ref,start,stop,strand):
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
196 translation_count = 0
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
197 regions = range(start, stop, max_region)
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
198 if not regions or regions[-1] < stop:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
199 regions.append(stop)
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
200 for end in regions[1:]:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
201 bedlines = get_transcripts_bed(species, ref, start, end, strand=strand, params=biotypes)
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
202 if args.verbose or args.debug:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
203 print >> sys.stderr,\
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
204 "%s\t%s\tstart: %d\tend: %d\tcDNA transcripts:%d"\
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
205 % (species, ref, start, end, len(bedlines))
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
206 # start, end, seq
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
207 for i, bedline in enumerate(bedlines):
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
208 try:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
209 bed = bed_from_line(bedline)\
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
210 if any([not args.raw, fa_wtr, bed_wtr])\
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
211 else None
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
212 if tx_wtr:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
213 tx_wtr.write(bedline if args.raw else str(bed))
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
214 tx_wtr.write("\n")
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
215 tx_wtr.flush()
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
216 if bed:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
217 translation_count += translate_bed(bed)
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
218 except Exception as e:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
219 print >> sys.stderr,\
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
220 "BED error (%s) : %s\n" % (e, bedline)
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
221 start = end + 1
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
222 return translation_count
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
223
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
224 if input_rdr:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
225 translation_count = 0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
226 for i, bedline in enumerate(input_rdr):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
227 try:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
228 bed = bed_from_line(bedline)
7
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
229 if bed is None:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
230 continue
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
231 if bed.biotype and biotypea and bed.biotype not in biotypea:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
232 continue
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
233 translation_count += translate_bed(bed)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
234 except:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
235 print >> sys.stderr, "BED format error: %s\n" % bedline
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
236 if args.debug or (args.verbose and any([fa_wtr, bed_wtr])):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
237 print >> sys.stderr,\
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
238 "%s\tcDNA translations:%d" % (species, translation_count)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
239 else:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
240 coord_systems = get_toplevel(species)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
241 if 'chromosome' in coord_systems:
7
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
242 ref_lengths = dict()
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
243 for ref in sorted(coord_systems['chromosome'].keys()):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
244 length = coord_systems['chromosome'][ref]
7
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
245 ref_lengths[ref] = length
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
246 if not any([tx_wtr, fa_wtr, bed_wtr]):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
247 print >> sys.stderr,\
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
248 "%s\t%s\tlength: %d" % (species, ref, length)
7
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
249 if selected_regions:
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
250 translation_count = 0
7
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
251 for ref in sorted(selected_regions.keys()):
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
252 if ref in ref_lengths:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
253 for reg in selected_regions[ref]:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
254 (_start,_stop,_strand) = reg
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
255 start = int(_start) if _start else 0
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
256 stop = int(_stop) if _stop else ref_lengths[ref]
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
257 strand = '' if not _strand else ':1' if _strand == '+' else ':-1'
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
258 translation_count += translate_region(species,ref,start,stop,strand)
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
259 else:
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
260 strand = ''
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
261 start = 0
7
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
262 for ref in sorted(ref_lengths.keys()):
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
263 length = ref_lengths[ref]
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
264 translation_count = 0
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
265 if args.debug:
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
266 print >> sys.stderr,\
7
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
267 "Retrieving transcripts: %s\t%s\tlength: %d"\
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
268 % (species, ref, length)
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
269 translation_count += translate_region(species,ref,start,length,strand)
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
270 if args.debug or (args.verbose and any([fa_wtr, bed_wtr])):
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
271 print >> sys.stderr,\
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
272 "%s\t%s\tlength: %d\tcDNA translations:%d"\
d59e3ce10e74 Uploaded
jjohnson
parents: 2
diff changeset
273 % (species, ref, length, translation_count)
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
274
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
275
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
276 if __name__ == "__main__":
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
277 __main__()