annotate tools/regVariation/t_test_two_samples.pl @ 1:cdcb0ce84a1b

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:15 -0500
parents 9071e359b9a3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 # A program to implement the non-pooled t-test for two samples where the alternative hypothesis is two-sided or one-sided.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2 # The first input file is a TABULAR format file representing the first sample and consisting of one column only.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 # The second input file is a TABULAR format file representing the first sample nd consisting of one column only.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 # The third input is the sidedness of the t-test: either two-sided or, one-sided with m1 less than m2 or,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 # one-sided with m1 greater than m2.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 # The fourth input is the equality status of the standard deviations of both populations
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 # The output file is a TXT file representing the result of the two sample t-test.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 use strict;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 use warnings;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 #variable to handle the motif information
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 my $motif;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 my $motifName = "";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 my $motifNumber = 0;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 my $totalMotifsNumber = 0;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 my @motifNamesArray = ();
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 # check to make sure having correct files
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 my $usage = "usage: non_pooled_t_test_two_samples_galaxy.pl [TABULAR.in] [TABULAR.in] [testSidedness] [standardDeviationEquality] [TXT.out] \n";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 die $usage unless @ARGV == 5;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 #get the input arguments
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 my $firstSampleInputFile = $ARGV[0];
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 my $secondSampleInputFile = $ARGV[1];
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 my $testSidedness = $ARGV[2];
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 my $standardDeviationEquality = $ARGV[3];
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 my $outputFile = $ARGV[4];
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 #open the input files
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 open (INPUT1, "<", $firstSampleInputFile) || die("Could not open file $firstSampleInputFile \n");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 open (INPUT2, "<", $secondSampleInputFile) || die("Could not open file $secondSampleInputFile \n");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 open (OUTPUT, ">", $outputFile) || die("Could not open file $outputFile \n");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 #variables to store the name of the R script file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 my $r_script;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 # R script to implement the two-sample test on the motif frequencies in upstream flanking region
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 #construct an R script file and save it in the same directory where the perl file is located
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 $r_script = "non_pooled_t_test_two_samples.r";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 open(Rcmd,">", $r_script) or die "Cannot open $r_script \n\n";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 print Rcmd "
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 sampleTable1 <- read.table(\"$firstSampleInputFile\", header=FALSE);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 sample1 <- sampleTable1[, 1];
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 sampleTable2 <- read.table(\"$secondSampleInputFile\", header=FALSE);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 sample2 <- sampleTable2[, 1];
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 testSideStatus <- \"$testSidedness\";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 STEqualityStatus <- \"$standardDeviationEquality\";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 #open the output a text file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 sink(file = \"$outputFile\");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 #check if the t-test is two-sided
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 if (testSideStatus == \"two-sided\"){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 #check if the standard deviations are equal in both populations
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 if (STEqualityStatus == \"equal\"){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 #two-sample t-test where standard deviations are assumed to be unequal, the test is two-sided
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 testResult <- t.test(sample1, sample2, var.equal = TRUE);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 } else{
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 #two-sample t-test where standard deviations are assumed to be unequal, the test is two-sided
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 testResult <- t.test(sample1, sample2, var.equal = FALSE);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 } else{ #the t-test is one sided
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 #check if the t-test is two-sided with m1 < m2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 if (testSideStatus == \"one-sided:_m1_less_than_m2\"){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 #check if the standard deviations are equal in both populations
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 if (STEqualityStatus == \"equal\"){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 #two-sample t-test where standard deviations are assumed to be unequal, the test is one-sided: Halt: m1 < m2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 testResult <- t.test(sample1, sample2, var.equal = TRUE, alternative = \"less\");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 } else{
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 #two-sample t-test where standard deviations are assumed to be unequal, the test is one-sided: Halt: m1 < m2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 testResult <- t.test(sample1, sample2, var.equal = FALSE, alternative = \"less\");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 } else{ #the t-test is one-sided with m1 > m2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 #check if the standard deviations are equal in both populations
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 if (STEqualityStatus == \"equal\"){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 #two-sample t-test where standard deviations are assumed to be unequal, the test is one-sided: Halt: m1 < m2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 testResult <- t.test(sample1, sample2, var.equal = TRUE, alternative = \"greater\");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 } else{
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 #two-sample t-test where standard deviations are assumed to be unequal, the test is one-sided: Halt: m1 < m2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 testResult <- t.test(sample1, sample2, var.equal = FALSE, alternative = \"greater\");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 #save the output of the t-test into the output text file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 testResult;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 #close the output text file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 sink();
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 #eof" . "\n";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 close Rcmd;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 system("R --no-restore --no-save --no-readline < $r_script > $r_script.out");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 #close the input and output files
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 close(OUTPUT);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 close(INPUT2);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 close(INPUT1);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109