comparison src/test-IO/test-IO.R @ 8:e9677425c6c3 default tip

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