~ubuntu-branches/ubuntu/vivid/r-cran-readbrukerflexdata/vivid-proposed

« back to all changes in this revision

Viewing changes to R/readBrukerFlexDir-functions.R

  • Committer: Package Import Robot
  • Author(s): The Debichem Group
  • Date: 2013-08-28 13:42:19 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20130828134219-2w0p8unqlgysyt6d
Tags: 1.7-1
* New upstream release.
* debian/control.in:
  - use canonical URI in Vcs-Git/Vcs-Browser.
  - set debhelper (>= 9)
* Uploaded by Filippo Rusconi <lopippo@debian.org>.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
#' Reads recursively mass spectrometry data in Bruker Daltonics XMASS format.
20
20
#'
21
 
#' This function leads recursively all mass spectrometry data in 
 
21
#' This function leads recursively all mass spectrometry data in
22
22
#' Bruker Daltonics XMASS format in a specified directory.
23
23
#'
24
24
#' @details
25
25
#' See \code{\link[readBrukerFlexData]{readBrukerFlexFile}}.
26
26
#'
27
 
#' @param brukerFlexDir \code{character}, path to \emph{directory} which 
 
27
#' @param brukerFlexDir \code{character}, path to \emph{directory} which
28
28
#'  should be read recursively.
29
29
#' @param removeCalibrationScans \code{logical}, if \code{TRUE} all scans in
30
30
#'  directories called \code{[Cc]alibration} will be ignored.
31
31
#' @param removeMetaData \code{logical}, to calculate mass data a lot of
32
32
#'  meta data are needed. To save memory they could be deleted after
33
 
#'  calculation. 
 
33
#'  calculation.
34
34
#' @param useHpc \code{logical}, should Bruker Daltonics' High Precision
35
 
#'  Calibration be used if available? (see also: 
 
35
#'  Calibration be used if available? (see also:
36
36
#'  \code{\link[readBrukerFlexData]{.hpc}})
37
37
#' @param useSpectraNames \code{logical}, if \code{TRUE} all list elements
38
38
#'  get an unique name from metaData otherwise file path is used.
39
 
#'  (If \sQuote{removeMetaData} is \code{TRUE} \sQuote{useSpectraNames} 
 
39
#'  (If \sQuote{removeMetaData} is \code{TRUE} \sQuote{useSpectraNames}
40
40
#'  has no effect.)
41
41
#' @param filterZeroIntensities \code{logical}, don't change it. If \code{TRUE}
42
42
#'  all intensities equal \code{0.0} are removed.
60
60
#' @examples
61
61
#' ## load library
62
62
#' library("readBrukerFlexData")
63
 
#' 
 
63
#'
64
64
#' ## get examples directory
65
65
#' exampleDirectory <- system.file("Examples", package="readBrukerFlexData")
66
 
#' 
 
66
#'
67
67
#' ## read example spectra
68
68
#' spec <- readBrukerFlexDir(file.path(exampleDirectory,
69
69
#'   "2010_05_19_Gibb_C8_A1"))
70
 
#' 
 
70
#'
71
71
#' ## plot spectra
72
72
#' plot(spec[[1]]$spectrum$mass, spec[[1]]$spectrum$intensity, type="n")
73
 
#' 
 
73
#'
74
74
#' l <- length(spec)
75
75
#' legendStr <- character(l)
76
76
#' for (i in seq(along=spec)) {
78
78
#'         col=rainbow(l)[i])
79
79
#'   legendStr[i] <- spec[[i]]$metaData$fullName
80
80
#' }
81
 
#' 
 
81
#'
82
82
#' ## draw legend
83
83
#' legend(x="topright", legend=legendStr, col=rainbow(l), lwd=1)
84
84
#'
85
 
readBrukerFlexDir <- function(brukerFlexDir, removeCalibrationScans=TRUE, 
86
 
                              removeMetaData=FALSE, useHpc=TRUE, 
87
 
                              useSpectraNames=TRUE, 
 
85
readBrukerFlexDir <- function(brukerFlexDir, removeCalibrationScans=TRUE,
 
86
                              removeMetaData=FALSE, useHpc=TRUE,
 
87
                              useSpectraNames=TRUE,
88
88
                              filterZeroIntensities=FALSE, verbose=FALSE) {
89
89
  if (verbose) {
90
90
    message("Look for spectra in ", sQuote(brukerFlexDir), " ...")
91
91
  }
92
 
  
 
92
 
93
93
  if ((!file.exists(brukerFlexDir)) || (!file.info(brukerFlexDir)$isdir)) {
94
94
    stop("Directory ", sQuote(brukerFlexDir),
95
95
         " doesn't exists or is no directory!")
96
96
  }
97
 
  
 
97
 
98
98
  ## look for fid files (alphabetical sort)
99
99
  files <- list.files(path=brukerFlexDir, pattern="^fid$", recursive=TRUE)
100
 
  
 
100
 
101
101
  ## remove calibrations scans?
102
102
  if (removeCalibrationScans) {
103
103
    calibrationScans <- grep(pattern="[Cc]alibration", x=files, value=TRUE)
105
105
      files <- setdiff(files, calibrationScans)
106
106
    }
107
107
  }
108
 
  
 
108
 
109
109
  if (length(files) <= 0) {
110
110
    stop("Directory doesn't contain any fid file.")
111
111
  }
112
 
  
 
112
 
113
113
  ## generate 'path/files'
114
114
  files <- sapply(files, function(x) {
115
115
    x <- file.path(brukerFlexDir, x)
116
116
    return(x)
117
117
  })
118
 
  
 
118
 
119
119
  ## read fid files
120
120
  brukerFlexData <- lapply(X=files, FUN=function(f) {
121
121
    return(readBrukerFlexFile(fidFile=f, removeMetaData=removeMetaData,
123
123
                              filterZeroIntensities=filterZeroIntensities,
124
124
                              verbose=verbose))
125
125
  })
126
 
  
 
126
 
127
127
  if (!removeMetaData & useSpectraNames) {
128
128
    ## rewrite names if metadata exists
129
129
    if (verbose) {
130
130
      message("look for spectra names ...")
131
131
    }
132
 
    
 
132
 
133
133
    names(brukerFlexData) <- sapply(X=brukerFlexData, FUN=function(x) {
134
134
      if (!is.null(x$metaData$sampleName)) {
135
135
        if (!is.na(x$metaData$sampleName)) {
136
 
          return(paste("s", x$metaData$fullName, ".", 
137
 
                       x$metaData$targetIdString, sep=""))
 
136
          return(paste0("s", x$metaData$fullName, ".",
 
137
                        x$metaData$targetIdString))
138
138
        }
139
139
      }
140
140
      return(NA)
141
141
    }, USE.NAMES=FALSE)
142
142
  }
143
 
  
 
143
 
144
144
  return(brukerFlexData)
145
145
}
146
 
 
 
146