15
|
1 #!/usr/bin/env python3
|
|
2 import argparse
|
|
3 import tempfile
|
|
4 import shutil
|
|
5 import sys
|
|
6
|
|
7 def parse_args():
|
|
8 '''Argument parsin'''
|
|
9 description = """
|
|
10 parsing cap3 assembly aln output
|
|
11 """
|
|
12
|
|
13 parser = argparse.ArgumentParser(
|
|
14 description=description,
|
|
15 formatter_class=argparse.RawTextHelpFormatter)
|
|
16 parser.add_argument(
|
|
17 '-g',
|
|
18 '--gff_file',
|
|
19 default=None,
|
|
20 required=True,
|
|
21 help="input gff3 file for appending coverage information",
|
|
22 type=str,
|
|
23 action='store')
|
|
24 parser.add_argument(
|
|
25 '-p',
|
|
26 '--profile',
|
|
27 default=None,
|
|
28 required=True,
|
|
29 help="output file for coverage profile",
|
|
30 type=str,
|
|
31 action="store")
|
|
32 return parser.parse_args()
|
|
33
|
|
34 def read_coverage(profile):
|
|
35 with open(profile) as p:
|
|
36 d = {}
|
|
37 for name, prof in zip(p, p):
|
|
38 d[name[1:].strip()] = [int(i) for i in prof.split()]
|
|
39 return d
|
|
40
|
|
41
|
|
42 def main():
|
|
43 args = parse_args()
|
|
44 coverage_hash = read_coverage(args.profile)
|
|
45 gff_tmp = tempfile.NamedTemporaryFile()
|
|
46 with open(args.gff_file) as f, open(gff_tmp.name, 'w') as out:
|
|
47 for line in f:
|
|
48 if line[0] == "#":
|
|
49 out.write(line)
|
|
50 else:
|
|
51 line_parts = line.split()
|
|
52 start = int(line_parts[3])
|
|
53 end = int(line_parts[4])
|
|
54 coverage = round( sum(coverage_hash[line_parts[0]][(
|
|
55 start - 1):end]) / (end - start + 1), 3)
|
|
56 new_line = "{};Coverage={}\n".format(line.strip(), coverage)
|
|
57 out.write(new_line)
|
|
58
|
|
59 shutil.copyfile(gff_tmp.name, args.gff_file)
|
|
60
|
|
61
|
|
62 if __name__ == "__main__":
|
|
63
|
|
64 main()
|