annotate compStrains.pl @ 15:3e0c20f67033 draft

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