-
Notifications
You must be signed in to change notification settings - Fork 4
/
MSexport.R
179 lines (148 loc) · 8.96 KB
/
MSexport.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#' @name export2MSP
#' @aliases export2MSP
#' @title Export spectra to MSP
#' @description Export spectra to MSP format for comparison with the NIST library.
#' @param Experiment A 'MetaboSet' S4 object containing the experiment.
#' @param export.id If NULL, all the spectra in the experiment will be exported. Otherwise, only the AlignID in export.id will be exported
#' @param id.database The mass-spectra library used in the experiment.
#' @param store.path The path where the converted files are to be exported.
#' @param alg.version Different algorithm implementations. Users have to chose what version works with their NIST MSearch or other software version. By default, alg.version is set to 1. If it not works, try setting alg.version to 2 ;).
#' @export
export2MSP <- function(Experiment, export.id=NULL, id.database = mslib, store.path=getwd(), alg.version=1)
{
if(is.null(nrow(Experiment@Results@Identification)) | nrow(Experiment@Results@Identification)==0)
{
if(!is.null(export.id)) Experiment@Results@Alignment <- Experiment@Results@Alignment[which(Experiment@Results@Alignment$AlignID %in% export.id),]
SpectList <- sapply(Experiment@Results@Alignment$Spectra, function(x) {
splitted.spectra.list <- strsplit(as.character(x), split = " ")[[1]]
splitted.spectra.list <- gsub(",", " ", splitted.spectra.list)
if(alg.version==1){
splitted.spectra.list <- as.character(as.vector(sapply(splitted.spectra.list, function(x) paste(c(x,";"), collapse=""))))
Npeaks <- length(splitted.spectra.list)
if(Npeaks>=6)
{
sequ <- seq(1, Npeaks, 5)
if(sequ[length(sequ)]!=Npeaks) sequ <- c(sequ, Npeaks)
splitted.spectra.list <- paste(unlist(sapply(1:(length(sequ)-1), function(x) paste(c(splitted.spectra.list[sequ[x]:sequ[(x+1)]], "\n")))), collapse=" ")
}else{
splitted.spectra.list <- paste(splitted.spectra.list, collapse=" ")
}
PeakChar <- paste(gsub(",", " ", splitted.spectra.list), collapse="; ")
}
if(alg.version==2) {
splitted.spectra.list <- as.character(as.vector(sapply(splitted.spectra.list, function(x) paste(c(x,"\n"), collapse=""))))
Npeaks <- length(splitted.spectra.list)
PeakChar <- paste(splitted.spectra.list, collapse="")
}
return(list(Npeaks=Npeaks, PeakChar=PeakChar))
})
SpectList <- split(SpectList,seq(NROW(SpectList)))
SpectString <- as.vector(unlist(SpectList[[2]]))
Npeaks <- as.vector(unlist(SpectList[[1]]))
SpectNames.2 <- as.character(as.vector(Experiment@Results@Alignment$Factor))
SpectNames.3 <- sapply(as.numeric(as.vector(Experiment@Results@Alignment$tmean)), function(x) paste("Rt:", x))
SpectNames <- apply(cbind(SpectNames.3,SpectNames.2), 1, function(x) paste(x, collapse=" @ "))
}else{
if(Experiment@Results@Parameters@Identification$database.name != id.database@name) {
error.msg <- paste("This experiment was not processed with the database selected. Please use ",
Experiment@Results@Parameters@Identification$database.name,
sep = "")
stop(error.msg)
}
if(!is.null(export.id)) Experiment@Results@Identification<- Experiment@Results@Identification[which(Experiment@Results@Identification$AlignID %in% export.id),]
SpectList <- sapply(Experiment@Results@Identification$Spectra, function(x) {
splitted.spectra.list <- strsplit(as.character(x), split = " ")[[1]]
splitted.spectra.list <- gsub(",", " ", splitted.spectra.list)
if(alg.version==1){
splitted.spectra.list <- as.character(as.vector(sapply(splitted.spectra.list, function(x) paste(c(x,";"), collapse=""))))
Npeaks <- length(splitted.spectra.list)
if(Npeaks>=6)
{
sequ <- seq(1, Npeaks, 5)
if(sequ[length(sequ)]!=Npeaks) sequ <- c(sequ, Npeaks)
splitted.spectra.list <- paste(unlist(sapply(1:(length(sequ)-1), function(x) paste(c(splitted.spectra.list[sequ[x]:sequ[(x+1)]], "\n")))), collapse=" ")
}else{
splitted.spectra.list <- paste(splitted.spectra.list, collapse=" ")
}
PeakChar <- paste(gsub(",", " ", splitted.spectra.list), collapse="; ")
}
if(alg.version==2) {
splitted.spectra.list <- as.character(as.vector(sapply(splitted.spectra.list, function(x) paste(c(x,"\n"), collapse=""))))
Npeaks <- length(splitted.spectra.list)
PeakChar <- paste(splitted.spectra.list, collapse="")
}
return(list(Npeaks=Npeaks, PeakChar=PeakChar))
})
SpectList <- split(SpectList,seq(NROW(SpectList)))
SpectString <- as.vector(unlist(SpectList[[2]]))
Npeaks <- as.vector(unlist(SpectList[[1]]))
id.found <- as.numeric(as.vector(Experiment@Results@Identification[, "DB.Id.1"]))
met.name <- unlist(lapply(id.database@database[c(id.found)], function(x) x$Name))
SpectNames.1 <- sapply(as.character(as.vector(Experiment@Results@Identification$AlignID)), function(x) paste("(Align ID: ", x, ")", sep=""))
SpectNames.2 <- sapply(as.numeric(as.vector(Experiment@Results@Identification$tmean)), function(x) paste("Rt:", x))
SpectNames.3 <- apply(cbind(SpectNames.2,SpectNames.1), 1, function(x) paste(x, collapse=" "))
SpectNames <- as.character(as.vector(apply(cbind(SpectNames.3,met.name), 1, function(x) paste(x, collapse=" @ "))))
}
dir.create(file.path(store.path, "ExportMSP") , showWarnings = FALSE)
filename <- paste(c(store.path, "/ExportMSP/", "ExportedMSP", ".msp"), collapse="")
fileTag <- character()
for(i in 1:length(SpectString))
{
fileTag[i] <- paste(c("Name: ", SpectNames[i], "\n", "Comments: MSP spectra exported by eRah \n", "Num Peaks: ", Npeaks[i], "\n",SpectString[i]), collapse="")
}
fileTagGen <- paste(fileTag, collapse="\n")
writeLines(fileTagGen, filename)
cat("Spectra saved at: ", store.path, "/ExportMSP", sep="")
}
#' @name export2CEF
#' @aliases export2CEF
#' @title Export spectra to CEF
#' @description Export spectra to CEF format for comparison with the NIST library through MassHunter interface.
#' @usage export2CEF(Experiment, export.id = NULL,
#' id.database = mslib, store.path = getwd())
#' @param Experiment A 'MetaboSet' S4 object containing the experiment.
#' @param export.id If NULL, all the spectra in the experiment will be exported. Otherwise, only the AlignID in export.id will be exported
#' @param id.database The mass-spectra library used in the experiment.
#' @param store.path The path where the converted files are to be exported.
#' @export
export2CEF <- function (Experiment, export.id = NULL, id.database = mslib, store.path = getwd())
{
if (!is.null(export.id)) Experiment@Results@Alignment <- Experiment@Results@Alignment[which(Experiment@Results@Alignment$AlignID %in% export.id), ]
SpectList <- sapply(Experiment@Results@Alignment$Spectra, function(x) {
splitted.spectra.list <- strsplit(as.character(x), split = " ")[[1]]
as.vector(sapply(splitted.spectra.list, function(y){
y.split <- strsplit(y, ",")[[1]]
x.cor <- y.split[1]
y.cor <- y.split[2]
paste("<p x=", '"', as.numeric(x.cor), '" y="', y.cor, '" />', sep="")
}))
})
SpectNames.2 <- as.character(as.vector(Experiment@Results@Alignment$Factor))
SpectRT <- as.numeric(as.vector(Experiment@Results@Alignment$tmean))
SpectNames.3 <- sapply(as.numeric(as.vector(Experiment@Results@Alignment$tmean)), function(x) paste("Rt:", x))
SpectNames <- apply(cbind(SpectNames.2, SpectNames.3), 1, function(x) paste(x, collapse = " @ "))
dir.create(file.path(store.path, "ExportCEF"), showWarnings = FALSE)
filename <- paste(c(store.path, "/ExportCEF/", "ExportedCEF", ".cef"), collapse = "")
fileTag <- character()
fileTag[1] <- paste("<?xml version=", '"', "1.0", '"' , "encoding=", '"', "utf-8",'"', "?><CEF version=",'"',"1.0.0.0",'"',"><CompoundList>", sep="")
for (i in 1:length(SpectList)) {
fileTag[length(fileTag) + 1] <- ""
fileTag[length(fileTag) + 1] <- paste("<Compound mppid=", '"',SpectNames[i], '"'," algo=", '"',"FindByAMDIS", '"'," >", sep="")
fileTag[length(fileTag) + 1] <- paste("<Location rt=", '"',SpectRT[i], '"'," />", sep="")
fileTag[length(fileTag) + 1] <- ""
fileTag[length(fileTag) + 1] <- paste("<Spectrum type=", '"',"AMDIS", '"'," cpdAlgo=", '"',"FindByAMDIS", '"',">", sep="")
fileTag[length(fileTag) + 1] <- paste("<MSDetails p=", '"',"+", '"'," />", sep="")
fileTag[length(fileTag) + 1] <- "<MSPeaks>"
srtI <- (length(fileTag) + 1)
srtE <- (length(fileTag) + 1) + length(SpectList[[i]]) - 1
fileTag[srtI:srtE] <- SpectList[[i]]
fileTag[length(fileTag) + 1] <- "</MSPeaks>"
fileTag[length(fileTag) + 1] <- "</Spectrum>"
fileTag[length(fileTag) + 1] <- "</Compound>"
}
fileTag[length(fileTag) + 1] <- ""
fileTag[length(fileTag) + 1] <- "</CompoundList></CEF>"
#fileTagGen <- paste(fileTag, collapse = "\n \n")
writeLines(fileTag, filename)
cat("Spectra saved at: ", store.path, "/ExportCEF", sep = "")
}