annotate robustZScoreNormalization.pl @ 0:7ecbc3ec2f55 draft default tip

Uploaded
author pmac
date Wed, 01 Jun 2016 03:59:51 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
1 ###############################################################################
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
2 # In summary this script calculates the robust z score for one or more plates
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
3 # of data.
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
4 #
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
5 # Args:
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
6 # input.data.frame contains one or more columns of plate data. The first column
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
7 # must contain the well annotation ie A01, A02 etc. Each subsequent column represents
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
8 # a plate of data where the column name is the plate name.
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
9 #
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
10 # plate.conf.data.frame is a file of the following format:
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
11 # well, type (syntax must be either "poscontr" or "negcontr" or "empty"), name (name of control)
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
12 #
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
13 # Returns:
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
14 # robust z score normalized values per plate.
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
15 #
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
16 # Author: kate gould
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
17 ###############################################################################
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
18
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
19 use strict;
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
20 use warnings;
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
21 use IO::Handle;
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
22 use File::Temp qw/ tempfile tempdir /;
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
23 my $tdir = tempdir( CLEANUP => 0 );
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
24
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
25 # check to make sure having correct input and output files
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
26 my $usage = "usage: robustZScoreNormalization.pl [TABULAR.in] [TABULAR.in] [TABULAR.out] \n";
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
27 die $usage unless @ARGV == 3;
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
28
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
29 #get the input arguments
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
30 my $plateData = $ARGV[0];
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
31 my $plateConfig = $ARGV[1];
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
32 my $zNorm = $ARGV[2];
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
33
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
34 #open the input files
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
35 open (INPUT1, "<", $plateData) || die("Could not open file $plateData \n");
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
36 open (INPUT2, "<", $plateConfig) || die("Could not open file $plateConfig \n");
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
37 open (OUTPUT1, ">", $zNorm) || die("Could not open file $zNorm \n");
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
38
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
39 #variable to store the name of the R script file
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
40 my $r_script;
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
41
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
42 # R script to implement the calcualtion of q-values based on multiple simultaneous tests p-values
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
43 # construct an R script file and save it in a temp directory
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
44 chdir $tdir;
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
45 $r_script = "robustZScoreNormalization.r";
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
46
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
47 open(Rcmd,">", $r_script) or die "Cannot open $r_script \n\n";
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
48 print Rcmd "
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
49 #options(show.error.messages = FALSE);
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
50 header <- read.table(\"$plateData\", head=F, sep=\"\\t\", comment=\"\", nrows=1);
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
51 input.data.frame <- read.table(\"$plateData\", head=F, sep=\"\\t\", skip=1);
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
52 plate.conf.data.frame <- read.table(\"$plateConfig\", head=T, sep=\"\\t\", comment=\"\");
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
53
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
54 # assumed second column is type ie negcontr or poscontr and third column is name of control
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
55 excluded.wells <- unique(plate.conf.data.frame[,1])
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
56
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
57 robust.z.score.normalise <- function(x, y) return ((x-median(y))/mad(y))
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
58 robust.z.score.normalisation.data.frame <- data.frame(well=input.data.frame[,1], stringsAsFactors=FALSE)
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
59
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
60 for (i in 2:length(colnames(input.data.frame))){
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
61 replicate.sample.values <- input.data.frame[((!(input.data.frame[,1] %in% excluded.wells))&(!(is.na(input.data.frame[,i])))),][,i]
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
62 robust.z.score.normalisation.data.frame <- cbind(robust.z.score.normalisation.data.frame, round(sapply(input.data.frame[,i], FUN=robust.z.score.normalise, replicate.sample.values), 2))
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
63 }
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
64 names(robust.z.score.normalisation.data.frame) <- names(input.data.frame)
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
65 write.table(header, file=\"$zNorm\", quote=F, sep=\"\\t\", row.names=F, col.names=F);
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
66 write.table(robust.z.score.normalisation.data.frame, file=\"$zNorm\", quote=F, sep=\"\\t\", row.names=F, col.names=F, append=T);
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
67 #eof\n";
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
68
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
69 close Rcmd;
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
70
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
71 system("R --no-restore --no-save --no-readline < $r_script > $r_script.out");
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
72
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
73 #close the input and output files
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
74 close(OUTPUT1);
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
75 close(INPUT1);
7ecbc3ec2f55 Uploaded
pmac
parents:
diff changeset
76 close(INPUT2);