Skip to content

Commit

Permalink
Implement new swapped amino acid decoy generation method
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgeiszler committed May 6, 2024
1 parent 277a268 commit 9e53107
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 26 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ java {
targetCompatibility = JavaVersion.VERSION_1_9
}

version = '3.0.0-rc5'
version = '3.0.0-rc6'

application {
// Define the main class for the application
Expand Down
2 changes: 1 addition & 1 deletion src/edu/umich/andykong/ptmshepherd/PTMShepherd.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
public class PTMShepherd {

public static final String name = "PTM-Shepherd";
public static final String version = "3.0.0-rc5";
public static final String version = "3.0.0-rc6";

static HashMap<String,String> params;
static TreeMap<String,ArrayList<String []>> datasets;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,13 @@ private void fitMatchedIonDistribution() throws Exception {
int mutationSite = decoyPep.mutatedResidue;

// Get sites that are/would be shifted
ArrayList<Float> sitePepFrags = DownstreamPepFragGenerator.calculatePeptideFragments(
targetPep, this.ionTypes, mutationSite, 1);
ArrayList<Float> decoySitePepFrags = DownstreamPepFragGenerator.calculatePeptideFragments(
decoyPep, this.ionTypes, mutationSite, 1);
//ArrayList<Float> sitePepFrags = DownstreamPepFragGenerator.calculatePeptideFragments(
// targetPep, this.ionTypes, mutationSite, 1);
//ArrayList<Float> decoySitePepFrags = DownstreamPepFragGenerator.calculatePeptideFragments(
// decoyPep, this.ionTypes, mutationSite, 1);

ArrayList<Float> sitePepFrags = targetPep.calculatePeptideFragments(this.ionTypes, 1);
ArrayList<Float> decoySitePepFrags = decoyPep.calculatePeptideFragments(this.ionTypes, 1);

// Add target peptide values to matched ion histograms
float[][] matchedIons = findMatchedIons(sitePepFrags, peakMzs, peakInts);
Expand Down Expand Up @@ -240,6 +243,7 @@ private void calculateLocalizationProbabilities() throws Exception {
ArrayList<String> specNames = new ArrayList<>();
ArrayList<String> strOutputProbs = new ArrayList<>();
ArrayList<String> strMaxProbs = new ArrayList<>();
ArrayList<String> strMaxProbsDecoy = new ArrayList<>();
ArrayList<String> strEntropies = new ArrayList<>();
ArrayList<String> strMaxProbs2 = new ArrayList<>();

Expand Down Expand Up @@ -288,6 +292,7 @@ private void calculateLocalizationProbabilities() throws Exception {
specNames.add(specName);
strOutputProbs.add(""); // Add empty string if zero bin
strMaxProbs.add("");
strMaxProbsDecoy.add("");
strEntropies.add("");
strMaxProbs2.add("");
continue;
Expand Down Expand Up @@ -335,20 +340,21 @@ private void calculateLocalizationProbabilities() throws Exception {
specNames.add(specName);
strOutputProbs.add(probabilitiesToPepString(pep, dMass, siteProbs, allowedPoses));
double maxProb = findMaxLocalizationProbability(siteProbs);
strMaxProbs2.add(Double.toString(maxProb));
String maxProbAA = findMaxLocalizationProbabilitySite(siteProbs, pep);
int maxProbI = findMaxLocalizationProbabilitySiteIndex(siteProbs);
strMaxProbs.add(maxProbToString(maxProb, maxProbAA));
double locEntropy = calculateLocalizationEntropy(siteProbs, allowedPoses);
strEntropies.add(entropyToString(locEntropy));

// TODO check decoy usefulness
Peptide decoyPep = Peptide.generateDecoy(pep, mods, this.rng, "mutated");
Peptide decoyPep = Peptide.generateDecoy(pep, mods, maxProbI, this.rng, "mono-swapped");
boolean[] decoyAllowedPoses = parseAllowedPositions(decoyPep.pepSeq,
this.allowedAAs, decoyPep.mods);
double[] decoySiteProbs = localizePsm(psm, spec, decoyPep.pepSeq, decoyPep.mods, dMassApex,
cBin, decoyAllowedPoses);
double decoyMaxProb = findMaxLocalizationProbability(decoySiteProbs);
String decoyMaxProbAA = findMaxLocalizationProbabilitySite(decoySiteProbs, decoyPep.pepSeq);
strMaxProbsDecoy.add(maxProbToString(decoyMaxProb, decoyMaxProbAA));
if (debugFlag) {
if (decoyMaxProb >= maxProb) {
System.out.println(specName + "\t" + pep + "\t" + decoyPep.pepSeq + "\t" +
Expand All @@ -367,6 +373,8 @@ private void calculateLocalizationProbabilities() throws Exception {
"PTM-Shepherd Localization", specNames, strOutputProbs);
psmf.addColumn(psmf.getColumn("PTM-Shepherd Localization") + 1,
"PTM-Shepherd Best Localization", specNames, strMaxProbs);
psmf.addColumn(psmf.getColumn("PTM-Shepherd Best Localization") + 1,
"PTM-Shepherd Best Decoy Localization", specNames, strMaxProbsDecoy);
//psmf.addColumn(psmf.getColumn("delta_mass_maxloc") + 1,
// "delta_mass_entropy", specNames, strEntropies);
//psmf.addColumn(psmf.getColumn("PTM-Shepherd Best Localization") + 1,
Expand Down Expand Up @@ -500,11 +508,14 @@ private void calculateFalseLocalizationRates() throws Exception { //TODO this ne
class SpecProbQ implements Comparable<SpecProbQ> {
String spec;
double prob;
double probDecoy;
double q;
double qDecoy;

SpecProbQ(String spec, double prob) {
SpecProbQ(String spec, double prob, double probDecoy) {
this.spec = spec;
this.prob = prob;
this.probDecoy = probDecoy;
}

@Override
Expand All @@ -531,6 +542,7 @@ public int compareTo(SpecProbQ o) {
ArrayList<String> specs = psmf.getColumnValues("Spectrum");
ArrayList<String> peps = psmf.getColumnValues("Peptide");
ArrayList<String> maxProbs = psmf.getColumnValues("PTM-Shepherd Best Localization");
ArrayList<String> maxProbsDecoy = psmf.getColumnValues("PTM-Shepherd Best Decoy Localization");
//ArrayList<String> entropies = psmf.getColumnValues("delta_mass_entropy");

// Add probs to target and decoy histos and calculate nTarget and nDecoyAAs
Expand Down Expand Up @@ -564,10 +576,12 @@ public int compareTo(SpecProbQ o) {
}
for (int j = 0; j < specs.size(); j++) {
String maxProb = maxProbs.get(j);
String maxProbDecoy = maxProbsDecoy.get(j);
if (maxProb.equals(""))
continue;
double prob = Double.parseDouble(subString(maxProb, "(", ")"));
spqs.add(new SpecProbQ(specs.get(j), prob));
double probDecoy = Double.parseDouble(subString(maxProbDecoy, "(", ")"));
spqs.add(new SpecProbQ(specs.get(j), prob, probDecoy));
}

}
Expand All @@ -576,7 +590,7 @@ public int compareTo(SpecProbQ o) {
// Sort in descending probability order
Collections.sort(spqs, Collections.reverseOrder());

// Calculate local q-val
// Calculate local q-val using probability model
double runningQSum = 0.0;
int runningCount = 0;
for (SpecProbQ spq : spqs) {
Expand All @@ -585,7 +599,17 @@ public int compareTo(SpecProbQ o) {
spq.q = runningQSum / runningCount;
}

// Ensure q-val monotonicity
// Calculate local q-val using decoy model
runningCount = 0;
int runningDecoy = 1;
for (SpecProbQ spq : spqs) {
if (spq.probDecoy > spq.prob)
runningDecoy++;
runningCount++;
spq.qDecoy = (double) runningDecoy / (double) runningCount;
}

// Ensure q-val monotonicity for probability model
double cMin = 10000.0;
for (int i = spqs.size() - 1; i >= 0; i--) {
double tQ = spqs.get(i).q;
Expand All @@ -600,10 +624,28 @@ public int compareTo(SpecProbQ o) {
}
}

// Ensure q-val monotonicity for decoy model
cMin = 10000.0;
for (int i = spqs.size() - 1; i >= 0; i--) {
double tQ = spqs.get(i).qDecoy;
if (spqs.get(i).qDecoy < cMin) {
cMin = tQ;
} else {
while ((i >= 0) && (spqs.get(i).qDecoy >= cMin)) {
spqs.get(i).qDecoy = cMin;
i--;
}
i++;
}
}

// Extract spectrum to q-val and probability mappings in matched order
HashMap<String, Double> qValsMap = new HashMap<>();
for (SpecProbQ spq : spqs)
HashMap<String, Double> qValsDecoyMap = new HashMap<>();
for (SpecProbQ spq : spqs) {
qValsMap.put(spq.spec, spq.q);
qValsDecoyMap.put(spq.spec, spq.qDecoy);
}

// Calculate the FLRs of each type
// Three different FLRs to compute
Expand Down Expand Up @@ -676,7 +718,7 @@ public int compareTo(SpecProbQ o) {
qEntropyDecoyModel[i] = min;
}


/**
// Print to test
for (int i = flrProb.length-1; i >= 0; i--) {
Expand Down Expand Up @@ -717,6 +759,7 @@ public int compareTo(SpecProbQ o) {

// Assign each q-Val
ArrayList<String> probModelQVals = new ArrayList<>(specNames.size());
ArrayList<String> decoyModelQVals = new ArrayList<>(specNames.size());
//ArrayList<String> probDecoyModelVals = new ArrayList<>(specNames.size());
//ArrayList<String> entropyDecoyModelVals = new ArrayList<>(specNames.size());
for (int j = 0; j < specNames.size(); j++) {
Expand All @@ -725,13 +768,15 @@ public int compareTo(SpecProbQ o) {
if (unmodFlag) {
// prob model
probModelQVals.add("");
decoyModelQVals.add("");
// prob model with decoys
//probDecoyModelVals.add("");
// entropy model
//entropyDecoyModelVals.add("");
} else {
// prob model
probModelQVals.add(new DecimalFormat("0.0000").format(qValsMap.get(specNames.get(j))));
decoyModelQVals.add(new DecimalFormat("0.0000").format(qValsDecoyMap.get(specNames.get(j))));
// prob model with decoys
//probDecoyModelVals.add(new DecimalFormat("0.0000").format(qProbDecoyModel[(int) (maxProb * 1000)]));
// entropy model
Expand All @@ -743,6 +788,8 @@ public int compareTo(SpecProbQ o) {
// Send to PSM file
psmf.addColumn(psmf.getColumn("PTM-Shepherd Best Localization") + 1, "PTM-Shepherd q-val",
specNames, probModelQVals);
psmf.addColumn(psmf.getColumn("PTM-Shepherd q-val") + 1, "PTM-Shepherd decoy q-val",
specNames, decoyModelQVals);
/** //TODO figure out what's going on with these before implementing them, assuming they're even worth doing
psmf.addColumn(psmf.getColumn("delta_mass_BH_loc_q") + 1, "delta_mass_prob_decoyAA_q",
specNames, probDecoyModelVals);
Expand Down Expand Up @@ -1161,6 +1208,19 @@ private String findMaxLocalizationProbabilitySite(double[] locProbs, String pep)
return site;
}

private int findMaxLocalizationProbabilitySiteIndex(double[] locProbs) {
double max = 0.0;
int maxI = -1;
for (int i = 0; i < locProbs.length; i++) {
if (locProbs[i] > max) {
max = locProbs[i];
maxI = i;
}
}

return maxI;
}

private void advanceEpochBins(double convCriterion) {
for (int i = 0; i < this.peaks[0].length; i++) {
if (i == this.zeroBin)
Expand Down
82 changes: 70 additions & 12 deletions src/edu/umich/andykong/ptmshepherd/utils/Peptide.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,26 @@ else if (curIonType == 'x' || curIonType == 'y' || curIonType == 'z')

public Peptide generateDecoy(Random rng, String method) {
if (method.equals("shuffled"))
return generateShuffledDecoy(this.pepSeq, mods, rng);
return generateShuffledDecoy(this.pepSeq, this.mods, rng);
else if (method.equals("full-shuffled"))
return generateFullShuffledDecoy(this.pepSeq, this.mods, rng);
else if (method.equals("swapped"))
return generateSwappedDecoy(this.pepSeq, this.mods, rng);
else if (method.equals("mutated"))
return generateMutatedDecoy(this.pepSeq, mods);
return generateMutatedDecoy(this.pepSeq, this.mods);
else if (method.equals("mono-mutated"))
return generateMonoMutatedDecoy(this.pepSeq, mods, rng);
else
return null; //TODO
}

public Peptide generateDecoy(Random rng, String method, int pos) {
if (method.equals("swap"))
return generateSwappedDecoy(this.pepSeq, this.mods, pos, rng);
else
return null; //TODO
}

public static Peptide generateDecoy(String pep, float[] mods, Random rng, String method) {
if (method.equals("shuffled"))
return generateShuffledDecoy(pep, mods, rng);
else if (method.equals("full-shuffled"))
return generateFullShuffledDecoy(pep, mods, rng);
else if (method.equals("swapped"))
return generateSwappedDecoy(pep, mods, rng);
else if (method.equals("mutated"))
return generateMutatedDecoy(pep, mods);
else if (method.equals("mono-mutated"))
Expand All @@ -99,6 +100,12 @@ else if (method.equals("mono-mutated"))
return null; //TODO
}

public static Peptide generateDecoy(String pep, float[] mods, int pos, Random rng, String method) {
if (method.equals("mono-swapped"))
return generateMonoSwappedDecoy(pep, mods, pos, rng);
else
return null; //TODO
}

public static Peptide generateShuffledDecoy(String pep, float[] mods, Random rng) {
ArrayList<Site> sites = new ArrayList<>(pep.length());
Expand All @@ -124,6 +131,25 @@ public static Peptide generateShuffledDecoy(String pep, float[] mods, Random rng
return new Peptide(newPep.toString(), newMods);
}

public static Peptide generateFullShuffledDecoy(String pep, float[] mods, Random rng) {
ArrayList<Site> sites = new ArrayList<>(pep.length());

// Shuffle pep
for (int i = 0; i < pep.length(); i++)
sites.add(new Site(pep.charAt(i), mods[i]));
Collections.shuffle(sites, rng);

StringBuilder newPep = new StringBuilder();
float[] newMods = new float[mods.length];

for (int i = 0; i < sites.size(); i++) {
newPep.append(sites.get(i).aa);
newMods[i] = sites.get(i).mod;
}

return new Peptide(newPep.toString(), newMods);
}

public static Peptide generateMutatedDecoy(String pep, float[] mods) {
ArrayList<Site> sites = new ArrayList<>(pep.length());
for (int i = 0; i < pep.length(); i++)
Expand Down Expand Up @@ -155,9 +181,41 @@ public static Peptide generateMonoMutatedDecoy(String pep, float[] mods, int mut
return new Peptide(newPep.toString(), mods, mutSite);
}

public static Peptide generateSwappedDecoy(String pepSeq, float[] mods, int pos, Random rng) {
//TODO
return new Peptide(pepSeq, mods);
public static Peptide generateSwappedDecoy(String pepSeq, float[] mods, Random rng) {
int pos = rng.nextInt(pepSeq.length());
int swapPos = pos;
while (swapPos == pos)
swapPos = rng.nextInt(pepSeq.length());

char swapAA = pepSeq.charAt(swapPos);
float swapMod = mods[swapPos];

char[] pepSeqArray = pepSeq.toCharArray();
pepSeqArray[swapPos] = pepSeqArray[pos];
mods[swapPos] = mods[pos];

pepSeqArray[pos] = swapAA;
mods[pos] = swapMod;

return new Peptide(pepSeqArray.toString(), mods);
}

public static Peptide generateMonoSwappedDecoy(String pepSeq, float[] mods, int pos, Random rng) {
int swapPos = pos;
while (Math.abs(swapPos - pos) < (pepSeq.length() / 2))
swapPos = rng.nextInt(pepSeq.length());

char swapAA = pepSeq.charAt(swapPos);
float swapMod = mods[swapPos];

char[] pepSeqArray = pepSeq.toCharArray();
pepSeqArray[swapPos] = pepSeqArray[pos];
mods[swapPos] = mods[pos];

pepSeqArray[pos] = swapAA;
mods[pos] = swapMod;

return new Peptide(String.valueOf(pepSeqArray), mods);
}

static class Site {
Expand Down

0 comments on commit 9e53107

Please sign in to comment.