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

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