annotate jbrowse2/gff3_rebase.py @ 10:0db895a99532 draft default tip

Uploaded
author fubar
date Fri, 05 Jan 2024 22:26:16 +0000
parents 88b9b105c09b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
1 #!/usr/bin/env python
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
2 import argparse
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
3 import copy
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
4 import logging
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
5 import sys
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
6
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
7 from BCBio import GFF
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
8 from Bio.SeqFeature import FeatureLocation
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
9
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
10 logging.basicConfig(level=logging.INFO)
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
11 log = logging.getLogger(__name__)
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
12
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
13 __author__ = "Eric Rasche"
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
14 __version__ = "0.4.0"
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
15 __maintainer__ = "Eric Rasche"
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
16 __email__ = "esr@tamu.edu"
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
17
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
18
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
19 def feature_lambda(feature_list, test, test_kwargs, subfeatures=True):
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
20 """Recursively search through features, testing each with a test function, yielding matches.
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
21
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
22 GFF3 is a hierachical data structure, so we need to be able to recursively
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
23 search through features. E.g. if you're looking for a feature with
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
24 ID='bob.42', you can't just do a simple list comprehension with a test
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
25 case. You don't know how deeply burried bob.42 will be in the feature tree. This is where feature_lambda steps in.
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
26
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
27 :type feature_list: list
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
28 :param feature_list: an iterable of features
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
29
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
30 :type test: function reference
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
31 :param test: a closure with the method signature (feature, **kwargs) where
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
32 the kwargs are those passed in the next argument. This
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
33 function should return True or False, True if the feature is
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
34 to be yielded as part of the main feature_lambda function, or
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
35 False if it is to be ignored. This function CAN mutate the
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
36 features passed to it (think "apply").
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
37
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
38 :type test_kwargs: dictionary
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
39 :param test_kwargs: kwargs to pass to your closure when it is called.
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
40
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
41 :type subfeatures: boolean
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
42 :param subfeatures: when a feature is matched, should just that feature be
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
43 yielded to the caller, or should the entire sub_feature
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
44 tree for that feature be included? subfeatures=True is
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
45 useful in cases such as searching for a gene feature,
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
46 and wanting to know what RBS/Shine_Dalgarno_sequences
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
47 are in the sub_feature tree (which can be accomplished
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
48 with two feature_lambda calls). subfeatures=False is
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
49 useful in cases when you want to process (and possibly
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
50 return) the entire feature tree, such as applying a
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
51 qualifier to every single feature.
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
52
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
53 :rtype: yielded list
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
54 :return: Yields a list of matching features.
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
55 """
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
56 # Either the top level set of [features] or the subfeature attribute
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
57 for feature in feature_list:
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
58 if test(feature, **test_kwargs):
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
59 if not subfeatures:
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
60 feature_copy = copy.deepcopy(feature)
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
61 feature_copy.sub_features = []
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
62 yield feature_copy
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
63 else:
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
64 yield feature
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
65
6
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
66 if hasattr(feature, "sub_features"):
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
67 for x in feature_lambda(
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
68 feature.sub_features, test, test_kwargs, subfeatures=subfeatures
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
69 ):
0
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
70 yield x
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
71
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
72
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
73 def feature_test_qual_value(feature, **kwargs):
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
74 """Test qualifier values.
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
75
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
76 For every feature, check that at least one value in
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
77 feature.quailfiers(kwargs['qualifier']) is in kwargs['attribute_list']
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
78 """
6
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
79 for attribute_value in feature.qualifiers.get(kwargs["qualifier"], []):
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
80 if attribute_value in kwargs["attribute_list"]:
0
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
81 return True
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
82 return False
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
83
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
84
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
85 def __get_features(child, interpro=False):
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
86 child_features = {}
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
87 for rec in GFF.parse(child):
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
88 # Only top level
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
89 for feature in rec.features:
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
90 # Get the record id as parent_feature_id (since this is how it will be during remapping)
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
91 parent_feature_id = rec.id
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
92 # If it's an interpro specific gff3 file
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
93 if interpro:
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
94 # Then we ignore polypeptide features as they're useless
6
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
95 if feature.type == "polypeptide":
0
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
96 continue
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
97 # If there's an underscore, we strip up to that underscore?
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
98 # I do not know the rationale for this, removing.
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
99 # if '_' in parent_feature_id:
6
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
100 # parent_feature_id = parent_feature_id[parent_feature_id.index('_') + 1:]
0
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
101
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
102 try:
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
103 child_features[parent_feature_id].append(feature)
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
104 except KeyError:
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
105 child_features[parent_feature_id] = [feature]
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
106 # Keep a list of feature objects keyed by parent record id
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
107 return child_features
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
108
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
109
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
110 def __update_feature_location(feature, parent, protein2dna):
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
111 start = feature.location.start
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
112 end = feature.location.end
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
113 if protein2dna:
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
114 start *= 3
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
115 end *= 3
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
116
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
117 if parent.location.strand >= 0:
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
118 ns = parent.location.start + start
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
119 ne = parent.location.start + end
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
120 st = +1
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
121 else:
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
122 ns = parent.location.end - end
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
123 ne = parent.location.end - start
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
124 st = -1
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
125
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
126 # Don't let start/stops be less than zero. It's technically valid for them
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
127 # to be (at least in the model I'm working with) but it causes numerous
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
128 # issues.
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
129 #
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
130 # Instead, we'll replace with %3 to try and keep it in the same reading
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
131 # frame that it should be in.
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
132 if ns < 0:
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
133 ns %= 3
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
134 if ne < 0:
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
135 ne %= 3
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
136
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
137 feature.location = FeatureLocation(ns, ne, strand=st)
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
138
6
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
139 if hasattr(feature, "sub_features"):
0
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
140 for subfeature in feature.sub_features:
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
141 __update_feature_location(subfeature, parent, protein2dna)
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
142
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
143
6
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
144 def rebase(parent, child, interpro=False, protein2dna=False, map_by="ID"):
0
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
145 # get all of the features we will be re-mapping in a dictionary, keyed by parent feature ID
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
146 child_features = __get_features(child, interpro=interpro)
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
147
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
148 for rec in GFF.parse(parent):
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
149 replacement_features = []
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
150 for feature in feature_lambda(
6
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
151 rec.features,
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
152 # Filter features in the parent genome by those that are
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
153 # "interesting", i.e. have results in child_features array.
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
154 # Probably an unnecessary optimisation.
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
155 feature_test_qual_value,
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
156 {
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
157 "qualifier": map_by,
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
158 "attribute_list": child_features.keys(),
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
159 },
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
160 subfeatures=False,
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
161 ):
0
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
162
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
163 # Features which will be re-mapped
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
164 to_remap = child_features[feature.id]
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
165 # TODO: update starts
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
166 fixed_features = []
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
167 for x in to_remap:
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
168 # Then update the location of the actual feature
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
169 __update_feature_location(x, feature, protein2dna)
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
170
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
171 if interpro:
6
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
172 for y in ("status", "Target"):
0
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
173 try:
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
174 del x.qualifiers[y]
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
175 except Exception:
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
176 pass
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
177
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
178 fixed_features.append(x)
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
179 replacement_features.extend(fixed_features)
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
180 # We do this so we don't include the original set of features that we
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
181 # were rebasing against in our result.
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
182 rec.features = replacement_features
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
183 rec.annotations = {}
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
184 GFF.write([rec], sys.stdout)
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
185
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
186
6
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
187 if __name__ == "__main__":
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
188 parser = argparse.ArgumentParser(
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
189 description="rebase gff3 features against parent locations", epilog=""
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
190 )
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
191 parser.add_argument(
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
192 "parent", type=argparse.FileType("r"), help="Parent GFF3 annotations"
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
193 )
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
194 parser.add_argument(
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
195 "child",
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
196 type=argparse.FileType("r"),
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
197 help="Child GFF3 annotations to rebase against parent",
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
198 )
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
199 parser.add_argument(
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
200 "--interpro", action="store_true", help="Interpro specific modifications"
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
201 )
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
202 parser.add_argument(
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
203 "--protein2dna",
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
204 action="store_true",
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
205 help="Map protein translated results to original DNA data",
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
206 )
88b9b105c09b Uploaded
fubar
parents: 0
diff changeset
207 parser.add_argument("--map_by", help="Map by key", default="ID")
0
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
208 args = parser.parse_args()
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
209 rebase(**vars(args))