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

Uploaded
author pmac
date Wed, 01 Jun 2016 03:52:52 -0400
parents
children
line wrap: on
line source

###############################################################################
# In summary this script normalizes each plate of input data to each of its negative
# controls.
#
# Args:
#   input.data.frame contains one or more columns of plate data. The first column 
#   must contain the well annotation ie A01, A02 etc. Each subsequent column represents
#   a plate of data where the column name is the plate name.
#
#   plate.conf.data.frame is a file of the following format:
#	  well, type (syntax must be either "poscontr" or "negcontr" or "empty"), name (name of control)
#
# Returns:
#   plate values normalized to each negative control type per plate.
#
# Author: kate gould
###############################################################################

use strict;
use warnings;
use IO::Handle;
use File::Temp qw/ tempfile tempdir /;
my $tdir = tempdir( CLEANUP => 0 );

# check to make sure having correct input and output files
my $usage = "usage: normalizeMedianNegCntrl.pl [TABULAR.in] [TABULAR.in] [TABULAR.out] \n";
die $usage unless @ARGV == 3;

#get the input arguments
my $plateData = $ARGV[0];
my $plateConfig =  $ARGV[1];
my $normMedNeg =  $ARGV[2];

#open the input files
open (INPUT1, "<", $plateData) || die("Could not open file $plateData \n");
open (INPUT2, "<", $plateConfig) || die("Could not open file $plateConfig \n");
open (OUTPUT1, ">", $normMedNeg) || die("Could not open file $normMedNeg \n");

#variable to store the name of the R script file
my $r_script;

# R script to implement the calcualtion of q-values based on multiple simultaneous tests p-values 	
# construct an R script file and save it in a temp directory
chdir $tdir;
$r_script = "normalizeMedianNegCntrl.r";

open(Rcmd,">", $r_script) or die "Cannot open $r_script \n\n"; 
print Rcmd "
	#options(show.error.messages = FALSE);
	header <- read.table(\"$plateData\", head=F, sep=\"\\t\", comment=\"\", nrows=1);
	input.data.frame <- read.table(\"$plateData\", head=F, sep=\"\\t\", skip=1);
	plate.conf.data.frame <- read.table(\"$plateConfig\", head=T, sep=\"\\t\", comment=\"\");

	normalise <- function(x, y){if (!is.na(x)) return (x/y) else (return (NA))}
	
	# assumed second column is type ie negcontr and third column is name of control
	negative.controls.list <- unique(plate.conf.data.frame[which (plate.conf.data.frame[,2] == \"negcontr\"),][,3]);
	
	normalized.data.frame <- data.frame(well=input.data.frame[,1], stringsAsFactors=FALSE);
	column.name.list <- c();
	for (negative.control in negative.controls.list){
		negative.control.wells <- plate.conf.data.frame[which (plate.conf.data.frame[3] == negative.control),][,1];
		
		for (i in 2:length(colnames(input.data.frame))){
			column.name <- paste(colnames(input.data.frame)[i], \".normalised.to.\", negative.control, sep=\"\");
			column.name.list <- append(column.name.list, column.name);
			
			negative.control.values <- input.data.frame[((input.data.frame[,1] %in% negative.control.wells)&(!(is.na(input.data.frame[,i])))),][,i];
			median.negative.value <- median(negative.control.values);
			
			normalised.to.median.negative.control.value <- round(sapply(input.data.frame[,i], FUN=normalise, median.negative.value), 2);
			normalized.data.frame <- cbind(normalized.data.frame, normalised.to.median.negative.control.value);
		}
	}  
	header <- c(header[1], column.name.list)
	write.table(header, file=\"$normMedNeg\", quote=F, sep=\"\\t\", row.names=F, col.names=F);
	write.table(normalized.data.frame, file=\"$normMedNeg\", quote=F, sep=\"\\t\", row.names=F, col.names=F, append=T);
	#eof\n";

close Rcmd;	

system("R --no-restore --no-save --no-readline < $r_script > $r_script.out");

#close the input and output files
close(OUTPUT1);
close(INPUT1);
close(INPUT2);