forked from MSGFPlus/msgfplus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpectraAccessor.java
More file actions
200 lines (175 loc) · 6.94 KB
/
SpectraAccessor.java
File metadata and controls
200 lines (175 loc) · 6.94 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package edu.ucsd.msjava.msutil;
import edu.ucsd.msjava.mzid.Constants;
import edu.ucsd.msjava.mzml.StaxMzMLParser;
import edu.ucsd.msjava.mzml.StaxMzMLSpectraIterator;
import edu.ucsd.msjava.mzml.StaxMzMLSpectraMap;
import edu.ucsd.msjava.parser.*;
import uk.ac.ebi.jmzidml.model.mzidml.CvParam;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
public class SpectraAccessor {
private final File specFile;
private final SpecFileFormat specFormat;
private SpectrumParser spectrumParser;
private StaxMzMLParser staxParser = null;
private int minMSLevel = 2;
private int maxMSLevel = 2;
SpectrumAccessorBySpecIndex specMap = null;
Iterator<Spectrum> specItr = null;
/**
* Constructor that accepts a file
* Determines the file format based on the file extension
*
* @param specFile
*/
public SpectraAccessor(File specFile) {
this(specFile, SpecFileFormat.getSpecFileFormat(specFile.getName()));
}
/**
* Constructor that accepts a file and a file format
*
* @param specFile
* @param specFormat
*/
public SpectraAccessor(File specFile, SpecFileFormat specFormat) {
if (specFormat == null) {
throw new IllegalArgumentException("Unsupported spectrum file format: " + specFile.getName());
}
this.specFile = specFile;
this.specFormat = specFormat;
this.spectrumParser = null;
}
/**
* Set the MS level range for spectrum filtering (both inclusive).
*
* @param minMSLevel minimum MS level to consider (inclusive).
* @param maxMSLevel maximum MS level to consider (inclusive).
*/
public void setMSLevelRange(int minMSLevel, int maxMSLevel) {
this.minMSLevel = minMSLevel;
this.maxMSLevel = maxMSLevel;
}
public SpectrumAccessorBySpecIndex getSpecMap() {
if (specMap == null) {
if (specFormat == SpecFileFormat.MZML) {
if (staxParser == null) {
try {
staxParser = new StaxMzMLParser(specFile);
} catch (Exception e) {
throw new RuntimeException("Failed to parse mzML file: " + specFile.getAbsolutePath(), e);
}
}
specMap = new StaxMzMLSpectraMap(staxParser, minMSLevel, maxMSLevel);
} else if (specFormat == SpecFileFormat.DTA_TXT)
specMap = new PNNLSpectraMap(specFile.getPath());
else {
SpectrumParser parser = null;
if (specFormat == SpecFileFormat.MGF)
parser = new MgfSpectrumParser();
else if (specFormat == SpecFileFormat.MS2)
parser = new MS2SpectrumParser();
else if (specFormat == SpecFileFormat.PKL)
parser = new PklSpectrumParser();
else
return null;
spectrumParser = parser;
specMap = new SpectraMap(specFile.getPath(), parser);
}
}
if (specMap == null) {
System.out.println("No spectra were found");
System.out.println("File: " + specFile.getAbsolutePath());
System.out.println("Format: " + specFormat.getPSIName());
}
return specMap;
}
public Iterator<Spectrum> getSpecItr() {
if (specItr == null) {
if (specFormat == SpecFileFormat.MZML) {
if (staxParser == null) {
try {
staxParser = new StaxMzMLParser(specFile);
} catch (Exception e) {
throw new RuntimeException("Failed to parse mzML file: " + specFile.getAbsolutePath(), e);
}
}
specItr = new StaxMzMLSpectraIterator(staxParser, minMSLevel, maxMSLevel);
} else if (specFormat == SpecFileFormat.DTA_TXT)
try {
specItr = new PNNLSpectraIterator(specFile.getPath());
} catch (IOException e) {
e.printStackTrace();
}
else {
SpectrumParser parser = null;
if (specFormat == SpecFileFormat.MGF)
parser = new MgfSpectrumParser();
else if (specFormat == SpecFileFormat.MS2)
parser = new MS2SpectrumParser();
else if (specFormat == SpecFileFormat.PKL)
parser = new PklSpectrumParser();
else
return null;
spectrumParser = parser;
try {
specItr = new SpectraIterator(specFile.getPath(), parser);
} catch (IOException e) {
e.printStackTrace();
}
}
}
return specItr;
}
public Spectrum getSpectrumBySpecIndex(int specIndex) {
return getSpecMap().getSpectrumBySpecIndex(specIndex);
}
public Spectrum getSpectrumById(String specId) {
return getSpecMap().getSpectrumById(specId);
}
/**
* Get the current spectrum parser, or null if no parser
* @return
*/
public SpectrumParser getSpectrumParser() {
return spectrumParser;
}
public String getID(int specIndex) {
return getSpecMap().getID(specIndex);
}
public float getPrecursorMz(int specIndex) {
return getSpecMap().getPrecursorMz(specIndex);
}
public String getTitle(int specIndex) {
return getSpecMap().getTitle(specIndex);
}
public CvParam getSpectrumIDFormatCvParam() {
CvParam cvParam = null;
if (specFormat == SpecFileFormat.DTA_TXT
|| specFormat == SpecFileFormat.MGF
|| specFormat == SpecFileFormat.PKL
|| specFormat == SpecFileFormat.MS2
)
cvParam = Constants.makeCvParam("MS:1000774", "multiple peak list nativeID format");
else if (specFormat == SpecFileFormat.MZDATA)
cvParam = Constants.makeCvParam("MS:1000777", "spectrum identifier nativeID format");
else if (specFormat == SpecFileFormat.MZML) {
if (staxParser == null) {
try {
staxParser = new StaxMzMLParser(specFile);
} catch (Exception e) {
throw new RuntimeException("Failed to parse mzML file: " + specFile.getAbsolutePath(), e);
}
}
String[] idFormat = staxParser.detectSpectrumIDFormat();
if (idFormat != null) {
cvParam = Constants.makeCvParam(idFormat[0], idFormat[1]);
} else {
throw new IllegalStateException("Unsupported mzML format: " + specFile.getAbsolutePath()
+ " does not contain a child term of MS:1000767 (native spectrum identifier format)");
}
}
return cvParam;
}
}