Question: MSnbase extracting individual spectrum based on precursor ion m/z
0
13 days ago by
garfield32020
garfield32020 wrote:

Hi,

I'm having trouble with extracting individual MS spectrum from my metadata based on precursor ion m/z.

rawdata <- readMSData("file.mzXML", msLevel = 2,  mode = "onDisk")


I'm aware that I can assess individual MS spectrum objects by using index, like rawdata[[1]] . But I was trying to get the index I want based on retention time or m/z of precursor ion. For example, my rawdata[[1]] looks like this:

> rawdata[[1]]
Object of class "Spectrum2"
Precursor: 808.5253
Retention time: 0:8
Charge: 2
MSn level: 2
Peaks count: 462
Total ion count: 1423873


For retention time, this worked:

head(rtime(rawdata))   #got exact retention time
> which(rtime(rawdata)==7.81947)
F1.S00029
1


But when I do the same for precursor ion m/z, I don't get an index as an answer.

> which(precursorMz(rawdata)==808.5253)
named integer(0)


Why doesn't this work for precursor ion m/z? Is there any way to extract the index (and eventually the corresponding MS spectrum) based on precursor ion m/z?

msnbase • 36 views
modified 13 days ago by Laurent Gatto1.1k • written 13 days ago by garfield32020
Answer: MSnbase extracting individual spectrum based on precursor ion m/z
2
13 days ago by
Laurent Gatto1.1k
United Kingdom
Laurent Gatto1.1k wrote:

This is most likely due to the fact that the precursor M/Z isn't exactly 808.5253. 808.5253 is probably a rounded up value that is shown as part of showing the spectrum object, and == returns TRUE when the two values are exactly identical. You would need to check the exact value of the M/Z value - for example, the following should return what you are looking for

mz1 <- precursorMz(rawdata)[1]
which(precursorMz(rawdata) == mz1)


Alternatively, you could also use all.equal rather than ==:

> all.equal(808.5253, 808.52532355)
[1] "Mean relative difference: 2.91271e-08"
> all.equal(808.5253, 808.52532355, tolerance = 0.0001)
[1] TRUE


or round the M/Z value before doing the comparisons:

> all.equal(808.5253, round(808.52532355, 4))
[1] TRUE
> 808.5253 == round(808.52532355, 4)
[1] TRUE


Yes that was the problem, which(round(precursorMz(rawdata),4)==808.5253) gave me the index I wanted, thanks!

Yes that was the problem, which(round(precursorMz(rawdata),4)==808.5253) gave me the index I wanted, thanks!