Skip to content

MSExperiment: Store idenfication data and spectra separately - #5547

Closed
dvdvgt wants to merge 3 commits into
OpenMS:developfrom
dvdvgt:feature/AnnotatedMSExperiment
Closed

dvdvgt wants to merge 3 commits into
OpenMS:developfrom
dvdvgt:feature/AnnotatedMSExperiment

Conversation

@dvdvgt

@dvdvgt dvdvgt commented Sep 21, 2021

Copy link
Copy Markdown
Contributor

This PR aims to tackle the issue #4927; to separate identification data from spectra data. This involves removing

std::vector<PeptideIdentification> identification_;

all PeptideIdentification from SpectrumSettings and maybe also
std::vector<ProteinIdentification> protein_identifications_;

all ProteinIdentification from ExperimentSettings.

Description

@timosachsenberg proposed to implement a new class that realizes the mapping between ID and spectra called AnnotatedMSRawData (or something like that).

The idea is to offer an interface to conveniently get both MSSpectrum and PeptideIdentification as a std::pair while also maintaining the functionality offered by MSExperiment. I decided to go with inheritance instead of composition because it simplifies calling functions from MSExperiment since you don't have to access a MSExperiment member of AnnotatedMSRawData every time.
AnnotatedMSRawData implements iterator that returns a std::pair and can be used in e.g. range-based for loops.

Aside from the design of the new class AnnotatedMSRawData, the replacing of MSExperiment occurrences with MSAnnotatedMSRawData where identification data is needed, obliviously requires a considerable amount of refactoring.

I already started to do some of the refactoring but I would like to hear your thoughts and suggestions before proceeding further so that issues of any kind can be found early on and fixed easily.

Checklist:

  • Make sure that you are listed in the AUTHORS file
  • Add relevant changes and new features to the CHANGELOG file
  • I have commented my code, particularly in hard-to-understand areas
  • New and existing unit tests pass locally with my changes
  • Updated or added python bindings for changed or new classes. (Tick if no updates were necessary.)

How can I get additional information on failed tests during CI:

If your PR is failing you can check out

Note:

  • Once you opened a PR try to minimize the number of pushes to it as every push will trigger CI (automated builds and test) and is rather heavy on our infrastructure (e.g., if several pushes per day are performed).

Comment thread CMakeLists.txt
#------------------------------------------------------------------------------
SET(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer -O1")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO

private:
std::vector<std::vector<PeptideIdentification>> peptide_ids;
};
} No newline at end of file

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}
Suggested change
}
}

void LayerData::updateCache_()
{
if (peak_map_->getNrSpectra() > current_spectrum_idx_ && (*peak_map_)[current_spectrum_idx_].size() > 0)
if (peak_map_->getNrSpectra() > current_spectrum_idx_ && (*peak_map_)[current_spectrum_idx_].first.size() > 0)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm not sure about the .first .second syntax.

class PeptideIdentification;
class MSSpectrum;

class OPENMS_DLLAPI AnnotatedMSRawData : public MSExperiment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without inheritance might lead to a better design (but maybe some more changes)
e.g., one would need to write e.g. annot_ms_data.getMSExperiment()

~AnnotatedMSRawData() = default;

/// Get the peptide identifications for a single spectrum.
std::vector<PeptideIdentification>& getPeptideIds();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

size_t index probably missing

return PairIterator(spectra_.cend(), peptide_ids.cend());
}

inline Mapping operator[](size_t idx)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kind of don't like the accession with .first .second that much as one always needs to remember what first and second is.
what would be the advantage for the user when he mainly writes something like:
annot_ms.getExperiment()[idx] or annot_ms.getIdentification()[idx]
but rarely needs both

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same concern here.

From what I see, the code will usually not compile anyway even if using inheritance, e.g.
spectrum.getPeptideIdentifications(); will fail. So we need to rewrite the callers anyway, which means we can go composition.

/// Move constructor for efficiently loading a MSExperiment without a deep copy.
explicit AnnotatedMSRawData(MSExperiment&& experiment) : MSExperiment(std::move(experiment)) {};
AnnotatedMSRawData(AnnotatedMSRawData&&) = default;
~AnnotatedMSRawData() = default;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add (and test) move assignment

/// Get the peptide identifications for a single spectrum.
std::vector<PeptideIdentification>& getPeptideIds();
/// Get all peptide identifications for all spectra.
std::vector<std::vector<PeptideIdentification>>& getAllPeptideIds();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure about this function..
Its somewhat unclear how the indexing works (could be with gaps, i.e. the outer vector corresponds to the indices of ms spectra in the MSExperiment, or without gaps, i.e. just all PSMs, no empty inner vectors...)...

Also, it seems a bit dangerous to let the user modify this...
Maybe remove for now and see what use cases we have. Maybe some wrapper functions would be more appropriate...

@dvdvgt dvdvgt Oct 30, 2021 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also not sure how to handle indexing. I was thinking to use a 2d vector

std::vector<std::vector<PeptideIdentification>>

where, as you mentioned, the indices of spectra in the MSExperiment correspond to the indices of the outter vector. That means however that in order to add or edit peptide ids of a specific spectrum, you'd have to know the index. I can imagine that there are cases where the index is unknown. As of now peptide identification is stored within the spectra itself so indices are not used for storing these IDs, at least not directly. Also using indices feels a bit unsafe since accessing the correct spectrum cannot be guarantied by the interface itself but must be ensured by the user himself.

Maybe it would be better to use a std::map<MSSpectrum, std::vector<PeptideIdentification>> so accessing IDs is more straight forward. This would come at the disadvantage that upon the construction of AnnotatedMSRawData all spectra would need to be copied of into the map and the spectra vector in MSExperiment might no longer be the same as the keys in the map as things are added to the map. That means you'd have to prohibit accessing the MSExperiment::getSpectra function somehow.
Alternatively it might be possible to use a std::reference_wraper as key instead. This would complicate adding or removing spectra though because the map and actual vector of spectra always would need to be kept in sync.

I'm really unsure what correct way of implementing the mapping of spectra to peptide identification actually is. What are your thoughts on this? I'd appreciate your help on this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my take on what could work:
provide only std::vector - one PeptideIdentification (potentially empty) per spectrum
I think the vector of vector is not needed for all use cases we have (though the original design supported it)

std::vector<std::reference_wrapper<const PeptideHit>> all_hits;

for (const auto& spec : layer_->getPeakData()->getSpectra())
for (auto [spectra, peptide_ids] : *layer_->getPeakData())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is very nice to have!


#include <OpenMS/OpenMSConfig.h>
#include <OpenMS/KERNEL/MSExperiment.h>
#include <boost/range/combine.hpp>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you can get rid of boost, that would be very much appreciated :)

@cbielow cbielow left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice approach, see comments above.

I'd go for composition though, since its cleaner and it seems that rewriting the calling code is required for most places anyway.

Getting rid of the pair also seems a good idea, as Timo mentioned.
Maybe return a struct with named members.

@timosachsenberg

Copy link
Copy Markdown
Contributor

one note Julianus brought up.
It usually doesn't make sense to have multiple PeptideIdentifications per spectrum. Would it simplify the code if you would only assume one instead of a vector per spectrum?

dvdvgt added 2 commits November 6, 2021 10:53
…RawData"

Build target "OpenMS" is successfully compiling now.
@dvdvgt
dvdvgt force-pushed the feature/AnnotatedMSExperiment branch from dc2fbdf to 3bf651b Compare November 6, 2021 14:40
@timosachsenberg timosachsenberg added the major critical issues that are important to fix label Mar 22, 2023
@timosachsenberg

Copy link
Copy Markdown
Contributor

still important to get revived and merged at some point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

major critical issues that are important to fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants