Mercurial > repos > george-weingart > maaslin
comparison maaslin-4450aa4ecc84/src/test-Utility/test-Utility.R @ 1:a87d5a5f2776
Uploaded the version running on the prod server
author | george-weingart |
---|---|
date | Sun, 08 Feb 2015 23:08:38 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:e0b5980139d9 | 1:a87d5a5f2776 |
---|---|
1 c_strDir <- file.path(getwd( ),"..") | |
2 | |
3 source(file.path(c_strDir,"lib","Constants.R")) | |
4 source(file.path(c_strDir,"lib","Utility.R")) | |
5 | |
6 context("Test funcRename") | |
7 test_that("Test that unclassified and none otus are represented as 2 terminal clades and others are 1",{ | |
8 expect_equal(funcRename(paste("A","B","C","D",c_strUnclassified, sep=c_cFeatureDelim)),paste("D",c_strUnclassified, sep=c_cFeatureDelim)) | |
9 expect_equal(funcRename(paste("A","B","C","D","101", sep=c_cFeatureDelim)),paste("D","101", sep=c_cFeatureDelim)) | |
10 expect_equal(funcRename(paste("A","B","C","D", sep=c_cFeatureDelim)),paste("D", sep=c_cFeatureDelim)) | |
11 expect_equal(funcRename(paste("A", sep=c_cFeatureDelim)),paste("A", sep=c_cFeatureDelim)) | |
12 expect_equal(funcRename(paste(c_strUnclassified, sep=c_cFeatureDelim)),paste(c_strUnclassified, sep=c_cFeatureDelim)) | |
13 expect_equal(funcRename(paste("101", sep=c_cFeatureDelim)),paste("101", sep=c_cFeatureDelim)) | |
14 }) | |
15 | |
16 context("Test funcColorHelper") | |
17 test_that("Test that min is min and max is max and average is average even if given as NA",{ | |
18 expect_equal(funcColorHelper( dMax = 1, dMin = 1, dMed = NA ), list( dMin = 1, dMax = 1, dMed = 1)) | |
19 expect_equal(funcColorHelper( dMax = -3, dMin = 10, dMed = NA ), list( dMin = -3, dMax = 10, dMed = 3.5)) | |
20 expect_equal(funcColorHelper( dMax = 1, dMin = 11, dMed = NA ), list( dMin = 1, dMax = 11, dMed = 6)) | |
21 expect_equal(funcColorHelper( dMax = 4, dMin = 10, dMed = 5 ), list( dMin = 4, dMax = 10, dMed = 5)) | |
22 expect_equal(funcColorHelper( dMax = 10, dMin = 4, dMed = 5 ), list( dMin = 4, dMax = 10, dMed = 5)) | |
23 }) | |
24 | |
25 context("Test funcTrim") | |
26 test_that("Test that white spaces at the beginning and end of s string are removed",{ | |
27 expect_equal(funcTrim("TRIM"),"TRIM") | |
28 expect_equal(funcTrim(" TRIM"),"TRIM") | |
29 expect_equal(funcTrim(" TRIM"),"TRIM") | |
30 expect_equal(funcTrim(" TRIM "),"TRIM") | |
31 expect_equal(funcTrim("TRIM "),"TRIM") | |
32 expect_equal(funcTrim(" TRIM "),"TRIM") | |
33 expect_equal(funcTrim("TR IM"),"TR IM") | |
34 expect_equal(funcTrim(" TR IM"),"TR IM") | |
35 expect_equal(funcTrim(" TR I M"),"TR I M") | |
36 expect_equal(funcTrim(" TR IM "),"TR IM") | |
37 expect_equal(funcTrim("T R IM "),"T R IM") | |
38 expect_equal(funcTrim(" T RIM "),"T RIM") | |
39 }) | |
40 | |
41 #TODO currently the capture versio of this does not produce a tabbed table (or default table delim) which is not consistent with the rest of the code base. | |
42 context("Test funcWrite") | |
43 #Answer files | |
44 c_sAnswerWriteFile1 = file.path(c_strDir,c_strTestingDirectory,c_strCorrectAnswers,"FuncWriteTemp1.txt") | |
45 c_sAnswerWriteFile2 = file.path(c_strDir,c_strTestingDirectory,c_strCorrectAnswers,"FuncWriteTemp2.txt") | |
46 print("c_sAnswerWriteFile2") | |
47 print(c_sAnswerWriteFile2) | |
48 c_sAnswerWriteDFFile1 = file.path(c_strDir,c_strTestingDirectory,c_strCorrectAnswers,"FuncWriteTempDF1.txt") | |
49 c_sAnswerWriteDFFile2 = file.path(c_strDir,c_strTestingDirectory,c_strCorrectAnswers,"FuncWriteTempDF2.txt") | |
50 | |
51 #Working files | |
52 c_sTempWriteFile1 = file.path(c_strDir,c_strTestingDirectory,c_strTemporaryFiles,"FuncWriteTemp1.txt") | |
53 c_sTempWriteFile2 = file.path(c_strDir,c_strTestingDirectory,c_strTemporaryFiles,"FuncWriteTemp2.txt") | |
54 c_sTempWriteDFFile1 = file.path(c_strDir,c_strTestingDirectory,c_strTemporaryFiles,"FuncWriteTempDF1.txt") | |
55 c_sTempWriteDFFile2 = file.path(c_strDir,c_strTestingDirectory,c_strTemporaryFiles,"FuncWriteTempDF2.txt") | |
56 dfTest = as.data.frame(as.matrix(cbind(c(1,11,111),c(2,22,222),c(3,33,333)))) | |
57 sWriteString = "Testing, 1,2,3 anything but that." | |
58 unlink(c_sTempWriteFile1) | |
59 unlink(c_sTempWriteFile2) | |
60 unlink(c_sTempWriteDFFile1) | |
61 unlink(c_sTempWriteDFFile2) | |
62 funcWrite(sWriteString,c_sTempWriteFile1) | |
63 funcWrite(sWriteString,c_sTempWriteFile2) | |
64 funcWrite(sWriteString,c_sTempWriteFile2) | |
65 funcWrite(dfTest,c_sTempWriteDFFile1) | |
66 funcWrite(dfTest,c_sTempWriteDFFile2) | |
67 funcWrite(dfTest,c_sTempWriteDFFile2) | |
68 | |
69 test_that("Test that a test file is written and appended to for strings and dataframes.",{ | |
70 expect_equal(readLines(c_sTempWriteFile1),readLines(c_sAnswerWriteFile1)) | |
71 expect_equal(readLines(c_sTempWriteFile2),readLines(c_sAnswerWriteFile2)) | |
72 expect_equal(readLines(c_sTempWriteDFFile1),readLines(c_sAnswerWriteDFFile1)) | |
73 expect_equal(readLines(c_sTempWriteDFFile2),readLines(c_sAnswerWriteDFFile2)) | |
74 }) | |
75 | |
76 context("Test funcWriteTable") | |
77 #Answer files | |
78 c_sAnswerWriteDFFile1 = file.path(c_strDir,c_strTestingDirectory,c_strCorrectAnswers,"FuncWriteTableTempDF1.txt") | |
79 c_sAnswerWriteDFFile2 = file.path(c_strDir,c_strTestingDirectory,c_strCorrectAnswers,"FuncWriteTableTempDF2.txt") | |
80 | |
81 #Working files | |
82 c_sTempWriteDFFile1 = file.path(c_strDir,c_strTestingDirectory,c_strTemporaryFiles,"FuncWriteTableTempDF1.txt") | |
83 c_sTempWriteDFFile2 = file.path(c_strDir,c_strTestingDirectory,c_strTemporaryFiles,"FuncWriteTableTempDF2.txt") | |
84 unlink(c_sTempWriteDFFile1) | |
85 unlink(c_sTempWriteDFFile2) | |
86 funcWriteTable(dfTest,c_sTempWriteDFFile1) | |
87 funcWriteTable(dfTest,c_sTempWriteDFFile2, fAppend=TRUE) | |
88 funcWriteTable(dfTest,c_sTempWriteDFFile2, fAppend=TRUE) | |
89 | |
90 test_that("Test that a test file is written and appended to for dataframes.",{ | |
91 expect_equal(readLines(c_sTempWriteDFFile1),readLines(c_sAnswerWriteDFFile1)) | |
92 expect_equal(readLines(c_sTempWriteDFFile2),readLines(c_sAnswerWriteDFFile2)) | |
93 }) | |
94 | |
95 context("Test funcCoef2Col") | |
96 dfTestWithFactors = as.data.frame(as.matrix(cbind(c(1,11,111),c(2,22,222),c(3,33,333)))) | |
97 colnames(dfTestWithFactors)=c("A","B","C") | |
98 dfTestWithFactors["B"]=as.factor(as.character(dfTestWithFactors[["B"]])) | |
99 test_that("Test that a coefficients are found or not given if they exist",{ | |
100 expect_equal(funcCoef2Col(strCoef="C",frmeData=dfTestWithFactors,astrCols=c()),"C") | |
101 expect_equal(funcCoef2Col(strCoef="A",frmeData=dfTestWithFactors,astrCols=c()),"A") | |
102 expect_equal(funcCoef2Col(strCoef=paste("B","2",sep=c_sFactorNameSep),frmeData=dfTestWithFactors,astrCols=c()),"B") | |
103 expect_equal(funcCoef2Col(strCoef=paste("B","22",sep=c_sFactorNameSep),frmeData=dfTestWithFactors,astrCols=c()),"B") | |
104 expect_equal(funcCoef2Col(strCoef=paste("B","222",sep=c_sFactorNameSep),frmeData=dfTestWithFactors,astrCols=c()),"B") | |
105 expect_equal(funcCoef2Col(strCoef="C",frmeData=dfTestWithFactors,astrCols=c("A","B","C")),"C") | |
106 expect_equal(funcCoef2Col(strCoef=paste("B","2",sep=c_sFactorNameSep),frmeData=dfTestWithFactors,astrCols=c("A","B")),"B") | |
107 expect_equal(funcCoef2Col(strCoef=paste("B","22",sep=c_sFactorNameSep),frmeData=dfTestWithFactors,astrCols=c("B","C")),"B") | |
108 expect_equal(funcCoef2Col(strCoef=paste("B","222",sep=c_sFactorNameSep),frmeData=dfTestWithFactors,astrCols=c("B")),"B") | |
109 }) | |
110 | |
111 context("Test funcMFAValue2Col") | |
112 dfTestWithFactors = data.frame(A=c(1,3,3,4,5,6,7,8),B=c(1.0,2.0, 5.8,4.6,4.7,8.9,9.0,2.0),C=c("one","two","one","two","one","two","one","two"),D=c("1","2","1","2","1","2","1","2")) | |
113 dfTestWithFactors["three"]=as.factor(dfTestWithFactors[["three"]]) | |
114 test_that("Test that a column names is found or not given if they exist",{ | |
115 expect_equal(funcMFAValue2Col(xValue=5.8,dfData=dfTestWithFactors, aiColumnIndicesToSearch=NULL),"B") | |
116 expect_equal(funcMFAValue2Col(xValue=6,dfData=dfTestWithFactors, aiColumnIndicesToSearch=NULL),"A") | |
117 expect_equal(funcMFAValue2Col(xValue="one",dfData=dfTestWithFactors, aiColumnIndicesToSearch=NULL),"C") | |
118 expect_equal(funcMFAValue2Col(xValue="two",dfData=dfTestWithFactors, aiColumnIndicesToSearch=NULL),"C") | |
119 expect_equal(funcMFAValue2Col(xValue=paste("D","1",sep=c_sMFANameSep1),dfData=dfTestWithFactors, aiColumnIndicesToSearch=NULL),"D") | |
120 expect_equal(funcMFAValue2Col(xValue=paste("D","2",sep=c_sMFANameSep1),dfData=dfTestWithFactors, aiColumnIndicesToSearch=NULL),"D") | |
121 expect_equal(funcMFAValue2Col(xValue=2.0,dfData=dfTestWithFactors, aiColumnIndicesToSearch=c(1,3)),NULL) | |
122 expect_equal(funcMFAValue2Col(xValue=6,dfData=dfTestWithFactors, aiColumnIndicesToSearch=c(2,3)),NULL) | |
123 expect_equal(funcMFAValue2Col(xValue="one",dfData=dfTestWithFactors, aiColumnIndicesToSearch=c(1,2)),NULL) | |
124 expect_equal(funcMFAValue2Col(xValue="two",dfData=dfTestWithFactors, aiColumnIndicesToSearch=c(1,2)),NULL) | |
125 expect_equal(funcMFAValue2Col(xValue=2.0,dfData=dfTestWithFactors, aiColumnIndicesToSearch=c(2)),"B") | |
126 expect_equal(funcMFAValue2Col(xValue=6,dfData=dfTestWithFactors, aiColumnIndicesToSearch=c(1)),"A") | |
127 expect_equal(funcMFAValue2Col(xValue="one",dfData=dfTestWithFactors, aiColumnIndicesToSearch=c(3)),"C") | |
128 expect_equal(funcMFAValue2Col(xValue=paste("D","2",sep=c_sMFANameSep1),dfData=dfTestWithFactors, aiColumnIndicesToSearch=c(4)),"D") | |
129 }) | |
130 | |
131 context("Test funcFormulaStrToList") | |
132 test_that("List of covariates are given, from lm or mixed model formulas",{ | |
133 expect_equal(funcFormulaStrToList("adCur ~ `1Covariate`"),c("1Covariate")) | |
134 expect_equal(funcFormulaStrToList("adCur ~ `1Covariate` + `2Covariate`"),c("1Covariate","2Covariate")) | |
135 expect_equal(funcFormulaStrToList("adCur ~ `1Covariate` + `2Covariate` + `3Covariate`"),c("1Covariate","2Covariate","3Covariate")) | |
136 expect_equal(funcFormulaStrToList("adCur ~ 1|`1Covariate`"),c("1Covariate")) | |
137 expect_equal(funcFormulaStrToList("adCur ~ 1|`1Covariate` + 1|`2Covariate`"),c("1Covariate","2Covariate")) | |
138 expect_equal(funcFormulaStrToList("adCur ~ 1|`1Covariate` + 1|`2Covariate` + 1|`3Covariate`"),c("1Covariate","2Covariate","3Covariate")) | |
139 expect_equal(funcFormulaStrToList("adCur ~ 1|`1Covariate` + `2Covariate` + 1|`3Covariate`"),c("1Covariate","2Covariate","3Covariate")) | |
140 expect_equal(funcFormulaStrToList("adCur ~ 1|`1Covariate` + 1|`2Covariate` + `3Covariate`"),c("1Covariate","2Covariate","3Covariate")) | |
141 expect_equal(funcFormulaStrToList("adCur ~ `1Covariate` + 1|`2Covariate` + 1|`3Covariate`"),c("1Covariate","2Covariate","3Covariate")) | |
142 expect_equal(funcFormulaStrToList("adCur ~ `1Covariate` + `2Covariate` + 1|`3Covariate`"),c("1Covariate","2Covariate","3Covariate")) | |
143 expect_equal(funcFormulaStrToList("adCur ~ 1|`1Covariate` + `2Covariate` + `3Covariate`"),c("1Covariate","2Covariate","3Covariate")) | |
144 expect_equal(funcFormulaStrToList("adCur ~ `1Covariate` + 1|`2Covariate` + `3Covariate`"),c("1Covariate","2Covariate","3Covariate")) | |
145 }) | |
146 | |
147 context("Test funcFormulaListToString") | |
148 test_that("The correct string formula for a lm or mixed model is created from a list of covariates",{ | |
149 expect_equal(funcFormulaListToString(NULL),c(NA,NA)) | |
150 expect_equal(funcFormulaListToString(c("1Covariate")),c("adCur ~ `1Covariate`",NA)) | |
151 expect_equal(funcFormulaListToString(c("1Covariate","2Covariate")),c("adCur ~ `1Covariate` + `2Covariate`",NA)) | |
152 expect_equal(funcFormulaListToString(c("1Covariate","2Covariate","3Covariate")),c("adCur ~ `1Covariate` + `2Covariate` + `3Covariate`",NA)) | |
153 expect_equal(funcFormulaListToString(c("1Covariate","2Covariate"),c("3Covariate")),c(NA,"adCur ~ `1Covariate` + `2Covariate` + 1|`3Covariate`")) | |
154 expect_equal(funcFormulaListToString(c("1Covariate","3Covariate"),c("2Covariate")),c(NA,"adCur ~ `1Covariate` + `3Covariate` + 1|`2Covariate`")) | |
155 expect_equal(funcFormulaListToString(c("2Covariate","3Covariate"),c("1Covariate")),c(NA,"adCur ~ `2Covariate` + `3Covariate` + 1|`1Covariate`")) | |
156 expect_equal(funcFormulaListToString(c("2Covariate"),c("1Covariate","3Covariate")),c(NA,"adCur ~ `2Covariate` + 1|`1Covariate` + 1|`3Covariate`")) | |
157 expect_equal(funcFormulaListToString(c("1Covariate"),c("2Covariate","3Covariate")),c(NA,"adCur ~ `1Covariate` + 1|`2Covariate` + 1|`3Covariate`")) | |
158 expect_equal(funcFormulaListToString(c("3Covariate"),c("1Covariate","2Covariate")),c(NA,"adCur ~ `3Covariate` + 1|`1Covariate` + 1|`2Covariate`")) | |
159 }) |