forked from drnida/biosort
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfailparser.py
More file actions
164 lines (127 loc) · 3.71 KB
/
failparser.py
File metadata and controls
164 lines (127 loc) · 3.71 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#builds key:value dictionaries
def dict_builder():
for line in f:
gene = line
#splits row from csv file into fields
ln = gene.split(",")
#ln0 is gen number
#ln1 is success/fail
#ln2 is ops count
#ln3 is lineage ID
#ln4 is directory
#ln5 is gene
#counts the occurances of a specific gene
if ln[5] in gene_count:
gene_count[ln[5]] += 1
else:
gene_count[ln[5]] = 1
if ln[5] in gen_appear and int(ln[0][1:]) < int(gen_appear[ln[5]]):
gen_appear[ln[5]] = ln[0][1:]
elif ln[5] not in gen_appear:
gen_appear[ln[5]] = ln[0][1:]
if ln[5] in success_fail:
success_fail[ln[5]].append(ln[1])
else:
success_fail[ln[5]] = [ln[1]]
try:
f = open("masterlog.txt", 'r')
except:
print "unable to open masterlog.txt"
gene_count = {}
gen_op_sum = {}
n_per_gen = {}
id_vs_gen = {}
gen_appear = {}
success_fail = {}
dict_builder()
try:
l = open("success_ratio.txt", 'w')
except:
print "unable to open success_ratio.txt"
l.write("gen_appeared,successes,failures,success/failure" +'\n')
for gene in success_fail:
successes = 0
failures = 0
for x in success_fail[gene]:
if x == "S":
successes += 1
if x == "F":
failures += 1
try:
ratio = float(successes)/float(failures)
except:
ratio = successes
if(ratio > 0):
csvrow = gen_appear[gene] +','+ str(successes) +','+ str(failures) +','+ str(ratio)
l.write(csvrow + '\n')
l.close
"""
maxcount = 0
for gene in gene_count:
if maxcount < int(gene_count[gene]):
maxcount = int(gene_count[gene])
print maxcount
#sums op counts for a generation
if ln[0] in gen_op_sum:
gen_op_sum[ln[0]] += int(ln[2])
else:
gen_op_sum[ln[0]] = int(ln[2])
#counts number of individuals in a generation
if ln[0] in n_per_gen:
n_per_gen[ln[0]] += 1
else:
n_per_gen[ln[0]] = 1
#creates list of all generations in which a given lineage ID occured
if ln[3] in id_vs_gen:
id_vs_gen[ln[3]].append(ln[0][1:])
else:
id_vs_gen[ln[3]] = [ln[0][1:]]
"""
"""
def lineage_vs_generations():
try:
l = open("lineage_vs_generation.txt", 'w')
except:
print "unable to open lineage_vs_generation.txt"
return 0
l.write("lineageID,number_of_gen_survived,first_gen_seen,last_gen_seen\n")
#prints "lineageID,number of gen survived by lineage, first gen seen, last gen seen"
for lineage in id_vs_gen:
first_gen = int(id_vs_gen[lineage][0])
last_gen = int(id_vs_gen[lineage][0])
for x in id_vs_gen[lineage]:
if int(x) < first_gen:
first_gen = int(x)
if int(x) > last_gen:
last_gen = int(x)
l.write( lineage +',' + str(last_gen - first_gen) +','+ str(first_gen) +','+ str(last_gen) + '\n')
l.close()
def ave_vs_generation():
try:
l = open("ave_vs_gen.txt", 'w')
except:
print "unable to open 'ave_vs_gen.txt'"
return 0
l.write("generation,ave_ops_per_gen\n")
#prints "generation,ave ops per generation"
for gen in gen_op_sum:
#print str(gen) + "," + str(gen_op_sum[gen]) + "," +str(n_per_gen[gen])
ave = int(gen_op_sum[gen])/int(n_per_gen[gen])
l.write( gen[1:] + ',' + str(ave) +'\n')
l.close()
def gene_lifespan():
try:
l = open("gene_lifespan.txt", 'w')
except:
print "Unable to open gene_lifespan.txt"
return 0
l.write("gen_survived\n")
max_gen = 0
#Number of generations a given gene survived
for gene in gene_count:
l.write(str(gene_count[gene]) + '\n')
if gene_count[gene] > max_gen:
max_gen = gene_count[gene]
l.write("Longest living gene (generations): " + str(max_gen))
l.close()
"""