comparison compRegions.pl @ 1:198cfbcae096 draft

Uploaded
author antmarge
date Tue, 28 Mar 2017 21:51:45 -0400
parents
children
comparison
equal deleted inserted replaced
0:2bc8d3ab6da4 1:198cfbcae096
1 #!/usr/bin/perl -w
2
3 #Margaret Antonio 17.01.10 For GALAXY
4
5 use Data::Dumper;
6 use strict;
7 use Getopt::Long;
8 use Statistics::Distributions;
9 no warnings;
10
11 #ASSIGN INPUTS TO VARIABLES USING FLAGS
12 our ($input1, $input2, $help,$out,$round,$l1, $l2);
13
14 GetOptions(
15 'input1:s' => \$input1,
16 'input2:s' => \$input2,
17 'h' => \$help,
18 'o:s' =>\$out,
19 'r:i'=> \$round,
20 'l1:s'=> \$l1,
21 'l2:s'=> \$l2,
22 );
23
24 sub print_usage() {
25 print "\n####################################################################\n";
26 print "\n";
27 print "compWindows.pl: compare regions outputted by the slidingWindow tool\n\n";
28 print "DESCRIPTION: Takes two slidingWindows.csv files and compares them by\n";
29 print "calculating the difference in mean fitness, the pval for each gene.\n";
30 print "Example: compare control vs antibiotic, where both from same strain (genome).\n";
31 print "Note: For different strains/genomes, use compStrains for a gene comparison.pl\n";
32
33 print "\nUSAGE:\n";
34 print "perl compGenes.pl -d inputs/ -l cond1,cond2 -r 2 -o comp-cond1cond2_date.csv\n";
35
36 print "\nREQUIRED:\n";
37 print " -d\tDirectory containing two slidingWindow.csv files (output of\n";
38 print " \tslidingWindow tool)\n";
39 print " \tOR\n";
40 print " \tIn the command line (without a flag), input the name(s) of\n";
41 print " \ttwo files.\n";
42
43 print "\nOPTIONAL:\n";
44 print " -h\tPrints usage and exits program\n";
45 print " -o\tOutput file for comparison data. Default: label1label2.csv\n";
46 print " -r\tRound final output numbers to this number of decimals\n";
47 print " -s\tColumn number to sort by. Default: difference of means\n";
48 print " -l\tLabels for input files. Default: filenames\n";
49 print " \tTwo strings, comma separated (i.e. -l expt1,expt2).\n";
50 print " \tOrder should match file order.\n";
51
52 print " \n~~~~Always check that file paths are correctly specified~~~~\n";
53 print "\n##################################################################\n";
54 }
55 if ($help){
56 print_usage();
57 exit;
58 }
59
60
61
62 #THE @files ARRAY WILL CONTAIN INPUT FILE NAMES
63 my @files=($input1,$input2);
64
65 #GET LABELS: USE (-l) OR USE FILNEAMES AS LABELS FOR COLUMNS IN OUTPUT FILE
66
67 my @labels=($l1,$l2);
68
69 #INDICES 0:start 1:end 2:mutants 3:insertions 4:TA_sites 5:ratio 6:p-value
70 #7:average 8:variance 9:stdev 10:stderr 11:genes 12:fit-mean
71
72 #CHECK IF REQ. VARIABLES WERE DEFINED USING FLAGS. IF NOT THEN USE DEFAULT VALUES
73
74 if (!$out) {$out="comp.".$labels[0].$labels[1].".csv"}
75 if (!$round){$round='%.4f'}
76
77 #READ INPUT FILES AND PUT GENE INFO FROM EACH LINE INTO HASH %all WHERE KEY=GENE_ID AND
78 #VALUE=GENE INFO (MEAN FITNESS, # INSERTIONS, ETC.)
79
80 my %all;
81 my @header;
82 for (my $i=0; $i<2; $i++){
83 print "File #",$i+1,"\t";
84 my $file=$files[$i];
85 print $file,"\n";
86
87 open(DATA, '<', $file) or (print "Could not open '$file'\n" and print_usage() and exit);
88
89 #EXTRACT THE HEADER (COLUMN NAMES) OF THE FILE AND KEEP FOR OUTPUT HEADER
90 #APPEND FILE NAME OR GIVEN LABEL (-l) TO COLUMN NAME SO ITS DIFFERENT FROM OTHER INPUT FILE
91
92 my $head=<DATA>;
93 my @cols=split(',',$head);
94 for (my $j=0;$j<scalar @cols;$j++){
95 my $colname=$cols[$j];
96 chomp($colname);
97 $cols[$j]=$colname.'-'.$labels[$i];
98 print $cols[$j],"\n";
99 }
100 push (@header,@cols);
101
102 while (my $entry = <DATA>) {
103 chomp $entry;
104 my @line=split(",",$entry);
105 if (!$line[11]){
106 $line[11]="NA";
107 }
108 my ($start,$end,$mutants,$insertions,$TAsites,$ratio,$pval,$avg,$var,$sd,$se,$genes)=@line;
109 $avg=sprintf("%.3f",$avg);
110
111 #if (!$line[6]){
112 # $line[6]=0;
113 #}
114 #@line=@line[0,1,2,3,4,5,6];
115
116
117 #PUT GENE AND INFO INTO THE HASH FOR EXISTING KEY (1ST FILE) OR CREATE NEW KEY (2ND FILE)
118 my $key=$start;
119 if(!exists $all{$key}){
120 my @info;
121 push (@info,@line);
122 $all{$key}=\@info;
123 }
124 #Otherwise the window existed for the prior file, and now need to calcualte extra stats
125 else{
126 my @info=@{$all{$key}};
127 my ($start2,$end2,$mutants2,$insertions2,$TAsites2,$ratio2,$pval2,$avg2,$var2,$sd2,$se2,$genes2)=@info;
128 my $diff=sprintf("$round",($avg-$avg2));
129 my $df=$insertions+$insertions2-2;
130 my $tdist;
131 my $pval;
132
133 # CHECK TO MAKE SURE ALL VARIABLES IN TDIST,PVAL CALCULATIONS ARE NUMBERS AND NO
134 # ZEROS (0) IN THE DENOMINATOR
135
136 if ($se eq "NA" or $se2 eq "NA" or $sd eq "NA" or $sd2 eq "NA" or $insertions==0 or $insertions2==0 or $sd==0 or $sd2==0 or $df<=0){
137 ($tdist,$pval)=(" "," ");
138 }
139 else{
140 $tdist=sqrt((($diff)/(sqrt((($sd**2)/$insertions)+(($sd2**2)/$insertions2))))**2);
141 $pval=Statistics::Distributions::tprob($df,$tdist);
142 }
143 push (@info,@line,$diff,$df,$tdist,$pval);
144 $all{$key}=\@info;
145 }
146 }
147 close DATA;
148 }
149
150 #READ ALL HASH CONTENTS INTO 2D ARRAY FOR EASY SORTING AND PRINTING TO OUT FILE
151 my @unsorted;
152
153 foreach my $entry (keys %all) {
154 my @info=@{$all{$entry}};
155 my @temp;
156 push (@temp,@info);
157 push (@unsorted,\@temp);
158 }
159
160 #SORT GENES BY PVALUE OR FITNESS DEPENDING ON WHICH FLAG WAS USED IN COMMANDLINE
161
162 my $sortkey=26; #default: sort by difference of means
163
164 my @sorted = sort { $b->[$sortkey] <=> $a->[$sortkey] } @unsorted;
165
166 #ADD NEW FIELDS TO HEADER (COLUMN NAMES)
167 my $field="Mean".$labels[0].'.'.$labels[1];
168 push (@header,$field,"DOF","TDIST","PVALUE");
169
170 #PRINT TO OUT FILE
171 open OUT, '>',"$out";
172 print OUT (join(',',@header),"\n");
173 foreach (@sorted){
174 my @woo=@{$_};
175 print OUT join(',',@woo),"\n";
176 }
177 close OUT;
178
179