-
Notifications
You must be signed in to change notification settings - Fork 0
/
gffcat.py
62 lines (50 loc) · 1.86 KB
/
gffcat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python3
import argparse
from sys import stderr, stdout, stdin
KNOWN_HEADERS = {
"##gff-version",
"##feature-ontology",
}
def gffcat_main(argv=None):
"""Concatenate GFF3 files, resepcting header lines and FASTA sections"""
ap = argparse.ArgumentParser("gffcat")
ap.add_argument("-o", "--output", type=argparse.FileType("wt"), default=stdout,
help="Output gff file")
ap.add_argument("inputs", nargs="+")
args = ap.parse_args(argv)
fastalines = []
bodylines = []
headerlines = {}
for file in args.inputs:
with open(file) as fh:
in_fasta = False
for line in fh:
line = line.rstrip()
if not line:
continue
ll = line.lower().split()[0]
if ll in KNOWN_HEADERS:
if ll in headerlines:
if headerlines[ll] != line:
print("WARN: header line with different value in differnt files. Only using value from first file", file=stderr)
else:
headerlines[ll] = line
elif ll.startswith("##fasta"):
in_fasta = True
else:
if in_fasta:
fastalines.append(line)
else:
bodylines.append(line)
for hdr in KNOWN_HEADERS:
if hdr in headerlines:
print(headerlines[hdr], file=args.output)
for line in bodylines:
print(line, file=args.output)
if fastalines:
print("##fasta", file=args.output)
for line in fastalines:
print(line, file=args.output)
print(f"DONE! {len(headerlines)} headers, {len(bodylines)} body lines, {len(fastalines)} FASTA lines", file=stderr)
if __name__ == "__main__":
gffcat_main()