use of de.ipbhalle.metfraglib.substructure.FingerprintGroup in project MetFragRelaunched by ipb-halle.
the class AutomatedLossFingerprintAnnotationScoreInitialiser method initScoreParameters.
@Override
public void initScoreParameters(Settings settings) throws Exception {
if (!settings.containsKey(VariableNames.LOSS_TO_FINGERPRINT_GROUP_LIST_COLLECTION_NAME) || settings.get(VariableNames.LOSS_TO_FINGERPRINT_GROUP_LIST_COLLECTION_NAME) == null) {
MassToFingerprintGroupListCollection lossToFingerprintGroupListCollection = new MassToFingerprintGroupListCollection();
DefaultPeakList peakList = (DefaultPeakList) settings.get(VariableNames.PEAK_LIST_NAME);
Double mzppm = (Double) settings.get(VariableNames.RELATIVE_MASS_DEVIATION_NAME);
Double mzabs = (Double) settings.get(VariableNames.ABSOLUTE_MASS_DEVIATION_NAME);
BufferedReader breader = null;
java.io.InputStream is = null;
if (settings.containsKey(VariableNames.FINGERPRINT_LOSS_ANNOTATION_FILE_NAME) && settings.get(VariableNames.FINGERPRINT_LOSS_ANNOTATION_FILE_NAME) != null) {
breader = new BufferedReader(new FileReader(new File((String) settings.get(VariableNames.FINGERPRINT_LOSS_ANNOTATION_FILE_NAME))));
} else {
String filename = "loss_annotations_neg.txt";
if ((Boolean) settings.get(VariableNames.IS_POSITIVE_ION_MODE_NAME))
filename = "loss_annotations_pos.txt";
is = AutomatedPeakFingerprintAnnotationScoreInitialiser.class.getResourceAsStream("/" + filename);
breader = new java.io.BufferedReader(new java.io.InputStreamReader(is));
}
Double neutralPrecursorMass = (Double) settings.get(VariableNames.PRECURSOR_NEUTRAL_MASS_NAME);
Double adductMass = Constants.getIonisationTypeMassCorrection(Constants.ADDUCT_NOMINAL_MASSES.indexOf((Integer) settings.get(VariableNames.PRECURSOR_ION_MODE_NAME)), (Boolean) settings.get(VariableNames.IS_POSITIVE_ION_MODE_NAME));
java.util.ArrayList<Double> massDifferences = this.calculatePeakDifferences(peakList, neutralPrecursorMass, adductMass);
java.util.ArrayList<Double> uniqueMassDifferences = this.calculateUniquePeakDifferences(massDifferences, mzppm, mzabs);
java.util.LinkedList<Double> lossMassesFound = new java.util.LinkedList<Double>();
String line = "";
int numMatchedObservationsMerged = 0;
java.util.HashMap<Double, MassToFingerprintGroupList> mergedFingerprintGroupLists = new java.util.HashMap<Double, MassToFingerprintGroupList>();
this.setPseudoCountValues(settings);
// first add non-matched masses with dummy fingerprint "0"
// these masses are present in the first line of the annotation file: mass[:counts]
String nonMatchedMassesString = breader.readLine().trim();
int numNonMatchElements = 0;
int numNonMatchOccurrences = 0;
if (!nonMatchedMassesString.equals("NA")) {
// masses are separated by ";"
String[] tmp = nonMatchedMassesString.split(";");
numNonMatchElements = tmp.length;
for (int k = 0; k < tmp.length; k++) {
// run over all masses
// split by ":" to separate mass[:counts]
String[] tmp2 = tmp[k].split("\\s+");
int count = 1;
// create mass value
Double newMass = Double.parseDouble(tmp2[0]);
// if count is present use count else use 1 (default)
if (tmp2.length == 2)
count = Integer.parseInt(tmp2[1]);
// save number non-matched occurences
numNonMatchOccurrences += count;
// check whether the newMass is also present in our found peak list losses
Double matchedMass = this.containsMass(newMass, uniqueMassDifferences, mzabs, mzppm);
// if not present and already larger than largest peak mass stop here
if (matchedMass == null && newMass > peakList.getMaximumMassValue())
break;
if (matchedMass != null) {
// if loss is present in our peak list add it to the annotation list
// prepare new element
FingerprintGroup group = new FingerprintGroup(1.0);
group.setNumberObserved(count);
group.setFingerprint("0");
if (mergedFingerprintGroupLists.containsKey(matchedMass)) {
// check if the mass was already inserted
MassToFingerprintGroupList currentGroupList = mergedFingerprintGroupLists.get(matchedMass);
FingerprintGroup curGroup = currentGroupList.getElementByFingerprint(group.getFingerprint());
// check if fingerprint was already inserted
if (// if not simply add it
curGroup == null)
// if not simply add it
currentGroupList.addElement(group);
else {
// if already present decrease number observed elements (as already observed)
numNonMatchElements--;
// adapt values
curGroup.setNumberObserved(curGroup.getNumberObserved() + group.getNumberObserved());
curGroup.setProbability(curGroup.getProbability() + group.getProbability());
}
} else {
// if mass not yet present simply add it
MassToFingerprintGroupList currentGroupList = new MassToFingerprintGroupList(matchedMass);
currentGroupList.addElement(group);
mergedFingerprintGroupLists.put(matchedMass, currentGroupList);
}
}
}
}
// now add loss-fingerprint assignments which were annotated in the training
while ((line = breader.readLine()) != null) {
line = line.trim();
if (line.length() == 0)
continue;
if (line.startsWith("#"))
continue;
if (line.startsWith("SUMMARY")) {
String[] tmp = line.split("\\s+");
// sum overall occurrences
settings.set(VariableNames.LOSS_FINGERPRINT_DENOMINATOR_COUNT_NAME, Double.parseDouble(tmp[2]) + numNonMatchOccurrences);
// number different peak pairs matched
settings.set(VariableNames.LOSS_FINGERPRINT_MATCHED_TUPLE_COUNT_NAME, Double.parseDouble(tmp[1]) - numMatchedObservationsMerged);
// number different peak pairs non-matched
settings.set(VariableNames.LOSS_FINGERPRINT_NON_MATCHED_TUPLE_COUNT_NAME, (double) numNonMatchElements);
continue;
}
String[] tmp = line.split("\\s+");
Double loss = Double.parseDouble(tmp[0]);
// check whether the current loss in our annotation is also present in the peak list
Double matchedMass = this.containsMass(loss, uniqueMassDifferences, mzabs, mzppm);
if (matchedMass != null) {
// if yes we need to consider it
// create fingerprint groups from annotation entry
FingerprintGroup[] groups = this.getFingerprintGroup(tmp);
if (mergedFingerprintGroupLists.containsKey(matchedMass)) {
// check whether mass is already present
MassToFingerprintGroupList currentGroupList = mergedFingerprintGroupLists.get(matchedMass);
for (int i = 0; i < groups.length; i++) {
// check if fingerprint is already inserted
FingerprintGroup curGroup = currentGroupList.getElementByFingerprint(groups[i].getFingerprint());
if (// if not simply add it
curGroup == null)
// if not simply add it
currentGroupList.addElement(groups[i]);
else {
// otherwise increase the number of matched observations to adapt number of unique tupels
if (curGroup.getFingerprint().getSize() != 1)
numMatchedObservationsMerged++;
// adapt loss-fingerprint assignment values
curGroup.setNumberObserved(curGroup.getNumberObserved() + groups[i].getNumberObserved());
curGroup.setProbability(curGroup.getProbability() + groups[i].getProbability());
}
}
} else {
// if mass not yet present simply add it
MassToFingerprintGroupList currentGroupList = new MassToFingerprintGroupList(matchedMass);
for (int i = 0; i < groups.length; i++) currentGroupList.addElement(groups[i]);
mergedFingerprintGroupLists.put(matchedMass, currentGroupList);
}
}
}
java.util.Iterator<Double> it = mergedFingerprintGroupLists.keySet().iterator();
while (it.hasNext()) {
lossToFingerprintGroupListCollection.addElementSorted(mergedFingerprintGroupLists.get(it.next()));
}
// store all mass differences (losses) found in the peak list
for (int i = 0; i < massDifferences.size(); i++) {
if (lossToFingerprintGroupListCollection.getElementByPeak(massDifferences.get(i), mzppm, mzabs) != null)
lossMassesFound.add(massDifferences.get(i));
}
breader.close();
settings.set(VariableNames.LOSS_MASSES_FOUND_PEAKLIST_NAME, lossMassesFound);
settings.set(VariableNames.LOSS_TO_FINGERPRINT_GROUP_LIST_COLLECTION_NAME, lossToFingerprintGroupListCollection);
}
}
use of de.ipbhalle.metfraglib.substructure.FingerprintGroup in project MetFragRelaunched by ipb-halle.
the class AutomatedLossFingerprintAnnotationScoreInitialiser method getFingerprintGroup.
protected FingerprintGroup[] getFingerprintGroup(String[] splittedLine) {
FingerprintGroup[] groups = new FingerprintGroup[(splittedLine.length - 1) / 2];
int index = 0;
for (int i = 1; i < splittedLine.length; i += 2) {
double count = Double.parseDouble(splittedLine[i]);
FingerprintGroup newGroup = new FingerprintGroup(count);
newGroup.setNumberObserved((int) count);
newGroup.setFingerprint(splittedLine[i + 1]);
groups[index] = newGroup;
index++;
}
return groups;
}
use of de.ipbhalle.metfraglib.substructure.FingerprintGroup in project MetFragRelaunched by ipb-halle.
the class AutomatedPeakFingerprintAnnotationScore method singlePostCalculate.
@Override
public void singlePostCalculate() {
this.value = 0.0;
MassToFingerprintGroupListCollection peakToFingerprintGroupListCollection = (MassToFingerprintGroupListCollection) this.settings.get(VariableNames.PEAK_TO_FINGERPRINT_GROUP_LIST_COLLECTION_NAME);
java.util.ArrayList<?> peakMatchList = (java.util.ArrayList<?>) this.candidate.getProperty("PeakMatchList");
int matches = 0;
// get foreground fingerprint observations (m_f_observed)
java.util.ArrayList<Double> matchMasses = new java.util.ArrayList<Double>();
java.util.ArrayList<Double> matchProb = new java.util.ArrayList<Double>();
// found - 1; non-found - 2 (fp="0"); alpha - 3; beta - 4
java.util.ArrayList<Integer> matchType = new java.util.ArrayList<Integer>();
// get foreground fingerprint observations (m_f_observed)
for (int i = 0; i < peakToFingerprintGroupListCollection.getNumberElements(); i++) {
// get f_m_observed
MassToFingerprintGroupList peakToFingerprintGroupList = peakToFingerprintGroupListCollection.getElement(i);
Double currentMass = peakToFingerprintGroupList.getPeakmz();
MassFingerprintMatch currentMatch = getMatchByMass(peakMatchList, currentMass);
if (currentMatch == null) {
FingerprintGroup fg = peakToFingerprintGroupList.getElementByFingerprint(new FastBitArray("0"));
if (fg == null) {
matchProb.add(peakToFingerprintGroupList.getBetaProb());
matchType.add(4);
this.value += Math.log(peakToFingerprintGroupList.getBetaProb());
} else {
matchProb.add(fg.getProbability());
matchType.add(2);
this.value += Math.log(fg.getProbability());
}
matchMasses.add(currentMass);
} else {
FastBitArray currentFingerprint = new FastBitArray(currentMatch.getFingerprint());
// ToDo: at this stage try to check all fragments not only the best one
// (p(m,f) + alpha) / sum_F(p(m,f)) + |F| * alpha
double matching_prob = peakToFingerprintGroupList.getMatchingProbability(currentFingerprint);
// |F|
if (matching_prob != 0.0) {
this.value += Math.log(matching_prob);
matchProb.add(matching_prob);
matchMasses.add(currentMass);
if (currentFingerprint.getSize() != 1) {
matches++;
matchType.add(1);
} else
matchType.add(2);
} else {
if (currentFingerprint.equals(new FastBitArray("0")) && peakToFingerprintGroupList.getElementByFingerprint(currentFingerprint) == null) {
matchType.add(4);
matchProb.add(peakToFingerprintGroupList.getBetaProb());
this.value += Math.log(peakToFingerprintGroupList.getBetaProb());
} else {
this.value += Math.log(peakToFingerprintGroupList.getAlphaProb());
matchProb.add(peakToFingerprintGroupList.getAlphaProb());
matchType.add(3);
}
matchMasses.add(currentMass);
}
}
}
if (peakToFingerprintGroupListCollection.getNumberElements() == 0)
this.value = 0.0;
this.candidate.setProperty("AutomatedPeakFingerprintAnnotationScore_Matches", matches);
this.candidate.setProperty("AutomatedPeakFingerprintAnnotationScore", this.value);
this.candidate.setProperty("AutomatedPeakFingerprintAnnotationScore_Probtypes", this.getProbTypeString(matchProb, matchType, matchMasses));
this.candidate.removeProperty("PeakMatchList");
}
use of de.ipbhalle.metfraglib.substructure.FingerprintGroup in project MetFragRelaunched by ipb-halle.
the class AutomatedPeakFingerprintAnnotationScoreInitialiser method getFingerprintGroup.
protected FingerprintGroup[] getFingerprintGroup(String[] splittedLine) {
FingerprintGroup[] groups = new FingerprintGroup[(splittedLine.length - 1) / 2];
int index = 0;
for (int i = 1; i < splittedLine.length; i += 2) {
double count = Double.parseDouble(splittedLine[i]);
FingerprintGroup newGroup = new FingerprintGroup(count);
newGroup.setNumberObserved((int) count);
newGroup.setFingerprint(splittedLine[i + 1]);
groups[index] = newGroup;
index++;
}
return groups;
}
use of de.ipbhalle.metfraglib.substructure.FingerprintGroup in project MetFragRelaunched by ipb-halle.
the class AutomatedPeakFingerprintAnnotationScoreInitialiser method initScoreParameters.
@Override
public void initScoreParameters(Settings settings) throws Exception {
if (!settings.containsKey(VariableNames.PEAK_TO_FINGERPRINT_GROUP_LIST_COLLECTION_NAME) || settings.get(VariableNames.PEAK_TO_FINGERPRINT_GROUP_LIST_COLLECTION_NAME) == null) {
MassToFingerprintGroupListCollection peakToFingerprintGroupListCollection = new MassToFingerprintGroupListCollection();
DefaultPeakList peakList = (DefaultPeakList) settings.get(VariableNames.PEAK_LIST_NAME);
Double mzppm = (Double) settings.get(VariableNames.RELATIVE_MASS_DEVIATION_NAME);
Double mzabs = (Double) settings.get(VariableNames.ABSOLUTE_MASS_DEVIATION_NAME);
BufferedReader breader = null;
java.io.InputStream is = null;
if (settings.containsKey(VariableNames.FINGERPRINT_PEAK_ANNOTATION_FILE_NAME) && settings.get(VariableNames.FINGERPRINT_PEAK_ANNOTATION_FILE_NAME) != null) {
breader = new BufferedReader(new FileReader(new File((String) settings.get(VariableNames.FINGERPRINT_PEAK_ANNOTATION_FILE_NAME))));
} else {
String filename = "peak_annotations_neg.txt";
if ((Boolean) settings.get(VariableNames.IS_POSITIVE_ION_MODE_NAME))
filename = "peak_annotations_pos.txt";
is = AutomatedPeakFingerprintAnnotationScoreInitialiser.class.getResourceAsStream("/" + filename);
breader = new java.io.BufferedReader(new java.io.InputStreamReader(is));
}
String line = "";
int numMatchedObservationsMerged = 0;
java.util.HashMap<Double, MassToFingerprintGroupList> mergedFingerprintGroupLists = new java.util.HashMap<Double, MassToFingerprintGroupList>();
this.setPseudoCountValues(settings);
String nonMatchedMassesString = breader.readLine().trim();
int numNonMatchElements = 0;
int numNonMatchOccurrences = 0;
if (!nonMatchedMassesString.equals("NA")) {
String[] tmp = nonMatchedMassesString.split(";");
numNonMatchElements = tmp.length;
for (int k = 0; k < tmp.length; k++) {
String[] tmp2 = tmp[k].split(":");
int count = 1;
Double newMass = Double.parseDouble(tmp2[0]);
if (tmp2.length == 2)
count = Integer.parseInt(tmp2[1]);
numNonMatchOccurrences += count;
Double matchedMass = peakList.getBestMatchingMass(newMass, mzppm, mzabs);
if (matchedMass == null && newMass > peakList.getMaximumMassValue())
break;
if (matchedMass != null) {
FingerprintGroup group = new FingerprintGroup(1.0);
group.setNumberObserved(count);
group.setFingerprint("0");
if (mergedFingerprintGroupLists.containsKey(matchedMass)) {
MassToFingerprintGroupList currentGroupList = mergedFingerprintGroupLists.get(matchedMass);
FingerprintGroup curGroup = currentGroupList.getElementByFingerprint(group.getFingerprint());
if (curGroup == null)
currentGroupList.addElement(group);
else {
numNonMatchElements--;
curGroup.setNumberObserved(curGroup.getNumberObserved() + group.getNumberObserved());
curGroup.setProbability(curGroup.getProbability() + group.getProbability());
}
} else {
MassToFingerprintGroupList currentGroupList = new MassToFingerprintGroupList(matchedMass);
currentGroupList.addElement(group);
mergedFingerprintGroupLists.put(matchedMass, currentGroupList);
}
}
}
}
while ((line = breader.readLine()) != null) {
line = line.trim();
if (line.length() == 0)
continue;
if (line.startsWith("#"))
continue;
if (line.startsWith("SUMMARY")) {
String[] tmp = line.split("\\s+");
// sum overall occurrences
settings.set(VariableNames.PEAK_FINGERPRINT_DENOMINATOR_COUNT_NAME, Double.parseDouble(tmp[2]) + numNonMatchOccurrences);
// number different peak pairs matched
settings.set(VariableNames.PEAK_FINGERPRINT_MATCHED_TUPLE_COUNT_NAME, Double.parseDouble(tmp[1]) - numMatchedObservationsMerged);
// number different peak pairs non-matched
settings.set(VariableNames.PEAK_FINGERPRINT_NON_MATCHED_TUPLE_COUNT_NAME, (double) numNonMatchElements);
continue;
}
String[] tmp = line.split("\\s+");
Double peak = Double.parseDouble(tmp[0]);
// find matching peak in spectrum
Double matchedMass = peakList.getBestMatchingMass(peak, mzppm, mzabs);
if (matchedMass != null) {
FingerprintGroup[] groups = this.getFingerprintGroup(tmp);
if (mergedFingerprintGroupLists.containsKey(matchedMass)) {
MassToFingerprintGroupList currentGroupList = mergedFingerprintGroupLists.get(matchedMass);
for (int i = 0; i < groups.length; i++) {
FingerprintGroup curGroup = currentGroupList.getElementByFingerprint(groups[i].getFingerprint());
if (curGroup == null)
currentGroupList.addElement(groups[i]);
else {
if (curGroup.getFingerprint().getSize() != 1)
numMatchedObservationsMerged++;
curGroup.setNumberObserved(curGroup.getNumberObserved() + groups[i].getNumberObserved());
curGroup.setProbability(curGroup.getProbability() + groups[i].getProbability());
}
}
} else {
MassToFingerprintGroupList currentGroupList = new MassToFingerprintGroupList(matchedMass);
for (int i = 0; i < groups.length; i++) currentGroupList.addElement(groups[i]);
mergedFingerprintGroupLists.put(matchedMass, currentGroupList);
}
}
}
java.util.Iterator<Double> it = mergedFingerprintGroupLists.keySet().iterator();
while (it.hasNext()) {
peakToFingerprintGroupListCollection.addElementSorted(mergedFingerprintGroupLists.get(it.next()));
}
breader.close();
settings.set(VariableNames.PEAK_TO_FINGERPRINT_GROUP_LIST_COLLECTION_NAME, peakToFingerprintGroupListCollection);
}
}
Aggregations