annotate calculateZPrimeFactor.pl @ 5:d7efb5d5352a draft default tip

Uploaded
author jason-ellul
date Wed, 01 Jun 2016 02:40:16 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
1 ###############################################################################
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
2 # In summary this script calculates the z prime factor for one or more plates
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
3 # of data.
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
4 #
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
5 # Args:
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
6 # input.data.frame contains one or more columns of plate data. The first column
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
7 # must contain the well annotation ie A01, A02 etc. Each subsequent column represents
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
8 # a plate of data where the column name is the plate name.
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
9 #
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
10 # plate.conf.data.frame is a file of the following format:
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
11 # well, type(syntax must be either "poscontr" or "negcontr"), name (name of control)
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
12 #
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
13 # Returns:
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
14 # z'factor for each plate for each combination of each type of positive vs negative control.
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
15 #
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
16 # Author: gouldkate
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
17 ###############################################################################
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
18
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
19 use strict;
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
20 use warnings;
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
21 use IO::Handle;
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
22 use File::Temp qw/ tempfile tempdir /;
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
23 my $tdir = tempdir( CLEANUP => 0 );
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
24
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
25 # check to make sure having correct input and output files
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
26 my $usage = "usage: calculateZPrimeFactor.pl [TABULAR.in] [TABULAR.in] [TABULAR.out] \n";
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
27 die $usage unless @ARGV == 3;
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
28
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
29 #get the input arguments
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
30 my $plateData = $ARGV[0];
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
31 my $plateConfig = $ARGV[1];
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
32 my $zPrimeFactor = $ARGV[2];
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
33
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
34 #open the input files
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
35 open (INPUT1, "<", $plateData) || die("Could not open file $plateData \n");
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
36 open (INPUT2, "<", $plateConfig) || die("Could not open file $plateConfig \n");
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
37 open (OUTPUT1, ">", $zPrimeFactor) || die("Could not open file $zPrimeFactor \n");
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
38
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
39 #variable to store the name of the R script file
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
40 my $r_script;
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
41
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
42 # R script to implement the calcualtion of q-values based on multiple simultaneous tests p-values
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
43 # construct an R script file and save it in a temp directory
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
44 chdir $tdir;
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
45 $r_script = "calculateZPrimeFactor.r";
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
46
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
47 open(Rcmd,">", $r_script) or die "Cannot open $r_script \n\n";
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
48 print Rcmd "
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
49 #options(show.error.messages = FALSE);
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
50 input.data.frame <- read.table(\"$plateData\", head=T, sep=\"\\t\", comment=\"\");
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
51 plate.conf.data.frame <- read.table(\"$plateConfig\", head=T, sep=\"\\t\", comment=\"\");
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
52
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
53 # assumed second column is type ie negcontr or poscontr and third column is name of control
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
54 negative.controls.list <- unique(plate.conf.data.frame[which (plate.conf.data.frame[,2] == \"negcontr\"),][,3])
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
55 positive.controls.list <- unique(plate.conf.data.frame[which (plate.conf.data.frame[,2] == \"poscontr\"),][,3])
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
56
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
57 z.prime.factor.report.data.frame <- data.frame()
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
58
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
59
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
60 for (negative.control in negative.controls.list){
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
61 for (positive.control in positive.controls.list){
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
62 for (i in 2:length(colnames(input.data.frame))){
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
63
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
64 negative.control.wells <- plate.conf.data.frame[which (plate.conf.data.frame[3] == negative.control),][,1]
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
65 positive.control.wells <- plate.conf.data.frame[which (plate.conf.data.frame[3] == positive.control),][,1]
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
66
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
67 control.duo <- paste(negative.control, positive.control, sep=\"/\")
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
68
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
69 negative.control.values <- input.data.frame[((input.data.frame[,1] %in% negative.control.wells)&(!(is.na(input.data.frame[,i])))),][,i]
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
70 positive.control.values <- input.data.frame[((input.data.frame[,1] %in% positive.control.wells)&(!(is.na(input.data.frame[,i])))),][,i]
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
71
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
72 if ((length(negative.control.values)==0)|(length(positive.control.values)==0)){
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
73 z.prime.factor <- NA
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
74 } else {
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
75 if ((sum(negative.control.values)) < (sum(positive.control.values))){
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
76 low.value.controls <- negative.control.values
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
77 high.value.controls <- positive.control.values
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
78 } else {
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
79 low.value.controls <- positive.control.values
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
80 high.value.controls <- negative.control.values
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
81 }
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
82 z.prime.factor <- round(1 - ((3 * (sd(high.value.controls, na.rm = TRUE) + sd(low.value.controls, na.rm = TRUE))/(abs(mean(high.value.controls, na.rm = TRUE) - mean(low.value.controls, na.rm = TRUE))))), 2)
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
83 }
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
84
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
85 plate.name <- colnames(input.data.frame)[i]
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
86 z.prime.factor.current.row.data.frame <- data.frame(plate=plate.name, control.duo=control.duo, z.prime.factor=z.prime.factor, stringsAsFactors=FALSE)
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
87 z.prime.factor.report.data.frame <-rbind(z.prime.factor.report.data.frame, z.prime.factor.current.row.data.frame)
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
88 }
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
89 }
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
90 }
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
91 z.prime.factor.report.data.frame <- z.prime.factor.report.data.frame[order(z.prime.factor.report.data.frame\$control.duo),]
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
92 write.table(z.prime.factor.report.data.frame, file=\"$zPrimeFactor\", quote=F, sep=\"\\t\", row.names=F);
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
93 #eof\n";
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
94
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
95 close Rcmd;
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
96
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
97 system("R --no-restore --no-save --no-readline < $r_script > $r_script.out");
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
98
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
99 #close the input and output files
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
100 close(OUTPUT1);
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
101 close(INPUT1);
d7efb5d5352a Uploaded
jason-ellul
parents:
diff changeset
102 close(INPUT2);