@Override
public void run() {
try {
PTMFactory ptmFactory = PTMFactory.getInstance();
ModificationProfile ptmProfile = peptideShakerGUI.getSearchParameters().getModificationProfile();
PTMScoringPreferences ptmScoringPreferences = peptideShakerGUI.getPtmScoringPreferences();
progressDialog.setPrimaryProgressCounterIndeterminate(false);
if (psmKeys != null) {
progressDialog.setMaxPrimaryProgressCounter(psmKeys.size());
} else {
progressDialog.setMaxPrimaryProgressCounter(identification.getSpectrumIdentificationSize());
}
if (includeHeader) {
if (indexes) {
writer.write(SEPARATOR);
}
if (accessions) {
writer.write("Protein(s)" + SEPARATOR);
}
if (proteinDescription) {
writer.write("Protein(s) Descriptions" + SEPARATOR);
}
if (sequence) {
writer.write("Sequence" + SEPARATOR);
}
if (modification) {
writer.write("Variable Modification(s)" + SEPARATOR);
}
if (location) {
writer.write("Location Confidence" + SEPARATOR);
if (ptmScoringPreferences.isProbabilitsticScoreCalculation()) {
writer.write(ptmScoringPreferences.getSelectedProbabilisticScore().getName() + SEPARATOR);
}
writer.write("D-score" + SEPARATOR);
}
if (file) {
writer.write("Spectrum File" + SEPARATOR);
}
if (title) {
writer.write("Spectrum Title" + SEPARATOR);
}
if (precursor) {
writer.write("Precursor m/z" + SEPARATOR);
writer.write("Precursor Charge" + SEPARATOR);
writer.write("Idenitification Charge" + SEPARATOR);
writer.write("Precursor Retention Time" + SEPARATOR);
writer.write("Peptide Theoretical Mass" + SEPARATOR);
if (peptideShakerGUI.getSearchParameters().isPrecursorAccuracyTypePpm()) {
writer.write("Mass Error [ppm]" + SEPARATOR);
} else {
writer.write("Mass Error [Da]" + SEPARATOR);
}
writer.write("Isotope number" + SEPARATOR);
}
if (score) {
writer.write("Score" + SEPARATOR);
}
if (confidence) {
writer.write("Confidence" + SEPARATOR);
}
writer.write("Validation" + SEPARATOR);
if (!onlyValidated) {
writer.write("Decoy" + SEPARATOR);
}
if (includeHidden) {
writer.write("Hidden" + SEPARATOR);
}
writer.write(System.getProperty("line.separator"));
}
PSParameter psParameter = new PSParameter();
int psmCounter = 0;
HashMap<String, ArrayList<String>> spectrumKeys = new HashMap<String, ArrayList<String>>();
if (psmKeys == null) {
spectrumKeys = identification.getSpectrumIdentificationMap();
} else {
for (String spectrumKey : psmKeys) {
String spectrumFile = Spectrum.getSpectrumFile(spectrumKey);
if (!spectrumKeys.containsKey(spectrumFile)) {
spectrumKeys.put(spectrumFile, new ArrayList<String>());
}
spectrumKeys.get(spectrumFile).add(spectrumKey);
}
}
int fileCounter = 0;
for (String spectrumFile : spectrumKeys.keySet()) {
if (psmKeys == null) {
progressDialog.setTitle("Copying Spectrum Matches to File. Please Wait... (" + ++fileCounter + "/" + spectrumKeys.size() + ")");
identification.loadSpectrumMatches(spectrumFile, progressDialog);
progressDialog.setTitle("Copying Spectrum Matches Details to File. Please Wait... (" + fileCounter + "/" + spectrumKeys.size() + ")");
identification.loadSpectrumMatchParameters(spectrumFile, psParameter, progressDialog);
} else {
progressDialog.setTitle("Copying Spectrum Matches to File. Please Wait... (" + ++fileCounter + "/" + spectrumKeys.size() + ")");
identification.loadSpectrumMatches(spectrumKeys.get(spectrumFile), progressDialog);
progressDialog.setTitle("Copying Spectrum Matches Details to File. Please Wait... (" + fileCounter + "/" + spectrumKeys.size() + ")");
identification.loadSpectrumMatchParameters(spectrumKeys.get(spectrumFile), psParameter, progressDialog);
}
progressDialog.setMaxPrimaryProgressCounter(spectrumKeys.get(spectrumFile).size());
progressDialog.setValue(0);
for (String psmKey : spectrumKeys.get(spectrumFile)) {
if (progressDialog.isRunCanceled()) {
break;
}
SpectrumMatch spectrumMatch = identification.getSpectrumMatch(psmKey);
psParameter = (PSParameter) identification.getSpectrumMatchParameter(psmKey, psParameter);
PeptideAssumption bestAssumption = spectrumMatch.getBestPeptideAssumption();
if (!bestAssumption.getPeptide().isDecoy(peptideShakerGUI.getSequenceMatchingPreferences()) || !onlyValidated) {
if ((onlyValidated && psParameter.getMatchValidationLevel().isValidated()) || !onlyValidated) {
if ((!includeHidden && !psParameter.isHidden()) || includeHidden) {
if ((onlyStarred && psParameter.isStarred()) || !onlyStarred) {
if (indexes) {
writer.write(++psmCounter + SEPARATOR);
}
if (accessions || proteinDescription) {
String proteinAccessions = "";
String proteinDescriptions = "";
boolean first = true;
for (String protein : bestAssumption.getPeptide().getParentProteins(peptideShakerGUI.getSequenceMatchingPreferences())) {
if (first) {
first = false;
} else {
if (accessions) {
proteinAccessions += ", ";
}
if (proteinDescription) {
proteinDescriptions += "; ";
}
}
if (accessions) {
proteinAccessions += protein;
}
if (proteinDescription) {
proteinDescriptions += sequenceFactory.getHeader(protein).getSimpleProteinDescription();
}
}
if (accessions) {
writer.write(proteinAccessions + SEPARATOR);
}
if (proteinDescription) {
writer.write(proteinDescriptions + SEPARATOR);
}
}
if (sequence) {
writer.write(bestAssumption.getPeptide().getSequence() + SEPARATOR);
}
if (modification) {
HashMap<String, ArrayList<Integer>> modMap = new HashMap<String, ArrayList<Integer>>();
for (ModificationMatch modificationMatch : bestAssumption.getPeptide().getModificationMatches()) {
if (modificationMatch.isVariable()) {
if (!modMap.containsKey(modificationMatch.getTheoreticPtm())) {
modMap.put(modificationMatch.getTheoreticPtm(), new ArrayList<Integer>());
}
modMap.get(modificationMatch.getTheoreticPtm()).add(modificationMatch.getModificationSite());
}
}
boolean first = true, first2;
ArrayList<String> mods = new ArrayList<String>(modMap.keySet());
Collections.sort(mods);
for (String mod : mods) {
if (first) {
first = false;
} else {
writer.write(", ");
}
first2 = true;
writer.write(mod + "(");
for (int aa : modMap.get(mod)) {
if (first2) {
first2 = false;
} else {
writer.write(", ");
}
writer.write(aa + "");
}
writer.write(")");
}
writer.write(SEPARATOR);
}
if (location) {
ArrayList<String> modList = new ArrayList<String>();
for (ModificationMatch modificationMatch : bestAssumption.getPeptide().getModificationMatches()) {
if (modificationMatch.isVariable()) {
PTM refPtm = ptmFactory.getPTM(modificationMatch.getTheoreticPtm());
for (String equivalentPtm : ptmProfile.getSimilarNotFixedModifications(refPtm.getMass())) {
if (!modList.contains(equivalentPtm)) {
modList.add(equivalentPtm);
}
}