From 48805284a0e9866525e7318cb6245193678b7fba Mon Sep 17 00:00:00 2001 From: KaiLiCn Date: Fri, 2 Dec 2022 15:05:28 -0500 Subject: [PATCH] Improve import speed 1. Move predicted spectra importing 2. Some bugs fixed --- .../GUI/Export/ExportExpectedSizeDialog.java | 14 +- src/main/java/GUI/GUIMainClass.java | 60 ++++---- src/main/java/GUI/SpectrumMainPanel.java | 57 +++++++- src/main/java/GUI/utils/ImportData.java | 129 +++--------------- 4 files changed, 102 insertions(+), 158 deletions(-) diff --git a/src/main/java/GUI/Export/ExportExpectedSizeDialog.java b/src/main/java/GUI/Export/ExportExpectedSizeDialog.java index 96dcca6..5f71529 100644 --- a/src/main/java/GUI/Export/ExportExpectedSizeDialog.java +++ b/src/main/java/GUI/Export/ExportExpectedSizeDialog.java @@ -73,6 +73,9 @@ public class ExportExpectedSizeDialog extends JDialog { * Check peptide or not */ private Boolean isCheckPeptide; + private Boolean isNormPeptide; + private Boolean isCheckSpectra; + private Boolean isPredict; /** * Pattern removing illegal */ @@ -96,7 +99,7 @@ public class ExportExpectedSizeDialog extends JDialog { * @param spectrumKey Spectrum Key */ public ExportExpectedSizeDialog(SpectrumMainPanel spectrumMainPanel, JLayeredPane jLayeredPane, SequenceFragmentationPanel secondarySpectrumPlotsJPanel, JPanel mirrorSequenceFragmentationPanel, - SpectrumContainer spectrumJPanel, Integer peptideSize, String spectrumKey, Boolean isCheckPeptide){ + SpectrumContainer spectrumJPanel, Integer peptideSize, String spectrumKey, Boolean isNormPeptide, Boolean isCheckSpectra, Boolean isCheckPeptide, Boolean isPredict){ super(spectrumMainPanel.parentFrame, true); this.jLayeredPane = jLayeredPane; @@ -104,6 +107,9 @@ public ExportExpectedSizeDialog(SpectrumMainPanel spectrumMainPanel, JLayeredPan this.mirrorSequenceFragmentationPanel = mirrorSequenceFragmentationPanel; this.spectrumJPanel = spectrumJPanel; this.isCheckPeptide = isCheckPeptide; + this.isNormPeptide = isNormPeptide; + this.isCheckSpectra = isCheckSpectra; + this.isPredict = isPredict; this.spectrumMainPanel = spectrumMainPanel; this.parentJFrame = spectrumMainPanel.parentFrame; this.peptideSize = peptideSize; @@ -339,12 +345,14 @@ public void windowClosing(WindowEvent evt) { * @param evt window event */ private void formWindowClosing(WindowEvent evt) { - if(mirrorSequenceFragmentationPanel != null){ + if(isCheckSpectra){ spectrumMainPanel.showMirrorJMenuItemActionPerformed(null); } else if(isCheckPeptide){ spectrumMainPanel.showCheckPeptideJMenuItemActionPerformed(null); - } else { + } else if (isNormPeptide){ spectrumMainPanel.showSpectrumJMenuItemAction(null); + } else if (isPredict){ + spectrumMainPanel.showPredictionJMenuItemActionPerformed(null); } spectrumMainPanel.setBounds(0, 0, oldWidth, oldHeight); spectrumMainPanel.spectrumShowPanel.setBounds(0, 0, oldWidth, oldHeight); diff --git a/src/main/java/GUI/GUIMainClass.java b/src/main/java/GUI/GUIMainClass.java index 4721c56..f49ffe2 100644 --- a/src/main/java/GUI/GUIMainClass.java +++ b/src/main/java/GUI/GUIMainClass.java @@ -260,7 +260,7 @@ public class GUIMainClass extends JFrame { /** * Results folder */ - private File resultsFolder; + public File resultsFolder; /** * Threads number */ @@ -276,7 +276,7 @@ public class GUIMainClass extends JFrame { /** * */ - private HashMap experimentInfo = new HashMap<>(); + public HashMap experimentInfo = new HashMap<>(); /** * */ @@ -313,6 +313,14 @@ public class GUIMainClass extends JFrame { * */ public HashMap predictionEntryHashMap = new HashMap<>(); + /** + * Experiment information + */ + public ArrayList expInformation = new ArrayList<>(); + /** + * + */ + public Boolean hasPredictionSpectra = false; /** * */ @@ -2427,7 +2435,10 @@ private void spectrumJTableKeyReleased(KeyEvent evt) { } else { spectrumMatch = sqliteConnection.getSpectrumMatch(selectedPsmKey); } - updateSpectrum(getSpectrum(selectedPsmKey), spectrumMatch); + String spectrumTitle = sqliteConnection.getSpectrumOldTitle(selectedPsmKey); + checkSpectrumFactory(spectrumTitle.split("\\.")[0]); + + updateSpectrum(getSpectrum(selectedPsmKey), spectrumMatch); } catch (SQLException | FileParsingException | MzMLUnmarshallerException | IOException e) { e.printStackTrace(); } @@ -3219,38 +3230,13 @@ private void checkSpectrumFactory(String spectrumFileName){ if (!finishedSpectrumFiles.contains(spectrumFileName)) { if (readFactoryThread.isAlive()) { - readFactoryThread.interrupt(); + while (readFactoryThread.isInterrupted()){ + readFactoryThread.interrupt(); + } + updateSpectrumFactory(true, spectrumFileName); } } -// ProgressDialogX progressDialog = new ProgressDialogX(this, -// Toolkit.getDefaultToolkit().getImage(getClass().getResource("/icons/SeaGullMass.png")), -// Toolkit.getDefaultToolkit().getImage(getClass().getResource("/icons/SeaGullMassWait.png")), -// true); -// progressDialog.setPrimaryProgressCounterIndeterminate(true); -// progressDialog.setTitle("Load new spectrum file. Please Wait..."); -// -// new Thread(() -> { -// try { -// progressDialog.setVisible(true); -// } catch (IndexOutOfBoundsException ignored) { -// } -// }, "ProgressDialog").start(); -// new Thread("DisplayThread") { -// @Override -// public void run() { -// -// if (!finishedSpectrumFiles.contains(spectrumFileName)) { -// if (readFactoryThread.isAlive()) { -// readFactoryThread.interrupt(); -// updateSpectrumFactory(true, spectrumFileName); -// } -// } -// -// progressDialog.setRunFinished(); -// } -// }.start(); - } /** @@ -3440,6 +3426,8 @@ public void run() { spectrumFileOrder = oneImport.getSpectrumFileOrder(); spectrumFileMap = oneImport.getSpectrumFileMap(); predictionEntryHashMap = oneImport.getPredictionEntryHashMap(); + expInformation = oneImport.getExpInformation(); + hasPredictionSpectra = oneImport.getHasPredictionSpectra(); psmScoreName = oneImport.getPSMScoreName(); proteinScoreName = oneImport.getProteinScoreName(); @@ -3529,14 +3517,16 @@ public void run() { if (Files.exists(new File(spectralFilePath).toPath())) { loadingJButton.setText(eachFileName); // try { -// Thread.sleep(10000); +// sleep(1000); // } catch (InterruptedException e) { // e.printStackTrace(); // } try { readSpectrumFile(spectralFilePath); - } catch (IOException | ClassNotFoundException e) { - e.printStackTrace(); + } catch (Exception e) { + if (e.getClass() != InterruptedException.class){ + e.printStackTrace(); + } } } else { JOptionPane.showMessageDialog( diff --git a/src/main/java/GUI/SpectrumMainPanel.java b/src/main/java/GUI/SpectrumMainPanel.java index 939fd1b..b4c86a4 100644 --- a/src/main/java/GUI/SpectrumMainPanel.java +++ b/src/main/java/GUI/SpectrumMainPanel.java @@ -21,9 +21,11 @@ import com.compomics.util.gui.spectrum.FragmentIonTable; import com.compomics.util.gui.spectrum.SequenceFragmentationPanel; import com.compomics.util.gui.spectrum.SpectrumPanel; +import com.compomics.util.gui.waiting.waitinghandlers.ProgressDialogX; import com.compomics.util.preferences.LastSelectedFolder; import com.compomics.util.preferences.SequenceMatchingPreferences; import com.compomics.util.preferences.UtilitiesUserPreferences; +import umich.ms.fileio.filetypes.diann.DiannSpeclibReader; import umich.ms.fileio.filetypes.diann.PredictionEntry; import javax.swing.*; @@ -1136,7 +1138,7 @@ private void exportSpectrumAsMGF(){ */ private void exportSpectrumAsFigure() { - ExportExpectedSizeDialog exportExpectedSizeDialog = new ExportExpectedSizeDialog(this, spectrumJLayeredPane, sequenceFragmentationPanel, null, spectrumPanel, currentPeptideSequence.length(), currentSpectrum.getSpectrumTitle(), false); + ExportExpectedSizeDialog exportExpectedSizeDialog = new ExportExpectedSizeDialog(this, spectrumJLayeredPane, sequenceFragmentationPanel, null, spectrumPanel, currentPeptideSequence.length(), currentSpectrum.getSpectrumTitle(), true, false, false, false); spectrumSetAction.setExportDialog(exportExpectedSizeDialog); @@ -1148,7 +1150,7 @@ private void exportSpectrumAsFigure() { */ private void exportMirrorSpectrumAsFigure() { - ExportExpectedSizeDialog exportExpectedSizeDialog = new ExportExpectedSizeDialog(this, mirrorJLayeredPane, sequenceFragmentationPanelMirror, mirrorFragmentPanel, mirrorSpectrumPanel, currentPeptideSequence.length(), currentSpectrum.getSpectrumTitle(), false); + ExportExpectedSizeDialog exportExpectedSizeDialog = new ExportExpectedSizeDialog(this, mirrorJLayeredPane, sequenceFragmentationPanelMirror, mirrorFragmentPanel, mirrorSpectrumPanel, currentPeptideSequence.length(), currentSpectrum.getSpectrumTitle(), false, true, false, false); mirrorSetAction.setExportDialog(exportExpectedSizeDialog); @@ -1160,7 +1162,7 @@ private void exportMirrorSpectrumAsFigure() { */ private void exportCheckSpectrumAsFigure() { - ExportExpectedSizeDialog exportExpectedSizeDialog = new ExportExpectedSizeDialog(this, checkPeptideJLayeredPane, sequenceFragmentationPanelCheck, checkFragmentPanel, checkPeptideSpectrumPanel, currentPeptideSequence.length(), currentSpectrum.getSpectrumTitle(), true); + ExportExpectedSizeDialog exportExpectedSizeDialog = new ExportExpectedSizeDialog(this, checkPeptideJLayeredPane, sequenceFragmentationPanelCheck, checkFragmentPanel, checkPeptideSpectrumPanel, currentPeptideSequence.length(), currentSpectrum.getSpectrumTitle(), false, false, true, false); checkSetAction.setExportDialog(exportExpectedSizeDialog); @@ -1172,7 +1174,7 @@ private void exportCheckSpectrumAsFigure() { */ private void exportPreSpectrumAsFigure() { - ExportExpectedSizeDialog exportExpectedSizeDialog = new ExportExpectedSizeDialog(this, predictionJLayeredPane, sequenceFragmentationPanelPredicted, predictedFragmentPanel, predictedSpectrumPanel, currentPeptideSequence.length(), currentSpectrum.getSpectrumTitle(), false); + ExportExpectedSizeDialog exportExpectedSizeDialog = new ExportExpectedSizeDialog(this, predictionJLayeredPane, sequenceFragmentationPanelPredicted, predictedFragmentPanel, predictedSpectrumPanel, currentPeptideSequence.length(), currentSpectrum.getSpectrumTitle(), false, false, false, true); preSetAction.setExportDialog(exportExpectedSizeDialog); @@ -1345,7 +1347,7 @@ public void showCheckPeptideJMenuItemActionPerformed(ActionEvent evt) { peptideCheckMenu.setVisible(true); } - private void showPredictionJMenuItemActionPerformed(ActionEvent evt){ + public void showPredictionJMenuItemActionPerformed(ActionEvent evt){ showSpectrumSelected = false; ionTableSelected = false; mirrorSelected = false; @@ -1360,6 +1362,47 @@ private void showPredictionJMenuItemActionPerformed(ActionEvent evt){ exportGraphicsMenu.setVisible(true); + if (parentFrame.predictionEntryHashMap.size() == 0){ + ProgressDialogX progressDialog = new ProgressDialogX(parentFrame, + Toolkit.getDefaultToolkit().getImage(getClass().getResource("/icons/SeaGullMass.png")), + Toolkit.getDefaultToolkit().getImage(getClass().getResource("/icons/SeaGullMassWait.png")), + true); + progressDialog.setPrimaryProgressCounterIndeterminate(true); + progressDialog.setTitle("First time Loading predicted spectra. Please Wait..."); + + new Thread(() -> { + try { + progressDialog.setVisible(true); + } catch (IndexOutOfBoundsException ignored) { + } + }, "ProgressDialog").start(); + new Thread("Import_Spectra") { + @Override + public void run() { + try { + if (parentFrame.expInformation.contains("inner_defined_empty_exp")) { + DiannSpeclibReader dslr = new DiannSpeclibReader(parentFrame.resultsFolder.getAbsolutePath() + "/spectraRT.predicted.bin"); + parentFrame.predictionEntryHashMap = dslr.getPreds(); + } else { + for (File eachFileInMax : Objects.requireNonNull(parentFrame.resultsFolder.listFiles())) { + if (parentFrame.expInformation.contains(eachFileInMax.getName())) { + DiannSpeclibReader dslr = new DiannSpeclibReader(eachFileInMax.getAbsolutePath() + "/spectraRT.predicted.bin"); + parentFrame.predictionEntryHashMap.putAll(dslr.getPreds()); + } + } + } + progressDialog.setRunFinished(); + updateSpectrum(); + } catch (Exception e){ + progressDialog.setRunFinished(); + JOptionPane.showMessageDialog( + parentFrame, "Failed to load predicted spectra, please check it.", + "Loading spectrum file error", JOptionPane.ERROR_MESSAGE); + } + } + }.start(); + } + mainShowJPanel.removeAll(); GroupLayout mainShowJPanelLayout = new GroupLayout(mainShowJPanel); mainShowJPanel.setLayout(mainShowJPanelLayout); @@ -2162,7 +2205,7 @@ public void updateSpectrum(){ predictionJLayeredPane.add(sequenceFragmentationPanelPredicted); zoomAction(sequenceFragmentationPanelPredicted, modSequence, false); - mirrorSetAction = new SetAction(this, predictionJLayeredPane, sequenceFragmentationPanelPredicted, predictedFragmentPanel, predictedSpectrumPanel, 0, 0, spectrumShowPanel); + preSetAction = new SetAction(this, predictionJLayeredPane, sequenceFragmentationPanelPredicted, predictedFragmentPanel, predictedSpectrumPanel, 0, 0, spectrumShowPanel); ArrayList> allAnnotations = new ArrayList<>(); allAnnotations.add(annotations); @@ -2358,7 +2401,7 @@ public void updateSpectrum(SpectrumIdentificationAssumption spectrumIdentificati } } - if (parentFrame.predictionEntryHashMap.size() == 0){ + if (!parentFrame.hasPredictionSpectra){ showPredictionJMenuItem.setEnabled(false); } diff --git a/src/main/java/GUI/utils/ImportData.java b/src/main/java/GUI/utils/ImportData.java index abea34e..5e7a50f 100644 --- a/src/main/java/GUI/utils/ImportData.java +++ b/src/main/java/GUI/utils/ImportData.java @@ -104,6 +104,10 @@ public class ImportData { * */ private HashMap predictionEntryHashMap = new HashMap<>(); + /** + * + */ + private Boolean hasPredictionSpectra = false; /** * */ @@ -240,18 +244,6 @@ private Boolean checkFileOpen(File eachFile){ } private void goThroughFolder() throws IOException { - -// ArrayList allManiFile = new ArrayList<>(); -// -// for(File eachFileInMax : Objects.requireNonNull(resultsFolder.listFiles())) { -// -// if (eachFileInMax.getName().endsWith("manifest")) { -// allManiFile.add(eachFileInMax.getAbsolutePath()); -// } -// } -// -// allManiFile.sort(Collections.reverseOrder()); -// latestManiFestFile = new File(allManiFile.get(0)); processManifestFile(latestManiFestFile); if (expInformation.contains("inner_defined_empty_exp")){ @@ -265,9 +257,9 @@ private void goThroughFolder() throws IOException { }}); if (new File(resultsFolder.getAbsolutePath() + "/spectraRT.predicted.bin").exists()){ - DiannSpeclibReader dslr = new DiannSpeclibReader(resultsFolder.getAbsolutePath() + "/spectraRT.predicted.bin"); - - predictionEntryHashMap = dslr.getPreds(); + hasPredictionSpectra = true; +// DiannSpeclibReader dslr = new DiannSpeclibReader(resultsFolder.getAbsolutePath() + "/spectraRT.predicted.bin"); +// predictionEntryHashMap = dslr.getPreds(); } } else { @@ -285,9 +277,10 @@ private void goThroughFolder() throws IOException { }}); if (new File(eachFileInMax.getAbsolutePath() + "/spectraRT.predicted.bin").exists()){ - DiannSpeclibReader dslr = new DiannSpeclibReader(eachFileInMax.getAbsolutePath() + "/spectraRT.predicted.bin"); - - predictionEntryHashMap.putAll(dslr.getPreds()); + hasPredictionSpectra = true; +// DiannSpeclibReader dslr = new DiannSpeclibReader(eachFileInMax.getAbsolutePath() + "/spectraRT.predicted.bin"); +// +// predictionEntryHashMap.putAll(dslr.getPreds()); } } @@ -343,7 +336,7 @@ private void processManifestFile(File mainFestFile) throws IOException { if (!isDIAUmpire){ ddaSpectrumFiles.get(expName).add(lineSplit[0]); } else { - String mzmlName = lineSplit[0].split(pattern)[lineSplit[0].split(pattern).length - 1].split("\\.d")[0]; + String mzmlName = lineSplit[0].split(pattern)[lineSplit[0].split(pattern).length - 1].split("\\.")[0]; for (File eachFileInMax : Objects.requireNonNull(resultsFolder.listFiles())){ if (eachFileInMax.getName().startsWith(mzmlName + "_") && eachFileInMax.getName().endsWith("mzML")){ diaSpectrumFiles.get(expName).add(eachFileInMax.getAbsolutePath()); @@ -382,7 +375,6 @@ private void processManifestFile(File mainFestFile) throws IOException { } } } - bufferedReader.close(); } @@ -461,95 +453,6 @@ private void importExistDB(String dbName) throws SQLException, ClassNotFoundExce // processSpectralFilesBack(resultsDict.keySet()); } - private void processSpectralFiles(String expName) throws IOException, ClassNotFoundException { - expName = expName.replace("_Dash_", "-"); - long time1 = System.currentTimeMillis(); - for (String spectralFilePath:ddaSpectrumFiles.get(expName)){ - if (Files.exists(new File(spectralFilePath).toPath())) { - readSpectrumFile(spectralFilePath); - } else { - JOptionPane.showMessageDialog( - null, "Invalid spectrum file path, please check it.", - "Loading spectrum file error", JOptionPane.ERROR_MESSAGE); - progressDialog.setRunFinished(); - break; - } - } - System.out.println("Read mzml cost: " + (System.currentTimeMillis() - time1)/1000); - for (String spectralFilePath:diaSpectrumFiles.get(expName)){ - if (Files.exists(new File(spectralFilePath).toPath())) { - readSpectrumFile(spectralFilePath); - } else { - JOptionPane.showMessageDialog( - null, "Invalid spectrum file path, please check it.", - "Loading spectrum file error", JOptionPane.ERROR_MESSAGE); - progressDialog.setRunFinished(); - break; - } - } - - } - - private void readSpectrumFile(String spectralFilePath) throws IOException, ClassNotFoundException { - String spectrumName = new File(spectralFilePath).getName().split("\\.")[0]; - if (!spectrumFileTypes.containsKey(spectrumName)){ - - if (spectralFilePath.endsWith("mgf")){ - spectrumFactory.addSpectra(new File(spectralFilePath)); - mgfFiles.add(spectralFilePath); - spectrumFileTypes.put(spectrumName, "mgf"); - } else if (spectralFilePath.endsWith("raw")){ - if (new File(spectralFilePath.replace(".raw", "_uncalibrated.mgf")).exists()){ -// spectrumFactory.addSpectra(new File(spectralFilePath.replace(".raw", "_calibrated.mgf"))); -// mgfFiles.add(spectralFilePath.replace(".raw", "_calibrated.mgf")); -// spectrumFileTypes.put(spectrumName, "_calibrated.mgf"); - addOneMzML(spectrumName, spectralFilePath.replace(".raw", "_uncalibrated.mzml")); - } else { -// spectrumFactory.addSpectra(new File(spectralFilePath.replace(".raw", "_uncalibrated.mgf"))); -// mgfFiles.add(spectralFilePath.replace(".raw", "_uncalibrated.mgf")); -// spectrumFileTypes.put(spectrumName, "_uncalibrated.mgf"); - addOneMzML(spectrumName, spectralFilePath.replace(".raw", "_calibrated.mzml")); - } - - } else if (spectralFilePath.endsWith(".d")){ - if (new File(spectralFilePath.replace(".d", "_uncalibrated.mgf")).exists()){ -// spectrumFactory.addSpectra(new File(spectralFilePath.replace(".d", "_calibrated.mgf"))); -// mgfFiles.add(spectralFilePath.replace(".d", "_calibrated.mgf")); -// spectrumFileTypes.put(spectrumName, "_calibrated.mgf"); - addOneMzML(spectrumName, spectralFilePath.replace(".d", "_uncalibrated.mzml")); - } else { -// spectrumFactory.addSpectra(new File(spectralFilePath.replace(".d", "_uncalibrated.mgf"))); -// mgfFiles.add(spectralFilePath.replace(".d", "_uncalibrated.mgf")); -// spectrumFileTypes.put(spectrumName, "_uncalibrated.mgf"); - addOneMzML(spectrumName, spectralFilePath.replace(".d", "_calibrated.mzml")); - } - - } else if (spectralFilePath.toLowerCase().endsWith("mzml")){ - addOneMzML(spectrumName, spectralFilePath); - - } else if (spectralFilePath.toLowerCase().endsWith("mzxml")){ - //Will do - } - } - } - - private void addOneMzML(String spectrumName, String spectralFilePath){ - MZMLFile mzmlFile = new MZMLFile(spectralFilePath); - mzmlFile.setNumThreadsForParsing(threadsNumber); - ScanCollectionDefault scans = new ScanCollectionDefault(); - scans.setDefaultStorageStrategy(StorageStrategy.SOFT); - scans.isAutoloadSpectra(true); - scans.setDataSource(mzmlFile); - try { - scans.loadData(LCMSDataSubset.STRUCTURE_ONLY); - scansFileHashMap.put(spectrumName, scans); - } catch (Exception e) { - progressDialog.setRunFinished(); - e.printStackTrace(); - } - spectrumFileTypes.put(spectrumName, "mzml"); - } - private void processTable() throws SQLException, IOException, ClassNotFoundException { Connection connection = sqliteConnection.getConnection(); @@ -1389,12 +1292,12 @@ public HashMap getSpectrumFileTypes() { return spectrumFileTypes; } - public SpectrumFactory getSpectrumFactory() { - return spectrumFactory; + public Boolean getHasPredictionSpectra() { + return hasPredictionSpectra; } - public ArrayList getMgfFiles() { - return mgfFiles; + public ArrayList getExpInformation() { + return expInformation; } public HashMap getPredictionEntryHashMap() {