comparison cpt_promote_qualifiers/promote_qualifier.py @ 0:6f4c46313117 draft default tip

Uploaded
author cpt
date Fri, 17 Jun 2022 13:05:11 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:6f4c46313117
1 #!/usr/bin/env python
2 import argparse
3 import sys
4 import logging
5 from CPT_GFFParser import gffParse, gffWrite
6 from gff3 import feature_lambda, feature_test_type
7
8 logging.basicConfig(level=logging.INFO)
9 log = logging.getLogger(__name__)
10
11
12 def promote_qualifier(qualifier, parent, child, gff3):
13 for record in gffParse(gff3):
14 for parent_feature in feature_lambda(
15 record.features, feature_test_type, {"type": parent}, subfeatures=True
16 ):
17 # for each feature of the parent type, get the first subfeature of the child type
18 try:
19 first_child = sorted(
20 list(
21 feature_lambda(
22 parent_feature.sub_features,
23 feature_test_type,
24 {"type": child},
25 subfeatures=False,
26 )
27 ),
28 key=lambda x: x.location.start
29 if parent_feature.strand > 0
30 else x.location.end,
31 reverse=False if parent_feature.strand > 0 else True,
32 )[0]
33 except IndexError:
34 logging.warning("Child type %s not found under parent %s" % (child, parent_feature.qualifiers["ID"]))
35 continue
36 try:
37 parent_feature.qualifiers[qualifier] = first_child.qualifiers[qualifier]
38 logging.info(
39 "Promoted %s=%s in child %s to parent %s"
40 % (
41 qualifier,
42 first_child.qualifiers[qualifier],
43 first_child.qualifiers["ID"],
44 parent_feature.qualifiers["ID"],
45 )
46 )
47 except KeyError:
48 logging.warning(
49 "Qualifier %s not found in child feature %s"
50 % (qualifier, first_child.qualifiers["ID"])
51 )
52 gffWrite([record], sys.stdout)
53
54
55 if __name__ == "__main__":
56 parser = argparse.ArgumentParser(
57 description="Promote a child feature's qualifer to the parent feature's qualifier",
58 epilog="",
59 )
60 parser.add_argument("gff3", type=argparse.FileType("r"), help="GFF3 File")
61 parser.add_argument(
62 "parent",
63 type=str,
64 help="Feature type of the target parent feature (ex: gene, mrna, exon",
65 )
66 parser.add_argument(
67 "child",
68 type=str,
69 help="Feature type of the target child feature (ex: mrna, exon, CDS",
70 )
71 parser.add_argument(
72 "qualifier", help="Sepcific qualifier to promote (ex: Name, product, notes"
73 )
74 args = parser.parse_args()
75 promote_qualifier(**vars(args))