###################################################
### chunk number 1: LibraryPreload
###################################################
library(xcms)
library(TargetSearch)
library(TargetSearchData)


###################################################
### chunk number 2: ImportSample
###################################################
library(TargetSearchData)
library(TargetSearch)
cdf.path <- system.file("gc-ms-data", package = "TargetSearchData")
sample.file <- file.path(cdf.path, "samples.txt")
samples <- ImportSamples(sample.file, CDFpath = cdf.path,
                         RIpath = ".")


###################################################
### chunk number 3: ImportSample2
###################################################
cdffiles <- dir(cdf.path, pattern="cdf$")
# define the RI file names
rifiles  <- paste("RI_", sub("cdf","txt", cdffiles), sep = "")
# take the measurement day info from the cdf file names.
days <- substring(cdffiles, 1, 4)
# sample names 
smp_names <- sub("\\.cdf", "", cdffiles)
# add some sample info
smp_data <- data.frame(CDF_FILE =cdffiles, GROUP = gl(5,3))
# create the sample object
samples <- new("tsSample", Names = smp_names, CDFfiles = cdffiles,
               CDFpath = cdf.path, RIpath = ".", days = days,
               RIfiles = rifiles, data = smp_data)


###################################################
### chunk number 4: ImportFameSettings
###################################################
rim.file  <- file.path(cdf.path, "rimLimits.txt")
rimLimits <- ImportFameSettings(rim.file, mass = 87)


###################################################
### chunk number 5: RIcorrection
###################################################
RImatrix <- RIcorrect(samples, rimLimits, massRange = c(85,500),
            IntThreshold = 10, pp.method = "smoothing", Window = 7)


###################################################
### chunk number 6: PeakIdentification
###################################################
outliers <- FAMEoutliers(samples, RImatrix, threshold = 3)


###################################################
### chunk number 7: plotFAME
###################################################
plotFAME(samples, RImatrix, 1)


###################################################
### chunk number 8: ImportLibrary
###################################################
lib.file <- file.path(cdf.path, "library.txt")
lib      <- ImportLibrary(lib.file, RI_dev = c(2000,1000,200),
            TopMasses = 15, ExcludeMasses = c(147, 148, 149))


###################################################
### chunk number 9: LibrarySearch1
###################################################
lib <- medianRILib(samples, lib)


###################################################
### chunk number 10: medianLib
###################################################
resPeaks <- FindPeaks(RIfiles(samples), refLib(lib, w = 1, sel = TRUE))
plotRIdev(lib, resPeaks, libId = 1:9)


###################################################
### chunk number 11: LibrarySearch2
###################################################
cor_RI <- sampleRI(samples, lib, r_thres = 0.95,
                     method = "dayNorm")


###################################################
### chunk number 12: LibrarySearch3
###################################################
peakData <- peakFind(samples, lib, cor_RI)


###################################################
### chunk number 13: LibrarySearch4
###################################################
met.RI        <- retIndex(peakData)
met.Intensity <- Intensity(peakData)


###################################################
### chunk number 14: MetaboliteProfile
###################################################
Profile <- Profile(samples, lib, peakData, r_thres = 0.95,
                      method = "dayNorm")


###################################################
### chunk number 15: MetaboliteProfile2
###################################################
finalProfile <- ProfileCleanUp(Profile, timeSplit = 500,
                                r_thres = 0.95)


###################################################
### chunk number 16: plotSpectra
###################################################
grep("Valine", libName(lib))
plotSpectra(lib, peakData, libId = 3, type = "ht")


###################################################
### chunk number 17: plotPeak
###################################################
# select "Valine" top masses
top.masses <- topMass(lib)[[3]]
# we select the first sample
sample.id <- 1
cdf.file  <- file.path(cdf.path, cdffiles[sample.id])
rawpeaks  <- peakCDFextraction(cdf.file, massRange = c(85,500))
plotPeak(rawpeaks, time.range = libRI(lib)[3] + c(-2000,2000),
    masses = top.masses, useRI = TRUE, rimTime = RImatrix[,sample.id],
    standard = rimStandard(rimLimits), massRange = c(85, 500),
    main = "Valine")


