-
Notifications
You must be signed in to change notification settings - Fork 2
/
make_index.py
65 lines (50 loc) · 1.25 KB
/
make_index.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/python
#
# genome reading functions
#
#
#
#######################################################
import sys
from math import log10
def main(genome_file):
print "File:", genome_file
fragments_points = make_index(genome_file)
if fragments_points == []:
print "There is no genome sequence in the file:%s\n" % genome_file
else:
print "%i genomes found:" % len(fragments_points)
def make_index(data_file):
res = []
print "Reading file. Please wait..."
with open(data_file) as f:
line = f.readline()
Cn = 0
N = 0
K = 0
ID = ''
while line != '':
LL = len(line)
if line[0] == '>':
res.append((ID,N,K))
K = 0
N = Cn+LL
ID = line.split()[0][1:]
else:
K += LL
Cn += LL
line = f.readline()
res.append((ID,N,K))
res.pop(0)
f.close()
with open(data_file+'.index','w') as f:
for t in res:
f.write(t[0]+" "+str(t[1])+" "+str(t[2])+"\n")
f.close()
return res
if __name__ == '__main__':
args = sys.argv[:]
if len(args) == 2:
main(args[1])
else:
print "Usage: <python> <genome_read.py> <data_file>"