### R code from vignette source 'TargetSearch.Rnw'

###################################################
### code chunk number 1: LibraryPreload
###################################################
options(width=60)
library(TargetSearch)
library(TargetSearchData)


###################################################
### code 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 = ".")


###################################################
### code 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 <- sub("^([[:digit:]]+).*$","\\1",cdffiles)
# 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)


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


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


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


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


###################################################
### code 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))


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


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


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


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


###################################################
### code chunk number 13: LibrarySearch4
###################################################
met.RI        <- retIndex(peakData)
met.Intensity <- Intensity(peakData)
# show the intensity values of the first metabolite.
met.Intensity[[1]]


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


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


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


###################################################
### code chunk number 17: plotPeak
###################################################
# 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))
# which.met=3 (id of Valine)
plotPeak(samples, lib, MetabProfile, rawpeaks, which.smp=sample.id,
    which.met=3, massRange=c(85,500), corMass=FALSE)


###################################################
### code chunk number 18: untargeted1
###################################################
metRI    <- seq(200000, 300000, by = 5000)
metMZ    <- 85:250
metNames <- paste("Metab",format(metRI,digits=6), sep = "_")


###################################################
### code chunk number 19: untargeted2
###################################################
metLib   <- new("tsLib", Name = metNames, RI = metRI,
     selMass = rep(list(metMZ), length(metRI)), RIdev = c(3000, 1500, 500))


###################################################
### code chunk number 20: untargeted3
###################################################
metLib <- medianRILib(samples, metLib)
metCorRI <- sampleRI(samples, metLib)
metPeakData <- peakFind(samples, metLib, metCorRI)
metProfile <- Profile(samples, metLib, metPeakData)
metFinProf <- ProfileCleanUp(metProfile, timeSplit = 500)


###################################################
### code chunk number 21: untargeted4
###################################################
sum(  profileInfo(metFinProf)$Mass_count > 5)
tmp   <-  profileInfo(metFinProf)$Mass_count > 5
metRI    <- profileInfo(metFinProf)$RI[tmp]
metNames <-  as.character( profileInfo(metFinProf)$Name[tmp] )
metMZ <- sapply(profileInfo(metFinProf)$Masses[tmp],
	function(x) as.numeric(unlist(strsplit(x,";"))) )
metLib   <- new("tsLib", Name = metNames, RI = metRI,
     selMass = metMZ, RIdev = c(1500, 750, 250))