comparison compStrains.pl @ 8:5bd43e313819 draft

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