annotate compGenes.pl @ 15:2efae0932a7a draft default tip

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