annotate aggregate.py @ 9:95d062ea06c3 draft

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