Mercurial > repos > labis-app > galaxy_proteomics
annotate t-test.R @ 1:f3fa21cda5f5 draft default tip
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
author | labis-app |
---|---|
date | Fri, 10 Aug 2018 15:45:44 -0400 |
parents | ba070efb6f78 |
children |
rev | line source |
---|---|
0
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
1 #!/usr/bin/env Rscript |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
2 |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
3 # t-test.R |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
4 # AUTHOR: Daniel Travieso |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
5 # E-mail: danielgtravieso@gmail.com |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
6 # LAST REVISED: April 2015 |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
7 # |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
8 # Required packages to work: (getopt", "gtools") |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
9 # Laboratory of Mass Spectrometry at Brazilian Biosciences National Laboratory |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
10 # http://lnbio.cnpem.br/ |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
11 # Copyright CC BY-NC-SA (c) 2014 Brazilian Center for Research in Energy and Materials |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
12 # All rights reserved. |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
13 require('gtools', quietly=TRUE); |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
14 require('getopt', quietly=TRUE); |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
15 #include and execute the read util script |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
16 library('read_util.R'); |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
17 library('write_util.R'); |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
18 |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
19 #define de options input that the read_util$code will have |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
20 opt = matrix(c( |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
21 'inputfile_name', 'i', 1, 'character', |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
22 'type', 't', 1, 'character', |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
23 'outputfile_name', 'o', 1, 'character' |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
24 ),byrow=TRUE, ncol=4); |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
25 |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
26 # parse de input |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
27 options = getopt(opt); |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
28 |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
29 read_util <- read_function(options); |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
30 |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
31 i<-1; |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
32 columns <- list(); |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
33 aux <- c(); |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
34 for (cat in read_util$diff_cat) { |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
35 col <- read_util$col_names[gsub(read_util$regex, "\\1", read_util$col_names) == cat] |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
36 aux <- c(aux, col); |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
37 columns[[i]] <- col; |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
38 i<-i+1; |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
39 } |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
40 # this is a filtered read_util$table to help with calculations |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
41 table_only_columns <- read_util$table[-1, aux] |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
42 |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
43 # this loop computes the ttest result for each row |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
44 # and adds it to a vector |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
45 i <- 2; |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
46 ttestresult <- c(""); |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
47 ttestsignificant <- c(""); |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
48 if (length(read_util$diff_cat) < 2) { |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
49 print(sprintf("Can't calculate t-test. There is only one category for %s collumns", read_util$code)); |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
50 q(1,save="no"); |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
51 } |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
52 |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
53 for (i in seq(2, nrow(table_only_columns)+1)) { |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
54 # the t-test arguments are the control values vector, the treatment values vector |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
55 # and some extra arguments. var.equal says it's a student t-test with stardard |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
56 # deviations assumed equal. mu=0 sets the hipothesis to be null. |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
57 ttestresult[i] <- t.test(table_only_columns[i-1, columns[[1]]], |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
58 table_only_columns[i-1, columns[[2]]], var.equal=TRUE, mu=0)$p.value; |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
59 if (is.na(ttestresult[i])) |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
60 ttestresult[i] = 1.0 |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
61 } |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
62 |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
63 # this defines if the p-value returned for each row is significant |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
64 ttestsignificant[ttestresult <= 0.05] <- "+" |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
65 ttestsignificant[ttestresult > 0.05] <- "" |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
66 |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
67 |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
68 # create two extra rows on the read_util$table, one for p-values and other |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
69 # for siginificance |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
70 #TODO: ou colocar perto da intensidade que se refere ou na 3ยช coluna |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
71 read_util$table[paste0("T.test.result.", read_util$code)] <- NA; |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
72 read_util$table[paste0("T.test.result.", read_util$code)] <- ttestresult; |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
73 read_util$table[paste0("T.test.significant.", read_util$code)] <- NA; |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
74 read_util$table[paste0("T.test.significant.", read_util$code)] <- ttestsignificant; |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
75 |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
76 |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
77 |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
78 |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
79 # write out the read_util$table |
ba070efb6f78
planemo upload commit 13e72e84c523bda22bda792bbebf4720d28542d5-dirty
labis-app
parents:
diff
changeset
|
80 writeout(options$outputfile_name, read_util$table); |