9
|
1 #!/usr/bin/perl -w
|
|
2
|
|
3 #Margaret Antonio 17.01.06 without essentiality. For original, unmodified aggregate.pl output (where unique insertions are not included).
|
|
4
|
|
5 use Getopt::Long;
|
|
6 use Statistics::Distributions;
|
|
7 use strict;
|
|
8 use autodie;
|
|
9 no warnings;
|
|
10
|
|
11 #no warnings;
|
|
12
|
|
13 #ASSIGN INPUTS TO VARIABLES USING FLAGS
|
|
14 our ($input1, $input2, $h, $out, $sortkey, $round, $l1, $l2, $outfile);
|
|
15
|
|
16 GetOptions(
|
|
17 'input1:s' => \$input1,
|
|
18 'input2:s' => \$input2,
|
|
19 'h' => \$h,
|
|
20 'o:s' =>\$outfile,
|
|
21 'r:i'=> \$round,
|
|
22
|
|
23 );
|
|
24
|
|
25 sub print_usage() {
|
|
26 print "\n####################################################################\n";
|
|
27
|
|
28 print "compGenes: compare genes for an organism under different conditions\n\n";
|
|
29 print "DESCRIPTION: Takes two aggregate.pl outputs and compares them by\n";
|
|
30 print "calculating the difference in mean fitness for each gene.\n";
|
|
31 print "Example: compare organism in presence of control vs antibiotic.\n";
|
|
32 print "Note: For different strains/genomes, use compStrains.pl\n";
|
|
33
|
|
34 print "\nUSAGE:";
|
|
35 print "perl compGenes.pl -d inputs/ \n\n";
|
|
36
|
|
37 print "REQUIRED:\n";
|
|
38 print " -d\tDirectory containing all input files (files from\n";
|
|
39 print " \taggregate script)\n";
|
|
40 print " \tOR\n";
|
|
41 print " \tIn the command line (without a flag), input the name(s) of\n";
|
|
42 print " \ttwo files containing aggregate gene fitness values. \n\n";
|
|
43
|
|
44 print "OPTIONAL:\n";
|
|
45 print " -h\tPrints usage and exits program\n";
|
|
46 print " -o\tOutput file for comparison data. Default: label1label2.csv\n";
|
|
47 print " -r\tRound final output numbers to this number of decimals\n";
|
|
48 print " -l1\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 }
|
|
56
|
|
57 # Check if help needed or if improper inputs
|
|
58
|
|
59 if ($h){
|
|
60 print_usage();
|
|
61 exit;
|
|
62 }
|
|
63
|
|
64 if (! $outfile){
|
|
65 $outfile="geneComp.csv"
|
|
66 }
|
|
67 $round = '%.4f';
|
|
68
|
|
69 my @files=($input1,$input2);
|
|
70
|
|
71 #GET LABELS: USE (-l) OR USE FILNEAMES AS LABELS FOR COLUMNS IN OUTPUT FILE
|
|
72
|
|
73 my @labels=($l1,$l2);
|
|
74
|
|
75 #CHECK IF REQ. VARIABLES WERE DEFINED USING FLAGS. IF NOT THEN USE DEFAULT VALUES
|
|
76
|
|
77 #$out="comp.".$labels[0].$labels[1].".csv";
|
|
78
|
|
79 #READ INPUT FILES AND PUT GENE INFO FROM EACH LINE INTO HASH %all WHERE KEY=GENE_ID AND
|
|
80 #VALUE=GENE INFO (MEAN FITNESS, # INSERTIONS, ETC.)
|
|
81
|
|
82 my %all;
|
|
83 my @header;
|
|
84
|
|
85 #OPEN TWO COMPARISON FILES, ONE PER LOOP
|
|
86 for (my $i=0; $i<2; $i++){
|
|
87 print "File #",$i+1,"\t";
|
|
88 my $file=$files[$i];
|
|
89 print $file,"\n";
|
|
90
|
|
91 open(DATA, '<', $file) or die "Could not open '$file'\n";
|
|
92
|
|
93 #EXTRACT THE HEADER (COLUMN NAMES) OF THE FILE AND KEEP FOR OUTPUT HEADER
|
|
94 #APPEND FILE NAME OR GIVEN LABEL (-l) TO COLUMN NAME SO ITS DIFFERENT FROM OTHER INPUT FILE
|
|
95
|
|
96 my $head=<DATA>;
|
|
97 my @temp = split("\n",$head);
|
|
98 $head = $temp[0];
|
|
99
|
|
100 my @cols=split(',',$head);
|
|
101 @cols = @cols[0,1,2,3,4,5];
|
|
102 for (my $j=0;$j<scalar @cols;$j++){
|
|
103 $cols[$j]=$cols[$j].'-'.$labels[$i];
|
|
104 }
|
|
105 push (@header,@cols);
|
|
106 while (my $entry = <DATA>) {
|
|
107 chomp $entry;
|
|
108 my @line=split(",",$entry);
|
|
109 if (!$line[5]){
|
|
110 $line[5]="NA";
|
|
111 }
|
|
112
|
|
113 @line=@line[0,1,2,3,4,5];
|
|
114 my $gene=$line[0];
|
|
115 chomp($gene);
|
|
116
|
|
117 #PUT GENE AND INFO INTO THE HASH FOR EXISTING KEY (1ST FILE) OR CREATE NEW KEY (2ND FILE)
|
|
118
|
|
119 if (!exists $all{$gene}){
|
|
120 my @info;
|
|
121 push (@info,@line);
|
|
122 $all{$gene}=\@info;
|
|
123 }
|
|
124 else{
|
|
125 my @info=@{$all{$gene}};
|
|
126 push (@info,@line);
|
|
127 my $diff=sprintf("$round",($info[1]-$info[7]));
|
|
128 my $total1=$info[2];
|
|
129 my $total2=$info[8];
|
|
130 my $sd1=$info[3];
|
|
131 my $se1=$info[4];
|
|
132 my $sd2=$info[9];
|
|
133 my $se2=$info[10];
|
|
134 my $df=$total1+$total2-2;
|
|
135 my $tdist;
|
|
136 my $pval;
|
|
137
|
|
138 # CHECK TO MAKE SURE ALL VARIABLES IN TDIST,PVAL CALCULATIONS ARE NUMBERS AND NO
|
|
139 # ZEROS (0) IN THE DENOMINATOR
|
|
140
|
|
141 if ($se1 eq "X" or $se2 eq "X" or $sd1 eq "X" or $sd2 eq "X" or $total1==0 or $total2==0 or $sd1==0 or $sd2==0 or $df<=0){
|
|
142 ($tdist,$pval)=("NA","NA");
|
|
143 }
|
|
144 else{
|
|
145 $tdist=sqrt((($diff)/(sqrt((($sd1**2)/$total1)+(($sd2**2)/$total2))))**2);
|
|
146
|
|
147 $pval=Statistics::Distributions::tprob($df,$tdist);
|
|
148 }
|
|
149 push (@info,$diff,$df,$tdist,$pval);
|
|
150 $all{$gene}=\@info;
|
|
151 }
|
|
152 }
|
|
153 close DATA;
|
|
154 }
|
|
155
|
|
156 #READ ALL HASH CONTENTS INTO 2D ARRAY FOR EASY SORTING AND PRINTING TO OUT FILE
|
|
157 my @unsorted;
|
|
158
|
|
159 foreach my $entry (keys %all) {
|
|
160 my @info=@{$all{$entry}};
|
|
161 my @temp;
|
|
162 push (@temp,@info);
|
|
163 push (@unsorted,\@temp);
|
|
164 }
|
|
165
|
|
166 #SORT GENES BY PVALUE OR FITNESS DEPENDING ON WHICH FLAG WAS USED IN COMMANDLINE
|
|
167
|
|
168 $sortkey=15; #default: sort by difference of means
|
|
169
|
|
170 my @sorted = sort { $a->[$sortkey] <=> $b->[$sortkey] } @unsorted;
|
|
171
|
|
172 #ADD NEW FIELDS TO HEADER (COLUMN NAMES)
|
|
173 my $field="Mean".$labels[0].'.'.$labels[1];
|
|
174 push (@header,$field,"DOF","TDIST","PVALUE");
|
|
175
|
|
176 #PRINT TO OUT FILE
|
|
177 open OUT, '>',"$outfile";
|
|
178 print OUT (join(',',@header),"\n");
|
|
179 foreach (@sorted){
|
|
180 my @woo=@{$_};
|
|
181 print OUT join(',',@woo),"\n";
|
|
182 }
|
|
183 close OUT;
|
|
184
|
|
185
|