annotate compStrains.pl @ 17:bc81db58983c draft

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