annotate src/test-IO/test-IO.R @ 0:e0b5980139d9

maaslin
author george-weingart
date Tue, 13 May 2014 22:00:40 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
1 c_strDir <- file.path(getwd( ),"..")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
2
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
3 source(file.path(c_strDir,"lib","Constants.R"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
4 source(file.path(c_strDir,"lib","ValidateData.R"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
5 strTestingDirectory = file.path(c_strDir,c_strTestingDirectory)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
6
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
7 expect_equal(funcParseIndexSlices("1",cNames),c(1))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
8
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
9 cNames = c("One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven",
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
10 "Twelve","Thirteen","Fourteen","Fifteen")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
11
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
12 test_that("Just Numerics are parsed",{
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
13 expect_equal(funcParseIndexSlices("1",cNames),c(1))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
14 expect_equal(funcParseIndexSlices("8,10",cNames),c(8,10))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
15 expect_equal(funcParseIndexSlices("2-6",cNames), c(2,3,4,5,6))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
16 expect_equal(funcParseIndexSlices("3,7,10-12",cNames), c(3,7,10,11,12))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
17 })
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
18
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
19 test_that("Missing numbers are parsed",{
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
20 expect_equal(funcParseIndexSlices("-",cNames), c(2:15))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
21 expect_equal(funcParseIndexSlices("-4",cNames), c(2,3,4))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
22 expect_equal(funcParseIndexSlices("3-",cNames), c(3:15))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
23 })
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
24
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
25 test_that("Words are parsed correctly",{
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
26 expect_equal(funcParseIndexSlices("One",cNames), c(1))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
27 expect_equal(funcParseIndexSlices("Eight,Ten",cNames), c(8,10))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
28 expect_equal(funcParseIndexSlices("Two-Six",cNames), c(2,3,4,5,6))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
29 expect_equal(funcParseIndexSlices("Three,Seven,Ten-Twelve",cNames), c(3,7,10,11,12))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
30 })
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
31
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
32 test_that("Missing words are parsed",{
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
33 expect_equal(funcParseIndexSlices("-Four",cNames), c(2:4))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
34 expect_equal(funcParseIndexSlices("Three-",cNames), c(3:15))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
35 })
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
36
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
37 test_that("Words and numbers are parsed correctly",{
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
38 expect_equal(funcParseIndexSlices("Eight,10",cNames), c(8,10))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
39 expect_equal(funcParseIndexSlices("2-Six",cNames), c(2,3,4,5,6))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
40 expect_equal(funcParseIndexSlices("Three,7,10-Twelve",cNames), c(3,7,10,11,12))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
41 })
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
42
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
43
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
44 context("Test funcWriteMatrixToReadConfigFile")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
45 # File to temporarily write to
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
46 strWriteMatrixRCTestFile = file.path(strTestingDirectory,c_strTemporaryFiles,"FuncWriteMatrixToReadConfigFileTemp.read.config")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
47 # Files that hold answers
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
48 strFileSimpleRCFileAnswer = file.path(strTestingDirectory,c_strCorrectAnswers,"FuncWriteMatrixToReadConfigFile_SimpleAnswer.read.config")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
49 strFileUseAllRCFileAnswer = file.path(strTestingDirectory,c_strCorrectAnswers,"FuncWriteMatrixToReadConfigFile_AllAnswer.read.config")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
50 strFileAppendTrueRCFileAnswer = file.path(strTestingDirectory,c_strCorrectAnswers,"FuncWriteMatrixToReadConfigFile_AppendAnswer.read.config")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
51 #Input matrix file
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
52 strFileMatrix = file.path(strTestingDirectory,c_strTestingInput,"TestMatrix.tsv")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
53
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
54 #Get read config files in different scenarios
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
55 funcWriteMatrixToReadConfigFile(strWriteMatrixRCTestFile,"SimpleMatrix")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
56 strSimpleInterface = readLines(strWriteMatrixRCTestFile)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
57 funcWriteMatrixToReadConfigFile(strWriteMatrixRCTestFile,"AllMatrix",strRowIndices="1,2,3,4,5", strColIndices="10,11,12",acharDelimiter=" ")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
58 strUseAllParametersInterface = readLines(strWriteMatrixRCTestFile)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
59 funcWriteMatrixToReadConfigFile(strWriteMatrixRCTestFile,"SimpleMatrix")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
60 funcWriteMatrixToReadConfigFile(strWriteMatrixRCTestFile,"SimpleMatrix")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
61 strAppendFalseInterface = readLines(strWriteMatrixRCTestFile)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
62 funcWriteMatrixToReadConfigFile(strWriteMatrixRCTestFile,"SimpleMatrix")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
63 funcWriteMatrixToReadConfigFile(strWriteMatrixRCTestFile,"SimpleMatrix",fAppend=TRUE)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
64 strAppendTrueInterface = readLines(strWriteMatrixRCTestFile)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
65
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
66 test_that("Correct config file is written",{
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
67 expect_equal(strSimpleInterface,readLines(strFileSimpleRCFileAnswer))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
68 expect_equal(strUseAllParametersInterface,readLines(strFileUseAllRCFileAnswer))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
69 expect_equal(strAppendFalseInterface,readLines(strFileSimpleRCFileAnswer))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
70 expect_equal(strAppendTrueInterface,readLines(strFileAppendTrueRCFileAnswer))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
71 })
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
72
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
73 context("Test funcReadConfigFile")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
74 lsSimpleRC = funcReadConfigFile(strFileSimpleRCFileAnswer,strFileMatrix)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
75 lsAllRC = funcReadConfigFile(strFileUseAllRCFileAnswer,strFileMatrix)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
76
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
77 lsSimpleListAnswer = list()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
78 lsSimpleListAnswer[[1]]=c("SimpleMatrix",strFileMatrix,"\t","-","-")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
79 lsAllListAnswer = list()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
80 lsAllListAnswer[[1]]=c("AllMatrix",strFileMatrix," ","1,2,3,4,5","10,11,12")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
81
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
82 test_that("Test readConfigFile reads in files correctly.",{
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
83 expect_equal(lsSimpleRC,lsSimpleListAnswer)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
84 expect_equal(lsAllRC,lsAllListAnswer)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
85 })
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
86
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
87
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
88 context("Test funcReadMatrix")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
89
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
90 #Read in config files
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
91 dfSimpleRead = funcReadMatrix("SimpleMatrix",strFileMatrix,"\t","2,4,5","7,3,5")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
92 dfUseAllParametersRead = funcReadMatrix("AllMatrix",strFileMatrix,"\t","2,3,4","6,2,4")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
93
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
94 dfSimpleReadCorrect = as.data.frame(as.matrix(rbind(c(21,23,24),c(41,43,44),c(61,63,64))))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
95 rownames(dfSimpleReadCorrect) = c("Feature2", "Feature4", "Feature6")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
96 colnames(dfSimpleReadCorrect) = c("Sample1", "Sample3", "Sample4")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
97
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
98 dfUseAllReadCorrect = as.data.frame(as.matrix(rbind(c(11,12,13),c(31,32,33),c(51,52,53))))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
99 rownames(dfUseAllReadCorrect) = c("Feature1", "Feature3", "Feature5")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
100 colnames(dfUseAllReadCorrect) = c("Sample1", "Sample2", "Sample3")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
101
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
102 test_that("Matrix file is read correctly.",{
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
103 expect_equal(dfSimpleRead,dfSimpleReadCorrect)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
104 expect_equal(dfUseAllParametersRead,dfUseAllReadCorrect)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
105 })
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
106
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
107 context("Test funcReadMatrices")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
108
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
109 sConfigureFile1Matrix = file.path(strTestingDirectory,c_strTestingInput,"1Matrix.read.config")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
110 mtxOne = as.data.frame(as.matrix(rbind(c(11,12,13,14,15),c(21,22,23,24,25),c(31,32,33,34,35),c(41,42,43,44,45),
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
111 c(51,52,53,54,55),c(61,62,63,64,65),c(71,72,73,74,75),c(81,82,83,84,85),
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
112 c(91,92,93,94,95),c(101,102,103,104,105),c(111,112,113,114,115),c(121,122,123,124,125),
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
113 c(131,132,133,134,135),c(141,142,143,144,145),c(151,152,153,154,155))))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
114 rownames(mtxOne) = c("Feature1","Feature2","Feature3","Feature4","Feature5","Feature6","Feature7","Feature8","Feature9","Feature10",
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
115 "Feature11","Feature12","Feature13","Feature14","Feature15")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
116 colnames(mtxOne) = c("Sample1","Sample2","Sample3","Sample4","Sample5")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
117 sConfigureFile2Matrix = file.path(strTestingDirectory,c_strTestingInput,"2Matrix.read.config")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
118 mtxTwo = as.data.frame(as.matrix(rbind(c(11,12,13),c(21,22,23),c(31,32,33))))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
119 rownames(mtxTwo) = c("Feature1","Feature2","Feature3")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
120 colnames(mtxTwo) = c("Sample1","Sample2","Sample3")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
121
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
122 sConfigureFile3Matrix = file.path(strTestingDirectory,c_strTestingInput,"3Matrix.read.config")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
123 mtxThree = as.data.frame(as.matrix(rbind(c(11,12,14),c(21,22,24),c(31,32,34),c(41,42,44),
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
124 c(51,52,54),c(61,62,64),c(71,72,74),c(81,82,84),c(91,92,94))))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
125 rownames(mtxThree) = c("Feature1","Feature2","Feature3","Feature4","Feature5","Feature6","Feature7","Feature8","Feature9")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
126 colnames(mtxThree) = c("Sample1","Sample2","Sample4")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
127
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
128 #Read one matrix
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
129 ldfRet1 = funcReadMatrices(configureFile=sConfigureFile1Matrix,strFileMatrix)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
130 ldfRet1Answer = list( "Matrix1" = mtxOne)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
131
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
132 #Read two matrices
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
133 ldfRet2 = funcReadMatrices(configureFile=sConfigureFile2Matrix,strFileMatrix)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
134 ldfRet2Answer = list( "Matrix1" = mtxOne,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
135 "Matrix2" = mtxTwo)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
136
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
137 #Read three matrices from two different files
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
138 ldfRet3 = funcReadMatrices(configureFile=sConfigureFile3Matrix,strFileMatrix)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
139 ldfRet3Answer = list( "Matrix1" = mtxOne,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
140 "Matrix2" = mtxTwo,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
141 "Matrix3" = mtxThree)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
142
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
143 test_that("Test funcReadMatrices read in the correct matrices not matter the number or source",{
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
144 expect_equal(ldfRet1,ldfRet1Answer)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
145 expect_equal(ldfRet2,ldfRet2Answer)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
146 expect_equal(ldfRet3,ldfRet3Answer)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
147 })
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
148
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
149 context("Test funcWriteMatrices")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
150 strFuncWriteMatricesMatrix1 = file.path(strTestingDirectory,c_strTemporaryFiles,"FuncWriteMatrices1.tsv")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
151 strFuncWriteMatricesMatrix2 = file.path(strTestingDirectory,c_strTemporaryFiles,"FuncWriteMatrices2.tsv")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
152 strFuncWriteMatricesMatrix1Answer = file.path(strTestingDirectory, c_strCorrectAnswers,"FuncWriteMatrices1.tsv")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
153 strFuncWriteMatricesMatrix2Answer = file.path(strTestingDirectory, c_strCorrectAnswers,"FuncWriteMatrices2.tsv")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
154 strFuncWriteMatricesRCFile = file.path(strTestingDirectory,c_strTemporaryFiles,"FuncWriteMatrices.read.config")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
155 strFuncWriteMatricesRCFileAnswer = file.path(strTestingDirectory, c_strCorrectAnswers,"FuncWriteMatrices.read.config")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
156 funcWriteMatrices(list("1"=mtxOne, "2"=mtxThree),c(strFuncWriteMatricesMatrix1, strFuncWriteMatricesMatrix2), strFuncWriteMatricesRCFile)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
157
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
158 test_that("Test that writing to a file occurs correctly, for both matrix and configure file.",{
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
159 expect_equal(readLines(strFuncWriteMatricesMatrix1Answer),readLines(strFuncWriteMatricesMatrix1))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
160 expect_equal(readLines(strFuncWriteMatricesMatrix2Answer),readLines(strFuncWriteMatricesMatrix2))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
161 expect_equal(readLines(strFuncWriteMatricesRCFileAnswer),readLines(strFuncWriteMatricesRCFile))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
162 })