annotate aggregate.py @ 37:ceb1ee961db1 draft default tip

Uploaded
author kaymccoy
date Sun, 06 Nov 2016 20:30:31 -0500
parents bc8b54089989
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
36
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
1 # A translation of aggregate.pl into python! For analysis of Tn-Seq.
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
2 # This script requires BioPython just like calc_fitness.py, so you need it installed along with its dependencies if you want to run these scripts on your own.
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
3 # How to install BioPython and a list of its dependencies can be found here: http://biopython.org/DIST/docs/install/Installation.html
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
4 # K. McCoy
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
5
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
6
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
7
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
8
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
9
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
10
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
11
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
12
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
13
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
14 ##### ARGUMENTS #####
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
15
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
16 def print_usage():
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
17 print "Aggregate.py's usage is as follows:" + "\n\n"
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
18 print "\033[1m" + "Required" + "\033[0m" + "\n"
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
19 print "-o" + "\t\t" + "Output file for aggregated data." + "\n"
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
20 print "\n"
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
21 print "\033[1m" + "Optional" + "\033[0m" + "\n"
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
22 print "-c" + "\t\t" + "Check for missing genes in the data set - provide a reference genome in genbank format. Missing genes will be sent to stdout." + "\n"
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
23 print "-m" + "\t\t" + "Place a mark in an extra column for this set of genes. Provide a file with a list of genes seperated by newlines." + "\n"
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
24 print "-x" + "\t\t" + "Cutoff: Don't include fitness scores with average counts (c1+c2)/2 < x (default: 0)" + "\n"
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
25 print "-b" + "\t\t" + "Blanks: Exclude -b % of blank fitness scores (scores where c2 = 0) (default: 0 = 0%)" + "\n"
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
26 print "-f" + "\t\t" + "An in-between file carrying information on the blank count found from calc_fitness or consol_fitness; one of two ways to pass a blank count to this script" + "\n"
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
27 print "-w" + "\t\t" + "Use weighted algorithm to calculate averages, variance, sd, se" + "\n"
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
28 print "-l" + "\t\t" + "Weight ceiling: maximum value to use as a weight (default: 999,999)" + "\n"
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
29 print "\n"
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
30 print "All remainder arguements will be treated as fitness files (those files created by calc_fitness.py)" + "\n"
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
31 print "\n"
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
32
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
33 import argparse
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
34 parser = argparse.ArgumentParser()
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
35 parser.add_argument("-o", action="store", dest="summary")
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
36 parser.add_argument("-c", action="store", dest="find_missing")
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
37 parser.add_argument("-m", action="store", dest="marked")
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
38 parser.add_argument("-x", action="store", dest="cutoff")
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
39 parser.add_argument("-b", action="store", dest="blank_pc")
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
40 parser.add_argument("-f", action="store", dest="blank_file")
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
41 parser.add_argument("-w", action="store", dest="weighted")
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
42 parser.add_argument("-l", action="store", dest="weight_ceiling")
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
43 parser.add_argument("fitnessfiles", nargs=argparse.REMAINDER)
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
44
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
45 arguments = parser.parse_args()
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
46
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
47 if not arguments.summary:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
48 print "\n" + "You are missing a value for the -o flag. "
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
49 print_usage()
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
50 quit()
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
51
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
52 if not arguments.fitnessfiles:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
53 print "\n" + "You are missing fitness file(s); these should be entered immediately after all the flags. "
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
54 print_usage()
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
55 quit()
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
56
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
57 # 999,999 is a trivial placeholder number
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
58
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
59 if (not arguments.weight_ceiling):
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
60 arguments.weight_ceiling = 999999
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
61
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
62 # Cutoff exists to discard positions with a low number of counted transcripts, because their fitness may not be as accurate - for the same reasoning that studies with low sample sizes can be innacurate.
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
63
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
64 if (not arguments.cutoff):
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
65 arguments.cutoff = 0
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
66
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
67 # Gets information from the txt output file of calc_fit / consol, if inputted
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
68
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
69 if arguments.blank_file:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
70 with open(arguments.blank_file) as file:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
71 blank_pc = file.read().splitlines()
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
72 arguments.blank_pc = float(blank_pc[0].split()[1])
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
73
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
74 if (not arguments.blank_pc):
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
75 arguments.blank_pc = 0
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
76
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
77
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
78
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
79
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
80
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
81 ##### SUBROUTINES #####
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
82
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
83 # A subroutine that calculates the average, variance, standard deviation (sd), and standard error (se) of a group of scores; for use when aggregating scores by gene later on
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
84
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
85 import math
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
86 def unweighted_average(scores):
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
87 sum = 0
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
88 num = 0
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
89 i = 0
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
90 while i < len(scores):
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
91 if not scores[i]:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
92 scores[i] = 0.0
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
93 sum += float(scores[i])
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
94 num += 1
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
95 i += 1
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
96 average = sum/num
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
97 xminusxbars = 0
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
98 while i < len(scores):
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
99 xminusxbars += (float(scores[i]) - average)**2
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
100 if num <= 1:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
101 variance = 0
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
102 else:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
103 variance = xminusxbars/(num-1)
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
104 sd = math.sqrt(variance)
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
105 se = sd / math.sqrt(num)
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
106 return (average, variance, sd, se)
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
107
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
108 # A subroutine that calculates the weighted average, variance, standard deviation (sd), and standard error (se) of a group of scores; the weights come from the number of reads each insertion location has
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
109 # For use when aggregating scores by gene later on, if the weighted argument is called
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
110
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
111 def weighted_average(scores,weights):
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
112 sum = 0
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
113 weighted_average = 0
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
114 weighted_variance = 0
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
115 top = 0
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
116 bottom = 0
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
117 i = 0
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
118 while i < len(weights):
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
119 if not scores[i]:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
120 scores[i] = 0.0
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
121 top += float(weights[i])*float(scores[i])
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
122 bottom += float(weights[i])
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
123 i += 1
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
124 if bottom == 0:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
125 return 0
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
126 weighted_average = top/bottom
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
127 top = 0
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
128 bottom = 0
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
129 i = 0
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
130 while i < len(weights):
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
131 top += float(weights[i]) * (float(scores[i]) - weighted_average)**2
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
132 bottom += float(weights[i])
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
133 i += 1
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
134 weighted_variance = top/bottom
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
135 weighted_stdev = math.sqrt(weighted_variance)
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
136 weighted_stder = weighted_stdev/math.sqrt(len(scores))
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
137 return (weighted_average, weighted_variance, weighted_stdev, weighted_stder)
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
138
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
139
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
140
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
141
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
142
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
143
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
144
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
145
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
146
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
147
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
148 ##### AGGREGATION / CALCULATIONS #####
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
149
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
150 #Reads the genes which should be marked in the final aggregate file into an array
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
151
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
152 import os.path
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
153 if arguments.marked:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
154 with open(arguments.marked) as file:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
155 marked_set = file.read().splitlines()
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
156
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
157 #Creates a dictionary of dictionaries to contain a summary of all genes and their fitness values
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
158 #The fitness values and weights match up, so that the weight of gene_summary[locus]["w"][2] would be gene_summary[locus]["s"][2]
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
159
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
160 import csv
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
161 gene_summary = {}
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
162 for eachfile in arguments.fitnessfiles:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
163 with open(eachfile) as csvfile:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
164 lines = csv.reader(csvfile)
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
165 for line in lines:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
166 locus = line[9]
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
167 w = line[12]
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
168 if w == 'nW':
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
169 continue
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
170 if not w:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
171 w == 0
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
172 c1 = float(line[2])
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
173 c2 = float(line[3])
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
174 avg = (c1+c2)/2
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
175 if avg < float(arguments.cutoff):
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
176 continue
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
177 if avg > float(arguments.weight_ceiling):
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
178 avg = arguments.weight_ceiling
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
179 if locus not in gene_summary:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
180 gene_summary[locus] = {"w" : [], "s": []}
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
181 gene_summary[locus]["w"].append(w)
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
182 gene_summary[locus]["s"].append(avg)
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
183
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
184 #If finding any missing gene loci is requested in the arguments, starts out by loading all the known features from a genbank file
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
185
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
186 from Bio import SeqIO
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
187 if (arguments.find_missing):
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
188 output = [["locus","mean","var","sd","se","gene","Total","Blank","Not Blank","Blank Removed","M\n"]]
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
189 handle = open(arguments.find_missing, "rU")
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
190 for record in SeqIO.parse(handle, "genbank"):
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
191 refname = record.id
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
192 features = record.features
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
193 handle.close()
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
194
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
195 #Goes through the features to find which are genes
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
196
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
197 for feature in features:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
198 gene = ""
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
199 if feature.type == "gene":
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
200 locus = "".join(feature.qualifiers["locus_tag"])
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
201 if "gene" in feature.qualifiers:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
202 gene = "".join(feature.qualifiers["gene"])
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
203 else:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
204 continue
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
205
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
206 #Goes through the fitness scores of insertions within each gene, and removes whatever % of blank fitness scores were requested along with their corresponding weights
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
207
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
208 sum = 0
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
209 num = 0
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
210 avgsum = 0
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
211 blank_ws = 0
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
212 i = 0
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
213 if locus in gene_summary.keys():
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
214 for w in gene_summary[locus]["w"]:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
215 if float(w) == 0:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
216 blank_ws += 1
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
217 else:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
218 sum += float(w)
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
219 num += 1
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
220 count = num + blank_ws
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
221 removed = 0
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
222 to_remove = int(float(arguments.blank_pc)*count)
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
223 if blank_ws > 0:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
224 i = 0
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
225 while i < len(gene_summary[locus]["w"]):
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
226 w = gene_summary[locus]["w"][i]
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
227 if removed == to_remove:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
228 break
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
229 if float(w) == 0:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
230 del gene_summary[locus]["w"][i]
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
231 del gene_summary[locus]["s"][i]
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
232 removed += 1
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
233 i -= 1
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
234 i += 1
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
235
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
236 #If all the fitness values within a gene are empty, sets mean/var to 0.10 and Xs out sd/se; marks the gene if that's requested
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
237
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
238 if num == 0:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
239 if (arguments.marked and locus in marked_set):
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
240 output.append([locus, "0.10", "0.10", "X", "X", gene, count, blank_ws, num, removed, "M", "\n"])
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
241 else:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
242 output.append([locus, "0.10", "0.10", "X", "X", gene, count, blank_ws, num, removed, "\n"])
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
243
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
244 #Otherwise calls average() or weighted_average() to find the aggregate w / count / standard deviation / standard error of the insertions within each gene; marks the gene if that's requested
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
245
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
246 else:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
247 if not arguments.weighted:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
248 (average, variance, stdev, stderr) = unweighted_average(gene_summary[locus]["w"])
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
249 else:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
250 (average, variance, stdev, stderr) = weighted_average(gene_summary[locus]["w"],gene_summary[locus]["s"])
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
251 if (arguments.marked and locus in marked_set):
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
252 output.append([locus, average, variance, stdev, stderr, gene, count, blank_ws, num, removed, "M", "\n"])
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
253 else:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
254 output.append([locus, average, variance, stdev, stderr, gene, count, blank_ws, num, removed, "\n"])
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
255
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
256 #If a gene doesn't have any insertions, sets mean/var to 0.10 and Xs out sd/se, plus leaves count through removed blank because there were no reads.
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
257
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
258 else:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
259 if (arguments.marked and locus in marked_set):
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
260 output.append([locus, "0.10", "0.10", "X", "X", gene, "", "", "", "", "M", "\n"])
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
261 else:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
262 output.append([locus, "0.10", "0.10", "X", "X", gene, "", "", "", "", "\n"])
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
263
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
264 #Writes the aggregated fitness file
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
265
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
266 with open(arguments.summary, "wb") as csvfile:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
267 writer = csv.writer(csvfile)
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
268 writer.writerows(output)
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
269
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
270 #If finding missing genes is not requested, just finds the aggregate w / count / standard deviation / standard error of the insertions within each gene, and writes them to a file, plus marks the genes requested
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
271 #This is never called through Galaxy since finding missing genes is just better than not finding them.
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
272
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
273 else:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
274 output = [["Locus","W","Count","SD","SE","M\n"]]
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
275 for gene in gene_summary.keys():
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
276 sum = 0
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
277 num = 0
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
278 average = 0
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
279 if "w" not in gene_summary[gene]:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
280 continue
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
281 for i in gene_summary[gene]["w"]:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
282 sum += i
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
283 num += 1
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
284 average = sum/num
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
285 xminusxbars = 0
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
286 for i in w:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
287 xminusxbars += (i-average)**2
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
288 if num > 1:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
289 sd = math.sqrt(xminusxbars/(num-1))
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
290 se = sd / math.sqrt(num)
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
291 if (arguments.marked and locus in marked_set):
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
292 output.append([gene, average, num, sd, se, "M", "\n"])
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
293 else:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
294 output.append([gene, average, num, sd, se, "\n"])
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
295 with open(arguments.summary, "wb") as csvfile:
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
296 writer = csv.writer(csvfile)
bc8b54089989 Uploaded
kaymccoy
parents:
diff changeset
297 writer.writerows(output)