diff --git a/src/edu/umich/andykong/ptmshepherd/iterativelocalization/BinPriorProbabilities.java b/src/edu/umich/andykong/ptmshepherd/iterativelocalization/BinPriorProbabilities.java index 8de2b99..f7dd606 100644 --- a/src/edu/umich/andykong/ptmshepherd/iterativelocalization/BinPriorProbabilities.java +++ b/src/edu/umich/andykong/ptmshepherd/iterativelocalization/BinPriorProbabilities.java @@ -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'}; @@ -55,7 +54,6 @@ public BinPriorProbabilities() { this.nT1 = 0.0; this.cT1 = 0.0; - this.epoch = 0; this.priorVectorNorm = 0.0; this.isConverged = false; @@ -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 { @@ -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"); diff --git a/src/edu/umich/andykong/ptmshepherd/iterativelocalization/IterativeLocalizer.java b/src/edu/umich/andykong/ptmshepherd/iterativelocalization/IterativeLocalizer.java index ad93154..c1d9779 100644 --- a/src/edu/umich/andykong/ptmshepherd/iterativelocalization/IterativeLocalizer.java +++ b/src/edu/umich/andykong/ptmshepherd/iterativelocalization/IterativeLocalizer.java @@ -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; @@ -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.*; @@ -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"); @@ -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 linesWithoutSpectra = new ArrayList<>(); //TODO @@ -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 @@ -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); + } + } } \ No newline at end of file