Skip to content

Commit

Permalink
Merge pull request #154 from julianu/master
Browse files Browse the repository at this point in the history
compatibility with OpenMS 2.5.0
  • Loading branch information
julianu committed Aug 10, 2020
2 parents 3f9dc9a + ec5129b commit 9238682
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 40 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>de.mpc.pia</groupId>
<artifactId>pia</artifactId>
<version>1.3.12</version>
<version>1.3.13</version>
<name>PIA - Protein Inference Algorithms</name>
<url>https://github.com/mpc-bioinformatics/pia</url>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,9 @@ private static int[] processPeptideIdentification(PeptideIdentification pepID, P

// get sourceID, if possible
String sourceID = getSpectrumSourceIDFromUserParams(pepID.getUserParam());
if (sourceID == null) {
sourceID = getSpectrumSourceIDFromSpectrumReference(pepID.getSpectrumReference());
String spectrumReference = pepID.getSpectrumReference();
if ((spectrumReference != null) && (sourceID == null)) {
sourceID = getSpectrumSourceIDFromSpectrumReference(pepID.getSpectrumReference());
}
if (sourceID == null) {
// nothing given in this file, create an ID using M/Z and RT
Expand Down Expand Up @@ -374,7 +375,7 @@ private static int[] processPeptideIdentification(PeptideIdentification pepID, P
sequence,
missedCleavages,
sourceID,
null,
spectrumReference,
file,
spectrumID);
specNr++;
Expand Down Expand Up @@ -451,6 +452,8 @@ private static String getSpectrumSourceIDFromUserParams(List<UserParamIdXML> use
private static String getSpectrumSourceIDFromSpectrumReference(String spectrumReference) {
String sourceID = null;

LOGGER.debug("spectrumReference: " + spectrumReference);

if (spectrumReference != null) {
Matcher matcher = MzIdentMLTools.patternScanInTitle.matcher(spectrumReference);
if (matcher.matches()) {
Expand Down
100 changes: 64 additions & 36 deletions src/main/java/de/mpc/pia/modeller/exporter/IdXMLExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,20 @@ public boolean exportPSMLevel(Long fileID, File exportFile, boolean filterExport

public boolean exportToIdXML(Long fileID, File exportFile,
boolean proteinLevel, boolean filterExport) {
OutputStream out = null;
boolean error = false;

try {
out = new FileOutputStream(exportFile, false);
try (OutputStream out = new FileOutputStream(exportFile, false)) {
// create an XMLOutputFactory
String encoding = StandardCharsets.UTF_8.name();
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLStreamWriter streamWriter = new IndentingXMLStreamWriter(outputFactory.createXMLStreamWriter(out, encoding));

// write common idXML header
streamWriter.writeStartDocument(encoding, "1.0");
streamWriter.writeProcessingInstruction("xml-stylesheet",
"type=\"text/xsl\" href=\"http://open-ms.sourceforge.net/XSL/IdXML.xsl\"");
streamWriter.writeCharacters("\n");

streamWriter.writeStartElement("IdXML");

// <SearchParameters one for each IdentificationRun
Expand All @@ -106,7 +104,7 @@ public boolean exportToIdXML(Long fileID, File exportFile,
inputFileIDToSearchParameter.put(0L, "SP_0");
writeSearchParameters(streamWriter,
"SP_0", "", "", "", MassType.MONOISOTOPIC, "", DigestionEnzyme.UNKNOWN_ENZYME,
0, 0.0, false, 0.0, false);
0, 0.0, false, 0.0, false, true);
} else {
inputFileIDToSearchParameter.put(fileID, "SP_0");

Expand All @@ -120,7 +118,8 @@ public boolean exportToIdXML(Long fileID, File exportFile,
getDigestionEnzyme(fileID),
0, // TODO: get the missed cleavages
0.0, false, // TODO: get the precursor tolerances
0.0, false); // TODO: get the peak mass tolerances
0.0, false, // TODO: get the peak mass tolerances
false);
}

writeIdentificationRun(streamWriter, fileID, inputFileIDToSearchParameter.get(fileID),
Expand All @@ -138,15 +137,6 @@ public boolean exportToIdXML(Long fileID, File exportFile,
} catch (XMLStreamException e) {
LOGGER.error("Error while writing XML to " + exportFile.getAbsolutePath(), e);
error = true;
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
LOGGER.error("Error while trying to close " + exportFile.getAbsolutePath(), e);
error = true;
}
}
}

return !error;
Expand Down Expand Up @@ -175,7 +165,8 @@ private static void writeSearchParameters(XMLStreamWriter streamWriter,
String id, String db, String dbVersion, String taxonomy, MassType massType,
String charges, DigestionEnzyme enzyme, Integer missedCleavages,
Double precursorPeakTolerance, Boolean precursorPeakTolerancePPM,
Double peakMassTolerance, Boolean peakMassTolerancePPM)
Double peakMassTolerance, Boolean peakMassTolerancePPM,
boolean inferenceEngine)
throws XMLStreamException {
streamWriter.writeStartElement("SearchParameters");

Expand All @@ -191,6 +182,10 @@ private static void writeSearchParameters(XMLStreamWriter streamWriter,
streamWriter.writeAttribute("precursor_peak_tolerance_ppm", precursorPeakTolerancePPM.toString());
streamWriter.writeAttribute("peak_mass_tolerance", peakMassTolerance.toString());
streamWriter.writeAttribute("peak_mass_tolerance_ppm", peakMassTolerancePPM.toString());

if (inferenceEngine) {
writeUserParam(streamWriter, "InferenceEngine", STRING_TYPE, "PIA", null, null, null);
}

streamWriter.writeEndElement();
}
Expand Down Expand Up @@ -230,6 +225,11 @@ private void writeIdentificationRun(XMLStreamWriter streamWriter, Long fileID,
// only the IdentificationRun for the selected file will be written
// TODO: set the searchEngine and version
}

boolean isFDRCalculated = false;
if (piaModeller.getPSMModeller().isFDRCalculated(fileID) != null) {
isFDRCalculated = piaModeller.getPSMModeller().isFDRCalculated(fileID);
}

streamWriter.writeAttribute("date", date);
streamWriter.writeAttribute("search_engine", searchEngine);
Expand Down Expand Up @@ -260,7 +260,6 @@ private void writeIdentificationRun(XMLStreamWriter streamWriter, Long fileID,

// ---- Protein Identifications ----
streamWriter.writeStartElement("ProteinIdentification");
//<ProteinIdentification score_type="Mascot" higher_score_better="true" significance_threshold="0" >

streamWriter.writeAttribute("score_type", proteinScore);
if (proteinScoreHigherBetter != null) {
Expand All @@ -275,11 +274,18 @@ private void writeIdentificationRun(XMLStreamWriter streamWriter, Long fileID,
List<Object[]> indistinguishableList = new ArrayList<>();

if (proteinLevel) {

for (ReportProtein protein : piaModeller.getProteinModeller().getFilteredReportProteins(filters)) {
Double qvalue = null;

List<String> phIDs = writeAccessionsToXML(streamWriter, protein.getAccessions(), protein.getScore(), qvalue, fileID, accessionToPH);

Map<String, Boolean> isDecoyMap = new HashMap<>();
if (isFDRCalculated) {
for (Accession acc : protein.getAccessions()) {
isDecoyMap.put(acc.getAccession(), protein.getAccessionDecoyState(acc.getAccession()));
}
}

List<String> phIDs = writeAccessionsToXML(streamWriter, protein.getAccessions(),
protein.getScore(), qvalue, fileID, accessionToPH, isDecoyMap);

Object[] indisObject = new Object[protein.getAccessions().size() + 1];

Expand All @@ -291,13 +297,19 @@ private void writeIdentificationRun(XMLStreamWriter streamWriter, Long fileID,
indistinguishableList.add(indisObject);
}
}

Iterator<List<PSMReportItem>> peptideHitIter = peptideIdentifications.values().iterator();
while (peptideHitIter.hasNext()) {

for (PSMReportItem psmReportItem : peptideHitIter.next()) {
Map<String, Boolean> isDecoyMap = new HashMap<>();
if (isFDRCalculated) {
for (Accession acc : psmReportItem.getAccessions()) {
isDecoyMap.put(acc.getAccession(), psmReportItem.getIsDecoy());
}
}

writeAccessionsToXML(streamWriter, psmReportItem.getAccessions(), 0.0,
null, fileID, accessionToPH);
null, fileID, accessionToPH, isDecoyMap);
}
}

Expand Down Expand Up @@ -333,7 +345,7 @@ private void writeIdentificationRun(XMLStreamWriter streamWriter, Long fileID,
if (proteinLevel || (fileID < 1)) {
if (proteinLevel) {
AbstractProteinInference protInference = piaModeller.getProteinModeller().getAppliedProteinInference();
for (Setting setting : protInference.getScoring().getSettings()) {
for (Setting<HashMap<String, String>> setting : protInference.getScoring().getSettings()) {
if (setting.getShortName().equals(AbstractScoring.SCORING_SETTING_ID)) {
mainScoreShort = setting.getValue();
mainScore = piaModeller.getPSMModeller().getScoreName(mainScoreShort);
Expand All @@ -350,7 +362,7 @@ private void writeIdentificationRun(XMLStreamWriter streamWriter, Long fileID,
}
} else {
// get the main score of the exported file or overview
if (piaModeller.getPSMModeller().isFDRCalculated(fileID)) {
if (Boolean.TRUE.equals(isFDRCalculated)) {
// if the FDR is calculated, use PSM_LEVEL_FDR_SCORE
mainScore = ScoreModelEnum.PSM_LEVEL_FDR_SCORE.getName();
mainScoreShort = ScoreModelEnum.PSM_LEVEL_FDR_SCORE.getShortName();
Expand Down Expand Up @@ -389,10 +401,15 @@ private void writeIdentificationRun(XMLStreamWriter streamWriter, Long fileID,
if (psmIter.previousIndex() == 0) {
// this is the first, add RT and MZ to PeptideIdentification
streamWriter.writeAttribute("MZ", Double.toString(psm.getMassToCharge()));

Double rt = psm.getRetentionTime();
if (rt != null) {
streamWriter.writeAttribute("RT", rt.toString());
}

if (!psm.getSpectrumTitle().trim().isEmpty()) {
streamWriter.writeAttribute("spectrum_reference", psm.getSpectrumTitle().trim());
}
}

streamWriter.writeStartElement("PeptideHit");
Expand All @@ -418,8 +435,8 @@ private void writeIdentificationRun(XMLStreamWriter streamWriter, Long fileID,
// if there is decoy information, write it out
boolean writeDecoyInfo = false;
if (fileID > 0) {
if (piaModeller.getPSMModeller().getFileHasInternalDecoy(fileID) || // file has internal decoy info
piaModeller.getPSMModeller().isFDRCalculated(fileID)) { // FDR is calculated for the file
if (piaModeller.getPSMModeller().getFileHasInternalDecoy(fileID).booleanValue() || // file has internal decoy info
isFDRCalculated) { // FDR is calculated for the file
writeDecoyInfo = true;
}
} else {
Expand All @@ -428,8 +445,7 @@ private void writeIdentificationRun(XMLStreamWriter streamWriter, Long fileID,
}
}
if (writeDecoyInfo) {
writeUserParam(streamWriter, "target_decoy", STRING_TYPE,
psm.getIsDecoy() ? "decoy" : "target", null, null, null);
writeTargetDecoyUserParam(streamWriter, psm.getIsDecoy());
}

// write additional scores
Expand Down Expand Up @@ -609,6 +625,14 @@ private static void writeUserParam(XMLStreamWriter streamWriter, String name, St
}
streamWriter.writeEndElement();
}


private static void writeTargetDecoyUserParam(XMLStreamWriter streamWriter, boolean isDecoy)
throws XMLStreamException{
writeUserParam(streamWriter, "target_decoy", STRING_TYPE,
isDecoy ? "decoy" : "target",
null, null, null);
}


/**
Expand All @@ -627,14 +651,15 @@ private static void writeUserParam(XMLStreamWriter streamWriter, String name, St
* @throws XMLStreamException
*/
private static List<String> writeAccessionsToXML(XMLStreamWriter streamWriter, List<Accession> accessionsList,
Double score, Double qvalue, Long fileID, Map<String, String> accessionToPH)
Double score, Double qvalue, Long fileID, Map<String, String> accessionToPH, Map<String, Boolean> isDecoyMap)
throws XMLStreamException {
ListIterator<Accession> accIt = accessionsList.listIterator();
List<String> writtenPHs = new ArrayList<>(accessionsList.size());

while (accIt.hasNext()) {
Accession acc = accIt.next();
String accStr = acc.getAccession();

if (!accessionToPH.containsKey(accStr)) {
String proteinHitID = "PH_" + accessionToPH.size();
accessionToPH.put(accStr, proteinHitID);
Expand All @@ -645,10 +670,15 @@ private static List<String> writeAccessionsToXML(XMLStreamWriter streamWriter, L
streamWriter.writeAttribute("accession", accStr);

streamWriter.writeAttribute("score", score.toString());

if ((acc.getDbSequence() != null) && (acc.getDbSequence().trim().length() > 0)) {
streamWriter.writeAttribute("sequence", acc.getDbSequence());
streamWriter.writeAttribute("sequence", acc.getDbSequence());
}

if (isDecoyMap.get(accStr) != null) {
writeTargetDecoyUserParam(streamWriter, isDecoyMap.get(accStr));
}

String description = acc.getDescription(fileID);
if (description != null) {
writeUserParam(streamWriter, "Description", STRING_TYPE, description,
Expand All @@ -660,8 +690,6 @@ private static List<String> writeAccessionsToXML(XMLStreamWriter streamWriter, L
null, null, null);
}

// TODO: add decoy information, if it was set on the protein level

streamWriter.writeEndElement();

writtenPHs.add(proteinHitID);
Expand Down

0 comments on commit 9238682

Please sign in to comment.