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