annotate regionFitness.pl @ 11:2655e8056329 draft default tip

Uploaded
author antmarge
date Tue, 28 Mar 2017 14:05:20 -0400
parents e8749de582fa
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
e8749de582fa Uploaded
antmarge
parents:
diff changeset
1 #!/usr/bin/perl -w
e8749de582fa Uploaded
antmarge
parents:
diff changeset
2
e8749de582fa Uploaded
antmarge
parents:
diff changeset
3 #Margaret Antonio 17.01.06
e8749de582fa Uploaded
antmarge
parents:
diff changeset
4
e8749de582fa Uploaded
antmarge
parents:
diff changeset
5 # Sliding Window specifically for Galaxy implementation.
e8749de582fa Uploaded
antmarge
parents:
diff changeset
6
e8749de582fa Uploaded
antmarge
parents:
diff changeset
7 use strict;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
8 use Getopt::Long;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
9 use warnings;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
10 use Bio::SeqIO;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
11 use Data::Random qw(:all);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
12 use List::BinarySearch qw( :all );
e8749de582fa Uploaded
antmarge
parents:
diff changeset
13 use autodie;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
14 no warnings;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
15
e8749de582fa Uploaded
antmarge
parents:
diff changeset
16 #AVAILABLE OPTIONS. WILL PRINT UPON ERROR
e8749de582fa Uploaded
antmarge
parents:
diff changeset
17 sub print_usage() {
e8749de582fa Uploaded
antmarge
parents:
diff changeset
18
e8749de582fa Uploaded
antmarge
parents:
diff changeset
19 print "\n####################################################################\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
20 print "USAGE:\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
21 print "slidingWindow.pl -f <fasta file> -r <genbank file> <inputs>\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
22
e8749de582fa Uploaded
antmarge
parents:
diff changeset
23 print "\nREQUIRED:\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
24 print " -d\tDirectory containing all input files (output files from\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
25 print " \tcalcFitness tool)\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
26 print " \tOR\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
27 print " \tIn the command line (without a flag), input filename(s)\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
28 print " -f\tFilename for genome sequence, in fasta format\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
29 print " -r\tFilename for genome annotation, in GenBank format\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
30
e8749de582fa Uploaded
antmarge
parents:
diff changeset
31 print "\nOPTIONAL:\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
32 print " -h\tPrint usage\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
33 print " -size\tThe size of the sliding window. Default=500\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
34 print " -step\tThe window spacing. Default=10\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
35 print " -c\tExclude values with avg. counts less than x where (c1+c2)/2<x\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
36 print " \tDefault=15\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
37 print " -log\tSend all output to a log file instead of the terminal\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
38 print " -max\tExpected max number of TA sites in a window.\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
39 print " \tUsed for creating null distribution library. Default=100\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
40 print " -w\tDo weighted average for fitness per insertion\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
41 print " -wc\tInteger value for weight ceiling. Default=50\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
42
e8749de582fa Uploaded
antmarge
parents:
diff changeset
43 print " \n~~~~Always check that file paths are correctly specified~~~~\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
44 print "\n##################################################################\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
45
e8749de582fa Uploaded
antmarge
parents:
diff changeset
46
e8749de582fa Uploaded
antmarge
parents:
diff changeset
47 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
48
e8749de582fa Uploaded
antmarge
parents:
diff changeset
49 #ASSIGN INPUTS TO VARIABLES
e8749de582fa Uploaded
antmarge
parents:
diff changeset
50 our ($cutoff,$step, $size, $help,$fasta, $log, $ref,$weight_ceiling,$weight,$custom,$run,$max,$f1,$f2,$f3,$f4,$f5,$f6);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
51
e8749de582fa Uploaded
antmarge
parents:
diff changeset
52 GetOptions(
e8749de582fa Uploaded
antmarge
parents:
diff changeset
53 'c:i'=>\$cutoff,
e8749de582fa Uploaded
antmarge
parents:
diff changeset
54 'step:i' => \$step,
e8749de582fa Uploaded
antmarge
parents:
diff changeset
55 'size:i' => \$size,
e8749de582fa Uploaded
antmarge
parents:
diff changeset
56 'f:s' => \$fasta,
e8749de582fa Uploaded
antmarge
parents:
diff changeset
57 'r:s' => \$ref,
e8749de582fa Uploaded
antmarge
parents:
diff changeset
58 'log' => \$log,
e8749de582fa Uploaded
antmarge
parents:
diff changeset
59 'h' => \$help,
e8749de582fa Uploaded
antmarge
parents:
diff changeset
60 'm:i'=>\$max,
e8749de582fa Uploaded
antmarge
parents:
diff changeset
61 'wc:i'=>\$weight_ceiling,
e8749de582fa Uploaded
antmarge
parents:
diff changeset
62 'w'=>\$weight,
e8749de582fa Uploaded
antmarge
parents:
diff changeset
63 'u:s'=>\$custom,
e8749de582fa Uploaded
antmarge
parents:
diff changeset
64 'n:s'=>\$run,
e8749de582fa Uploaded
antmarge
parents:
diff changeset
65 'f1:s'=>\$f1,
e8749de582fa Uploaded
antmarge
parents:
diff changeset
66 'f2:s'=>\$f2,
e8749de582fa Uploaded
antmarge
parents:
diff changeset
67 'f3:s'=>\$f3,
e8749de582fa Uploaded
antmarge
parents:
diff changeset
68 'f4:s'=>\$f4,
e8749de582fa Uploaded
antmarge
parents:
diff changeset
69 'f5:s'=>\$f5,
e8749de582fa Uploaded
antmarge
parents:
diff changeset
70 'f6:s'=>\$f6,
e8749de582fa Uploaded
antmarge
parents:
diff changeset
71
e8749de582fa Uploaded
antmarge
parents:
diff changeset
72 );
e8749de582fa Uploaded
antmarge
parents:
diff changeset
73
e8749de582fa Uploaded
antmarge
parents:
diff changeset
74 sub get_time() {
e8749de582fa Uploaded
antmarge
parents:
diff changeset
75 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
76 return "$hour:$min:$sec";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
77 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
78
e8749de582fa Uploaded
antmarge
parents:
diff changeset
79 sub uniq {
e8749de582fa Uploaded
antmarge
parents:
diff changeset
80 my %seen;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
81 return grep { ! $seen{$_}++ } @_;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
82 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
83
e8749de582fa Uploaded
antmarge
parents:
diff changeset
84 # Just to test out the script opening
e8749de582fa Uploaded
antmarge
parents:
diff changeset
85 if ($help){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
86 print_usage();
e8749de582fa Uploaded
antmarge
parents:
diff changeset
87 print "\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
88 exit;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
89 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
90 my $round='%.4f';
e8749de582fa Uploaded
antmarge
parents:
diff changeset
91
e8749de582fa Uploaded
antmarge
parents:
diff changeset
92 # every output file will have this prefix
e8749de582fa Uploaded
antmarge
parents:
diff changeset
93 #if (!$run){$run = "run1";}
e8749de582fa Uploaded
antmarge
parents:
diff changeset
94 $run = $run . "_";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
95
e8749de582fa Uploaded
antmarge
parents:
diff changeset
96 if (!$weight_ceiling){$weight_ceiling=50;}
e8749de582fa Uploaded
antmarge
parents:
diff changeset
97 if (!$cutoff){$cutoff=15;}
e8749de582fa Uploaded
antmarge
parents:
diff changeset
98
e8749de582fa Uploaded
antmarge
parents:
diff changeset
99
e8749de582fa Uploaded
antmarge
parents:
diff changeset
100 #open (STDOUT, ">>", $run . "log.txt");
e8749de582fa Uploaded
antmarge
parents:
diff changeset
101
e8749de582fa Uploaded
antmarge
parents:
diff changeset
102 #CHECKING PARAMETERS: Check to make sure required option inputs are there and if not then assign default
e8749de582fa Uploaded
antmarge
parents:
diff changeset
103 if (!$size) { $size = 500 }; #set the default sliding window size to 500
e8749de582fa Uploaded
antmarge
parents:
diff changeset
104 if (!$step) { $step = 10 }; #set the default step size to 10
e8749de582fa Uploaded
antmarge
parents:
diff changeset
105 if (!$cutoff) { $cutoff = 10 };
e8749de582fa Uploaded
antmarge
parents:
diff changeset
106 if (!$max) { $max = 100 }; # most insertions expected in a given window region (for making null dist lib)
e8749de582fa Uploaded
antmarge
parents:
diff changeset
107
e8749de582fa Uploaded
antmarge
parents:
diff changeset
108 print "Window size: $size\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
109 print "Step value: $step\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
110 print "Cutoff: $cutoff\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
111
e8749de582fa Uploaded
antmarge
parents:
diff changeset
112
e8749de582fa Uploaded
antmarge
parents:
diff changeset
113 # Returns mean, variance, sd, se
e8749de582fa Uploaded
antmarge
parents:
diff changeset
114 sub average {
e8749de582fa Uploaded
antmarge
parents:
diff changeset
115 my $scoreref = shift @_;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
116 my @scores = @{$scoreref};
e8749de582fa Uploaded
antmarge
parents:
diff changeset
117
e8749de582fa Uploaded
antmarge
parents:
diff changeset
118 my $sum=0;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
119 my $num=0;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
120
e8749de582fa Uploaded
antmarge
parents:
diff changeset
121 # Get the average.
e8749de582fa Uploaded
antmarge
parents:
diff changeset
122 foreach my $w (@scores) {
e8749de582fa Uploaded
antmarge
parents:
diff changeset
123 $sum += $w;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
124 $num++;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
125 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
126 my $average= $sum/$num;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
127 my $xminusxbars = 0;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
128
e8749de582fa Uploaded
antmarge
parents:
diff changeset
129 # Get the variance.
e8749de582fa Uploaded
antmarge
parents:
diff changeset
130 foreach my $w (@scores) {
e8749de582fa Uploaded
antmarge
parents:
diff changeset
131 $xminusxbars += ($w-$average)**2;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
132 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
133 my $variance;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
134 if ($num<=1){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
135 $variance=0.10;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
136 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
137 else{
e8749de582fa Uploaded
antmarge
parents:
diff changeset
138 $variance = sprintf($round,(1/($num-1)) * $xminusxbars);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
139 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
140 my $sd = sprintf($round,sqrt($variance));
e8749de582fa Uploaded
antmarge
parents:
diff changeset
141 my $se = sprintf($round,$sd / sqrt($num));
e8749de582fa Uploaded
antmarge
parents:
diff changeset
142
e8749de582fa Uploaded
antmarge
parents:
diff changeset
143 return ($average, $variance, $sd, $se);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
144
e8749de582fa Uploaded
antmarge
parents:
diff changeset
145 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
146
e8749de582fa Uploaded
antmarge
parents:
diff changeset
147 # Function to clean unwanted characters from lines
e8749de582fa Uploaded
antmarge
parents:
diff changeset
148
e8749de582fa Uploaded
antmarge
parents:
diff changeset
149 sub cleaner{
e8749de582fa Uploaded
antmarge
parents:
diff changeset
150 my $line=$_[0];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
151 chomp($line);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
152 $line =~ s/\x0d{0,1}\x0a{0,1}\Z//s;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
153 return $line;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
154 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
155
e8749de582fa Uploaded
antmarge
parents:
diff changeset
156 # Takes two parameters, both hashrefs to lists.
e8749de582fa Uploaded
antmarge
parents:
diff changeset
157 # 1) hashref to list of scores
e8749de582fa Uploaded
antmarge
parents:
diff changeset
158 # 2) hashref to list of weights, equal in length to the scores.
e8749de582fa Uploaded
antmarge
parents:
diff changeset
159 sub weighted_average {
e8749de582fa Uploaded
antmarge
parents:
diff changeset
160
e8749de582fa Uploaded
antmarge
parents:
diff changeset
161 my $scoreref = shift @_;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
162 my $weightref = shift @_;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
163 my @scores = @{$scoreref};
e8749de582fa Uploaded
antmarge
parents:
diff changeset
164 my @weights = @{$weightref};
e8749de582fa Uploaded
antmarge
parents:
diff changeset
165
e8749de582fa Uploaded
antmarge
parents:
diff changeset
166 my $sum=0;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
167 my ($weighted_average, $weighted_variance)=(0,0);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
168 my $v2;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
169
e8749de582fa Uploaded
antmarge
parents:
diff changeset
170 # Go through once to get total, calculate V2.
e8749de582fa Uploaded
antmarge
parents:
diff changeset
171 for (my $i=0; $i<@weights; $i++) {
e8749de582fa Uploaded
antmarge
parents:
diff changeset
172 $sum += $weights[$i];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
173 $v2 += $weights[$i]**2;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
174 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
175 if ($sum == 0) { return 0; } # No scores given?
e8749de582fa Uploaded
antmarge
parents:
diff changeset
176
e8749de582fa Uploaded
antmarge
parents:
diff changeset
177 my $scor = join (' ', @scores);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
178 my $wght = join (' ', @weights);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
179
e8749de582fa Uploaded
antmarge
parents:
diff changeset
180 # Now calculated weighted average.
e8749de582fa Uploaded
antmarge
parents:
diff changeset
181 my ($top, $bottom) = (0,0);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
182 for (my $i=0; $i<@weights; $i++) {
e8749de582fa Uploaded
antmarge
parents:
diff changeset
183 $top += $weights[$i] * $scores[$i];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
184 $bottom += $weights[$i];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
185 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
186 $weighted_average = sprintf($round,$top/$bottom);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
187
e8749de582fa Uploaded
antmarge
parents:
diff changeset
188 ($top, $bottom) = (0,0);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
189 # Now calculate weighted sample variance.
e8749de582fa Uploaded
antmarge
parents:
diff changeset
190 for (my $i=0; $i<@weights; $i++) {
e8749de582fa Uploaded
antmarge
parents:
diff changeset
191 $top += ( $weights[$i] * ($scores[$i] - $weighted_average)**2);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
192 $bottom += $weights[$i];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
193 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
194 $weighted_variance = sprintf($round,$top/$bottom);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
195
e8749de582fa Uploaded
antmarge
parents:
diff changeset
196 my $weighted_stdev = sprintf($round,sqrt($weighted_variance));
e8749de582fa Uploaded
antmarge
parents:
diff changeset
197 my $weighted_stder = sprintf($round,$weighted_stdev / sqrt(@scores)); # / length scores.
e8749de582fa Uploaded
antmarge
parents:
diff changeset
198
e8749de582fa Uploaded
antmarge
parents:
diff changeset
199 return ($weighted_average, $weighted_variance, $weighted_stdev, $weighted_stder);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
200 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
201
e8749de582fa Uploaded
antmarge
parents:
diff changeset
202 #CREATE AN ARRAY OF DATA FROM INPUT CSV FILE(S)
e8749de582fa Uploaded
antmarge
parents:
diff changeset
203
e8749de582fa Uploaded
antmarge
parents:
diff changeset
204 my $rowCount=-1;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
205 my $last=0;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
206 my @unsorted;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
207 my @insertPos; #array to hold all positions of insertions. Going to use this later to match up with TA sites
e8749de582fa Uploaded
antmarge
parents:
diff changeset
208 my %wind_summary;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
209
e8749de582fa Uploaded
antmarge
parents:
diff changeset
210 my @files = @ARGV;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
211
e8749de582fa Uploaded
antmarge
parents:
diff changeset
212 my $num=(scalar @files);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
213
e8749de582fa Uploaded
antmarge
parents:
diff changeset
214 my $datestring = localtime();
e8749de582fa Uploaded
antmarge
parents:
diff changeset
215 print "Local date and time $datestring\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
216
e8749de582fa Uploaded
antmarge
parents:
diff changeset
217 print "\n---------Importing files--------\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
218 print "\tStart input array ",get_time(),"\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
219 print "\tNumber of csv files: ", $num,"\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
220
e8749de582fa Uploaded
antmarge
parents:
diff changeset
221
e8749de582fa Uploaded
antmarge
parents:
diff changeset
222 #Go through each file from the commandline (ARGV array) and read each line as an array
e8749de582fa Uploaded
antmarge
parents:
diff changeset
223 #into select array if values satisfy the cutoff
e8749de582fa Uploaded
antmarge
parents:
diff changeset
224 for (my $i=0; $i<$num; $i++){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
225 print "File #",$i+1,"\t";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
226 my $file=$files[$i];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
227 print $file,"\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
228
e8749de582fa Uploaded
antmarge
parents:
diff changeset
229 open(DATA, '<', $file) or die "Could not open '$file' Make sure input .csv files are entered in the command line\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
230 my $dummy=<DATA>; #read and store column names in dummy variable
e8749de582fa Uploaded
antmarge
parents:
diff changeset
231 while (my $entry = <DATA>) {
e8749de582fa Uploaded
antmarge
parents:
diff changeset
232 chomp $entry;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
233 my @line=split(",",$entry);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
234 my $locus = $line[9]; #gene id (SP_0000)
e8749de582fa Uploaded
antmarge
parents:
diff changeset
235 my $w = $line[12]; #nW
e8749de582fa Uploaded
antmarge
parents:
diff changeset
236 if (!$w){ $w=0 } # For blanks
e8749de582fa Uploaded
antmarge
parents:
diff changeset
237 my $c1 = $line[2];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
238 my $c2 = $line[3];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
239 my $avg = ($c1+$c2)/2;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
240 #Average counts must be greater than cutoff (minimum allowed)
e8749de582fa Uploaded
antmarge
parents:
diff changeset
241 if ($avg > $cutoff) {
e8749de582fa Uploaded
antmarge
parents:
diff changeset
242 my @select=($line[0],$w,$avg,$line[9]);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
243 my $select=\@select;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
244 push(@unsorted,$select);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
245 push(@insertPos,$line[0]); #keep track of actual insertion site position
e8749de582fa Uploaded
antmarge
parents:
diff changeset
246 $last=$select[0];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
247 $rowCount++;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
248 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
249 if ($avg >= $weight_ceiling) { $avg = $weight_ceiling; } # Maximum weight
e8749de582fa Uploaded
antmarge
parents:
diff changeset
250 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
251 close DATA;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
252 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
253
e8749de582fa Uploaded
antmarge
parents:
diff changeset
254 my @sorted = sort { $a->[0] <=> $b->[0] } @unsorted;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
255 @insertPos = sort { $a <=> $b } @insertPos;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
256 # Get unique
e8749de582fa Uploaded
antmarge
parents:
diff changeset
257 @insertPos= uniq(@insertPos);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
258
e8749de582fa Uploaded
antmarge
parents:
diff changeset
259 print "\n\tFinished input array ",get_time(),"\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
260
e8749de582fa Uploaded
antmarge
parents:
diff changeset
261 ###############################################################################################
e8749de582fa Uploaded
antmarge
parents:
diff changeset
262
e8749de582fa Uploaded
antmarge
parents:
diff changeset
263 print "\n---------Creating sliding windows across the genome--------\n\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
264
e8749de582fa Uploaded
antmarge
parents:
diff changeset
265 my $index=-1;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
266 my $marker=0;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
267 my $totalInsert=0;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
268 my $totalWindows=0;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
269
e8749de582fa Uploaded
antmarge
parents:
diff changeset
270
e8749de582fa Uploaded
antmarge
parents:
diff changeset
271 #SUBROUTINE FOR EACH WINDOW CALCULATION
e8749de582fa Uploaded
antmarge
parents:
diff changeset
272 sub OneWindow{
e8749de582fa Uploaded
antmarge
parents:
diff changeset
273 my $Wstart=shift @_;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
274 my $Wend=shift@_;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
275 my $Wcount=0;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
276 my $insertion=0;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
277 my $Wsum=0;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
278 my $lastPos=0;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
279 my $i;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
280 my @allgenes=();
e8749de582fa Uploaded
antmarge
parents:
diff changeset
281
e8749de582fa Uploaded
antmarge
parents:
diff changeset
282 for ($i=$marker;$i<$rowCount;$i++){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
283 my @fields=@{$sorted[$i]};
e8749de582fa Uploaded
antmarge
parents:
diff changeset
284 my $pos=$fields[0];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
285 my $w=$fields[1]; #fitness value for single insertion
e8749de582fa Uploaded
antmarge
parents:
diff changeset
286 my $avgCount=$fields[2];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
287 my $gene=$fields[3];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
288
e8749de582fa Uploaded
antmarge
parents:
diff changeset
289 if ($pos<$Wstart){ #if deleted, error shows up
e8749de582fa Uploaded
antmarge
parents:
diff changeset
290 next;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
291 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
292 if ($pos<=$Wend){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
293 if ($pos<($Wstart+$step)){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
294 $marker++;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
295 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
296 my @empty;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
297 if (!$wind_summary{$Wstart}) {
e8749de582fa Uploaded
antmarge
parents:
diff changeset
298 $wind_summary{$Wstart}{w} = [@empty];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
299 $wind_summary{$Wstart}{s} = [@empty];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
300 $wind_summary{$Wstart}{g} = [@empty];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
301 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
302 # Hash of Fitness scores.
e8749de582fa Uploaded
antmarge
parents:
diff changeset
303 $wind_summary{$Wstart}{w} = [@{$wind_summary{$Wstart}{w}}, $w];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
304 # Hash of counts used to generate those fitness scores.
e8749de582fa Uploaded
antmarge
parents:
diff changeset
305 $wind_summary{$Wstart}{s} = [@{$wind_summary{$Wstart}{s}}, $avgCount];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
306
e8749de582fa Uploaded
antmarge
parents:
diff changeset
307
e8749de582fa Uploaded
antmarge
parents:
diff changeset
308 if (!($gene =~ /^ *$/)){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
309 $gene=~ s/"//g;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
310 $gene=~ s/ //g;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
311 $gene=~ s/'//g;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
312 push @allgenes,$gene;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
313 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
314 $Wsum+=$w;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
315 $Wcount++;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
316 if ($pos!=$lastPos){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
317 $insertion+=1;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
318 $lastPos=$pos;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
319 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
320 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
321 #if ($fields[0]>$Wend) #finished with that window, then:
e8749de582fa Uploaded
antmarge
parents:
diff changeset
322 else{
e8749de582fa Uploaded
antmarge
parents:
diff changeset
323 if ($Wcount>0){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
324 $totalWindows++;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
325 $totalInsert+=$insertion;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
326
e8749de582fa Uploaded
antmarge
parents:
diff changeset
327 my ($average, $variance, $stdev, $stderr);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
328
e8749de582fa Uploaded
antmarge
parents:
diff changeset
329 if ($num <=1 ) {
e8749de582fa Uploaded
antmarge
parents:
diff changeset
330 ($average, $variance, $stdev, $stderr)=(0.10,0.10,"NA","NA");
e8749de582fa Uploaded
antmarge
parents:
diff changeset
331 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
332 else{
e8749de582fa Uploaded
antmarge
parents:
diff changeset
333 if (!$weight) {
e8749de582fa Uploaded
antmarge
parents:
diff changeset
334 ($average, $variance, $stdev, $stderr) = &average($wind_summary{$Wstart}{w});
e8749de582fa Uploaded
antmarge
parents:
diff changeset
335 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
336 else {
e8749de582fa Uploaded
antmarge
parents:
diff changeset
337 ($average, $variance, $stdev, $stderr)= &weighted_average($wind_summary{$Wstart}{w},$wind_summary{$Wstart}{s});
e8749de582fa Uploaded
antmarge
parents:
diff changeset
338 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
339 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
340
e8749de582fa Uploaded
antmarge
parents:
diff changeset
341 @allgenes= uniq(@allgenes);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
342 my @sortedGenes = sort { lc($a) cmp lc($b) } @allgenes;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
343 my $wind_genes=join(" ",@sortedGenes);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
344 my @window=($Wstart,$Wend,$Wcount,$insertion,$wind_genes,$average,$variance,$stdev,$stderr);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
345 return (\@window);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
346 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
347
e8749de582fa Uploaded
antmarge
parents:
diff changeset
348 else{
e8749de582fa Uploaded
antmarge
parents:
diff changeset
349 #Even if there were no insertions, still want window in file for consistent start/end
e8749de582fa Uploaded
antmarge
parents:
diff changeset
350 my @window=($Wstart,$Wend,$Wcount,$insertion," ","NA","NA","NA","NA");
e8749de582fa Uploaded
antmarge
parents:
diff changeset
351 return (\@window);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
352 } #Because count=0 (i.e. there were no insertion mutants in that window)
e8749de582fa Uploaded
antmarge
parents:
diff changeset
353 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
354 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
355 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
356
e8749de582fa Uploaded
antmarge
parents:
diff changeset
357 sub customWindow{
e8749de582fa Uploaded
antmarge
parents:
diff changeset
358
e8749de582fa Uploaded
antmarge
parents:
diff changeset
359 my $Wstart=shift @_;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
360 my $Wend=shift@_;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
361 my $Wavg=0;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
362 my $Wcount=0;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
363 my $insertion=0;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
364 my $Wsum=0;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
365 my $lastPos=0;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
366 my @allgenes=();
e8749de582fa Uploaded
antmarge
parents:
diff changeset
367 my $i;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
368
e8749de582fa Uploaded
antmarge
parents:
diff changeset
369
e8749de582fa Uploaded
antmarge
parents:
diff changeset
370 for ($i=$marker;$i<$rowCount;$i++){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
371 my @fields=@{$sorted[$i]};
e8749de582fa Uploaded
antmarge
parents:
diff changeset
372 my $pos=$fields[0];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
373 my $w=$fields[1]; #fitness value for single insertion
e8749de582fa Uploaded
antmarge
parents:
diff changeset
374 my $avgCount=$fields[2];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
375 my $gene=$fields[3];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
376
e8749de582fa Uploaded
antmarge
parents:
diff changeset
377
e8749de582fa Uploaded
antmarge
parents:
diff changeset
378
e8749de582fa Uploaded
antmarge
parents:
diff changeset
379 if ($pos<$Wstart){ #if deleted, error shows up
e8749de582fa Uploaded
antmarge
parents:
diff changeset
380 next;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
381 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
382 if ($pos<=$Wend){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
383 my @empty;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
384 if (!$wind_summary{$Wstart}) {
e8749de582fa Uploaded
antmarge
parents:
diff changeset
385 $wind_summary{$Wstart}{w} = [@empty];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
386 $wind_summary{$Wstart}{s} = [@empty];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
387 $wind_summary{$Wstart}{g} = [@empty];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
388 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
389 # Hash of fitness scores
e8749de582fa Uploaded
antmarge
parents:
diff changeset
390 $wind_summary{$Wstart}{w} = [@{$wind_summary{$Wstart}{w}}, $w];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
391 # Hash of counts used to generate those fitness scores.
e8749de582fa Uploaded
antmarge
parents:
diff changeset
392 $wind_summary{$Wstart}{s} = [@{$wind_summary{$Wstart}{s}}, $avgCount];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
393
e8749de582fa Uploaded
antmarge
parents:
diff changeset
394 if (!($gene =~ /^ *$/)){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
395 $gene=~ s/"//g;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
396 $gene=~ s/ //g;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
397 $gene=~ s/'//g;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
398 push @allgenes,$gene;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
399 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
400 $Wsum+=$w;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
401 $Wcount++;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
402 if ($pos!=$lastPos){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
403 $insertion+=1;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
404 $lastPos=$pos;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
405 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
406 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
407
e8749de582fa Uploaded
antmarge
parents:
diff changeset
408 #if ($fields[0]>$Wend){ #if finished with that window, then:
e8749de582fa Uploaded
antmarge
parents:
diff changeset
409 else{
e8749de582fa Uploaded
antmarge
parents:
diff changeset
410 if ($Wcount>0){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
411 $totalWindows++;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
412 $totalInsert+=$insertion;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
413
e8749de582fa Uploaded
antmarge
parents:
diff changeset
414 my ($average, $variance, $stdev, $stderr);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
415
e8749de582fa Uploaded
antmarge
parents:
diff changeset
416 if ($num <=1 ) {
e8749de582fa Uploaded
antmarge
parents:
diff changeset
417 ($average, $variance, $stdev, $stderr)=(0.10,0.10,"NA","NA");
e8749de582fa Uploaded
antmarge
parents:
diff changeset
418 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
419 else{
e8749de582fa Uploaded
antmarge
parents:
diff changeset
420 if (!$weight) {
e8749de582fa Uploaded
antmarge
parents:
diff changeset
421 ($average, $variance, $stdev, $stderr) = &average($wind_summary{$Wstart}{w});
e8749de582fa Uploaded
antmarge
parents:
diff changeset
422 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
423 else {
e8749de582fa Uploaded
antmarge
parents:
diff changeset
424 ($average, $variance, $stdev, $stderr)= &weighted_average($wind_summary{$Wstart}{w},$wind_summary{$Wstart}{s});
e8749de582fa Uploaded
antmarge
parents:
diff changeset
425 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
426 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
427
e8749de582fa Uploaded
antmarge
parents:
diff changeset
428 @allgenes= uniq(@allgenes);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
429 my @sortedGenes = sort { lc($a) cmp lc($b) } @allgenes;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
430 my $wind_genes=join(" ",@sortedGenes);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
431 print TEST $wind_genes,"\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
432 my @window=($Wstart,$Wend,$Wcount,$insertion,$wind_genes,$average,$variance,$stdev,$stderr);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
433 return (\@window);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
434 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
435
e8749de582fa Uploaded
antmarge
parents:
diff changeset
436 else{ #Even if there were no insertions, still want window in file for consistent start/end
e8749de582fa Uploaded
antmarge
parents:
diff changeset
437 my @window=($Wstart,$Wend,$Wcount,$insertion," ","NA","NA","NA","NA");
e8749de582fa Uploaded
antmarge
parents:
diff changeset
438 return (\@window);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
439 } #Because count=0 (i.e. there were no insertion mutants in that window)
e8749de582fa Uploaded
antmarge
parents:
diff changeset
440 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
441 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
442 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
443
e8749de582fa Uploaded
antmarge
parents:
diff changeset
444 print "Start calculation: ",get_time(),"\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
445 my $start=1;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
446 my $end=$size+1;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
447 my $windowNum=0;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
448 my @allWindows; #will be a 2D array containing all window info to be written into output file
e8749de582fa Uploaded
antmarge
parents:
diff changeset
449
e8749de582fa Uploaded
antmarge
parents:
diff changeset
450
e8749de582fa Uploaded
antmarge
parents:
diff changeset
451
e8749de582fa Uploaded
antmarge
parents:
diff changeset
452 #IF A CUSTOM LIST OF START/END COORDINATES IS GIVEN THEN CREATE WINDOWS USING IT
e8749de582fa Uploaded
antmarge
parents:
diff changeset
453 if ($custom){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
454 open (CUST, '<', $custom);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
455 while(my $line=<CUST>){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
456 $line=cleaner($line);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
457 my @cwind=split(",",$line);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
458 my $start=$cwind[0];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
459 my $end=$cwind[1];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
460 my $window=customWindow($start,$end);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
461 push @allWindows,$window;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
462 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
463 close CUST;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
464 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
465
e8749de582fa Uploaded
antmarge
parents:
diff changeset
466 else{ #if not custom list
e8749de582fa Uploaded
antmarge
parents:
diff changeset
467 #WHILE LOOP TO CALL THE ONE WINDOW SUBROUTINE FOR CALCULATIONS===INCREMENTS START AND END VALUES OF THE WINDOW
e8749de582fa Uploaded
antmarge
parents:
diff changeset
468 while ($end<=$last-$size){ #100,000bp-->9,950 windows--> only 8500 windows in csv because 0
e8749de582fa Uploaded
antmarge
parents:
diff changeset
469 my($window)=OneWindow($start,$end);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
470 push (@allWindows,$window);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
471 $start=$start+$step;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
472 $end=$end+$step;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
473 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
474 print "End calculation: ",get_time(),"\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
475 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
476
e8749de582fa Uploaded
antmarge
parents:
diff changeset
477
e8749de582fa Uploaded
antmarge
parents:
diff changeset
478 #my $avgInsert=$totalInsert/$totalWindows;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
479 #print "Average number of insertions for $size base pair windows: $avgInsert\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
480
e8749de582fa Uploaded
antmarge
parents:
diff changeset
481
e8749de582fa Uploaded
antmarge
parents:
diff changeset
482 #ESSENTIALS: Counting the number of TA sites in the genome and whether an insertion occurred there or not
e8749de582fa Uploaded
antmarge
parents:
diff changeset
483
e8749de582fa Uploaded
antmarge
parents:
diff changeset
484 print "\n---------Assessing essentiality of genome region in each window--------\n\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
485
e8749de582fa Uploaded
antmarge
parents:
diff changeset
486 my @sites;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
487
e8749de582fa Uploaded
antmarge
parents:
diff changeset
488 #First read fasta file into a string
e8749de582fa Uploaded
antmarge
parents:
diff changeset
489 my $seqio = Bio::SeqIO->new(-file => $fasta, '-format' => 'Fasta');
e8749de582fa Uploaded
antmarge
parents:
diff changeset
490 my $prev;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
491 my $total=0;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
492 while(my $seq = $seqio->next_seq) {
e8749de582fa Uploaded
antmarge
parents:
diff changeset
493 $fasta = $seq->seq;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
494 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
495 #Get number of "TA" sites, regardless of insertion---this is just the fasta file
e8749de582fa Uploaded
antmarge
parents:
diff changeset
496 my $x="TA";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
497 my @c = $fasta =~ /$x/g;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
498 my $countTA = @c;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
499
e8749de582fa Uploaded
antmarge
parents:
diff changeset
500 #At what positions in the genome do TA sites occur?
e8749de582fa Uploaded
antmarge
parents:
diff changeset
501 my $pos=0;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
502 my $countInsert=0;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
503 my @selector; #use this array to hold all 1 and 0 of whether insertion happened or not.
e8749de582fa Uploaded
antmarge
parents:
diff changeset
504
e8749de582fa Uploaded
antmarge
parents:
diff changeset
505 #Go through all TA sites identified above and see if an insertion occurs there.
e8749de582fa Uploaded
antmarge
parents:
diff changeset
506 #Push results onto two arrays a 2d array with the position and a 1D array with just the 0 or 1
e8749de582fa Uploaded
antmarge
parents:
diff changeset
507
e8749de582fa Uploaded
antmarge
parents:
diff changeset
508 my @unmatched; #hold all unmatched ta sites
e8749de582fa Uploaded
antmarge
parents:
diff changeset
509 my @allTAsites; #2d array to hold all occurences of TA sites in genome
e8749de582fa Uploaded
antmarge
parents:
diff changeset
510 my $unmatchedCount=0;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
511 my $offset=0;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
512 my $genPos = index($fasta,'TA',$offset);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
513
e8749de582fa Uploaded
antmarge
parents:
diff changeset
514 while (($genPos != -1) and ($pos<scalar @insertPos)) { #as long as the TA site is foun
e8749de582fa Uploaded
antmarge
parents:
diff changeset
515 my $res=0;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
516 if ($genPos>$insertPos[$pos]){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
517 push @unmatched,$insertPos[$pos];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
518 $unmatchedCount++;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
519 $pos++;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
520 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
521 if ($genPos==$insertPos[$pos]){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
522 $res=1;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
523 $countInsert++;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
524 $pos++;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
525 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
526 my @sites=($genPos,$res);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
527 push @selector,$res;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
528 #push the 0 or 1 onto the array @selector---going to use this to draw random sets for the null distribution
e8749de582fa Uploaded
antmarge
parents:
diff changeset
529 push (@allTAsites,\@sites);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
530 $offset = $genPos + 1;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
531 $genPos = index($fasta, 'TA', $offset);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
532 $countTA++;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
533 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
534
e8749de582fa Uploaded
antmarge
parents:
diff changeset
535 #my $FILE1 = "$run" . "allTAsites.txt";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
536 my $FILE1 = $f1;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
537 open (ALL_TA, ">", $FILE1);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
538 foreach my $sit(@allTAsites){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
539 foreach (@$sit){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
540 print ALL_TA $_, "\t";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
541 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
542 printf ALL_TA "\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
543 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
544 close ALL_TA;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
545
e8749de582fa Uploaded
antmarge
parents:
diff changeset
546 my $FILE2 = $f2; #$run . "unmatched.txt";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
547 unless(open UNM, ">", $FILE2){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
548 die "\nUnable to create $FILE2:\n$!";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
549 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
550 foreach (@unmatched){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
551 print UNM $_, "\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
552 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
553 close UNM;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
554
e8749de582fa Uploaded
antmarge
parents:
diff changeset
555 print "\n\nTotal of unmatched insertions: $unmatchedCount\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
556 print "See unmatched.txt for genome indices of unmatched sites\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
557 print "See allTAsites.txt for details on all TA sites and insertions\n\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
558
e8749de582fa Uploaded
antmarge
parents:
diff changeset
559 #print "\nTotal: $countInsert insertions in $countTA TA sites.\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
560
e8749de582fa Uploaded
antmarge
parents:
diff changeset
561 #--------------------------------------------------------------------------------------------------
e8749de582fa Uploaded
antmarge
parents:
diff changeset
562
e8749de582fa Uploaded
antmarge
parents:
diff changeset
563 #Now, have an array for each TA site and if an insertion occurred there.
e8749de582fa Uploaded
antmarge
parents:
diff changeset
564 #So per site @sites=(position, 0 or 1 for insertion).
e8749de582fa Uploaded
antmarge
parents:
diff changeset
565 #Next step, create null distribution of 10,000 random sets with same number of
e8749de582fa Uploaded
antmarge
parents:
diff changeset
566 #TA sites as the window and then calculate p-value
e8749de582fa Uploaded
antmarge
parents:
diff changeset
567
e8749de582fa Uploaded
antmarge
parents:
diff changeset
568 #SUBROUTINE FOR MAKING THE NULL DISTRIBUTION SPECIFIC TO THE WINDOW
e8749de582fa Uploaded
antmarge
parents:
diff changeset
569
e8749de582fa Uploaded
antmarge
parents:
diff changeset
570 #DISTRIBUTION'S STANDARD DEVIATION
e8749de582fa Uploaded
antmarge
parents:
diff changeset
571 my $N=10000;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
572
e8749de582fa Uploaded
antmarge
parents:
diff changeset
573 sub mean {
e8749de582fa Uploaded
antmarge
parents:
diff changeset
574 if (scalar @_ ==0){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
575 return 0;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
576 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
577 my $sum;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
578 grep { $sum += $_ } @_;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
579 return $sum/@_;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
580 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
581 sub stdev{
e8749de582fa Uploaded
antmarge
parents:
diff changeset
582 my @data = @{shift @_};
e8749de582fa Uploaded
antmarge
parents:
diff changeset
583 my $average=shift @_;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
584 my $sqtotal = 0;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
585 foreach(@data) {
e8749de582fa Uploaded
antmarge
parents:
diff changeset
586 $sqtotal += ($average-$_) ** 2;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
587 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
588 my $std = ($sqtotal / ($N-1)) ** 0.5;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
589 return $std;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
590 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
591
e8749de582fa Uploaded
antmarge
parents:
diff changeset
592 #MAKE LIBRARY OF NULL DISTRIBUTIONS:
e8749de582fa Uploaded
antmarge
parents:
diff changeset
593 print "Making a library of null distributions.\n For information on distribution library, see nullDist.txt\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
594
e8749de582fa Uploaded
antmarge
parents:
diff changeset
595 my @distLib;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
596
e8749de582fa Uploaded
antmarge
parents:
diff changeset
597 my $FILE3 = $f3; #$run . "nullDist.txt";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
598 unless(open DIST, ">", $FILE3){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
599 die "\nUnable to create $FILE3:\n$!";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
600 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
601 printf DIST "Sites\tSize(n)\tMin\tMax\tMean\tstdev\tvar\tMinPval\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
602
e8749de582fa Uploaded
antmarge
parents:
diff changeset
603 #Loop once for each distribution in the library
e8749de582fa Uploaded
antmarge
parents:
diff changeset
604 #(i.e. distribution of 10,000 sites each with 35 TA sites, then a distribution of 10,000 sites each with 36 TA sites, etc)
e8749de582fa Uploaded
antmarge
parents:
diff changeset
605
e8749de582fa Uploaded
antmarge
parents:
diff changeset
606 sub pvalue{
e8749de582fa Uploaded
antmarge
parents:
diff changeset
607
e8749de582fa Uploaded
antmarge
parents:
diff changeset
608 #takes in window count average (countAvg) and number of TAsites and makes a null distribution to calculate the pvalue, which it returns
e8749de582fa Uploaded
antmarge
parents:
diff changeset
609 my $mean=shift@_;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
610 my $TAsites=shift@_;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
611 my $N=10000;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
612 my @specDist=@{$distLib[$TAsites-1]};
e8749de582fa Uploaded
antmarge
parents:
diff changeset
613 my $rank= binsearch_pos { $a cmp $b } $mean,@specDist;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
614 my $i=$rank;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
615 while ($i<scalar(@specDist)-1 and $specDist[$i+1]==$specDist[$rank]){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
616 $i++;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
617 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
618 $rank=$i;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
619 my $pval=$rank/$N; #calculate pval as rank/N
e8749de582fa Uploaded
antmarge
parents:
diff changeset
620 return $pval;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
621
e8749de582fa Uploaded
antmarge
parents:
diff changeset
622 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
623
e8749de582fa Uploaded
antmarge
parents:
diff changeset
624 for (my $sitez=1; $sitez<=$max;$sitez++){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
625 #print "In the first for loop to make a null distribution\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
626 my @unsorted;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
627 my $count=0;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
628 my $sum=0;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
629
e8749de582fa Uploaded
antmarge
parents:
diff changeset
630 for (my $i=1; $i<=$N; $i++){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
631 my @random_set = rand_set( set => \@selector, size => $sitez);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
632 my $setMean=mean(@random_set);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
633 push (@unsorted, $setMean);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
634 #print "$i:\t", "$setMean\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
635 $count++;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
636 $sum+=$setMean;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
637 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
638 my @nullDist= sort { $a <=> $b } @unsorted;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
639 my $nullMean=sprintf("$round",($sum/$count));
e8749de582fa Uploaded
antmarge
parents:
diff changeset
640 my $standev =sprintf("$round",stdev(\@nullDist, $nullMean));
e8749de582fa Uploaded
antmarge
parents:
diff changeset
641 my $variance=sprintf("$round",($standev*$standev));
e8749de582fa Uploaded
antmarge
parents:
diff changeset
642 my $min=sprintf("$round",$nullDist[0]);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
643 my $max=sprintf("$round",$nullDist[scalar @nullDist-1]);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
644 my $setScalar=scalar @nullDist;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
645 push (@distLib,\@nullDist);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
646 my $minp=pvalue(0,$sitez);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
647 printf DIST "$sitez\t$N\t$min\t$max\t$nullMean\t$standev\t$variance\t$minp\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
648 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
649 close DIST;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
650
e8749de582fa Uploaded
antmarge
parents:
diff changeset
651 #SUBROUTINE TO CALCULATE THE P-VALUE OF THE WINDOW INSERTIONS AGAINST THE NULL DISTRIBUTION
e8749de582fa Uploaded
antmarge
parents:
diff changeset
652 #---------------------------------------------------------------------------------------------
e8749de582fa Uploaded
antmarge
parents:
diff changeset
653
e8749de582fa Uploaded
antmarge
parents:
diff changeset
654 #Now we have an array called @allTAsites which contains every TAsite position
e8749de582fa Uploaded
antmarge
parents:
diff changeset
655 #with a 0 next to it for "no insertion".
e8749de582fa Uploaded
antmarge
parents:
diff changeset
656 #Now just need to replace 0 with 1 if there IS and insertion at that site
e8749de582fa Uploaded
antmarge
parents:
diff changeset
657
e8749de582fa Uploaded
antmarge
parents:
diff changeset
658 my @newWindows=();
e8749de582fa Uploaded
antmarge
parents:
diff changeset
659 my $printNum=0;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
660
e8749de582fa Uploaded
antmarge
parents:
diff changeset
661 #my $allWindows=\@allWindows;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
662 for (my $i=0;$i<scalar @allWindows;$i++){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
663 my @win=@{$allWindows[$i]};
e8749de582fa Uploaded
antmarge
parents:
diff changeset
664 my $starter=$win[0];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
665 my $ender=$win[1];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
666 my $insertions=$win[3];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
667 my $mutcount=$win[2];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
668 #print "num $printNum -->\tStart pos: $starter\tEnd pos: $ender\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
669 #How many TA sites are there from $genome[$start] to $genome[$end]?
e8749de582fa Uploaded
antmarge
parents:
diff changeset
670
e8749de582fa Uploaded
antmarge
parents:
diff changeset
671 my $seq = substr($fasta,$starter-1,$size); #start-1 becase $start and $end are positions in genome starting at 1,2,3.... substr(string,start, length) needs indexes
e8749de582fa Uploaded
antmarge
parents:
diff changeset
672 my $ta="TA";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
673 my @c = $seq =~ /$ta/g;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
674 my $TAsites = scalar @c;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
675 my $avgInsert=sprintf("$round",$insertions/$TAsites);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
676 my $pval=pvalue($avgInsert,$TAsites);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
677
e8749de582fa Uploaded
antmarge
parents:
diff changeset
678 #reorder so things make more sense: all fitness related calcs together and all sig together
e8749de582fa Uploaded
antmarge
parents:
diff changeset
679 my @new=($starter,$ender,$mutcount,$insertions,$TAsites,$avgInsert,$pval,$win[5],$win[6],$win[7],$win[8],$win[4]);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
680
e8749de582fa Uploaded
antmarge
parents:
diff changeset
681 push (@newWindows,\@new);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
682 $printNum++;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
683 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
684
e8749de582fa Uploaded
antmarge
parents:
diff changeset
685
e8749de582fa Uploaded
antmarge
parents:
diff changeset
686 print "End p-value calculation: ",get_time(),"\n\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
687
e8749de582fa Uploaded
antmarge
parents:
diff changeset
688 #print "This is the TA count: $count\nTotal genome size is: $total\n\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
689
e8749de582fa Uploaded
antmarge
parents:
diff changeset
690 #CALCULATE THE ABSOLUTE DIFFERENCE BETWEEN REGION'S MEAN FITNESS AND AVERAGE MEAN FITNESS
e8749de582fa Uploaded
antmarge
parents:
diff changeset
691 ### For all windows, add a field that has the difference between that window's mean fitness and the
e8749de582fa Uploaded
antmarge
parents:
diff changeset
692 #average mean fitness for all of the windows
e8749de582fa Uploaded
antmarge
parents:
diff changeset
693
e8749de582fa Uploaded
antmarge
parents:
diff changeset
694 my @allFits = map $_->[7], @newWindows;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
695 my $meanFit=mean(@allFits);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
696 print "Average fitness for all windows: ",$meanFit,"\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
697 my @expWindows=();
e8749de582fa Uploaded
antmarge
parents:
diff changeset
698 foreach (@newWindows){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
699 my @entry=@{$_};
e8749de582fa Uploaded
antmarge
parents:
diff changeset
700 my $mean=$entry[7];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
701 my $absdev=sprintf("$round",$mean-$meanFit);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
702 push (@entry,$absdev);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
703 push @expWindows,\@entry;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
704 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
705
e8749de582fa Uploaded
antmarge
parents:
diff changeset
706 @newWindows=@expWindows;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
707
e8749de582fa Uploaded
antmarge
parents:
diff changeset
708 #MAKE OUTPUT CSV FILE WITH ESSENTIAL WINDOW CALCULATIONS
e8749de582fa Uploaded
antmarge
parents:
diff changeset
709
e8749de582fa Uploaded
antmarge
parents:
diff changeset
710 print "Start csv ouput file creation: ",get_time(),"\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
711
e8749de582fa Uploaded
antmarge
parents:
diff changeset
712 open CSV, '>', $f3; # $run . "slidingWindows.csv" or die "Cannot open sliding window output file";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
713
e8749de582fa Uploaded
antmarge
parents:
diff changeset
714 my @csvHeader= ("start", "end","mutants","insertions","TA_sites","ratio","p-value","average", "variance","stdev","stderr","genes","fit-mean");
e8749de582fa Uploaded
antmarge
parents:
diff changeset
715 my $header=join(",",@csvHeader);
e8749de582fa Uploaded
antmarge
parents:
diff changeset
716 print CSV $header,"\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
717 foreach my $winLine(@newWindows){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
718 my $string=join(",",@{$winLine});
e8749de582fa Uploaded
antmarge
parents:
diff changeset
719 print CSV $string, "\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
720 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
721 close CSV;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
722 print "End csv ouput file creation: ",get_time(),"\n\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
723
e8749de582fa Uploaded
antmarge
parents:
diff changeset
724 my $in = Bio::SeqIO->new(-file=>$ref, -format => 'genbank');
e8749de582fa Uploaded
antmarge
parents:
diff changeset
725 my $refseq = $in->next_seq;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
726 my $refname = $refseq->id;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
727
e8749de582fa Uploaded
antmarge
parents:
diff changeset
728 #MAKE essentials WIG FILE---->later make BW--->IGV
e8749de582fa Uploaded
antmarge
parents:
diff changeset
729 sub printwig{
e8749de582fa Uploaded
antmarge
parents:
diff changeset
730 print "Start wig file creation: ",get_time(),"\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
731 my $in = Bio::SeqIO->new(-file=>$ref, -format => 'genbank');
e8749de582fa Uploaded
antmarge
parents:
diff changeset
732 my $refseq = $in->next_seq;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
733 my $refname = $refseq->id;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
734 my $ewig="essentialWindows.wig";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
735 my $FILE5 = $run . $ewig;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
736 open eWIG, ">", $f5; #$FILE5";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
737 printf eWIG "track type=wiggle_0 name=$ewig\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
738 printf eWIG "variableStep chrom=$refname\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
739 foreach my $wigLine(@newWindows){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
740 my @wigFields=@$wigLine;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
741 my $position=$wigFields[0];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
742 while ($position<=$wigFields[1]){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
743 printf eWIG $position, " ",$wigFields[7],"\n"; #7 for pvalue, but 2 for fitness!!!!!!
e8749de582fa Uploaded
antmarge
parents:
diff changeset
744 $position=$position+1;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
745 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
746 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
747 close eWIG;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
748 print "End wig file creation: ",get_time(),"\n\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
749 print "If this wig file needs to be converted to a Big Wig, then use USCS program wigToBigWig in terminal: \n \t./wigToBigWig gview/12G.wig organism.txt BigWig/output.bw \n\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
750 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
751
e8749de582fa Uploaded
antmarge
parents:
diff changeset
752
e8749de582fa Uploaded
antmarge
parents:
diff changeset
753 #OUTPUT REGULAR SLIDING INFORMATION WITHOUT ESSENTIALS CALCULATIONS (P-VALUES)
e8749de582fa Uploaded
antmarge
parents:
diff changeset
754
e8749de582fa Uploaded
antmarge
parents:
diff changeset
755
e8749de582fa Uploaded
antmarge
parents:
diff changeset
756 #MAKE OUTPUT CSV FILE WITH FITNESS WINDOW CALCULATIONS
e8749de582fa Uploaded
antmarge
parents:
diff changeset
757 print "Start csv ouput file creation: ",get_time(),"\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
758 open FITFILE, ">", $f4; #$run . "fitWindows.csv" or die "Failed to open file";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
759 my @head = ("start", "end","fitness","mutant_count", "\n");
e8749de582fa Uploaded
antmarge
parents:
diff changeset
760 print FITFILE join(",",@head); #header
e8749de582fa Uploaded
antmarge
parents:
diff changeset
761 foreach my $winLine(@allWindows){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
762 print FITFILE join(",", @$winLine),"\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
763 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
764 close FITFILE;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
765 print "End csv ouput file creation: ",get_time(),"\n\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
766
e8749de582fa Uploaded
antmarge
parents:
diff changeset
767 #MAKE WIG FILE---->later make BW--->IGV
e8749de582fa Uploaded
antmarge
parents:
diff changeset
768 print "Start wig file creation: ",get_time(),"\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
769 my $fin = Bio::SeqIO->new(-file=>$ref, -format => 'genbank');
e8749de582fa Uploaded
antmarge
parents:
diff changeset
770 $refseq = $fin->next_seq;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
771 $refname = $refseq->id;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
772 my $fwig="fitWindows.wig";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
773 open fWIG, ">", $f5; #$run . $fwig;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
774 print fWIG "track type=wiggle_0 name=$fwig\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
775 print fWIG "variableStep chrom=$refname\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
776 foreach my $wigLine(@allWindows){
e8749de582fa Uploaded
antmarge
parents:
diff changeset
777 my @wigFields=@$wigLine;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
778 my $pos=$wigFields[0];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
779 my $fit=$wigFields[7];
e8749de582fa Uploaded
antmarge
parents:
diff changeset
780 print fWIG $pos," ",$fit,"\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
781 }
e8749de582fa Uploaded
antmarge
parents:
diff changeset
782 close fWIG;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
783 print "End wig file creation: ",get_time(),"\n\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
784 print "If this wig file needs to be converted to a Big Wig,\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
785 print " then use USCS program wigToBigWig in terminal: \n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
786 print "\t./wigToBigWig gview/12G.wig organism.txt BigWig/output.bw \n\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
787
e8749de582fa Uploaded
antmarge
parents:
diff changeset
788
e8749de582fa Uploaded
antmarge
parents:
diff changeset
789 #GOING TO MAKE A TEXT FILE FOR BED CONVERSION TO BIGBED, NEED CHROM # IN COLUMN 0
e8749de582fa Uploaded
antmarge
parents:
diff changeset
790
e8749de582fa Uploaded
antmarge
parents:
diff changeset
791 my @cummulative;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
792
e8749de582fa Uploaded
antmarge
parents:
diff changeset
793 #MAKING A REGULAR TEXT FILE fields: [chrom,start,end,fitness,count]
e8749de582fa Uploaded
antmarge
parents:
diff changeset
794
e8749de582fa Uploaded
antmarge
parents:
diff changeset
795 print "Start text file creation time: ",get_time(),"\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
796 open my $TXT, '>', $f6; #$run . "fitWindows.txt" or die $!;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
797 print $TXT ("start","end","W","mutant_count","insertion_count\n");
e8749de582fa Uploaded
antmarge
parents:
diff changeset
798 print $TXT (join("\t",@$_),"\n") for @allWindows;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
799 close $TXT;
e8749de582fa Uploaded
antmarge
parents:
diff changeset
800 print "End text file creation: ",get_time(),"\n\n";
e8749de582fa Uploaded
antmarge
parents:
diff changeset
801
e8749de582fa Uploaded
antmarge
parents:
diff changeset
802