-
Notifications
You must be signed in to change notification settings - Fork 4
/
importTab-functions.R
77 lines (66 loc) · 2.71 KB
/
importTab-functions.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
## Copyright 2012-2014 Sebastian Gibb
## <mail@sebastiangibb.de>
##
## This file is part of MALDIquantForeign for R and related languages.
##
## MALDIquantForeign is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## MALDIquantForeign is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with MALDIquantForeign. If not, see <http://www.gnu.org/licenses/>
.importTab <- function(file, centroided=FALSE, massRange=c(0L, Inf),
minIntensity=0L, skip=0L, sep=NULL, header=NULL,
comment.char="#", encoding="unknown", verbose=FALSE,
...) {
text <- readLines(file, encoding=encoding)
text <- tail(text, length(text) - skip)
text <- text[!startsWith(trimws(text), comment.char)]
if (is.null(sep)) {
sep <- .autoSep(text)
}
if (is.null(header)) {
header <- .autoHeader(text, sep=sep)
}
## load ms file
s <- read.table(text=text, header=header, sep=sep, skip=skip,
comment.char=comment.char, stringsAsFactors=FALSE, ...)
list(.createMassObject(mass=s[, 1L], intensity=s[, 2L],
metaData=list(file=file),
centroided=centroided,
massRange=massRange,
minIntensity=minIntensity,
verbose=verbose))
}
.importCsv <- function(file, centroided=FALSE, massRange=c(0L, Inf),
minIntensity=0L, skip=0L, sep=NULL, header=NULL,
comment.char="#", encoding="unknown",
verbose=FALSE, ...) {
.importTab(file=file, centroided=centroided, massRange=massRange,
minIntensity=minIntensity, skip=skip, sep=sep,
header=header, comment.char=comment.char, encoding=encoding,
verbose=verbose, ...)
}
.autoHeader <- function(text, sep="\t") {
l <- gsub(pattern='[\\\\"]*', replacement="", x=text)
l <- strsplit(l, split=sep)[[1L]][1L]
!is.numeric(type.convert(l, as.is=TRUE))
}
.autoSep <- function(text, sep=c(",", ";", "\t", " ")) {
pattern <- paste0(".+", sep, ".+")
i <- vapply(pattern, function(x) {
g <- gregexpr(pattern=x, text=text)[[1L]]
all(g > 0L) & length(g) == 1L
}, logical(1L))
if (any(i)) {
sep[which(i)[1L]] ## return only first match
} else {
sep[1L]
}
}