Skip to content

Commit

Permalink
Add output file for prior probabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgeiszler committed May 9, 2024
1 parent ffbbcc9 commit b2a3766
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public class BinPriorProbabilities {
int nPsms;
double priorVectorNorm;
boolean isConverged;
int epoch;
final double INITVAL = 1.0 / 62.0;
final char[] AAs = {'A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L',
'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'Y'};
Expand Down Expand Up @@ -55,7 +54,6 @@ public BinPriorProbabilities() {
this.nT1 = 0.0;
this.cT1 = 0.0;

this.epoch = 0;
this.priorVectorNorm = 0.0;
this.isConverged = false;

Expand All @@ -71,11 +69,11 @@ public double[] computePriorProbs(String pep, boolean[] allowedPoses) {
char cAA = pep.charAt(i);
if (allowedPoses[i] == true) {
if (i == 0) { // Pick max probability for N-terminal options
double curProb = Math.max(this.n, Math.max(this.nProbs[cAA-'A'], this.probs[cAA-'A']));
double curProb = (this.n + this.nProbs[cAA-'A'] + this.probs[cAA-'A']) / 3.0;
priorProbs[i] = curProb;
sumProbs += curProb;
} else if (i == pep.length()-1) { // Pick max probability for C-terminal options
double curProb = Math.max(this.c, Math.max(this.cProbs[cAA-'A'], this.probs[cAA-'A']));
double curProb = (this.c + this.cProbs[cAA-'A'] + this.probs[cAA-'A']) / 3.0;
priorProbs[i] = curProb;
sumProbs += curProb;
} else {
Expand Down Expand Up @@ -216,6 +214,30 @@ public boolean getIsConverged() {
return this.isConverged;
}

static public String fileHeaderToString() {
return new String("mass_shift\tepoch\tlocation\tvalue\n");
}

public String fileLinesToString(float dMass, int epoch) {
StringBuffer sb = new StringBuffer();

sb.append(String.format("%f\t%d\t%s\t%.4f\n", dMass, epoch, "n", this.n));
sb.append(String.format("%f\t%d\t%s\t%.4f\n", dMass, epoch, "c", this.c));

for (Character c : this.AAs) {
sb.append(String.format("%f\t%d\tn%c\t%.4f\n", dMass, epoch, c, this.nProbs[c-'A']));
}

for (Character c : this.AAs) {
sb.append(String.format("%f\t%d\tc%c\t%.4f\n", dMass, epoch, c, this.cProbs[c-'A']));
}

for (Character c : this.AAs) {
sb.append(String.format("%f\t%d\t%c\t%.4f\n", dMass, epoch, c, this.probs[c-'A']));
}

return sb.toString();
}

public String toString() {
StringBuffer sb = new StringBuffer("\nprobs\t");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.umich.andykong.ptmshepherd.iterativelocalization;

import edu.umich.andykong.ptmshepherd.PSMFile;
import edu.umich.andykong.ptmshepherd.PTMShepherd;
import edu.umich.andykong.ptmshepherd.core.AAMasses;
import edu.umich.andykong.ptmshepherd.core.FastLocator;
import edu.umich.andykong.ptmshepherd.core.MXMLReader;
Expand All @@ -9,6 +10,9 @@
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.util.*;
Expand Down Expand Up @@ -211,7 +215,6 @@ private void fitMatchedIonDistribution() throws Exception {
this.matchedIonDist.calculateLdaWeights();
this.matchedIonDist.calculateIonPosterior();
this.matchedIonDist.calculateNegativePredictiveValue();
System.out.println(this.matchedIonDist.negPredictiveValue); //XXXQQQ
}
if (this.printIonDistribution) {
//this.matchedIonDist.printIntensityHisto("matched_ion_intensity_distribution.tsv");
Expand All @@ -227,6 +230,9 @@ private void fitMatchedIonDistribution() throws Exception {
private void calculateLocalizationProbabilities() throws Exception {
System.out.println("\tCalculating PSM-level localization probabilities");

// Set up bin-wise prior probability string to be updated every epoch
StringBuffer priorString = new StringBuffer(BinPriorProbabilities.fileHeaderToString());

// Set up missing spectra error handling
ArrayList<String> linesWithoutSpectra = new ArrayList<>(); //TODO

Expand Down Expand Up @@ -450,8 +456,20 @@ private void calculateLocalizationProbabilities() throws Exception {
if (finalPass)
System.out.println("\tUpdating PSM tables with localization information");

// Update output for prior probabilities
for (int i = 0; i < this.peaks[0].length; i++) {
if (i == this.zeroBin)
continue;
float dMassApex = (float) this.peaks[0][i];
priorString.append(this.priorProbs[i].fileLinesToString(dMassApex, epoch));
}

epoch++;
}

// Output prior probabilities
writePriorProbabilitiesOutput("prior_probabilities.tsv", priorString.toString());

}

//todo mods should be parsed here if we don't want to localize on top of var mods
Expand Down Expand Up @@ -1430,4 +1448,15 @@ private double safeDivide(int x, int y) {
else
return (double) x / (double) y;
}

private void writePriorProbabilitiesOutput(String fname, String content) throws IOException {
try {
PrintWriter out = new PrintWriter(new FileWriter(PTMShepherd.normFName(fname)));
out.write(content);
out.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}

0 comments on commit b2a3766

Please sign in to comment.