-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSubsetTableToVector.R
114 lines (78 loc) · 3.64 KB
/
SubsetTableToVector.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# Description -----------------------------------------------------------------
# Remove rows from one table based on a label column from another table
# User Input ------------------------------------------------------------------
# Path to folder with individual peak table directories
# for Windows operating system use \\ or / instead of \ to separate directories
# example:
# path to folder where output directory will be created
# put copies of the CSV files in this folder
outPutParentDir <- "E:\\Example1\\Example1"
# CSV file with column of subset list
CSV_subsetList <- "exampleFileName1.csv"
# column index number for filter vector
colIndexFilterVect <- 1
# Directory of table file to be subset
inputCSVDir <- "E:\\Example2\\Example2"
# CSV file of table to be subset
CSV_toSubset <- "exampleFileName2.csv"
# column index number corresponding to filter vector
colIndexFullVect <- 1
# End user input ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# advanced user input (probably don't need to change) ------------------------
# Library (packages)--------------------------------------------------------
library(data.table) # faster version of data frames
library(lcmsMetab)
# Timing --------------------------------------------------------------
startTime <- Sys.time()
startTimeStamp <- format(startTime, "(%Y%m%dh%H%M)")
# set or create the output folder -------------------------------------------
OutputFolderName <- "SubsetTableToVector"
# create output folder if it doesn't exist
outputFolderPath <- paste0(outPutParentDir,"/",
OutputFolderName,startTimeStamp)
if (!dir.exists(outputFolderPath)) {
dir.create(outputFolderPath)
}
# Initialize summary file -------------------------------------------
logFilePath <- paste0(outputFolderPath,"/",
OutputFolderName,"LogFile",
startTimeStamp, ".txt")
logText <- paste0("start time ", startTimeStamp)
logText <- UpdateLogText(logText,"Initialize Summary File")
UpdateLogFile(logFilePath, logText)
# import data -----------------------------------------------------------------
# import table to be subset
importFilePath <- paste0(inputCSVDir, "/", CSV_toSubset)
dt.main <- fread(file = importFilePath)
# import vector to subset to
importFilePath <- paste0(outPutParentDir, "/", CSV_subsetList)
dt.subsetList <- fread(file = importFilePath,
select = c(colIndexFilterVect),
col.names = c("filter"))
# subset ----------------------------------------------------------------------
# rename column for ease
saveName <- colnames(dt.main)[colIndexFullVect]
colnames(dt.main)[colIndexFullVect] <- "fullVect"
dt.main <- dt.main[fullVect %in% dt.subsetList$filter]
# revert to original column name
colnames(dt.main)[colIndexFullVect] <- saveName
# write subset table ---------------------------------------------------------
exportName <- paste0(gsub("\\.csv$", "",CSV_toSubset),"_subset",startTimeStamp)
WrtTable(dt.main, outputFolderPath, gsub("\\.csv$", "",exportName))
# Endscript (record keeping) -----------------------------------------
# get script file name
scriptName <- basename(sys.frame(1)$ofile)
# remove the file extension ".R"
scriptName <- gsub("\\.R","",scriptName)
# copy script to folder to save as record
file.copy(sys.frame(1)$ofile,
to = file.path(outputFolderPath,
paste0(scriptName, startTimeStamp, ".R")))
logText <-
UpdateLogText(logText,"end script",runTime(startTime))
UpdateLogFile(logFilePath, logText)
if (1 == 1) {
print("script complete")
print(runTime(startTime))
}