upstream-extended/iuc/tools/circos/tiles-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 "name": feature.qualifiers.get(attr, ["None"])[0] or feature.id,
26 "value": feature.qualifiers.get("score", [0])[0],
27 "type": feature.type,
28 }
29
30 line = [
31 record.id,
32 str(int(feature.location.start)),
33 str(int(feature.location.end)),
34 ",".join(["%s=%s" % x for x in sorted(kv.items())]),
35 ]
36
37 sys.stdout.write("\t".join(line))
38 sys.stdout.write("\n")