annotate src/lib/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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
1 #####################################################################################
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
2 #Copyright (C) <2012>
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
3 #
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
4 #Permission is hereby granted, free of charge, to any person obtaining a copy of
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
5 #this software and associated documentation files (the "Software"), to deal in the
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
6 #Software without restriction, including without limitation the rights to use, copy,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
7 #modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
8 #and to permit persons to whom the Software is furnished to do so, subject to
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
9 #the following conditions:
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
10 #
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
11 #The above copyright notice and this permission notice shall be included in all copies
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
12 #or substantial portions of the Software.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
13 #
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
14 #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
15 #INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
16 #PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
17 #HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
18 #OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
19 #SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
20 #
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
21 # This file is a component of the MaAsLin (Multivariate Associations Using Linear Models),
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
22 # authored by the Huttenhower lab at the Harvard School of Public Health
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
23 # (contact Timothy Tickle, ttickle@hsph.harvard.edu).
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
24 #####################################################################################
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
25
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
26 inlinedocs <- function(
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
27 ##author<< Curtis Huttenhower <chuttenh@hsph.harvard.edu> and Timothy Tickle <ttickle@hsph.harvard.edu>
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
28 ##description<< Collection of functions centered on custom reading of data and some IO services.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
29 ) { return( pArgs ) }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
30
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
31 #Project Constants
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
32
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
33 c_astrNA <- c(""," "," ","NA","na")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
34
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
35 #Do not report warnings
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
36 options(warn=-1)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
37
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
38 funcWriteMatrixToReadConfigFile = function(
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
39 ### Writes a read config file. Will write over a file by default
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
40 strConfigureFileName,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
41 ### Matrix that will be read
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
42 strMatrixName,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
43 ### Name of matrix that will be read
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
44 strRowIndices=NA,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
45 ### Rows which will be Read (TSV) by default all will be read
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
46 strColIndices=NA,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
47 ### Cols which will be Read (TSV) by default all will be read
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
48 acharDelimiter=c_strDefaultMatrixDelimiter,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
49 ### Delimiter for the matrix that will be read in\
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
50 fAppend=FALSE
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
51 ### Append to a current read config file
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
52 ){
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
53 #If no append delete previous file
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
54 if(!fAppend){unlink(strConfigureFileName)}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
55
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
56 #Make delimiter readable
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
57 switch(acharDelimiter,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
58 "\t" = {acharDelimiter = "TAB"},
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
59 " " = {acharDelimiter = "SPACE"},
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
60 "\r" = {acharDelimiter = "RETURN"},
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
61 "\n" = {acharDelimiter = "ENDLINE"})
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
62
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
63 #Manage NAs
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
64 if(is.na(strRowIndices)){strRowIndices="-"}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
65 if(is.na(strColIndices)){strColIndices="-"}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
66
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
67 #Required output
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
68 lsDataLines = c(paste(c_MATRIX_NAME,strMatrixName,sep=" "),
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
69 paste(c_DELIMITER,acharDelimiter,sep=" "),
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
70 paste(c_ID_ROW,"1",sep=" "),
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
71 paste(c_ID_COLUMN,"1",sep=" "),
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
72 paste(c_TSVROWS,strRowIndices,sep=" "),
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
73 paste(c_TSVCOLUMNS,strColIndices,sep=" "))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
74
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
75 lsDataLines = c(lsDataLines,"\n")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
76
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
77 #Output to file
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
78 lapply(lsDataLines, cat, file=strConfigureFileName, sep="\n", append=TRUE)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
79 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
80
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
81 funcWriteMatrices = function(
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
82 ### Write data frame data files with config files
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
83 dataFrameList,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
84 ### A named list of data frames (what you get directly from the read function)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
85 saveFileList,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
86 ### File names to save the data matrices in (one name per data frame)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
87 configureFileName,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
88 ### Name of the configure file to be written which will direct the reading of these data
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
89 acharDelimiter=c_strDefaultMatrixDelimiter,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
90 ### Matrix delimiter
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
91 log = FALSE
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
92 ### Indicates if logging should occur
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
93 ){
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
94 #Get names
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
95 dataFrameNames = names(dataFrameList)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
96
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
97 #Get length of dataFrameList
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
98 dataFrameListLength = length(dataFrameList)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
99
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
100 #Get length of save file list
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
101 saveFileListLength = length(saveFileList)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
102
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
103 #If the save file list length and data frame list length are not equal, abort
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
104 if(!saveFileListLength == dataFrameListLength)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
105 {stop(paste("Received a length of save files (",saveFileListLength,") that are different from the count of data frames (",dataFrameListLength,"). Stopped and returned false."),sep="")}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
106
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
107 #Delete the old config file
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
108 unlink(configureFileName)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
109
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
110 #For each data save
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
111 for (dataIndex in c(1:dataFrameListLength))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
112 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
113 #Current data frame
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
114 data = dataFrameList[[dataIndex]]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
115
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
116 #Get column count
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
117 columnCount = ncol(data)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
118
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
119 #Get row and column names
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
120 rowNames = row.names(data)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
121 rowNamesString = paste(rowNames,sep="",collapse=",")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
122 if(length(rowNamesString)==0){rowNamesString = NA}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
123
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
124 columnNamesString = paste(colnames(data),sep="",collapse=",")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
125 if(length(columnNamesString)==0){columnNamesString = NA}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
126
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
127 #Get row indices
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
128 rowStart = 1
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
129 if(!is.na(rowNamesString)){rowStart = 2}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
130 rowEnd = nrow(data)+rowStart - 1
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
131 rowIndices = paste(c(rowStart:rowEnd),sep="",collapse=",")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
132
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
133 #Get col indices
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
134 colStart = 1
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
135 if(!is.na(columnNamesString)){ colStart = 2}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
136 colEnd = columnCount+colStart - 1
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
137 colIndices = paste(c(colStart:colEnd),sep="",collapse=",")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
138
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
139 #Write Data to file
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
140 write.table(data, saveFileList[dataIndex], quote = FALSE, sep = acharDelimiter, col.names = NA, row.names = rowNames, na = "NA", append = FALSE)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
141
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
142 #Write the read config file
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
143 funcWriteMatrixToReadConfigFile(strConfigureFileName=configureFileName, strMatrixName=dataFrameNames[dataIndex],
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
144 strRowIndices=rowIndices, strColIndices=colIndices, acharDelimiter=acharDelimiter, fAppend=TRUE)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
145 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
146 return(TRUE)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
147 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
148
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
149 funcReadMatrices = function(
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
150 ### Dynamically Read a Matrix/Matrices from a configure file
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
151 configureFile,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
152 ### Read config file to guide reading in data
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
153 defaultFile = NA,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
154 ### Default data file to read
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
155 log = FALSE
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
156 ){
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
157 #Named vector to return data frames read
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
158 returnFrames = list()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
159 #Holds the names of the frames as they are being added
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
160 returnFrameNames = c()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
161 returnFramesIndex = 1
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
162
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
163 #Read in config file info
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
164 #Read each data block extracted from the config file
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
165 lsDataBlocks <- funcReadConfigFile(configureFile, defaultFile)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
166 if(!length(lsDataBlocks)) {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
167 astrMetadata <- NULL
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
168 astrMetadata[2] <- defaultFile
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
169 astrMetadata[5] <- "2"
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
170 astrData <- NULL
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
171 astrData[2] <- defaultFile
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
172 astrData[5] <- "3-"
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
173 lsDataBlocks <- list(astrMetadata, astrData)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
174 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
175 for(dataBlock in lsDataBlocks)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
176 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
177 #Read in matrix
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
178 returnFrames[[returnFramesIndex]] = funcReadMatrix(tempMatrixName=dataBlock[1], tempFileName=dataBlock[2], tempDelimiter=dataBlock[3], tempColumns=dataBlock[5], tempRows=dataBlock[4], tempLog=log)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
179 returnFrameNames = c(returnFrameNames,dataBlock[1])
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
180 returnFramesIndex = returnFramesIndex + 1
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
181 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
182 names(returnFrames) = returnFrameNames
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
183 return(returnFrames)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
184 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
185
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
186 funcReadMatrix = function(
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
187 ### Read one matrix
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
188 ### The name to give the block of data read in from file
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
189 tempMatrixName,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
190 ### ID rows and columns are assumed to be 1
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
191 tempFileName=NA,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
192 ### Data file to read
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
193 tempDelimiter=NA,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
194 ### Data matrix delimiter
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
195 tempColumns=NA,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
196 ### Data columns to read
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
197 tempRows=NA,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
198 ### Data rows to read
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
199 tempLog=FALSE
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
200 ### Indicator to log
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
201 ){
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
202 if(is.na(tempDelimiter)){tempDelimiter <- c_strDefaultMatrixDelimiter}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
203 if(is.na(tempColumns)){tempColumns <- c_strDefaultReadCols}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
204 if(is.na(tempRows)){tempRows <- c_strDefaultReadRows}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
205
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
206 #Check parameter and make sure not NA
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
207 if(is.na(tempMatrixName)){tempMatrixName <- ""}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
208 if(!funcIsValid(tempMatrixName)){stop(paste("Did not receive a valid matrix name, received ",tempMatrixName,"."))}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
209
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
210 #Check to make sure there is a file name for the matrix
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
211 if(! funcIsValidFileName(tempFileName))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
212 {stop(paste("No valid file name is given for the matrix ",tempMatrixName," from file: ",tempFileName,". Please add a valid file name to read the matrix from.", sep=""))}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
213
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
214 #Read in superset matrix and give names if indicated
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
215 #Read in matrix
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
216 dataMatrix = read.table(tempFileName, sep = tempDelimiter, as.is = TRUE, na.strings=c_astrNA, quote = "", comment.char = "")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
217 dataFrameDimension = dim(dataMatrix)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
218
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
219 #Get column names
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
220 columnNameList = as.matrix(dataMatrix[1,])
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
221 rowNameList = dataMatrix[1][[1]]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
222
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
223 #Convert characters to vectors of indices
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
224 tempColumns = funcParseIndexSlices(ifelse(is.na(tempColumns),"-",tempColumns), columnNameList)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
225 tempRows = funcParseIndexSlices(ifelse(is.na(tempRows),"-", tempRows), rowNameList)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
226
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
227 #Check indices
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
228 #Check to make sure valid id col/rows and data col/rows
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
229 if((!funcIsValid(tempColumns)) || (!funcIsValid(tempRows)))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
230 {stop(paste("Received invalid row or col. Rows=",tempRows," Cols=", tempColumns))}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
231
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
232 #Check to make sure only 1 row id is given and it is not repeated in the data rows
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
233 if(length(intersect(1,tempColumns)) == 1)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
234 {stop(paste("Index indicated as an id row but was found in the data row indices, can not be both. Index=1 Data indices=",tempColumns,sep=""))}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
235
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
236 #Check to make sure only one col id is given and it is not repeated in the data columns
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
237 #Id row/col should not be in data row/col
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
238 if(length(intersect(1, tempRows)) == 1)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
239 {stop(paste("Index indicated as an id column but was found in the data column indices, can not be both. ID Index=1 Data Indices=", tempRows,".",sep=""))}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
240
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
241 #If the row names have the same length as the column count and has column names
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
242 #it is assumed that the tempIdCol index item is associated with the column names.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
243 #Visa versa for rows, either way it is removed
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
244 #Remove ids from name vector
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
245 rowNameList = rowNameList[(-1)]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
246 #Remove ids from data
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
247 dataMatrix = dataMatrix[(-1)]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
248 #Adjust row ids given the removal of the id row
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
249 tempColumns=(tempColumns-1)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
250
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
251 ## Remove id rows/columns and set row/col names
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
252 #Remove ids from vector
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
253 columnNameList = columnNameList[(-1)]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
254 #Remove ids from data
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
255 dataMatrix = dataMatrix[(-1),]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
256 #Adjust column ids given the removal of the id column
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
257 tempRows =(tempRows-1)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
258 #Add row and column names
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
259 row.names(dataMatrix) = as.character(rowNameList)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
260 colnames(dataMatrix) = as.character(columnNameList)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
261
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
262 #Reduce matrix
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
263 #Account for when both column ranges and row ranges are given or just a column or a row range is given
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
264 dataMatrix = dataMatrix[tempRows, tempColumns, drop=FALSE]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
265
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
266 #Set all columns data types to R guessed default
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
267 for(i in 1:ncol(dataMatrix)){
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
268 dataMatrix[,i] <- type.convert(dataMatrix[,i], na.strings = c_astrNA)}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
269
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
270 #Return matrix
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
271 return(dataMatrix)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
272 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
273
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
274 funcReadConfigFile = function(
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
275 ### Reads in configure file and extracts the pieces needed for reading in a matrix
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
276 configureFile,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
277 ### Configure file = string path to configure file
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
278 defaultFile = NA
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
279 ### Used to set a default data file
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
280 ){
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
281 #Read configure file
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
282 fileDataList <- list()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
283 if(!is.null( configureFile ) ) {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
284 fileDataList <- scan( file = configureFile, what = character(), sep="\n", quiet=TRUE) }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
285 newList = list()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
286 for(sLine in fileDataList)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
287 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
288 sLine = gsub("\\s","",sLine)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
289 vUnits = unlist(strsplit(sLine,":"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
290 if(length(vUnits)>1)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
291 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
292 vUnits[1] = paste(vUnits[1],":",sep="")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
293 newList[[length(newList)+1]] = vUnits
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
294 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
295 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
296 fileDataList = unlist(newList)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
297
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
298 matrixName <- NA
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
299 fileName <- defaultFile
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
300
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
301 #Hold information on matrices to be read
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
302 matrixInformationList = list()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
303 matrixInformationListCount = 1
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
304
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
305 for(textIndex in c(1:length(fileDataList)))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
306 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
307 if(textIndex > length(fileDataList)) {break}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
308 #Start at the Matrix name
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
309 #Keep this if statement first so that you scan through until you find a matrix block
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
310 if(fileDataList[textIndex] == c_MATRIX_NAME)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
311 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
312 #If the file name is not NA then that is sufficient for a matrix, store
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
313 #Either way reset
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
314 if(funcIsValid(fileName)&&funcIsValid(matrixName))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
315 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
316 matrixInformationList[[matrixInformationListCount]] = c(matrixName,fileName,delimiter,rows,columns)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
317 matrixInformationListCount = matrixInformationListCount + 1
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
318 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
319
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
320 #Get the matrix name and store
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
321 matrixName = fileDataList[textIndex + 1]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
322
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
323 fileName = defaultFile
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
324 delimiter = "\t"
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
325 rows = NA
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
326 columns = NA
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
327 #If is not matrix name and no matrix name is known skip until you find the matrix name
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
328 #If matrix name is known, continue to collect information about that matrix
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
329 } else if(is.na(matrixName)){next}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
330
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
331 #Parse different keywords
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
332 strParseKey = fileDataList[textIndex]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
333 if(strParseKey == c_FILE_NAME){fileName=fileDataList[textIndex+1]}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
334 else if(strParseKey==c_FILE_NAME){fileName=fileDataList[textIndex+1]}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
335 else if(strParseKey %in% c(c_TSVROWS,c_PCLCOLUMNS,c_ROWS)){rows=fileDataList[textIndex+1]}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
336 else if(strParseKey %in% c(c_TSVCOLUMNS,c_PCLROWS,c_COLUMNS)){columns=fileDataList[textIndex+1]}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
337 else if(strParseKey==c_DELIMITER)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
338 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
339 switch(fileDataList[textIndex + 1],
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
340 "TAB" = {delimiter = "\t"},
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
341 "SPACE" = {delimiter = " "},
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
342 "RETURN" = {delimiter = "\r"},
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
343 "ENDLINE" = {delimiter = "\n"})
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
344 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
345 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
346 #If there is matrix information left
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
347 if((!is.na(matrixName)) && (!is.na(fileName)))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
348 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
349 matrixInformationList[[matrixInformationListCount]] = c(matrixName,fileName,delimiter,rows,columns)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
350 matrixInformationListCount = matrixInformationListCount + 1
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
351 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
352 return(matrixInformationList)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
353 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
354
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
355 funcParseIndexSlices = function(
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
356 ### Take a string of comma or dash seperated integer strings and convert into a vector
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
357 ### of integers to use in index slicing
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
358 strIndexString,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
359 ### String to be parsed into indicies vector
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
360 cstrNames
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
361 ### Column names of the data so names can be resolved to indicies
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
362 ){
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
363 #If the slices are NA then return
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
364 if(is.na(strIndexString)){return(strIndexString)}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
365
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
366 #List of indices to return
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
367 viRetIndicies = c()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
368
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
369 #Split on commas
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
370 lIndexString = sapply(strsplit(strIndexString, c_COMMA),function(x) return(x))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
371 for(strIndexItem in lIndexString)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
372 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
373 #Handle the - case
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
374 if(strIndexItem=="-"){strIndexItem = paste("2-",length(cstrNames),sep="")}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
375
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
376 #Split on dash and make sure it makes sense
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
377 lItemElement = strsplit(strIndexItem, c_DASH)[[1]]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
378 if(length(lItemElement)>2){stop("Error in index, too many dashes, only one is allowed. Index = ",strIndexItem,sep="")}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
379
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
380 #Switch names to numbers
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
381 aiIndices = which(is.na(as.numeric(lItemElement)))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
382 for( iIndex in aiIndices )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
383 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
384 lItemElement[iIndex] = which(cstrNames==lItemElement[iIndex])[1]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
385 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
386
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
387 #Make numeric
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
388 liItemElement = unlist(lapply(lItemElement, as.numeric))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
389
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
390 #If dash is at the end or the beginning add on the correct number
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
391 if(substr(strIndexItem,1,1)==c_DASH){liItemElement[1]=2}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
392 if(substr(strIndexItem,nchar(strIndexItem),nchar(strIndexItem))==c_DASH){liItemElement[2]=length(cstrNames)}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
393
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
394 #If multiple numbers turn to a slice
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
395 if(length(liItemElement)==2){liItemElement = c(liItemElement[1]:liItemElement[2])}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
396
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
397 #Update indices
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
398 viRetIndicies = c(viRetIndicies, liItemElement)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
399 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
400 if(length(viRetIndicies)==0){return(NA)}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
401 return(sort(unique(viRetIndicies)))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
402 ### Sorted indicies vector
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
403 }