annotate compStrains.pl @ 12:e627f5820272 draft

Uploaded
author antmarge
date Wed, 29 Mar 2017 14:13:50 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12
e627f5820272 Uploaded
antmarge
parents:
diff changeset
1 #!/usr/bin/perl -w
e627f5820272 Uploaded
antmarge
parents:
diff changeset
2
e627f5820272 Uploaded
antmarge
parents:
diff changeset
3 #Margaret Antonio 16.01.13
e627f5820272 Uploaded
antmarge
parents:
diff changeset
4
e627f5820272 Uploaded
antmarge
parents:
diff changeset
5 #DESCRIPTION: Takes two aggregate.pl outputs and compares them using mean difference, pval for each
e627f5820272 Uploaded
antmarge
parents:
diff changeset
6 #gene. Can compare, for example, 19F in glucose and TIGR4 in glucose.
e627f5820272 Uploaded
antmarge
parents:
diff changeset
7 #DIFFERENT GENOMES (ie. diff. strains).
e627f5820272 Uploaded
antmarge
parents:
diff changeset
8 #Requires CONVERSION FILE
e627f5820272 Uploaded
antmarge
parents:
diff changeset
9
e627f5820272 Uploaded
antmarge
parents:
diff changeset
10 #USAGE: perl compStrains.pl -c <conversion.csv> <options>
e627f5820272 Uploaded
antmarge
parents:
diff changeset
11 #[<aggregateFile1.csv aggregateFile2.csv> OR -indir <indir/>]
e627f5820272 Uploaded
antmarge
parents:
diff changeset
12
e627f5820272 Uploaded
antmarge
parents:
diff changeset
13 use Data::Dumper;
e627f5820272 Uploaded
antmarge
parents:
diff changeset
14 use strict;
e627f5820272 Uploaded
antmarge
parents:
diff changeset
15 use Getopt::Long;
e627f5820272 Uploaded
antmarge
parents:
diff changeset
16 use warnings;
e627f5820272 Uploaded
antmarge
parents:
diff changeset
17 use File::Path;
e627f5820272 Uploaded
antmarge
parents:
diff changeset
18 use File::Basename;
e627f5820272 Uploaded
antmarge
parents:
diff changeset
19 use Statistics::Distributions;
e627f5820272 Uploaded
antmarge
parents:
diff changeset
20
e627f5820272 Uploaded
antmarge
parents:
diff changeset
21 #ASSIGN INPUTS TO VARIABLES USING FLAGS
e627f5820272 Uploaded
antmarge
parents:
diff changeset
22 our ($input1,$input2,$out,$sortkey,$round,$l1,$l2,$cfile);
e627f5820272 Uploaded
antmarge
parents:
diff changeset
23 GetOptions(
e627f5820272 Uploaded
antmarge
parents:
diff changeset
24 'h' => \$h,
e627f5820272 Uploaded
antmarge
parents:
diff changeset
25 'o:s' =>\$out,
e627f5820272 Uploaded
antmarge
parents:
diff changeset
26 's:i' => \$sortkey,
e627f5820272 Uploaded
antmarge
parents:
diff changeset
27 'r:i'=> \$round,
e627f5820272 Uploaded
antmarge
parents:
diff changeset
28 'l1:s'=> \$l1,
e627f5820272 Uploaded
antmarge
parents:
diff changeset
29 'l2:s'=> \$l2,
e627f5820272 Uploaded
antmarge
parents:
diff changeset
30 'c:s'=> \$cfile,
e627f5820272 Uploaded
antmarge
parents:
diff changeset
31 'input1:s'=>\$input1,
e627f5820272 Uploaded
antmarge
parents:
diff changeset
32 'input2:s'=>\$input2
e627f5820272 Uploaded
antmarge
parents:
diff changeset
33 );
e627f5820272 Uploaded
antmarge
parents:
diff changeset
34
e627f5820272 Uploaded
antmarge
parents:
diff changeset
35 sub print_usage() {
e627f5820272 Uploaded
antmarge
parents:
diff changeset
36 print "\n";
e627f5820272 Uploaded
antmarge
parents:
diff changeset
37 print "\n##################################################################\n";
e627f5820272 Uploaded
antmarge
parents:
diff changeset
38 print "compStrains.pl: compare genes from a tn-seq experiment\n";
e627f5820272 Uploaded
antmarge
parents:
diff changeset
39 print "\tfor two DIFFERENT strains/genomes using aggregate files\n";
e627f5820272 Uploaded
antmarge
parents:
diff changeset
40
e627f5820272 Uploaded
antmarge
parents:
diff changeset
41 print "\nDESCRIPTION: Takes two aggregate.pl outputs and compares them by\n";
e627f5820272 Uploaded
antmarge
parents:
diff changeset
42 print "calculating the difference in mean fitness.\n";
e627f5820272 Uploaded
antmarge
parents:
diff changeset
43
e627f5820272 Uploaded
antmarge
parents:
diff changeset
44 print "Example: two strains tested under same condition.\n";
e627f5820272 Uploaded
antmarge
parents:
diff changeset
45 print "Note: For same strains (genomes), use compGenes.pl\n";
e627f5820272 Uploaded
antmarge
parents:
diff changeset
46
e627f5820272 Uploaded
antmarge
parents:
diff changeset
47 print "\nUSAGE:\n";
e627f5820272 Uploaded
antmarge
parents:
diff changeset
48 print "perl compStrains.pl -c conversion.csv -d inputs/\n";
e627f5820272 Uploaded
antmarge
parents:
diff changeset
49
e627f5820272 Uploaded
antmarge
parents:
diff changeset
50 print "\nREQUIRED:\n";
e627f5820272 Uploaded
antmarge
parents:
diff changeset
51 print " -d\tDirectory containing all input files (files from\n";
e627f5820272 Uploaded
antmarge
parents:
diff changeset
52 print " \taggregate fitness script)\n";
e627f5820272 Uploaded
antmarge
parents:
diff changeset
53 print " \tOR\n";
e627f5820272 Uploaded
antmarge
parents:
diff changeset
54 print " \tIn the command line (without a flag), input the name(s) of\n";
e627f5820272 Uploaded
antmarge
parents:
diff changeset
55 print " \ttwo files containing aggregate gene fitness values. \n\n";
e627f5820272 Uploaded
antmarge
parents:
diff changeset
56 print " -c\tConversion file: two columns with homologs for both organisms\n";
e627f5820272 Uploaded
antmarge
parents:
diff changeset
57
e627f5820272 Uploaded
antmarge
parents:
diff changeset
58 print "\nOPTIONAL:\n";
e627f5820272 Uploaded
antmarge
parents:
diff changeset
59 print " -h\tPrints usage and exits program\n";
e627f5820272 Uploaded
antmarge
parents:
diff changeset
60 print " -o\tOutput file for comparison data. Default: label1label2.csv\n";
e627f5820272 Uploaded
antmarge
parents:
diff changeset
61 print " -s\tSort output by this index of the file (indices begin at 0).\n";
e627f5820272 Uploaded
antmarge
parents:
diff changeset
62 print " \tDefault: by mean\n";
e627f5820272 Uploaded
antmarge
parents:
diff changeset
63 print " -r\tRound final output numbers to this number of decimals\n";
e627f5820272 Uploaded
antmarge
parents:
diff changeset
64 print " -l\tLabels for input files. Default: filenames\n";
e627f5820272 Uploaded
antmarge
parents:
diff changeset
65 print " \tTwo strings, comma separated (i.e. -l expt1,expt2).\n";
e627f5820272 Uploaded
antmarge
parents:
diff changeset
66 print " \tOrder should match file order.\n";
e627f5820272 Uploaded
antmarge
parents:
diff changeset
67 print " \n~~~~Always check that file paths are correctly specified~~~~\n";
e627f5820272 Uploaded
antmarge
parents:
diff changeset
68 print "\n##################################################################\n";
e627f5820272 Uploaded
antmarge
parents:
diff changeset
69 }
e627f5820272 Uploaded
antmarge
parents:
diff changeset
70
e627f5820272 Uploaded
antmarge
parents:
diff changeset
71 #THE @files ARRAY WILL CONTAIN INPUT FILE NAMES
e627f5820272 Uploaded
antmarge
parents:
diff changeset
72 my @files=($input1,$input2)
e627f5820272 Uploaded
antmarge
parents:
diff changeset
73
e627f5820272 Uploaded
antmarge
parents:
diff changeset
74 #GET LABELS:
e627f5820272 Uploaded
antmarge
parents:
diff changeset
75 my @labels = ($l1,$l2);
e627f5820272 Uploaded
antmarge
parents:
diff changeset
76
e627f5820272 Uploaded
antmarge
parents:
diff changeset
77
e627f5820272 Uploaded
antmarge
parents:
diff changeset
78 #CHECK IF REQ. VARIABLES WERE DEFINED USING FLAGS. IF NOT THEN USE DEFAULT VALUES
e627f5820272 Uploaded
antmarge
parents:
diff changeset
79
e627f5820272 Uploaded
antmarge
parents:
diff changeset
80 if (!$out) {$out="comp.".$labels[0].$labels[1].".csv"}
e627f5820272 Uploaded
antmarge
parents:
diff changeset
81 if (!$round){$round='%.4f'}
e627f5820272 Uploaded
antmarge
parents:
diff changeset
82
e627f5820272 Uploaded
antmarge
parents:
diff changeset
83 #OPEN INPUTTED AGGREGATE GENE FILES AND STORE THEIR CONTENTS INTO TWO HASHES
e627f5820272 Uploaded
antmarge
parents:
diff changeset
84 #FILE1 GOES INTO HASH %ONE AND FILE2 GOES INTO HASH %TWO.
e627f5820272 Uploaded
antmarge
parents:
diff changeset
85
e627f5820272 Uploaded
antmarge
parents:
diff changeset
86 #FILE1 OPENING ---> %one WHERE KEY:VALUE IS GENE_ID:(GENE_ID,INSERTIONS,MEAN,ETC.)
e627f5820272 Uploaded
antmarge
parents:
diff changeset
87 my @header;
e627f5820272 Uploaded
antmarge
parents:
diff changeset
88 my %one;
e627f5820272 Uploaded
antmarge
parents:
diff changeset
89
e627f5820272 Uploaded
antmarge
parents:
diff changeset
90 open (F1,'<',$files[0]);
e627f5820272 Uploaded
antmarge
parents:
diff changeset
91
e627f5820272 Uploaded
antmarge
parents:
diff changeset
92 #STORE COLUMN NAMES (FIRST LINE OF FILE1) FOR HEADER AND APPEND LABELS
e627f5820272 Uploaded
antmarge
parents:
diff changeset
93 my $head=<F1>; #the header in the file
e627f5820272 Uploaded
antmarge
parents:
diff changeset
94 my @cols=split(',',$head);
e627f5820272 Uploaded
antmarge
parents:
diff changeset
95 @cols=@cols[0,1,2,3,4,5,6]; #get rid of blank columns
e627f5820272 Uploaded
antmarge
parents:
diff changeset
96 for (my $j=0;$j<scalar @cols;$j++){
e627f5820272 Uploaded
antmarge
parents:
diff changeset
97 $cols[$j]=$cols[$j].'-'.$labels[0]; #mark each column name with file it comes from
e627f5820272 Uploaded
antmarge
parents:
diff changeset
98 }
e627f5820272 Uploaded
antmarge
parents:
diff changeset
99 push (@header,@cols);
e627f5820272 Uploaded
antmarge
parents:
diff changeset
100
e627f5820272 Uploaded
antmarge
parents:
diff changeset
101 while (my $line=<F1>){
e627f5820272 Uploaded
antmarge
parents:
diff changeset
102 chomp $line;
e627f5820272 Uploaded
antmarge
parents:
diff changeset
103 my @info=split(",",$line);
e627f5820272 Uploaded
antmarge
parents:
diff changeset
104 #Only keep the first 7 columns (Ones about blanks aren't needed for comparisons)
e627f5820272 Uploaded
antmarge
parents:
diff changeset
105 @info=@info[0,1,2,3,4,5,6];
e627f5820272 Uploaded
antmarge
parents:
diff changeset
106 #Sometimes genes that don't have a gene name can't be blank, so fill with NA
e627f5820272 Uploaded
antmarge
parents:
diff changeset
107 if (!$info[5]){
e627f5820272 Uploaded
antmarge
parents:
diff changeset
108 $info[5]="NA";
e627f5820272 Uploaded
antmarge
parents:
diff changeset
109 }
e627f5820272 Uploaded
antmarge
parents:
diff changeset
110 #If there are no insertions in the column "total", then make it =0 rather than blank
e627f5820272 Uploaded
antmarge
parents:
diff changeset
111 if (!$info[6]){
e627f5820272 Uploaded
antmarge
parents:
diff changeset
112 $info[6]=0;
e627f5820272 Uploaded
antmarge
parents:
diff changeset
113 }
e627f5820272 Uploaded
antmarge
parents:
diff changeset
114 $one{$info[0]}=\@info;
e627f5820272 Uploaded
antmarge
parents:
diff changeset
115 }
e627f5820272 Uploaded
antmarge
parents:
diff changeset
116 close F1;
e627f5820272 Uploaded
antmarge
parents:
diff changeset
117
e627f5820272 Uploaded
antmarge
parents:
diff changeset
118 #FILE2 OPENING ---> %two WHERE KEY:VALUE IS GENE_ID:(GENE_ID,INSERTIONS,MEAN,ETC.)
e627f5820272 Uploaded
antmarge
parents:
diff changeset
119
e627f5820272 Uploaded
antmarge
parents:
diff changeset
120 my %two;
e627f5820272 Uploaded
antmarge
parents:
diff changeset
121 open (F2,'<',$files[1]);
e627f5820272 Uploaded
antmarge
parents:
diff changeset
122
e627f5820272 Uploaded
antmarge
parents:
diff changeset
123 #STORE COLUMN NAMES (FIRST LINE OF FILE2) FOR HEADER AND APPEND LABELS
e627f5820272 Uploaded
antmarge
parents:
diff changeset
124 $head=<F2>; #the header in the file
e627f5820272 Uploaded
antmarge
parents:
diff changeset
125 @cols=split(',',$head);
e627f5820272 Uploaded
antmarge
parents:
diff changeset
126 @cols=@cols[0,1,2,3,4,5,6]; #get rid of blank columns
e627f5820272 Uploaded
antmarge
parents:
diff changeset
127 for (my $j=0;$j<scalar @cols;$j++){
e627f5820272 Uploaded
antmarge
parents:
diff changeset
128 $cols[$j]=$cols[$j].'-'.$labels[1]; #mark each column name with file it comes from
e627f5820272 Uploaded
antmarge
parents:
diff changeset
129 }
e627f5820272 Uploaded
antmarge
parents:
diff changeset
130 push (@header,@cols);
e627f5820272 Uploaded
antmarge
parents:
diff changeset
131
e627f5820272 Uploaded
antmarge
parents:
diff changeset
132 while (my $line=<F2>){
e627f5820272 Uploaded
antmarge
parents:
diff changeset
133 chomp $line;
e627f5820272 Uploaded
antmarge
parents:
diff changeset
134 my @info=split(",",$line);
e627f5820272 Uploaded
antmarge
parents:
diff changeset
135 @info=@info[0,1,2,3,4,5,6];
e627f5820272 Uploaded
antmarge
parents:
diff changeset
136 if (!$info[5]){
e627f5820272 Uploaded
antmarge
parents:
diff changeset
137 $info[5]="NA";
e627f5820272 Uploaded
antmarge
parents:
diff changeset
138 }
e627f5820272 Uploaded
antmarge
parents:
diff changeset
139 if (!$info[6]){
e627f5820272 Uploaded
antmarge
parents:
diff changeset
140 $info[6]=0;
e627f5820272 Uploaded
antmarge
parents:
diff changeset
141 }
e627f5820272 Uploaded
antmarge
parents:
diff changeset
142 $two{$info[0]}=\@info;
e627f5820272 Uploaded
antmarge
parents:
diff changeset
143 }
e627f5820272 Uploaded
antmarge
parents:
diff changeset
144 close F2;
e627f5820272 Uploaded
antmarge
parents:
diff changeset
145
e627f5820272 Uploaded
antmarge
parents:
diff changeset
146
e627f5820272 Uploaded
antmarge
parents:
diff changeset
147 #READ CONVERSION FILE INTO ARRAY.
e627f5820272 Uploaded
antmarge
parents:
diff changeset
148 #Conversion file must have strain 1 for file 1 in column 1 (index 0) and
e627f5820272 Uploaded
antmarge
parents:
diff changeset
149 #strain 2 for file 2 in column 2 (index 1)
e627f5820272 Uploaded
antmarge
parents:
diff changeset
150 #conversion file must be tab delimited with no NA fields
e627f5820272 Uploaded
antmarge
parents:
diff changeset
151 #If homologs (exist then take info from hashes (%one and %two) by referring to gene_id in KEY
e627f5820272 Uploaded
antmarge
parents:
diff changeset
152
e627f5820272 Uploaded
antmarge
parents:
diff changeset
153 my @all; #store all homologs in this hash
e627f5820272 Uploaded
antmarge
parents:
diff changeset
154 open (CONV,'<',$cfile);
e627f5820272 Uploaded
antmarge
parents:
diff changeset
155 while (my $line=<CONV>){
e627f5820272 Uploaded
antmarge
parents:
diff changeset
156 chomp $line;
e627f5820272 Uploaded
antmarge
parents:
diff changeset
157 my @genes=split("\t",$line); #Array @genes will contain two genes (SP_0000,SPT_0000)
e627f5820272 Uploaded
antmarge
parents:
diff changeset
158 if (scalar @genes==2 and $genes[0] ne "" and $genes[1] ne ""){
e627f5820272 Uploaded
antmarge
parents:
diff changeset
159 my @info;
e627f5820272 Uploaded
antmarge
parents:
diff changeset
160 my @oneArray=@{$one{$genes[0]}};
e627f5820272 Uploaded
antmarge
parents:
diff changeset
161 my @twoArray=@{$two{$genes[1]}};
e627f5820272 Uploaded
antmarge
parents:
diff changeset
162 push (@info,@oneArray,@twoArray);
e627f5820272 Uploaded
antmarge
parents:
diff changeset
163 my $diff=sprintf("$round",($info[1]-$info[8]));
e627f5820272 Uploaded
antmarge
parents:
diff changeset
164 my $total1=$info[6];
e627f5820272 Uploaded
antmarge
parents:
diff changeset
165 my $total2=$info[13];
e627f5820272 Uploaded
antmarge
parents:
diff changeset
166 my $sd1=$info[3];
e627f5820272 Uploaded
antmarge
parents:
diff changeset
167 my $se1=$info[4];
e627f5820272 Uploaded
antmarge
parents:
diff changeset
168 my $sd2=$info[10];
e627f5820272 Uploaded
antmarge
parents:
diff changeset
169 my $se2=$info[11];
e627f5820272 Uploaded
antmarge
parents:
diff changeset
170 my $df=$total1+$total2-2;
e627f5820272 Uploaded
antmarge
parents:
diff changeset
171 my $tdist;
e627f5820272 Uploaded
antmarge
parents:
diff changeset
172 my $pval;
e627f5820272 Uploaded
antmarge
parents:
diff changeset
173 #TDIST, PVAL calculations with fail if standard dev, error, or counts are not real numbers
e627f5820272 Uploaded
antmarge
parents:
diff changeset
174 #or if 0 ends up in denominator
e627f5820272 Uploaded
antmarge
parents:
diff changeset
175 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){
e627f5820272 Uploaded
antmarge
parents:
diff changeset
176 ($tdist,$pval)=("NA","NA");
e627f5820272 Uploaded
antmarge
parents:
diff changeset
177 }
e627f5820272 Uploaded
antmarge
parents:
diff changeset
178 else{
e627f5820272 Uploaded
antmarge
parents:
diff changeset
179 $tdist=sqrt((($diff)/(sqrt((($sd1**2)/$total1)+(($sd2**2)/$total2))))**2);
e627f5820272 Uploaded
antmarge
parents:
diff changeset
180 $pval=Statistics::Distributions::tprob($df,$tdist);
e627f5820272 Uploaded
antmarge
parents:
diff changeset
181 }
e627f5820272 Uploaded
antmarge
parents:
diff changeset
182 push (@info,$diff,$df,$tdist,$pval);
e627f5820272 Uploaded
antmarge
parents:
diff changeset
183 push (@all,\@info);
e627f5820272 Uploaded
antmarge
parents:
diff changeset
184 }
e627f5820272 Uploaded
antmarge
parents:
diff changeset
185 }
e627f5820272 Uploaded
antmarge
parents:
diff changeset
186 close CONV;
e627f5820272 Uploaded
antmarge
parents:
diff changeset
187
e627f5820272 Uploaded
antmarge
parents:
diff changeset
188 #SORT THE HOMOLOGS BY THE SORTKEY OR BY DEFAULT DIFFERENCE IN MEAN FITNESSES
e627f5820272 Uploaded
antmarge
parents:
diff changeset
189 if (!$sortkey){
e627f5820272 Uploaded
antmarge
parents:
diff changeset
190 $sortkey=14; #for mean difference
e627f5820272 Uploaded
antmarge
parents:
diff changeset
191 }
e627f5820272 Uploaded
antmarge
parents:
diff changeset
192 my @sorted = sort { $b->[$sortkey] <=> $a->[$sortkey] } @all;
e627f5820272 Uploaded
antmarge
parents:
diff changeset
193
e627f5820272 Uploaded
antmarge
parents:
diff changeset
194 #FINISH THE HEADER BY ADDING COLUMN NAMES FOR MEAN-DIFF, DOF, TDIST, AND PVALUE
e627f5820272 Uploaded
antmarge
parents:
diff changeset
195 my $field="MeanDiff(".$labels[0].'.'.$labels[1].")";
e627f5820272 Uploaded
antmarge
parents:
diff changeset
196 push (@header,$field,"DOF","TDIST","PVALUE");
e627f5820272 Uploaded
antmarge
parents:
diff changeset
197
e627f5820272 Uploaded
antmarge
parents:
diff changeset
198 #PRINT MATCHED HOMOLOG INFORMATION INTO A SINGLE OUTPUT FILE
e627f5820272 Uploaded
antmarge
parents:
diff changeset
199 open OUT, '>',"$out";
e627f5820272 Uploaded
antmarge
parents:
diff changeset
200 print OUT (join(',',@header),"\n");
e627f5820272 Uploaded
antmarge
parents:
diff changeset
201 foreach (@sorted){
e627f5820272 Uploaded
antmarge
parents:
diff changeset
202 my @woo=@{$_};
e627f5820272 Uploaded
antmarge
parents:
diff changeset
203 print OUT join(',',@woo),"\n";
e627f5820272 Uploaded
antmarge
parents:
diff changeset
204 }
e627f5820272 Uploaded
antmarge
parents:
diff changeset
205
e627f5820272 Uploaded
antmarge
parents:
diff changeset
206 close OUT;
e627f5820272 Uploaded
antmarge
parents:
diff changeset
207
e627f5820272 Uploaded
antmarge
parents:
diff changeset
208