Skip to content

Commit

Permalink
bump to 1.2.37. Separate figures for mass offset loess calibrations. …
Browse files Browse the repository at this point in the history
…Print out peptides used for calibration
  • Loading branch information
yangkl96 committed Jul 17, 2024
1 parent 036dab0 commit 6d9ce4c
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.example</groupId>
<artifactId>MSBooster</artifactId>
<version>1.2.36</version>
<version>1.2.37</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/Features/CalibrationFigure.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class CalibrationFigure {
String charge;
public CalibrationFigure() {}

//TODO: repeat but with single entries at a time, with different outFile names
public void plotFigure(MzmlReader mzml, String outFile, float opacity,
HashMap<String, double[][]> massToData,
HashMap<String, Function1<Double, Double>> loessFunctions) throws IOException {
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/Features/LoessUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ private static int getPSMs(MzmlReader mzml,

//utility for getBetas and LOESS
//returns exp and pred RT arrays
public static HashMap<String, double[][]> getArrays(MzmlReader mzml, int regressionSize, String mode, int charge)
public static Object[] getArrays(MzmlReader mzml, int regressionSize, String mode, int charge)
throws FileParsingException {
//returns HashMap<String, double[][]> for arrays, and HashMap<String, ArrayList<String>> for peptides
ArrayList<Float> expValues = new ArrayList<>();
ArrayList<Float> predValues = new ArrayList<>();
ArrayList<Float> eScores = new ArrayList<>(); //for sorting
Expand Down Expand Up @@ -146,15 +147,18 @@ public static HashMap<String, double[][]> getArrays(MzmlReader mzml, int regress
massesList.add("");
}

HashMap<String, ArrayList<String>> peptideMap = new HashMap<>();
for (String mass : massesList) {
ArrayList<Float> thisExpValues = new ArrayList<>();
ArrayList<Float> thisPredValues = new ArrayList<>();
ArrayList<Float> thisEscores = new ArrayList<>();
ArrayList<String> finalPeptides = new ArrayList<>();
//get PSMs specific to this mass
if (mass.isEmpty()) {
thisExpValues = expValues;
thisPredValues = predValues;
thisEscores = eScores;
finalPeptides = peptides;
} else if (mass.equals("others")) {
for (int i = 0; i < peptides.size(); i++) {
boolean peptideContains = false;
Expand All @@ -175,6 +179,7 @@ public static HashMap<String, double[][]> getArrays(MzmlReader mzml, int regress
thisExpValues.add(expValues.get(i));
thisPredValues.add(predValues.get(i));
thisEscores.add(eScores.get(i));
finalPeptides.add(peptides.get(i));
}
}
} else {
Expand All @@ -185,6 +190,7 @@ public static HashMap<String, double[][]> getArrays(MzmlReader mzml, int regress
thisExpValues.add(expValues.get(i));
thisPredValues.add(predValues.get(i));
thisEscores.add(eScores.get(i));
finalPeptides.add(peptides.get(i));
break;
}
}
Expand Down Expand Up @@ -231,11 +237,14 @@ public static HashMap<String, double[][]> getArrays(MzmlReader mzml, int regress
int[] sortedIndices2 = Arrays.copyOfRange(sortedIndices, 0, regressionSize);

double[][] thisValues = new double[2][regressionSize];
ArrayList<String> newFinalPeptides = new ArrayList<>();
for (int i = 0; i < regressionSize; i++) {
int idx = sortedIndices2[i];
thisValues[0][i] = thisExpValues.get(idx);
thisValues[1][i] = thisPredValues.get(idx);
newFinalPeptides.add(finalPeptides.get(idx));
}
finalPeptides = newFinalPeptides;
massToDataMap.put(mass, thisValues);
} else {
if (mass.isEmpty()) {
Expand All @@ -248,7 +257,9 @@ public static HashMap<String, double[][]> getArrays(MzmlReader mzml, int regress
thisValues[1] = thisPredValues.stream().mapToDouble(i -> i).toArray();
massToDataMap.put(mass, thisValues);
}

peptideMap.put(mass, finalPeptides);
}
return massToDataMap;
return new Object[] {massToDataMap, peptideMap};
}
}
2 changes: 1 addition & 1 deletion src/main/java/Features/MainClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class MainClass {
public static ScheduledThreadPoolExecutor executorService;
public static void main(String[] args) throws Exception {
Locale.setDefault(Locale.US);
printInfo("MSBooster v1.2.36");
printInfo("MSBooster v1.2.37");

try {
//accept command line inputs
Expand Down
20 changes: 17 additions & 3 deletions src/main/java/Features/MzmlReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public class MzmlReader {
public ArrayList<HashMap<String, Function1<Double, Double>>> IMLOESS = new ArrayList<>();
public HashMap<String, double[][]> expAndPredRTs;
public HashMap<Integer, HashMap<String, double[][]>> expAndPredIMsHashMap = new HashMap<>();
public HashMap<String, ArrayList<String>> RTpeptides;
public HashMap<String, ArrayList<String>> IMpeptides = new HashMap<>();
public List<Future> futureList = new ArrayList<>(Constants.numThreads);

public MzmlReader(String filename) throws FileParsingException, ExecutionException, InterruptedException {
Expand Down Expand Up @@ -806,7 +808,9 @@ public void setLOESS(int regressionSize, String bandwidth, int robustIters, Stri
}

if (mode.equals("RT")) {
expAndPredRTs = LoessUtilities.getArrays(this, regressionSize, mode, 0);
Object[] arraysAndPeptides = LoessUtilities.getArrays(this, regressionSize, mode, 0);
expAndPredRTs = (HashMap<String, double[][]>) arraysAndPeptides[0];
RTpeptides = (HashMap<String, ArrayList<String>>) arraysAndPeptides[1];

//repeat this process for each mass shift group
for (String mass : masses) {
Expand Down Expand Up @@ -870,8 +874,18 @@ public void setLOESS(int regressionSize, String bandwidth, int robustIters, Stri
}
} else if (mode.equals("IM")) {
for (int charge = 1; charge < IMFunctions.numCharges + 1; charge++) {
HashMap<String, double[][]> expAndPredIMs =
LoessUtilities.getArrays(this, regressionSize, mode, charge);
Object[] arraysAndPeptides = LoessUtilities.getArrays(this, regressionSize, mode, charge);
HashMap<String, double[][]> expAndPredIMs = (HashMap<String, double[][]>) arraysAndPeptides[0];
HashMap<String, ArrayList<String>> peptideMap =
(HashMap<String, ArrayList<String>>) arraysAndPeptides[1];
for (Map.Entry<String, ArrayList<String>> entry : peptideMap.entrySet()) {
if (!IMpeptides.containsKey(entry.getKey())) {
IMpeptides.put(entry.getKey(), new ArrayList<>());
}
ArrayList<String> peptides = IMpeptides.get(entry.getKey());
peptides.addAll(entry.getValue());
IMpeptides.put(entry.getKey(), peptides);
}
HashMap<String, Function1<Double, Double>> IMLOESSmap = new HashMap<>();

for (String mass : masses) {
Expand Down
85 changes: 83 additions & 2 deletions src/main/java/Features/PercolatorFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@
import com.google.common.collect.Range;
import com.google.common.collect.RangeMap;
import com.google.common.collect.TreeRangeMap;
import kotlin.jvm.functions.Function1;
import org.apache.commons.lang3.ArrayUtils;
import umich.ms.fileio.exceptions.FileParsingException;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLException;
import java.util.*;
Expand Down Expand Up @@ -518,8 +521,45 @@ public static void editPin(PinMzmlMatcher pmMatcher, String[] features, String o
}
}
if (plot) {
new RTCalibrationFigure(mzml, pinFiles[i].getCanonicalPath(), 0.2f,
CalibrationFigure cf = new RTCalibrationFigure(mzml, pinFiles[i].getCanonicalPath(), 0.2f,
mzml.expAndPredRTs, mzml.RTLOESS);

//individual figures by mass
//separate figures
for (String mass : mzml.expAndPredRTs.keySet()) {
if (!mass.isEmpty()) {
HashMap<String, double[][]> miniMassToData = new HashMap<>();
miniMassToData.put(mass, mzml.expAndPredRTs.get(mass));
HashMap<String, Function1<Double, Double>> miniLoessFunctions = new HashMap<>();
miniLoessFunctions.put(mass, mzml.RTLOESS.get(mass));
new RTCalibrationFigure(mzml,
pinFiles[i].getCanonicalPath().substring(0,
pinFiles[i].getCanonicalPath().length() - 4) + "_" + mass + ".pin",
0.2f, miniMassToData, miniLoessFunctions);
}
}

//write peptides used
File f = new File(pinFiles[i].getCanonicalPath());
String calibrationPeptideFilePathBase =
f.getParent() + File.separator + "MSBooster_plots" + File.separator + cf.folderString +
File.separator + f.getName().substring(0, f.getName().length() - 4) +
"_RTcalibrationPeptides";
for (Map.Entry<String, ArrayList<String>> entry : mzml.RTpeptides.entrySet()) {
String calibrationPeptideFilePath = calibrationPeptideFilePathBase;
if (!entry.getKey().isEmpty()) {
calibrationPeptideFilePath += "_" + entry.getKey();
}
try (BufferedWriter writer = new BufferedWriter(
new FileWriter(calibrationPeptideFilePath + ".txt"))) {
for (String peptide : entry.getValue()) {
writer.write(peptide);
writer.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
if (featuresList.contains("deltaRTlinear")) {
Expand Down Expand Up @@ -564,6 +604,8 @@ public static void editPin(PinMzmlMatcher pmMatcher, String[] features, String o
mzml.setLOESS(Constants.imLoessRegressionSize, Constants.loessBandwidth, Constants.robustIters, "IM");
mzml.predictIMLOESS(executorService);

CalibrationFigure cf = null;
boolean writePeptides = false;
for (int charge : mzml.expAndPredIMsHashMap.keySet()) {
boolean plot = false;
for (Map.Entry<String, double[][]> entry : mzml.expAndPredIMsHashMap.get(charge).entrySet()) {
Expand All @@ -573,8 +615,47 @@ public static void editPin(PinMzmlMatcher pmMatcher, String[] features, String o
}
}
if (plot) {
new IMCalibrationFigure(mzml, pinFiles[i].getCanonicalPath(), 0.2f,
cf = new IMCalibrationFigure(mzml, pinFiles[i].getCanonicalPath(), 0.2f,
mzml.expAndPredIMsHashMap.get(charge), mzml.IMLOESS.get(charge - 1), charge);
writePeptides = true;

//individual figures by mass
//separate figures
for (String mass : mzml.expAndPredIMsHashMap.get(charge).keySet()) {
if (!mass.isEmpty()) {
HashMap<String, double[][]> miniMassToData = new HashMap<>();
miniMassToData.put(mass, mzml.expAndPredIMsHashMap.get(charge).get(mass));
HashMap<String, Function1<Double, Double>> miniLoessFunctions = new HashMap<>();
miniLoessFunctions.put(mass, mzml.IMLOESS.get(charge - 1).get(mass));
new IMCalibrationFigure(mzml,
pinFiles[i].getCanonicalPath().substring(0,
pinFiles[i].getCanonicalPath().length() - 4) + "_" + mass + ".pin",
0.2f, miniMassToData, miniLoessFunctions, charge);
}
}
}
}
if (writePeptides) {
//write peptides used
File f = new File(pinFiles[i].getCanonicalPath());
String calibrationPeptideFilePathBase =
f.getParent() + File.separator + "MSBooster_plots" + File.separator + cf.folderString +
File.separator + f.getName().substring(0, f.getName().length() - 4) +
"_IMcalibrationPeptides";
for (Map.Entry<String, ArrayList<String>> entry : mzml.IMpeptides.entrySet()) {
String calibrationPeptideFilePath = calibrationPeptideFilePathBase;
if (!entry.getKey().isEmpty()) {
calibrationPeptideFilePath += "_" + entry.getKey();
}
try (BufferedWriter writer = new BufferedWriter(
new FileWriter(calibrationPeptideFilePath + ".txt"))) {
for (String peptide : entry.getValue()) {
writer.write(peptide);
writer.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/Features/RTFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public static float normalizeRT(float[] betas, float expRT) {
//assumes peptide objects already set
//TODO: only supporting getting PSMs for supported PTMs
public static float[] getBetas(MzmlReader mzml, int RTregressionSize) throws FileParsingException {
double[][] RTs = LoessUtilities.getArrays(mzml, RTregressionSize, "RT", 0).get("");
HashMap<String, double[][]> arrays = (HashMap<String, double[][]>)
LoessUtilities.getArrays(mzml, RTregressionSize, "RT", 0)[0];
double[][] RTs = arrays.get("");
return StatMethods.linearRegression(RTs[0], RTs[1]);
}

Expand Down

0 comments on commit 6d9ce4c

Please sign in to comment.