Repository navigation
Refactoring FragmentIndex - #7232
timosachsenberg merged 8 commits into
Conversation
…OpenMS into feature/fi_search_engine # Conflicts: # src/openms/source/ANALYSIS/ID/FragmentIndex.cpp
…Scorer are now relocated to FragmentIndex. BuildMinHeap was replaced by std::partial_sort
|
|
||
| struct sPeptide | ||
| { | ||
| UInt32 protein_idx_; |
There was a problem hiding this comment.
adding doxygen comments like this would be of great help to others because it is such a central data structure.
e.g.
| UInt32 protein_idx_; | |
| UInt32 protein_idx_; ///< The index in the protein database |
| { | ||
| UInt32 protein_idx_; | ||
| UInt32 modification_idx; | ||
| std::pair<Size, Size> sequence; |
There was a problem hiding this comment.
Size can be rater large.
is this just the index in the protein?
then maybe something like
UInt16, UInt16 could safe some space
There was a problem hiding this comment.
Can I simply cast pair<size_t, size_t> to pair<UInt16, UInt16> ?
There was a problem hiding this comment.
not directly, on construction might work or in make_pair(...)
| struct SpectrumMatch | ||
| { | ||
| uint32_t num_matched_{}; ///< Number of peaks-fragment hits | ||
| uint32_t precursor_charge_{}; ///< The precursor_charged used for the performed search |
There was a problem hiding this comment.
could be uint16_t. if it is listed directly before or after with isotope_error we would safe another word (64bit)
|
|
||
| SpectrumMatchesTopN() = default; | ||
|
|
||
| SpectrumMatchesTopN& operator+=(const SpectrumMatchesTopN& other) |
There was a problem hiding this comment.
add brief doc as operators are always a bit unpredicatable
| * @param peak | ||
| * @param peptide_idx_range | ||
| * @param peak_charge | ||
| * @return |
There was a problem hiding this comment.
document what is returned
|
|
||
|
|
||
| /** | ||
| * @brief: queries one complete experimental spectra against the Database. loops over all precursor charges |
There was a problem hiding this comment.
document what "loops over all precursor charges" means here.
|
|
||
| /** | ||
| * @brief queries exactly one peak, with a set range of potential peptides, isotope error and precursor charge Hits are transfered into the a PSM | ||
| * @param candidates |
There was a problem hiding this comment.
I think there is a doxygen keyword to document that this is an "out" parameter
| * @param sms | ||
| * @param charge | ||
| */ | ||
| void closedSearch(const MSSpectrum& spectrum, float mz, SpectrumMatchesTopN& sms, uint16_t charge); |
There was a problem hiding this comment.
this function looks a bit redundant to the query peak one? can you clarify a bit in the doc why a separate one is needed? e.g., does the other one call this one?
| */ | ||
| void openSearch(const MSSpectrum& spectrum, float precursor_mass, SpectrumMatchesTopN& sms, uint16_t charge); | ||
|
|
||
| /** @brief places the k-largest elements in the front of the input array. Inside of the k-largest elements and outside the elements are not sorted |
There was a problem hiding this comment.
so this does not trim?
is there a reason (e.g., perfomance etc.) why no trimming is performed? if so please document or rename to something with partial sort in its name.
There was a problem hiding this comment.
It does trim, doesn't it? Anyway, I looked again at openSearch and closedSearch and I think it would be best to make one function out of it, instead of two
There was a problem hiding this comment.
Yes, I was confused because your comment talks about some implementation detail (the partial sort) that suggests that it is indeed not trimmed. If you want to document how this is done I would propose doing it in the cpp file.
A comment that hides this implementation detail could be something like: @brief keeps the top k candidates (according to number of matched fragments)
| @@ -150,7 +149,7 @@ namespace OpenMS | |||
| /** @brief places the k-largest elements in the front of the input array. Inside of the k-largest elements and outside the elements are not sorted | |||
| * Brazenly stolen from sage. | |||
There was a problem hiding this comment.
I would remove this "stolen" but at the same time more clearly state that the code is inspired and partially directly translated from the rust Sage search engine by Michael R. Lazear.
| void updateMembers_() override; | ||
|
|
||
| FragmentIndexScorer fragment_index_; | ||
| //FragmentIndex fragment_index_; |
There was a problem hiding this comment.
| //FragmentIndex fragment_index_; |
There was a problem hiding this comment.
note: if you want to accept suggestions don't click on "Commit suggestion" but "Add suggestion to batch". Otherwise we will run our CI for every suggested commit.
| generate_peptides(fasta_entries); | ||
|
|
||
| size_t peptide_idx = 0; | ||
| cout << "test2" <<endl; |
There was a problem hiding this comment.
remove test output before we forget ;)
| auto debugingStuff = fasta_entries[pep.protein_idx_].sequence; | ||
| AASequence unmod_peptide = AASequence::fromString(fasta_entries[pep.protein_idx_].sequence.substr(pep.sequence.first, pep.sequence.second)); | ||
| vector<AASequence> mod_peptides; | ||
| if(!(modifications_fixed_.empty() && modifications_variable_.empty())) |
There was a problem hiding this comment.
| if(!(modifications_fixed_.empty() && modifications_variable_.empty())) | |
| if (!(modifications_fixed_.empty() && modifications_variable_.empty())) |
| ModifiedPeptideGenerator::applyFixedModifications(fixed_modifications, mod_peptide); | ||
| ModifiedPeptideGenerator::applyVariableModifications(variable_modifications, mod_peptide, max_variable_mods_per_peptide_, mod_peptides); | ||
| tsg.getSpectrum(b_y_ions, mod_peptides[pep.modification_idx], 1,1); | ||
| }else{ |
There was a problem hiding this comment.
| }else{ | |
| } | |
| else | |
| { |
|
|
||
| init_hits.hits_.resize(max_processed_hits_); | ||
| } | ||
| for(auto hit_iter = init_hits.hits_.begin(); hit_iter != init_hits.hits_.end();){ |
There was a problem hiding this comment.
also add space after for and put { into new line. Just some style convention we have ...
| { | ||
| pair<float, float> precursor_window; | ||
| if (open_precursor_window == 0) | ||
| precursor_window = make_pair(-precursor_mass * 0.05, precursor_mass * 0.01); |
There was a problem hiding this comment.
add some documentation
|
|
||
| //Search-related params | ||
|
|
||
| defaults_.setValue("Min_matched_peaks", 5, "Minimal number of matched ions to report a PSM"); |
| defaults_.setValue("min_isotope_error", 0, "Precursor isotope error"); | ||
| defaults_.setValue("max_isotope_error", 0, "precursor isotope error"); | ||
| defaults_.setValue("min_precursor_charge", 1, "min precursor charge"); | ||
| defaults_.setValue("max_precursor_charge", 1, "max precursor charge"); |
There was a problem hiding this comment.
maybe use more common defaults for min / max charge? e.g. see CometAdapter.cpp in OpenMS or defaults of Sage
| defaults_.setValue("max_isotope_error", 0, "precursor isotope error"); | ||
| defaults_.setValue("min_precursor_charge", 1, "min precursor charge"); | ||
| defaults_.setValue("max_precursor_charge", 1, "max precursor charge"); | ||
| defaults_.setValue("max_fragment_charge", 1, "max fragment charge"); |
There was a problem hiding this comment.
if max_fragment_charge = 2 does the used charge depend on the actual precursor charge?
I think in general it is not clear if these parameters configure how the fragment index is built, what is related to the query and what is related to the peptide search.
| void FragmentIndexScorer::trimHits(OpenMS::FragmentIndexScorer::SpectrumMatchesTopN& init_hits) const | ||
| { | ||
| buildKMinHeap<SpectrumMatch, uint32_t>(init_hits.hits_, max_processed_hits_, [](SpectrumMatch a) { return a.num_matched_; }); | ||
| if (max_processed_hits_ < init_hits.hits_.size()) |
There was a problem hiding this comment.
| if (max_processed_hits_ < init_hits.hits_.size()) | |
| if (init_hits.hits_.size() > max_processed_hits_) |
| for (const sPeptide& pep: fi_peptides_) | ||
| { | ||
| tsg.getSpectrum(b_y_ions, pep.sequence, 1, 1); | ||
| auto debugingStuff = fasta_entries[pep.protein_idx_].sequence; |
| peptide_idx++; | ||
| b_y_ions.clear(true); | ||
| } | ||
| cout << "test3" <<endl; |
| if (max_processed_hits_ < init_hits.hits_.size()) | ||
| { | ||
| std::partial_sort(init_hits.hits_.begin(), init_hits.hits_.begin() + max_processed_hits_, init_hits.hits_.end(), [](SpectrumMatch a, SpectrumMatch b){ | ||
| return a.num_matched_ > b.num_matched_; |
There was a problem hiding this comment.
this likely leads to non-deterministic sorting on different platforms in case num_matched peaks are identical.
To make it deterministic one usually includes more fields in the comparison.
e.g., return std::tie(a.num_matched, a.otherfield ..., -1.0 * a.anotherfields ...) > std::tie(b.num_matched, b.otherfield ..., -1.0 * b.anotherfields ...)
| for(auto hit_iter = init_hits.hits_.begin(); hit_iter != init_hits.hits_.end();){ | ||
| if(hit_iter->num_matched_ < min_matched_peaks_) | ||
| { | ||
| init_hits.hits_.erase(hit_iter); |
There was a problem hiding this comment.
I think this code can be made much faster!!!
e.g. iterate from rbegin() (note: from the right) until we find the first entry that has hit_iter->num_matched_ >= min_matched_peaks_
and then just do a resize to include this position.
No erasing needed. Every erase needs to relocate all elements after the moved element.
| AASequence candidate = fragment_index_.getDB().getPeptides()[sms.peptide_idx_].sequence; | ||
|
|
||
| pair<size_t, size_t> candidate_snippet = fragment_index_.getPeptides()[sms.peptide_idx_].sequence; | ||
| AASequence candidate = AASequence::fromString(fasta_db[sms.peptide_idx_].sequence.substr(candidate_snippet.first, candidate_snippet.second)); |
There was a problem hiding this comment.
hmm is this correct if modifications are involved? now this is only the plain peptide right?
| for (int16_t isotope_error = min_isotope_error_; isotope_error <= max_isotope_error_; isotope_error++) | ||
| { | ||
| SpectrumMatchesTopN candidates_iso_error; | ||
| mz += (float)isotope_error * (float)Constants::NEUTRON_MASS; |
There was a problem hiding this comment.
looks wrong.
You are adding 0, 1, 2 etc to the m/z every time, but m/z is never reset.
Charge is neglected? Or is this uncharged mass? In which case mz is not the best name.
Also NEUTRON_MASS (you meant NEUTRON_MASS_U???) is probably better replaced with C13C12_MASSDIFF_U since isotope m/z's are dominated by carbon.
| /** | ||
| * @brief: queries one complete experimental spectra against the Database. loops over all precursor charges | ||
| * @param spectrum experimental spectrum | ||
| * @param sms output |
| /** | ||
| * @brief queries exactly one peak, with a set range of potential peptides, isotope error and precursor charge Hits are transfered into the a PSM | ||
| * @param candidates | ||
| * @param peak |
| * @brief queries all peaks of a spectrum. loops over all preset isotope errors. The precursor-mass-window is ignored | ||
| * @param spectrum experimental query-spectrum | ||
| * @param mz | ||
| * @param sms |
| /**@brief queries all peaks of a spectrum. ignores isotope error, applies open window | ||
| * @param spectrum | ||
| * @param precursor_mass | ||
| * @param sms |
| if(!(modifications_fixed_.empty() && modifications_variable_.empty())) | ||
| { | ||
| AASequence mod_peptide = AASequence(unmod_peptide); // copy the peptide | ||
| ModifiedPeptideGenerator::applyFixedModifications(fixed_modifications, mod_peptide); |
There was a problem hiding this comment.
Note: does not support protein terminal mods
| tsg_params.setValue("add_z_ions", add_z_ions_); | ||
| tsg.setParameters(tsg_params); // TODO: this was missing before!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
| auto this_params = getParameters(); | ||
| tsg_params.setValue("add_a_ions", this_params.getValue("add_a_ions")); |
There was a problem hiding this comment.
Could be done based on activation method instead.
Note: z-ions are currently wrong
There was a problem hiding this comment.
hmm good idea but I think one doesn't want to build the fragment index on the a-ions for HCD data. Maybe this part should stay user customizable?
| AASequence mod_peptide = AASequence(unmod_peptide); // copy the peptide | ||
| ModifiedPeptideGenerator::applyFixedModifications(fixed_modifications, mod_peptide); | ||
| ModifiedPeptideGenerator::applyVariableModifications(variable_modifications, mod_peptide, max_variable_mods_per_peptide_, mod_peptides); | ||
| tsg.getSpectrum(b_y_ions, mod_peptides[pep.modification_idx], 1,1); |
There was a problem hiding this comment.
Note: modifications that fragment (e.g. TMT) need to be removed before generating the spectrum!
There was a problem hiding this comment.
Is there a fast way to tell if a modification is removed during Fragmentation?
There was a problem hiding this comment.
not really. I think we could add some special option/extra flag to the search engine to support TMT
…dex3D and reformate structs. Fix query bug
There was a problem hiding this comment.
is this still used?
There was a problem hiding this comment.
I think this datastructure is a bit too specialized to be in the DATASTRUCTURES folder. it is mainly for the 3D FragmentIndex if I understood correctly.
Maybe make it part of the FragmentIndex3D as inner class? I think it has no use outside.
There was a problem hiding this comment.
maybe double check includes. (I did not but maybe it doesn't need #include <OpenMS/DATASTRUCTURES/MultiFragment.h> or QCBase etc.)
| digestor.setMissedCleavages(missed_cleavages_); | ||
|
|
||
| size_t protein_idx = 0; | ||
| // UInt32 protein_idx = 0; |
| } | ||
| #pragma omp atomic | ||
| protein_idx++; | ||
| //#pragma omp atomic |
There was a problem hiding this comment.
remove commented out code
| } | ||
| // sort the peptide vector, critical for following steps | ||
| sort(fi_peptides_.begin(), fi_peptides_.end(), [](const Peptide& a, const Peptide& b){ return a.precursor_mz < b.precursor_mz; }); | ||
| sort(fi_peptides_.begin(), fi_peptides_.end(), [](const Peptide& a, const Peptide& b){ |
There was a problem hiding this comment.
| sort(fi_peptides_.begin(), fi_peptides_.end(), [](const Peptide& a, const Peptide& b){ | |
| sort(fi_peptides_.begin(), fi_peptides_.end(), [](const Peptide& a, const Peptide& b) | |
| { |
| PeakSpectrum b_y_ions; | ||
|
|
||
| /// generate all Peptides | ||
| generate_peptides(fasta_entries); |
There was a problem hiding this comment.
rename to generatePeptides
|
|
||
|
|
||
| /// For each Peptides get all theoretical b and y ions // TODO: include other fragmentation methods | ||
| for (const Peptide& pep: fi_peptides_) |
There was a problem hiding this comment.
can probably also be parallelized using openmp (use SignedSize peptide_index)
| { | ||
| tsg.getSpectrum(b_y_ions, pep.sequence, 1, 1); | ||
| AASequence unmod_peptide = AASequence::fromString(fasta_entries[pep.protein_idx_].sequence.substr(pep.sequence.first, pep.sequence.second)); | ||
| vector<AASequence> mod_peptides; |
There was a problem hiding this comment.
With OpenMP: e.g., declare outside, capture as thread local (private) and only clear(). Prevents frequent reallocations.
| { | ||
| for (uint16_t fragment_charge = 1; fragment_charge <= max_fragment_charge_; fragment_charge++) | ||
| { | ||
| for (auto hit : query(peak, candidates_range, fragment_charge)) |
There was a problem hiding this comment.
| if (init_hits.hits_.size() > max_processed_hits_) | ||
| { | ||
| std::partial_sort(init_hits.hits_.begin(), init_hits.hits_.begin() + max_processed_hits_, init_hits.hits_.end(), [](const SpectrumMatch& a,const SpectrumMatch& b){ | ||
| if(a.num_matched_ != b.num_matched_) return a.num_matched_ > b.num_matched_; |
There was a problem hiding this comment.
| if(a.num_matched_ != b.num_matched_) return a.num_matched_ > b.num_matched_; | |
| if (a.num_matched_ != b.num_matched_) | |
| { | |
| return a.num_matched_ > b.num_matched_ ; | |
| } |
| } | ||
| for (auto hit_iter = init_hits.hits_.rbegin(); hit_iter != init_hits.hits_.rend(); hit_iter++) | ||
| { | ||
| if(hit_iter->num_matched_ >= min_matched_peaks_) // search for the first element |
There was a problem hiding this comment.
| if(hit_iter->num_matched_ >= min_matched_peaks_) // search for the first element | |
| if (hit_iter->num_matched_ >= min_matched_peaks_) // search for the first element |
| int16_t max_isotope_error_applied; | ||
| float precursor_window_upper_applied; | ||
| float precursor_window_lower_applied; | ||
| if(open_search){ |
There was a problem hiding this comment.
| if(open_search){ | |
| if (open_search) | |
| { |
| for (int16_t isotope_error = min_isotope_error_applied; isotope_error <= max_isotope_error_applied; isotope_error++) | ||
| { | ||
| SpectrumMatchesTopN candidates_iso_error; | ||
| precursor_mass += (float)isotope_error * (float)Constants::C13C12_MASSDIFF_U; // TODO: Potentially still wrong |
There was a problem hiding this comment.
| precursor_mass += (float)isotope_error * (float)Constants::C13C12_MASSDIFF_U; // TODO: Potentially still wrong | |
| precursor_mass += (float)isotope_error * (float)Constants::C13C12_MASSDIFF_U; // TODO: Potentially still wrong |
| fi_peptides_.push_back({modified_peptide, protein_idx, modified_mz}); | ||
| } | ||
| { | ||
| fi_peptides_.push_back({static_cast<UInt32>(i), |
There was a problem hiding this comment.
Would emplace back make sense here for in place construction?
There was a problem hiding this comment.
yes.
@Trapho see
push_back vs. emplace_back
https://quick-bench.com/q/TzNTiv1qc6J8qaAwDdzWnnANl1Q
But this speed advantage only materializes if the most important optimization is done: reserving enough memory such that no reallocations happen .reserve()
https://quick-bench.com/q/Jx19hclilK1VUZz67bX8-eoPg3Q
There was a problem hiding this comment.
Cool benchmark but I think you just need to increase the number of items a bit to show the benefit with emplace_back (even without reserving).
Instead I would actually propose to benchmark the reserving strategy (if inside a loop) which could be worse than the STLs default ;)
There was a problem hiding this comment.
@timosachsenberg For fi_peptides_.reserve() something like the (size of the fasta_enties) * (average number of cleavage sites) ?
There was a problem hiding this comment.
I think this is a sensible choice. Will already prevent many reallocations. Hard to really estimate the exact numbers of unique peptides if also missed cleavages are allowed etc.
|
|
||
| #pragma omp critical(FIIndex) | ||
| { | ||
| fi_peptides_.push_back({static_cast<UInt32>(i), unmodified_mz}); |
There was a problem hiding this comment.
Emplace back?
You may need to implement a constructor for this.
| @@ -119,68 +116,92 @@ namespace OpenMS | |||
| continue; | |||
There was a problem hiding this comment.
This check could be done in ModPepGen at some point. Especially if only positive mods are considered you can stop quite early or discard the peptide from the beginning.
There was a problem hiding this comment.
Also if you merge #6859 you will not need to recalculate redundant masses for peptides with the same amount of modifications again.
Mass thresholds could be included in that PR.
- Replaced all push_back with emplace_back and added reserves
- build now parallelized; - added tests
| precursor_mz_(precursor_mz) | ||
| {} | ||
|
|
||
| UInt32 protein_idx_; ///< The index in fasta entries vector |
There was a problem hiding this comment.
note: underscore at the end means: private variable. here it is public
| struct SpectrumMatch | ||
| { | ||
| uint32_t num_matched_{}; ///< Number of peaks-fragment hits | ||
| uint32_t num_matched_{}; ///< Number of peaks-fragment hits |
There was a problem hiding this comment.
samre here... no underscore
| mod_peptide = mod_peptides[pep.modification_idx_]; | ||
| tsg.getSpectrum(b_y_ions, mod_peptide, charge, charge); | ||
| prec_theo.setMZ(mod_peptide.getMZ(charge)); | ||
| spec_theo.setMSLevel(2); |
There was a problem hiding this comment.
precursor charge not set here? not important?
There was a problem hiding this comment.
I wanted to test the scenario, in which the precursor charge is not known. In this case all different precursor charges are tested during the query (from min_precursor_charge to max_precursor_charge_).
Description
Changed the Format of the struct Peptide (called sPeptide for the beginning, in case something is wrong). During Fragment Creation modification are now recreated. All functions from FragmentIndexScorer are now in FragmentIndex.
The BuildMinHeap function is replaced by Partial-sort.
FragmentIndex3D.h and FragmentIndex3D.cpp is a adapted version of FragmentIndex, which can be used for Top-Down Proteomics. This current version is not finished yet.
Checklist
How can I get additional information on failed tests during CI
Click to expand
If your PR is failing you can check outIf you click in the column that lists the failed tests you will get detailed error messages.
Advanced commands (admins / reviewer only)
Click to expand
/reformat(experimental) applies the clang-format style changes as additional commit. Note: your branch must have a different name (e.g., yourrepo:feature/XYZ) than the receiving branch (e.g., OpenMS:develop). Otherwise, reformat fails to push.rebuild jenkinswill retrigger Jenkins-based CI builds