annotate reformatPlatesLinearToTabular.pl @ 0:2aafe60f2fad draft default tip

Uploaded
author pmac
date Wed, 01 Jun 2016 03:55:27 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
1 ###############################################################################
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
2 # This script converts plate data from linear to tabular format.
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
3 #
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
4 # Args:
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
5 # input file:
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
6 # a text file containing a set of linear data in either 384/96 well format,
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
7 #
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
8 # Returns:
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
9 # For each input file, a tabular version of the data is returned
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
10 # in the same format typically generated from synergy or cellomics software.
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
11 #
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
12 # Author: jason ellul
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
13 ###############################################################################
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
14
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
15 use strict;
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
16 use warnings;
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
17 use IO::Handle;
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
18 use File::Temp qw/ tempfile tempdir /;
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
19 my $tdir = tempdir( CLEANUP => 0 );
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
20
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
21 # check to make sure having correct input and output files
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
22 my $usage = "usage: reformatPlatesTabularToLinear.pl [TABULAR.in] [TABULAR.out] \n";
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
23 die $usage unless @ARGV == 2;
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
24
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
25 #get the input arguments
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
26 my $linearPlateTable = $ARGV[0];
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
27 my $tabularPlateTable = $ARGV[1];
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
28
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
29 #open the input files
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
30 open (INPUT, "<", $linearPlateTable) || die("Could not open file $linearPlateTable \n");
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
31 open (OUTPUT1, ">", $tabularPlateTable) || die("Could not open file $tabularPlateTable \n");
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
32
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
33 #variable to store the name of the R script file
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
34 my $r_script;
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
35
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
36 # R script to implement the calcualtion of q-values based on multiple simultaneous tests p-values
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
37 # construct an R script file and save it in a temp directory
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
38 chdir $tdir;
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
39 $r_script = "reformatPlatesLinearToTabular.r";
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
40
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
41 open(Rcmd,">", $r_script) or die "Cannot open $r_script \n\n";
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
42 print Rcmd "
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
43 #options(show.error.messages = FALSE);
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
44
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
45 #read the plates table
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
46 #tables <- read.table(\"$linearPlateTable\", sep=\"\\t\", head=T, comment=\"\");
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
47 tablesTMP <- scan(\"$linearPlateTable\", sep=\"\\n\", what=\"character\", quiet = TRUE);
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
48 tmp <- strsplit(tablesTMP[1], \"\t\")[[1]];
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
49 tables <- matrix(\"\", nrow=length(tablesTMP)-1, ncol=length(tmp))
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
50 colnames(tables) <- tmp;
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
51 for(i in 2:length(tablesTMP)) {
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
52 tmp <- strsplit(tablesTMP[i], \"\t\")[[1]];
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
53 if(length(tmp) > ncol(tables)) stop(paste(\"Error: Row\", i, \"has more columns than the header\"));
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
54 tables[i-1, 1:length(tmp)] <- tmp;
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
55 }
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
56 tables <- as.data.frame(tables, stringsAsFactors=F);
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
57
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
58 if(ncol(tables) < 2) {
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
59 stop(\"The first column of the table must contain the well ID from A01 to either H12 or P24 depending on the number of wells.\")
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
60 }
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
61
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
62 # check if the plate is in 96 or 384 well format
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
63 if(nrow(tables) == 96) {
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
64 nc <- 12;
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
65 nr <- 8;
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
66 } else if(nrow(tables) == 384) {
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
67 nc <- 24;
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
68 nr <- 16;
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
69 } else {
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
70 stop(\"Table is not for a 96 or 384 well plate. Please ensure you either have 96 or 384 rows plus a header.\")
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
71 }
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
72
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
73 # for each table
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
74 for(i in 2:ncol(tables)) {
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
75 # write the name of the table
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
76 write(paste(colnames(tables)[i], sep=\"\"), file=\"$tabularPlateTable\", append=T);
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
77 write(\"\", file=\"$tabularPlateTable\", append=T);
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
78 # the column header
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
79 write(paste(\"\\t\", paste(1:nc, collapse=\"\\t\"), sep=\"\"), file=\"$tabularPlateTable\", append=T);
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
80 # replace any NAs with blank
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
81 if(any(is.na(tables[,i]))) tables[which(is.na(tables[,i])),i] <- \"\";
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
82 # for each row print the values
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
83 curr <- 0;
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
84 for(j in LETTERS[1:nr]) {
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
85 write(paste(j, \"\\t\", paste(tables[(curr+1):(curr+nc), i], collapse=\"\\t\"), sep=\"\"), file=\"$tabularPlateTable\", append=T);
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
86 curr <- curr + nc;
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
87 }
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
88 # if we are at the last table do not add an extra line
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
89 if(i != ncol(tables)) write(\"\", file=\"$tabularPlateTable\", append=T);
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
90 }
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
91 #eof\n";
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
92
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
93 close Rcmd;
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
94
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
95 system("R --no-restore --no-save --no-readline < $r_script > $r_script.out");
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
96
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
97 #close the input and output files
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
98 close(OUTPUT1);
2aafe60f2fad Uploaded
pmac
parents:
diff changeset
99 close(INPUT);