annotate compGenes.pl @ 2:5ff933ea9878 draft

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