lgatto/MSnbase — pull request #32 by sgibb (2014-04-13 00:48:06+00:00)
Title: compareSpectra

Dear Laurent,

this is not a final PR yet but a basis for discussion.

This PR introduces a bunch of new functions/methods:

- `bin,Spectrum`, `bin,MSnExp`
- `compareSpectra,Spectrum,Spectrum`, `compareSpectra,MSnExp,missing`
- `plotSpectrumVsSpectrum` (not exported yet)
- `matchRelaxed` (similar to `base::match` but with tolerance and possible duplicates; not exported yet)
- `matchPeaks` (wrapper around `matchRelaxed`, removes duplicates, works on `Spectrum` objects; not exported yet)
- `commonPeaks` (same as `matchPeaks` but returns a `logical` instead of indices; not exported yet)
- `numberOfCommonPeaks` (used for `compareSpectra(..., fun = "common"); not exported)
- `dotproduct` (used for `compareSpectra(..., fun = "dotproduct"); not exported)
- `calculateFragments` (not exported yet)

Here a few examples (not very impressive, especially the plots are worse than for the `synapter` data):

```s
library("MSnbase")
example(addIdentificationData)

msexp <- pickPeaks(msexp)

compareSpectra(msexp[[1]], msexp[[2]])
# [1] 7

compareSpectra(msexp)
# X1.1 X2.1 X3.1 X4.1 X5.1
# X1.1 NA 7 8 7 5
# X2.1 7 NA 24 9 9
# X3.1 8 24 NA 11 13
# X4.1 7 9 11 NA 8
# X5.1 5 9 13 8 NA

compareSpectra(msexp, fun="cor")
# X1.1 X2.1 X3.1 X4.1 X5.1
# X1.1 NA -0.0003689564 0.003432202 -0.0004347059 0.001305087
# X2.1 -0.0003689564 NA 0.003709523 0.0003552788 0.006813591
# X3.1 0.0034322017 0.0037095229 NA 0.0143698687 0.037413213
# X4.1 -0.0004347059 0.0003552788 0.014369869 NA 0.008380477
# X5.1 0.0013050868 0.0068135912 0.037413213 0.0083804768 NA

MSnbase:::.plotSingleSpectrum(msexp[[3]])
```
![singlespectrum](https://cloud.githubusercontent.com/assets/1828443/2688466/1e3aa698-c2a2-11e3-9cd3-637f00cd38c2.png)
```s
MSnbase:::plotSpectrumVsSpectrum(list(msexp[[1]], msexp[[2]]),
sequences=fData(msexp)$pepseq[1:2])
```
![spectrumvsspectrum](https://cloud.githubusercontent.com/assets/1828443/2688467/21a8c24c-c2a2-11e3-8b98-f723e3fcbfe1.png)
```s
MSnbase:::plotSpectrumVsSpectrum(list(msexp[[1]], msexp[[2]]),
sequences=fData(msexp)$pepseq[1:2],
xlim=c(510, 580), ylim=c(-0.9, 0.5))
```
![spectrumvsspectrumdetails](https://cloud.githubusercontent.com/assets/1828443/2688468/24f2335c-c2a2-11e3-9019-71c32d574fb5.png)
```s
MSnbase:::calculateFragments(fData(msexp)$pepseq[5])
# mass fragment.str fragment.seq
# b1 114.0913 b1 L
# y1 175.1190 y1 R
# b2 213.1597 b2 LV
# y2 322.1874 y2 FR
# b3 326.2438 b3 LVI
# y3 435.2714 y3 LFR
# b4 439.3279 b4 LVIL
# y4 548.3555 y4 LLFR
# b5 552.4119 b5 LVILL
# y5 661.4395 y5 ILLFR
# b6 699.4803 b6 LVILLF
# y6 760.5080 y6 VILLFR
# b7 855.5814 b7 LVILLFR
# y7 873.5920 y7 LVILLFR
```

I like to discuss the following questions:

1. Should we export: `matchRelaxed` , `matchPeaks` , `commonPeaks`, `numberOfCommonPeaks` and `calculateFragments`?
2. What is the best name for the `plotSpectrumVsSpectrum` function to avoid any confusion with the current `plot,MSnExp`, `plot,Spectrum1` and `plot,Spectrum2` functions?
3. Should/Could we integrate the `calculateFragments` function into the current `plot,Spectrum1`, `plot,Spectrum2` functions (`ggplot` based; I have not any experience with `ggplot`.)?
4. Should I extend `calculateFragments` for multiple charge states (`z == 1:3`, e.g. for MS1 spectra)?

I already adapted `synapter` (my `fragmentmatching` branch) to `compareSpectra`, `plotSpectrumVsSpectrum` and `calculateFragments` (The accuracy is very high. That's why I do not store the `fragment.str` information from the `final_fragment.csv` file anymore.).

Best wishes,

Sebastian

P.S.: Sorry for this large PR.