Source code

Download source archive ↓

upstream-extended/iuc/tools/circos/text-from-gff3.py

1#!/usr/bin/env python
2import logging
3import sys
4
5from BCBio import GFF
6
7logging.basicConfig(level=logging.INFO)
8log = logging.getLogger()
9
10
11if __name__ == "__main__":
12    attr = sys.argv[2]
13
14    for record in GFF.parse(sys.argv[1]):
15        if len(record.features) == 0:
16            continue
17
18        for feature in sorted(record.features, key=lambda x: x.location.start):
19            # chrom chromStart chromEnd
20            # name score strand
21            # thickStart thickEnd itemRgb
22
23            kv = {
24                "strand": 0 if not feature.location.strand else feature.location.strand,
25                "value": feature.qualifiers.get("score", [0])[0],
26            }
27
28            if attr not in feature.qualifiers:
29                continue
30
31            name = feature.qualifiers[attr][0]
32
33            line = [
34                record.id,
35                str(int(feature.location.start)),
36                str(int(feature.location.end)),
37                name,
38                ",".join(["%s=%s" % x for x in sorted(kv.items())]),
39            ]
40
41            sys.stdout.write("\t".join(line))
42            sys.stdout.write("\n")