use of de.ipbhalle.metfraglib.fragment.HDTopDownBitArrayFragmentWrapper in project MetFragRelaunched by ipb-halle.
the class HDTopDownFragmenterAssignerScorer method calculate.
@Override
@SuppressWarnings("unchecked")
public void calculate() {
HDTopDownBitArrayPrecursor candidatePrecursor = (HDTopDownBitArrayPrecursor) (this.candidates[0]).getPrecursorMolecule();
// one native candidate can have multiple deuterated precursors
int deuteratedCandidateNumber = candidatePrecursor.getNumberDeuteratedCombinations();
/*
* generate root fragment
*/
AbstractTopDownBitArrayFragment root = candidatePrecursor.toFragment();
Byte maximumTreeDepth = (Byte) settings.get(VariableNames.MAXIMUM_TREE_DEPTH_NAME);
if (maximumTreeDepth == 0) {
maximumTreeDepth = candidatePrecursor.getNumNodeDegreeOne() >= 4 ? (byte) 3 : (byte) 2;
}
this.candidates[0].setProperty(VariableNames.MAXIMUM_TREE_DEPTH_NAME, maximumTreeDepth);
// native spectrum
SortedTandemMassPeakList tandemMassPeakList = (SortedTandemMassPeakList) settings.get(VariableNames.PEAK_LIST_NAME);
tandemMassPeakList.initialiseMassLimits((Double) settings.get(VariableNames.RELATIVE_MASS_DEVIATION_NAME), (Double) settings.get(VariableNames.ABSOLUTE_MASS_DEVIATION_NAME));
// hd spectrum
SortedTandemMassPeakList tandemMassPeakListHD = (SortedTandemMassPeakList) settings.get(VariableNames.HD_PEAK_LIST_NAME);
tandemMassPeakListHD.initialiseMassLimits((Double) settings.get(VariableNames.RELATIVE_MASS_DEVIATION_NAME), (Double) settings.get(VariableNames.ABSOLUTE_MASS_DEVIATION_NAME));
// native ion type
Integer precursorIonType = (Integer) settings.get(VariableNames.PRECURSOR_ION_MODE_NAME);
this.positiveMode = (Boolean) settings.get(VariableNames.IS_POSITIVE_ION_MODE_NAME);
this.precursorIonTypeIndex = Constants.ADDUCT_NOMINAL_MASSES.indexOf(precursorIonType);
// hd ion type
Integer precursorIonTypeHD = (Integer) settings.get(VariableNames.HD_PRECURSOR_ION_MODE_NAME);
this.precursorIonTypeIndexHD = Constants.ADDUCT_NOMINAL_MASSES.indexOf(precursorIonTypeHD);
// set the minimum mass limit for fragment generation
double minimumMassNative = tandemMassPeakList.getMinimumMassValue();
double minimumMassHD = tandemMassPeakListHD.getMinimumMassValue();
double minimumMass = minimumMassHD < minimumMassNative ? minimumMassHD : minimumMassNative;
this.fragmenter.setMinimumFragmentMassLimit(minimumMass - Math.max(Constants.ADDUCT_MASSES.get(precursorIonTypeIndex), Constants.ADDUCT_MASSES.get(precursorIonTypeIndexHD)));
/*
* prepare the processing
*/
java.util.Queue<HDTopDownBitArrayFragmentWrapper> toProcessFragments = new java.util.LinkedList<HDTopDownBitArrayFragmentWrapper>();
/*
* wrap the root fragment
*/
int[] currentPeakIndexPointerArrayHD = new int[deuteratedCandidateNumber];
for (int i = 0; i < currentPeakIndexPointerArrayHD.length; i++) currentPeakIndexPointerArrayHD[i] = tandemMassPeakListHD.getNumberElements() - 1;
HDTopDownBitArrayFragmentWrapper rootFragmentWrapper = new HDTopDownBitArrayFragmentWrapper(root, tandemMassPeakList.getNumberElements() - 1, currentPeakIndexPointerArrayHD);
toProcessFragments.add(rootFragmentWrapper);
/*
* define the match lists
*/
java.util.HashMap<Integer, MatchFragmentList> peakIndexToPeakMatch = new java.util.HashMap<Integer, MatchFragmentList>();
java.util.HashMap<Integer, MatchPeakList> fragmentIndexToPeakMatch = new java.util.HashMap<Integer, MatchPeakList>();
java.util.HashMap<Integer, MatchFragmentList>[] peakIndexToPeakMatchHD = new java.util.HashMap[deuteratedCandidateNumber];
java.util.HashMap<Integer, MatchPeakList>[] fragmentIndexToPeakMatchHD = new java.util.HashMap[deuteratedCandidateNumber];
for (int i = 0; i < deuteratedCandidateNumber; i++) {
peakIndexToPeakMatchHD[i] = new java.util.HashMap<Integer, MatchFragmentList>();
fragmentIndexToPeakMatchHD[i] = new java.util.HashMap<Integer, MatchPeakList>();
}
/*
* iterate over the maximal allowed tree depth
*/
for (int k = 1; k <= maximumTreeDepth; k++) {
java.util.Queue<HDTopDownBitArrayFragmentWrapper> newToProcessFragments = new java.util.LinkedList<HDTopDownBitArrayFragmentWrapper>();
/*
* use each fragment that is marked as to be processed
*/
while (!toProcessFragments.isEmpty()) {
/*
* generate fragments of new tree depth
*/
HDTopDownBitArrayFragmentWrapper wrappedPrecursorFragment = toProcessFragments.poll();
if (wrappedPrecursorFragment.getWrappedFragment().isDiscardedForFragmentation()) {
AbstractTopDownBitArrayFragment clonedFragment = (AbstractTopDownBitArrayFragment) wrappedPrecursorFragment.getWrappedFragment().clone(this.candidates[0].getPrecursorMolecule());
clonedFragment.setAsDiscardedForFragmentation();
newToProcessFragments.add(new HDTopDownBitArrayFragmentWrapper(clonedFragment, wrappedPrecursorFragment.getCurrentPeakIndexPointer(), wrappedPrecursorFragment.getCurrentPeakIndexPointerHD()));
continue;
}
/*
* generate fragments of next tree depth
*/
java.util.ArrayList<AbstractTopDownBitArrayFragment> fragmentsOfCurrentTreeDepth = this.fragmenter.getFragmentsOfNextTreeDepth(wrappedPrecursorFragment.getWrappedFragment());
/*
* get peak pointer of current precursor fragment
*/
int currentPeakPointer = wrappedPrecursorFragment.getCurrentPeakIndexPointer();
int[] currentPeakPointerHD = wrappedPrecursorFragment.getCurrentPeakIndexPointerArrayHD();
/*
* start loop over all child fragments from precursor fragment
* to try assigning them to the current peak
*/
for (int l = 0; l < fragmentsOfCurrentTreeDepth.size(); l++) {
AbstractTopDownBitArrayFragment currentFragment = fragmentsOfCurrentTreeDepth.get(l);
HDTopDownBitArrayFragmentWrapper newFragmentWrapper = new HDTopDownBitArrayFragmentWrapper(currentFragment, currentPeakPointer, currentPeakPointerHD.clone());
if (!fragmentsOfCurrentTreeDepth.get(l).isValidFragment()) {
newToProcessFragments.add(newFragmentWrapper);
continue;
}
/*
* needs to be set
* otherwise you get fragments generated by multiple cleavage in one chain
*/
if (this.wasAlreadyGeneratedByHashtable(currentFragment)) {
currentFragment.setAsDiscardedForFragmentation();
newToProcessFragments.add(newFragmentWrapper);
continue;
}
int tempPeakPointer = currentPeakPointer;
this.matchFragment(tempPeakPointer, newFragmentWrapper, tandemMassPeakList, peakIndexToPeakMatch, fragmentIndexToPeakMatch);
// run over all deuterated combinations
for (int d = 0; d < deuteratedCandidateNumber; d++) {
newFragmentWrapper.setPrecursorIndex(d);
int tempPeakPointerHD = newFragmentWrapper.getCurrentPeakIndexPointerHD();
this.matchFragmentHD(tempPeakPointerHD, newFragmentWrapper, tandemMassPeakListHD, peakIndexToPeakMatchHD[d], fragmentIndexToPeakMatchHD[d], d);
}
newToProcessFragments.add(newFragmentWrapper);
}
}
toProcessFragments = newToProcessFragments;
}
this.matchList = new MatchList();
/*
* collect score of all scores over all matches
*/
double[][] singleScores = new double[this.scoreCollection.getNumberScores()][peakIndexToPeakMatch.size()];
java.util.ArrayList<double[][]> singleScoresHD = new java.util.ArrayList<double[][]>();
for (int d = 0; d < deuteratedCandidateNumber; d++) singleScoresHD.add(new double[this.scoreCollection.getNumberScores()][peakIndexToPeakMatchHD[d].size()]);
/*
* collect the sum of all scores over all matches
*/
double[] summedScores = new double[this.scoreCollection.getNumberScores()];
double[][] summedScoresHD = new double[deuteratedCandidateNumber][this.scoreCollection.getNumberScores()];
ICandidate[] deuteratedCandidates = new ICandidate[deuteratedCandidateNumber];
deuteratedCandidates[0] = this.candidates[0];
for (int i = 1; i < deuteratedCandidateNumber; i++) {
deuteratedCandidates[i] = new TopDownPrecursorCandidate(this.candidates[0].getInChI(), this.candidates[0].getIdentifier());
deuteratedCandidates[i].setPrecursorMolecule(candidatePrecursor);
}
this.calculateFragmenterScores(peakIndexToPeakMatch, singleScores, summedScores);
for (int d = 0; d < deuteratedCandidateNumber; d++) {
this.calculateFragmenterScoresHD(peakIndexToPeakMatchHD[d], singleScoresHD.get(d), summedScoresHD[d], deuteratedCandidates[d], d);
}
this.settings.set(VariableNames.PEAK_INDEX_TO_PEAK_MATCH_NAME, peakIndexToPeakMatch);
this.settings.set(VariableNames.HD_PEAK_INDEX_TO_PEAK_MATCH_NAME, peakIndexToPeakMatchHD[0]);
this.settings.set(VariableNames.CANDIDATE_NAME, this.candidates[0]);
this.candidates[0].setMatchList(this.matchList);
if (this.scoreCollection == null)
return;
try {
for (int i = 0; i < this.scoreCollection.getNumberScores(); i++) {
if (!this.scoreCollection.getScore(i).calculationFinished()) {
this.scoreCollection.getScore(i).calculate();
} else {
this.scoreCollection.getScore(i).setValue(summedScores[i] + summedScoresHD[0][i]);
}
if (singleScores[i].length != 0 && this.scoreCollection.getScore(i).hasInterimResults() && !this.scoreCollection.getScore(i).isInterimResultsCalculated()) {
this.scoreCollection.getScore(i).setOptimalValues(singleScores[i]);
}
}
// set score values directly for the first candidate only
String[] score_types = (String[]) this.settings.get(VariableNames.METFRAG_SCORE_TYPES_NAME);
for (int i = 0; i < score_types.length; i++) {
if (scoreCollection.getScore(i).getValue() != null) {
if (!scoreCollection.getScore(i).isUserDefinedPropertyScore()) {
this.candidates[0].setProperty(score_types[i], scoreCollection.getScore(i).getValue());
}
if (scoreCollection.getScore(i).hasInterimResults()) {
this.candidates[0].setProperty(score_types[i] + "_Values", scoreCollection.getScore(i).getOptimalValuesToString());
}
}
}
// set scores for additional deuterated candidates
for (int d = 1; d < deuteratedCandidateNumber; d++) {
// firstly set the HDPeakIndexToPeakMatch needed for HDFragmentPairScore
this.settings.set(VariableNames.HD_PEAK_INDEX_TO_PEAK_MATCH_NAME, peakIndexToPeakMatchHD[d]);
for (int i = 0; i < score_types.length; i++) {
if (this.scoreCollection.getScore(i).getValue() != null) {
if (this.scoreCollection.getScore(i).isUsesPiecewiseCalculation()) {
deuteratedCandidates[d].setProperty(score_types[i], summedScores[i] + summedScoresHD[d][i]);
} else if (this.scoreCollection.getScore(i).calculationFinished()) {
deuteratedCandidates[d].setProperty(score_types[i], this.scoreCollection.getScore(i).getValue());
} else if (!this.scoreCollection.getScore(i).calculationFinished()) {
this.scoreCollection.getScore(i).calculate();
deuteratedCandidates[d].setProperty(score_types[i], this.scoreCollection.getScore(i).getValue());
}
}
}
}
this.settings.remove(VariableNames.PEAK_INDEX_TO_PEAK_MATCH_NAME);
this.settings.remove(VariableNames.HD_PEAK_INDEX_TO_PEAK_MATCH_NAME);
this.candidates = deuteratedCandidates;
for (int d = 0; d < deuteratedCandidateNumber; d++) this.candidates[d].setProperty(VariableNames.HD_GROUP_FLAG_NAME, this.candidates[0].getIdentifier());
} catch (Exception e) {
e.printStackTrace();
logger.warn("candidate score calculation interrupted");
return;
}
this.candidates[0].setProperty(VariableNames.HD_NUMBER_PEAKS_USED_COLUMN, tandemMassPeakListHD.getNumberPeaksUsed());
}
Aggregations