annotate gff3_rebase.py @ 0:e54940ea270c draft

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