annotate normalizeMedianNegCntrl.pl @ 0:087ab474dfa4 draft default tip

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