annotate cpt_phageqc_annotation/shinefind.py @ 0:c3140b08d703 draft default tip

Uploaded
author cpt
date Fri, 17 Jun 2022 13:00:50 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
c3140b08d703 Uploaded
cpt
parents:
diff changeset
1 #!/usr/bin/env python
c3140b08d703 Uploaded
cpt
parents:
diff changeset
2 import re
c3140b08d703 Uploaded
cpt
parents:
diff changeset
3 import sys
c3140b08d703 Uploaded
cpt
parents:
diff changeset
4 import argparse
c3140b08d703 Uploaded
cpt
parents:
diff changeset
5 import logging
c3140b08d703 Uploaded
cpt
parents:
diff changeset
6 from CPT_GFFParser import gffParse, gffWrite, gffSeqFeature
c3140b08d703 Uploaded
cpt
parents:
diff changeset
7 from Bio import SeqIO
c3140b08d703 Uploaded
cpt
parents:
diff changeset
8 from Bio.SeqRecord import SeqRecord
c3140b08d703 Uploaded
cpt
parents:
diff changeset
9 from Bio.SeqFeature import FeatureLocation
c3140b08d703 Uploaded
cpt
parents:
diff changeset
10 from gff3 import (
c3140b08d703 Uploaded
cpt
parents:
diff changeset
11 feature_lambda,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
12 feature_test_type,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
13 feature_test_true,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
14 feature_test_quals,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
15 get_id,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
16 ensure_location_in_bounds,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
17 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
18
c3140b08d703 Uploaded
cpt
parents:
diff changeset
19 logging.basicConfig(level=logging.INFO)
c3140b08d703 Uploaded
cpt
parents:
diff changeset
20 log = logging.getLogger()
c3140b08d703 Uploaded
cpt
parents:
diff changeset
21
c3140b08d703 Uploaded
cpt
parents:
diff changeset
22
c3140b08d703 Uploaded
cpt
parents:
diff changeset
23 class NaiveSDCaller(object):
c3140b08d703 Uploaded
cpt
parents:
diff changeset
24
c3140b08d703 Uploaded
cpt
parents:
diff changeset
25 # TODO May make switch for different sequence sets
c3140b08d703 Uploaded
cpt
parents:
diff changeset
26 SD_SEQUENCES = (
c3140b08d703 Uploaded
cpt
parents:
diff changeset
27 "AGGAGGT",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
28 "GGAGGT",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
29 "AGGAGG",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
30 "GGGGGG",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
31 "AGGAG",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
32 "GAGGT",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
33 "GGAGG",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
34 "GGGGG",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
35 "AGGT",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
36 "GGGT",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
37 "GAGG",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
38 "GGGG",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
39 "AGGA",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
40 "GGAG",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
41 "GGA",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
42 "GAG",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
43 "AGG",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
44 "GGT",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
45 "GGG",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
46 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
47
c3140b08d703 Uploaded
cpt
parents:
diff changeset
48 def __init__(self):
c3140b08d703 Uploaded
cpt
parents:
diff changeset
49 self.sd_reg = [re.compile(x, re.IGNORECASE) for x in self.SD_SEQUENCES]
c3140b08d703 Uploaded
cpt
parents:
diff changeset
50
c3140b08d703 Uploaded
cpt
parents:
diff changeset
51 def list_sds(self, sequence, sd_min=3, sd_max=17):
c3140b08d703 Uploaded
cpt
parents:
diff changeset
52 hits = []
c3140b08d703 Uploaded
cpt
parents:
diff changeset
53 for regex in self.sd_reg:
c3140b08d703 Uploaded
cpt
parents:
diff changeset
54 for match in regex.finditer(sequence):
c3140b08d703 Uploaded
cpt
parents:
diff changeset
55 spacing = len(sequence) - len(match.group()) - match.start()
c3140b08d703 Uploaded
cpt
parents:
diff changeset
56 if sd_max >= spacing+sd_min and spacing+sd_min >= sd_min:
c3140b08d703 Uploaded
cpt
parents:
diff changeset
57 #if the spacing is within gap limits, add
c3140b08d703 Uploaded
cpt
parents:
diff changeset
58 #(search space is [sd_max+7 .. sd_min] so actual gap is spacing+sd_min)
c3140b08d703 Uploaded
cpt
parents:
diff changeset
59 #print('min %d max %d - adding SD with gap %d' % (sd_min, sd_max, spacing+sd_min))
c3140b08d703 Uploaded
cpt
parents:
diff changeset
60 hits.append(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
61 {
c3140b08d703 Uploaded
cpt
parents:
diff changeset
62 "spacing": spacing,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
63 "hit": match.group(),
c3140b08d703 Uploaded
cpt
parents:
diff changeset
64 "start": match.start(),
c3140b08d703 Uploaded
cpt
parents:
diff changeset
65 "end": match.end(),
c3140b08d703 Uploaded
cpt
parents:
diff changeset
66 "len": len(match.group()),
c3140b08d703 Uploaded
cpt
parents:
diff changeset
67 }
c3140b08d703 Uploaded
cpt
parents:
diff changeset
68 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
69 hits = sorted(hits, key= lambda x: (-x['len'],x['spacing']))
c3140b08d703 Uploaded
cpt
parents:
diff changeset
70 return hits
c3140b08d703 Uploaded
cpt
parents:
diff changeset
71
c3140b08d703 Uploaded
cpt
parents:
diff changeset
72 @classmethod
c3140b08d703 Uploaded
cpt
parents:
diff changeset
73 def highlight_sd(cls, sequence, start, end):
c3140b08d703 Uploaded
cpt
parents:
diff changeset
74 return " ".join(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
75 [
c3140b08d703 Uploaded
cpt
parents:
diff changeset
76 sequence[0:start].lower(),
c3140b08d703 Uploaded
cpt
parents:
diff changeset
77 sequence[start:end].upper(),
c3140b08d703 Uploaded
cpt
parents:
diff changeset
78 sequence[end:].lower(),
c3140b08d703 Uploaded
cpt
parents:
diff changeset
79 ]
c3140b08d703 Uploaded
cpt
parents:
diff changeset
80 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
81
c3140b08d703 Uploaded
cpt
parents:
diff changeset
82 @classmethod
c3140b08d703 Uploaded
cpt
parents:
diff changeset
83 def to_features(cls, hits, strand, parent_start, parent_end, feature_id=None, sd_min=3, sd_max=17):
c3140b08d703 Uploaded
cpt
parents:
diff changeset
84 results = []
c3140b08d703 Uploaded
cpt
parents:
diff changeset
85 for idx, hit in enumerate(hits):
c3140b08d703 Uploaded
cpt
parents:
diff changeset
86 # gene complement(124..486)
c3140b08d703 Uploaded
cpt
parents:
diff changeset
87 # -1 491 501 0 5 5
c3140b08d703 Uploaded
cpt
parents:
diff changeset
88 # -1 491 501 0 4 5
c3140b08d703 Uploaded
cpt
parents:
diff changeset
89 # -1 491 501 1 4 5
c3140b08d703 Uploaded
cpt
parents:
diff changeset
90 # -1 491 501 2 3 5
c3140b08d703 Uploaded
cpt
parents:
diff changeset
91 # -1 491 501 1 3 5
c3140b08d703 Uploaded
cpt
parents:
diff changeset
92 # -1 491 501 0 3 5
c3140b08d703 Uploaded
cpt
parents:
diff changeset
93
c3140b08d703 Uploaded
cpt
parents:
diff changeset
94 qualifiers = {
c3140b08d703 Uploaded
cpt
parents:
diff changeset
95 "source": "CPT_ShineFind",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
96 "ID": "%s.rbs-%s" % (feature_id, idx),
c3140b08d703 Uploaded
cpt
parents:
diff changeset
97 }
c3140b08d703 Uploaded
cpt
parents:
diff changeset
98
c3140b08d703 Uploaded
cpt
parents:
diff changeset
99 if strand > 0:
c3140b08d703 Uploaded
cpt
parents:
diff changeset
100 start = parent_end - hit["spacing"] - hit["len"]
c3140b08d703 Uploaded
cpt
parents:
diff changeset
101 end = parent_end - hit["spacing"]
c3140b08d703 Uploaded
cpt
parents:
diff changeset
102 else:
c3140b08d703 Uploaded
cpt
parents:
diff changeset
103 start = parent_start + hit["spacing"]
c3140b08d703 Uploaded
cpt
parents:
diff changeset
104 end = parent_start + hit["spacing"] + hit["len"]
c3140b08d703 Uploaded
cpt
parents:
diff changeset
105 # check that the END of the SD sequence is within the given min/max of parent start/end
c3140b08d703 Uploaded
cpt
parents:
diff changeset
106
c3140b08d703 Uploaded
cpt
parents:
diff changeset
107 # gap is either the sd_start-cds_end (neg strand) or the sd_end-cds_start (pos strand)
c3140b08d703 Uploaded
cpt
parents:
diff changeset
108 # minimum absolute value of these two will be the proper gap regardless of strand
c3140b08d703 Uploaded
cpt
parents:
diff changeset
109 tmp = gffSeqFeature(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
110 FeatureLocation(min(start, end), max(start, end), strand=strand),
c3140b08d703 Uploaded
cpt
parents:
diff changeset
111 #FeatureLocation(min(start, end), max(start, end), strand=strand),
c3140b08d703 Uploaded
cpt
parents:
diff changeset
112 type="Shine_Dalgarno_sequence",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
113 qualifiers=qualifiers,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
114 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
115 results.append(tmp)
c3140b08d703 Uploaded
cpt
parents:
diff changeset
116 return results
c3140b08d703 Uploaded
cpt
parents:
diff changeset
117
c3140b08d703 Uploaded
cpt
parents:
diff changeset
118 def testFeatureUpstream(self, feature, record, sd_min=3, sd_max=17):
c3140b08d703 Uploaded
cpt
parents:
diff changeset
119 # Strand information necessary to getting correct upstream sequence
c3140b08d703 Uploaded
cpt
parents:
diff changeset
120 strand = feature.location.strand
c3140b08d703 Uploaded
cpt
parents:
diff changeset
121
c3140b08d703 Uploaded
cpt
parents:
diff changeset
122 # n_bases_upstream (plus/minus 7 upstream to make the min/max define the possible gap position)
c3140b08d703 Uploaded
cpt
parents:
diff changeset
123 if strand > 0:
c3140b08d703 Uploaded
cpt
parents:
diff changeset
124 start = feature.location.start - sd_max - 7
c3140b08d703 Uploaded
cpt
parents:
diff changeset
125 end = feature.location.start - sd_min
c3140b08d703 Uploaded
cpt
parents:
diff changeset
126 else:
c3140b08d703 Uploaded
cpt
parents:
diff changeset
127 start = feature.location.end + sd_min
c3140b08d703 Uploaded
cpt
parents:
diff changeset
128 end = feature.location.end + sd_max + 7
c3140b08d703 Uploaded
cpt
parents:
diff changeset
129
c3140b08d703 Uploaded
cpt
parents:
diff changeset
130 (start, end) = ensure_location_in_bounds(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
131 start=start, end=end, parent_length=len(record)
c3140b08d703 Uploaded
cpt
parents:
diff changeset
132 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
133
c3140b08d703 Uploaded
cpt
parents:
diff changeset
134 # Create our temp feature used to obtain correct portion of
c3140b08d703 Uploaded
cpt
parents:
diff changeset
135 # genome
c3140b08d703 Uploaded
cpt
parents:
diff changeset
136 tmp = gffSeqFeature(FeatureLocation(min(start, end), max(start, end), strand=strand), type="domain")
c3140b08d703 Uploaded
cpt
parents:
diff changeset
137 seq = str(tmp.extract(record.seq))
c3140b08d703 Uploaded
cpt
parents:
diff changeset
138 return self.list_sds(seq, sd_min, sd_max), start, end, seq
c3140b08d703 Uploaded
cpt
parents:
diff changeset
139
c3140b08d703 Uploaded
cpt
parents:
diff changeset
140 def hasSd(self, feature, record, sd_min=3, sd_max=17):
c3140b08d703 Uploaded
cpt
parents:
diff changeset
141 sds, start, end, seq = self.testFeatureUpstream(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
142 feature, record, sd_min=sd_min, sd_max=sd_max
c3140b08d703 Uploaded
cpt
parents:
diff changeset
143 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
144 return len(sds) > 0
c3140b08d703 Uploaded
cpt
parents:
diff changeset
145
c3140b08d703 Uploaded
cpt
parents:
diff changeset
146
c3140b08d703 Uploaded
cpt
parents:
diff changeset
147 # Cycle through subfeatures, set feature's location to be equal
c3140b08d703 Uploaded
cpt
parents:
diff changeset
148 # to the smallest start and largest end.
c3140b08d703 Uploaded
cpt
parents:
diff changeset
149 # Remove pending bugfix for feature display in Apollo
c3140b08d703 Uploaded
cpt
parents:
diff changeset
150 def fminmax(feature):
c3140b08d703 Uploaded
cpt
parents:
diff changeset
151 fmin = None
c3140b08d703 Uploaded
cpt
parents:
diff changeset
152 fmax = None
c3140b08d703 Uploaded
cpt
parents:
diff changeset
153 for sf in feature_lambda([feature], feature_test_true, {}, subfeatures=True):
c3140b08d703 Uploaded
cpt
parents:
diff changeset
154 if fmin is None:
c3140b08d703 Uploaded
cpt
parents:
diff changeset
155 fmin = sf.location.start
c3140b08d703 Uploaded
cpt
parents:
diff changeset
156 fmax = sf.location.end
c3140b08d703 Uploaded
cpt
parents:
diff changeset
157 if sf.location.start < fmin:
c3140b08d703 Uploaded
cpt
parents:
diff changeset
158 fmin = sf.location.start
c3140b08d703 Uploaded
cpt
parents:
diff changeset
159 if sf.location.end > fmax:
c3140b08d703 Uploaded
cpt
parents:
diff changeset
160 fmax = sf.location.end
c3140b08d703 Uploaded
cpt
parents:
diff changeset
161 return fmin, fmax
c3140b08d703 Uploaded
cpt
parents:
diff changeset
162
c3140b08d703 Uploaded
cpt
parents:
diff changeset
163
c3140b08d703 Uploaded
cpt
parents:
diff changeset
164 def fix_gene_boundaries(feature):
c3140b08d703 Uploaded
cpt
parents:
diff changeset
165 # There is a bug in Apollo whereby we have created gene
c3140b08d703 Uploaded
cpt
parents:
diff changeset
166 # features which are larger than expected, but we cannot see this.
c3140b08d703 Uploaded
cpt
parents:
diff changeset
167 # We only see a perfect sized gene + SD together.
c3140b08d703 Uploaded
cpt
parents:
diff changeset
168 #
c3140b08d703 Uploaded
cpt
parents:
diff changeset
169 # So, we clamp the location of the gene feature to the
c3140b08d703 Uploaded
cpt
parents:
diff changeset
170 # contained mRNAs. Will remove pending Apollo upgrade.
c3140b08d703 Uploaded
cpt
parents:
diff changeset
171 fmin, fmax = fminmax(feature)
c3140b08d703 Uploaded
cpt
parents:
diff changeset
172 if feature.location.strand > 0:
c3140b08d703 Uploaded
cpt
parents:
diff changeset
173 feature.location = FeatureLocation(fmin, fmax, strand=1)
c3140b08d703 Uploaded
cpt
parents:
diff changeset
174 else:
c3140b08d703 Uploaded
cpt
parents:
diff changeset
175 feature.location = FeatureLocation(fmin, fmax, strand=-1)
c3140b08d703 Uploaded
cpt
parents:
diff changeset
176 return feature
c3140b08d703 Uploaded
cpt
parents:
diff changeset
177
c3140b08d703 Uploaded
cpt
parents:
diff changeset
178 def shinefind(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
179 fasta,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
180 gff3,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
181 gff3_output=None,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
182 table_output=None,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
183 lookahead_min=3,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
184 lookahead_max=17,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
185 top_only=False,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
186 add=False,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
187 ):
c3140b08d703 Uploaded
cpt
parents:
diff changeset
188 table_output.write(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
189 "\t".join(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
190 [
c3140b08d703 Uploaded
cpt
parents:
diff changeset
191 "ID",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
192 "Name",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
193 "Terminus",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
194 "Terminus",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
195 "Strand",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
196 "Upstream Sequence",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
197 "SD",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
198 "Spacing",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
199 ]
c3140b08d703 Uploaded
cpt
parents:
diff changeset
200 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
201 + "\n"
c3140b08d703 Uploaded
cpt
parents:
diff changeset
202 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
203
c3140b08d703 Uploaded
cpt
parents:
diff changeset
204 sd_finder = NaiveSDCaller()
c3140b08d703 Uploaded
cpt
parents:
diff changeset
205 # Load up sequence(s) for GFF3 data
c3140b08d703 Uploaded
cpt
parents:
diff changeset
206 seq_dict = SeqIO.to_dict(SeqIO.parse(fasta, "fasta"))
c3140b08d703 Uploaded
cpt
parents:
diff changeset
207 # Parse GFF3 records
c3140b08d703 Uploaded
cpt
parents:
diff changeset
208 for record in gffParse(gff3, base_dict=seq_dict):
c3140b08d703 Uploaded
cpt
parents:
diff changeset
209 # Shinefind's gff3_output.
c3140b08d703 Uploaded
cpt
parents:
diff changeset
210 gff3_output_record = SeqRecord(record.seq, record.id)
c3140b08d703 Uploaded
cpt
parents:
diff changeset
211 # Filter out just coding sequences
c3140b08d703 Uploaded
cpt
parents:
diff changeset
212 ignored_features = []
c3140b08d703 Uploaded
cpt
parents:
diff changeset
213 for x in record.features:
c3140b08d703 Uploaded
cpt
parents:
diff changeset
214 # If feature X does NOT contain a CDS, add to ignored_features
c3140b08d703 Uploaded
cpt
parents:
diff changeset
215 # list. This means if we have a top level gene feature with or
c3140b08d703 Uploaded
cpt
parents:
diff changeset
216 # without a CDS subfeature, we're catch it appropriately here.
c3140b08d703 Uploaded
cpt
parents:
diff changeset
217 if (
c3140b08d703 Uploaded
cpt
parents:
diff changeset
218 len(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
219 list(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
220 feature_lambda(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
221 [x], feature_test_type, {"type": "CDS"}, subfeatures=True
c3140b08d703 Uploaded
cpt
parents:
diff changeset
222 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
223 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
224 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
225 == 0
c3140b08d703 Uploaded
cpt
parents:
diff changeset
226 ):
c3140b08d703 Uploaded
cpt
parents:
diff changeset
227 ignored_features.append(x)
c3140b08d703 Uploaded
cpt
parents:
diff changeset
228
c3140b08d703 Uploaded
cpt
parents:
diff changeset
229 # Loop over all gene features
c3140b08d703 Uploaded
cpt
parents:
diff changeset
230 for gene in feature_lambda(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
231 record.features, feature_test_type, {"type": "gene"}, subfeatures=True
c3140b08d703 Uploaded
cpt
parents:
diff changeset
232 ):
c3140b08d703 Uploaded
cpt
parents:
diff changeset
233
c3140b08d703 Uploaded
cpt
parents:
diff changeset
234 # Get the CDS from this gene.
c3140b08d703 Uploaded
cpt
parents:
diff changeset
235 feature = sorted(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
236 list(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
237 feature_lambda(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
238 gene.sub_features,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
239 feature_test_type,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
240 {"type": "CDS"},
c3140b08d703 Uploaded
cpt
parents:
diff changeset
241 subfeatures=True,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
242 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
243 ),
c3140b08d703 Uploaded
cpt
parents:
diff changeset
244 key=lambda x: x.location.start,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
245 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
246 # If no CDSs are in this gene feature, then quit
c3140b08d703 Uploaded
cpt
parents:
diff changeset
247 if len(feature) == 0:
c3140b08d703 Uploaded
cpt
parents:
diff changeset
248 # We've already caught these above in our ignored_features
c3140b08d703 Uploaded
cpt
parents:
diff changeset
249 # list, so we skip out on the rest of this for loop
c3140b08d703 Uploaded
cpt
parents:
diff changeset
250 continue
c3140b08d703 Uploaded
cpt
parents:
diff changeset
251 else:
c3140b08d703 Uploaded
cpt
parents:
diff changeset
252 # Otherwise pull the first on the strand.
c3140b08d703 Uploaded
cpt
parents:
diff changeset
253 feature = feature[0]
c3140b08d703 Uploaded
cpt
parents:
diff changeset
254
c3140b08d703 Uploaded
cpt
parents:
diff changeset
255 # Three different ways RBSs can be stored that we expect.
c3140b08d703 Uploaded
cpt
parents:
diff changeset
256 rbs_rbs = list(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
257 feature_lambda(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
258 gene.sub_features,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
259 feature_test_type,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
260 {"type": "RBS"},
c3140b08d703 Uploaded
cpt
parents:
diff changeset
261 subfeatures=False,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
262 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
263 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
264 rbs_sds = list(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
265 feature_lambda(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
266 gene.sub_features,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
267 feature_test_type,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
268 {"type": "Shine_Dalgarno_sequence"},
c3140b08d703 Uploaded
cpt
parents:
diff changeset
269 subfeatures=False,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
270 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
271 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
272 regulatory_elements = list(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
273 feature_lambda(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
274 gene.sub_features,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
275 feature_test_type,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
276 {"type": "regulatory"},
c3140b08d703 Uploaded
cpt
parents:
diff changeset
277 subfeatures=False,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
278 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
279 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
280 rbs_regulatory = list(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
281 feature_lambda(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
282 regulatory_elements,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
283 feature_test_quals,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
284 {"regulatory_class": ["ribosome_binding_site"]},
c3140b08d703 Uploaded
cpt
parents:
diff changeset
285 subfeatures=False,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
286 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
287 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
288 rbss = rbs_rbs + rbs_sds + rbs_regulatory
c3140b08d703 Uploaded
cpt
parents:
diff changeset
289
c3140b08d703 Uploaded
cpt
parents:
diff changeset
290 # If someone has already annotated an RBS, we move to the next gene
c3140b08d703 Uploaded
cpt
parents:
diff changeset
291 if len(rbss) > 0:
c3140b08d703 Uploaded
cpt
parents:
diff changeset
292 log.debug("Has %s RBSs", len(rbss))
c3140b08d703 Uploaded
cpt
parents:
diff changeset
293 ignored_features.append(gene)
c3140b08d703 Uploaded
cpt
parents:
diff changeset
294 continue
c3140b08d703 Uploaded
cpt
parents:
diff changeset
295
c3140b08d703 Uploaded
cpt
parents:
diff changeset
296 sds, start, end, seq = sd_finder.testFeatureUpstream(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
297 feature, record, sd_min=lookahead_min, sd_max=lookahead_max
c3140b08d703 Uploaded
cpt
parents:
diff changeset
298 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
299
c3140b08d703 Uploaded
cpt
parents:
diff changeset
300 feature_id = get_id(feature)
c3140b08d703 Uploaded
cpt
parents:
diff changeset
301 sd_features = sd_finder.to_features(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
302 sds, feature.location.strand, start, end, feature_id=feature.id
c3140b08d703 Uploaded
cpt
parents:
diff changeset
303 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
304
c3140b08d703 Uploaded
cpt
parents:
diff changeset
305 human_strand = "+" if feature.location.strand == 1 else "-"
c3140b08d703 Uploaded
cpt
parents:
diff changeset
306
c3140b08d703 Uploaded
cpt
parents:
diff changeset
307 # http://book.pythontips.com/en/latest/for_-_else.html
c3140b08d703 Uploaded
cpt
parents:
diff changeset
308 log.debug("Found %s SDs", len(sds))
c3140b08d703 Uploaded
cpt
parents:
diff changeset
309 for (sd, sd_feature) in zip(sds, sd_features):
c3140b08d703 Uploaded
cpt
parents:
diff changeset
310 # If we only want the top feature, after the bulk of the
c3140b08d703 Uploaded
cpt
parents:
diff changeset
311 # forloop executes once, we append the top feature, and fake a
c3140b08d703 Uploaded
cpt
parents:
diff changeset
312 # break, because an actual break triggers the else: block
c3140b08d703 Uploaded
cpt
parents:
diff changeset
313 table_output.write(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
314 "\t".join(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
315 map(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
316 str,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
317 [
c3140b08d703 Uploaded
cpt
parents:
diff changeset
318 feature.id,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
319 feature_id,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
320 feature.location.start,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
321 feature.location.end,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
322 human_strand,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
323 sd_finder.highlight_sd(seq, sd["start"], sd["end"]),
c3140b08d703 Uploaded
cpt
parents:
diff changeset
324 sd["hit"],
c3140b08d703 Uploaded
cpt
parents:
diff changeset
325 int(sd["spacing"]) + lookahead_min,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
326 ],
c3140b08d703 Uploaded
cpt
parents:
diff changeset
327 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
328 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
329 + "\n"
c3140b08d703 Uploaded
cpt
parents:
diff changeset
330 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
331
c3140b08d703 Uploaded
cpt
parents:
diff changeset
332 if add:
c3140b08d703 Uploaded
cpt
parents:
diff changeset
333 # Append the top RBS to the gene feature
c3140b08d703 Uploaded
cpt
parents:
diff changeset
334 gene.sub_features.append(sd_feature)
c3140b08d703 Uploaded
cpt
parents:
diff changeset
335 # Pick out start/end locations for all sub_features
c3140b08d703 Uploaded
cpt
parents:
diff changeset
336 locations = [x.location.start for x in gene.sub_features] + [
c3140b08d703 Uploaded
cpt
parents:
diff changeset
337 x.location.end for x in gene.sub_features
c3140b08d703 Uploaded
cpt
parents:
diff changeset
338 ]
c3140b08d703 Uploaded
cpt
parents:
diff changeset
339 # Update gene's start/end to be inclusive
c3140b08d703 Uploaded
cpt
parents:
diff changeset
340 gene.location._start = min(locations)
c3140b08d703 Uploaded
cpt
parents:
diff changeset
341 gene.location._end = max(locations)
c3140b08d703 Uploaded
cpt
parents:
diff changeset
342 # Also register the feature with the separate GFF3 output
c3140b08d703 Uploaded
cpt
parents:
diff changeset
343 sd_feature = fix_gene_boundaries(sd_feature)
c3140b08d703 Uploaded
cpt
parents:
diff changeset
344 gff3_output_record.features.append(sd_feature)
c3140b08d703 Uploaded
cpt
parents:
diff changeset
345
c3140b08d703 Uploaded
cpt
parents:
diff changeset
346 if top_only or sd == (sds[-1]):
c3140b08d703 Uploaded
cpt
parents:
diff changeset
347 break
c3140b08d703 Uploaded
cpt
parents:
diff changeset
348 else:
c3140b08d703 Uploaded
cpt
parents:
diff changeset
349 table_output.write(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
350 "\t".join(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
351 map(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
352 str,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
353 [
c3140b08d703 Uploaded
cpt
parents:
diff changeset
354 feature.id,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
355 feature_id,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
356 feature.location.start,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
357 feature.location.end,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
358 human_strand,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
359 seq,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
360 None,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
361 -1,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
362 ],
c3140b08d703 Uploaded
cpt
parents:
diff changeset
363 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
364 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
365 + "\n"
c3140b08d703 Uploaded
cpt
parents:
diff changeset
366 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
367
c3140b08d703 Uploaded
cpt
parents:
diff changeset
368 record.annotations = {}
c3140b08d703 Uploaded
cpt
parents:
diff changeset
369 gffWrite([record], sys.stdout)
c3140b08d703 Uploaded
cpt
parents:
diff changeset
370
c3140b08d703 Uploaded
cpt
parents:
diff changeset
371 gff3_output_record.features = sorted(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
372 gff3_output_record.features, key=lambda x: x.location.start
c3140b08d703 Uploaded
cpt
parents:
diff changeset
373 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
374 gff3_output_record.annotations = {}
c3140b08d703 Uploaded
cpt
parents:
diff changeset
375 gffWrite([gff3_output_record], gff3_output)
c3140b08d703 Uploaded
cpt
parents:
diff changeset
376
c3140b08d703 Uploaded
cpt
parents:
diff changeset
377
c3140b08d703 Uploaded
cpt
parents:
diff changeset
378 if __name__ == "__main__":
c3140b08d703 Uploaded
cpt
parents:
diff changeset
379 parser = argparse.ArgumentParser(description="Identify shine-dalgarno sequences")
c3140b08d703 Uploaded
cpt
parents:
diff changeset
380 parser.add_argument("fasta", type=argparse.FileType("r"), help="Fasta Genome")
c3140b08d703 Uploaded
cpt
parents:
diff changeset
381 parser.add_argument("gff3", type=argparse.FileType("r"), help="GFF3 annotations")
c3140b08d703 Uploaded
cpt
parents:
diff changeset
382
c3140b08d703 Uploaded
cpt
parents:
diff changeset
383 parser.add_argument(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
384 "--gff3_output",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
385 type=argparse.FileType("w"),
c3140b08d703 Uploaded
cpt
parents:
diff changeset
386 help="GFF3 Output",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
387 default="shinefind.gff3",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
388 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
389 parser.add_argument(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
390 "--table_output",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
391 type=argparse.FileType("w"),
c3140b08d703 Uploaded
cpt
parents:
diff changeset
392 help="Tabular Output",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
393 default="shinefind.tbl",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
394 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
395
c3140b08d703 Uploaded
cpt
parents:
diff changeset
396 parser.add_argument(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
397 "--lookahead_min",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
398 nargs="?",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
399 type=int,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
400 help="Number of bases upstream of CDSs to end search",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
401 default=3,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
402 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
403 parser.add_argument(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
404 "--lookahead_max",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
405 nargs="?",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
406 type=int,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
407 help="Number of bases upstream of CDSs to begin search",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
408 default=17,
c3140b08d703 Uploaded
cpt
parents:
diff changeset
409 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
410
c3140b08d703 Uploaded
cpt
parents:
diff changeset
411 parser.add_argument("--top_only", action="store_true", help="Only report best hits")
c3140b08d703 Uploaded
cpt
parents:
diff changeset
412 parser.add_argument(
c3140b08d703 Uploaded
cpt
parents:
diff changeset
413 "--add",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
414 action="store_true",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
415 help='Function in "addition" mode whereby the '
c3140b08d703 Uploaded
cpt
parents:
diff changeset
416 + "RBSs are added directly to the gene model.",
c3140b08d703 Uploaded
cpt
parents:
diff changeset
417 )
c3140b08d703 Uploaded
cpt
parents:
diff changeset
418
c3140b08d703 Uploaded
cpt
parents:
diff changeset
419 args = parser.parse_args()
c3140b08d703 Uploaded
cpt
parents:
diff changeset
420 shinefind(**vars(args))