Search in sources :

Example 1 with Importer

use of de.unijena.cheminf.mortar.model.io.Importer in project MORTAR by FelixBaensch.

the class MainViewController method importMoleculeFile.

// 
/**
 * Loads molecule file and opens molecules tab
 *
 * @param aParentStage Stage
 */
private void importMoleculeFile(Stage aParentStage) {
    if (this.moleculeDataModelList.size() > 0) {
        if (!this.isFragmentationStopAndDataLossConfirmed()) {
            return;
        }
    }
    Importer tmpImporter = new Importer(this.settingsContainer);
    File tmpFile = tmpImporter.loadFile(aParentStage);
    if (Objects.isNull(tmpFile)) {
        return;
    }
    if (this.isFragmentationRunning) {
        this.interruptFragmentation();
    }
    if (this.isImportRunningProperty.get()) {
        this.interruptImport();
    }
    if (this.isExportRunningProperty.get()) {
        this.interruptExport();
    }
    this.clearGuiAndCollections();
    this.importTask = new Task<>() {

        @Override
        protected IAtomContainerSet call() throws Exception {
            IAtomContainerSet tmpSet = tmpImporter.importMoleculeFile(tmpFile);
            return tmpSet;
        }
    };
    this.importTask.setOnSucceeded(event -> {
        // note: setOnSucceeded() takes place in the JavaFX GUI thread again but still runLater() is necessary to wait
        // for the thread to be free for the update
        Platform.runLater(() -> {
            IAtomContainerSet tmpAtomContainerSet = null;
            try {
                tmpAtomContainerSet = importTask.get();
            } catch (InterruptedException | ExecutionException anException) {
                MainViewController.LOGGER.log(Level.SEVERE, anException.toString(), anException);
                GuiUtil.guiExceptionAlert(Message.get("Error.ExceptionAlert.Title"), Message.get("Importer.FileImportExceptionAlert.Header"), Message.get("Importer.FileImportExceptionAlert.Text") + "\n" + FileUtil.getAppDirPath() + File.separator + BasicDefinitions.LOG_FILES_DIRECTORY + File.separator, anException);
                this.updateStatusBar(this.importerThread, Message.get("Status.importFailed"));
            }
            if (tmpAtomContainerSet == null || tmpAtomContainerSet.isEmpty()) {
                return;
            }
            this.mainView.getMainMenuBar().getExportMenu().setDisable(true);
            this.primaryStage.setTitle(Message.get("Title.text") + " - " + tmpImporter.getFileName() + " - " + tmpAtomContainerSet.getAtomContainerCount() + " molecules");
            int tmpExceptionCount = 0;
            for (IAtomContainer tmpAtomContainer : tmpAtomContainerSet.atomContainers()) {
                // returns null if no SMILES code could be created
                String tmpSmiles = ChemUtil.createUniqueSmiles(tmpAtomContainer);
                if (tmpSmiles == null) {
                    tmpExceptionCount++;
                    continue;
                }
                MoleculeDataModel tmpMoleculeDataModel;
                if (this.settingsContainer.getKeepAtomContainerInDataModelSetting()) {
                    tmpMoleculeDataModel = new MoleculeDataModel(tmpAtomContainer);
                } else {
                    tmpMoleculeDataModel = new MoleculeDataModel(tmpSmiles, tmpAtomContainer.getTitle(), tmpAtomContainer.getProperties());
                }
                tmpMoleculeDataModel.setName(tmpAtomContainer.getProperty(Importer.MOLECULE_NAME_PROPERTY_KEY));
                this.moleculeDataModelList.add(tmpMoleculeDataModel);
            }
            MainViewController.LOGGER.log(Level.INFO, "Imported " + tmpAtomContainerSet.getAtomContainerCount() + " molecules from file: " + tmpImporter.getFileName() + " " + tmpExceptionCount + " molecules could not be parsed into the internal data model.");
            this.updateStatusBar(this.importerThread, Message.get("Status.loaded"));
            this.isImportRunningProperty.setValue(false);
            this.openMoleculesTab();
        });
    });
    this.importTask.setOnCancelled(event -> {
        this.updateStatusBar(this.importerThread, Message.get("Status.canceled"));
        this.isImportRunningProperty.setValue(false);
    });
    this.importTask.setOnFailed(event -> {
        this.updateStatusBar(this.importerThread, Message.get("Status.importFailed"));
        this.isImportRunningProperty.setValue(false);
        LogUtil.getUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), event.getSource().getException());
    });
    this.importerThread = new Thread(importTask);
    this.importerThread.setName(ThreadType.IMPORT_THREAD.getThreadName());
    this.importerThread.setUncaughtExceptionHandler(LogUtil.getUncaughtExceptionHandler());
    this.importerThread.setDaemon(false);
    // magic number
    this.importerThread.setPriority(Thread.currentThread().getPriority() - 2);
    this.isImportRunningProperty.setValue(true);
    this.updateStatusBar(this.importerThread, Message.get("Status.loading"));
    this.importerThread.start();
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) ExecutionException(java.util.concurrent.ExecutionException) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) MoleculeDataModel(de.unijena.cheminf.mortar.model.data.MoleculeDataModel) ExecutionException(java.util.concurrent.ExecutionException) File(java.io.File) Importer(de.unijena.cheminf.mortar.model.io.Importer)

Aggregations

MoleculeDataModel (de.unijena.cheminf.mortar.model.data.MoleculeDataModel)1 Importer (de.unijena.cheminf.mortar.model.io.Importer)1 File (java.io.File)1 ExecutionException (java.util.concurrent.ExecutionException)1 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)1 IAtomContainerSet (org.openscience.cdk.interfaces.IAtomContainerSet)1