-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmakeAGPfromFasta.py
65 lines (53 loc) · 1.81 KB
/
makeAGPfromFasta.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
63
64
65
#!/usr/bin/env python
#requires exactly 2 inputs
#a fasta file to print contig lengths
#writes results to stdout
# from juicebox https://github.com/phasegenomics/juicebox_scripts/blob/master/juicebox_scripts/makeAgpFromFasta.py
from __future__ import print_function
import sys
def printUsage():
print("\nmakeAgpFromFasta.py usage:", end='')
print("\tmakeAgpFromFasta.py <fasta_file> <agp_out_file>")
return
#this funtion returns true if line is a contig+bin line
def isContigBinLine(line):
ret = 0
if len(line) > 1:
ret = (line[0:1] == ">")
return ret
#this function returns the contig+bin portion of a contig+bin line
def getContigBinFromLine(line):
ret = ""
if isContigBinLine(line):
#trim the >, split the line, and grab the first token, in case there is a long description on the line
ret = line[1:].split()[0].strip()
return ret
def main():
if len(sys.argv) != 3:
printUsage()
sys.exit()
fname = sys.argv[1]
outfile = sys.argv[2]
contig = ""
counting = 0
count = 0
total = 0
with open(fname) as file:
with open(outfile, 'w') as outf:
for line in file:
line = line.strip()
if isContigBinLine(line):
if counting:
outf.write("{0}\t0\t{1}\t1\tW\t{0}\t1\t{1}\t+\n".format(contig, count))
else:
counting = 1
contig = getContigBinFromLine(line)
count = 0
elif counting:
count += len(line)
total += len(line)
else:
continue
outf.write("{0}\t0\t{1}\t1\tW\t{0}\t1\t{1}\t+\n".format(contig, count))
if __name__ == "__main__":
main()