public Object execute(ExecutionEvent event) throws ExecutionException { ISelection sel = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection(); if (!sel.isEmpty() && sel instanceof IStructuredSelection) { IStructuredSelection ssel = (IStructuredSelection) sel; ICDKManager cdk = Activator.getDefault().getJavaCDKManager(); IUIManager ui = net.bioclipse.ui.business.Activator.getDefault().getUIManager(); List<IMolecule> molecules = new ArrayList<IMolecule>(ssel.size()); for (Object file : ssel.toList()) molecules.add(cdk.loadMolecule((IFile) file)); String path = "/Virtual/matrix.csv"; while (ui.fileExists(path)) path = "/Virtual/matrix" + UUID.randomUUID() + ".csv"; try { String matrix = cdk.calculateTanimoto(molecules, path); ui.open(matrix, "net.bioclipse.editors.MatrixGridEditor"); } catch (BioclipseException cause) { throw new ExecutionException("Error while calculating Tanimoto matrix...", cause); } } return null; }
@Override public Object execute(ExecutionEvent event) throws ExecutionException { ISelection sel = HandlerUtil.getCurrentSelection(event); if (!(sel instanceof IStructuredSelection)) throw new ExecutionException("Selection is not a SMILES file"); IStructuredSelection ssel = (IStructuredSelection) sel; // We onlu operate on a single file currently Object obj = ssel.getFirstElement(); if (!(obj instanceof IFile)) throw new ExecutionException("Selection is not a SMILES file"); final IFile input = (IFile) obj; IPath outPath = input.getFullPath().removeFileExtension().addFileExtension("sdf"); final IFile output = ResourcesPlugin.getWorkspace().getRoot().getFile(outPath); if (ui.fileExists(output)) { if (!MessageDialog.openConfirm( PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "SD-file already exists, confirm overwrite", "A file with the same name but the sdf file ending " + "already exists. Okey to overwrite it?")) { return null; } } Job job = new Job("Converting " + input.getFullPath().toPortableString() + " to SDF") { @Override protected IStatus run(IProgressMonitor monitor) { monitor.beginTask("Converting SMILES", 100); ICDKManager cdk = Activator.getDefault().getJavaCDKManager(); int count = 0; Scanner s; try { s = new Scanner(input.getContents()); } catch (CoreException e) { LogUtils.handleException(e, logger, "net.bioclipse.cdk.ui"); return new Status( IStatus.ERROR, net.bioclipse.cdk.ui.Activator.PLUGIN_ID, "Error, failed to read file."); } while (s.hasNextLine()) { s.nextLine(); count++; } Iterator<ICDKMolecule> iterator = null; try { iterator = cdk.createMoleculeIterator(input); } catch (Exception e) { LogUtils.handleException(e, logger, "net.bioclipse.cdk.ui"); return new Status( IStatus.ERROR, net.bioclipse.cdk.ui.Activator.PLUGIN_ID, "Error, failed to read file."); } if (ui.fileExists(output)) { ui.remove(output); } monitor.beginTask("Converting file", count); AtomTypeAwareSaturationChecker ataSatChecker = new AtomTypeAwareSaturationChecker(); // FixBondOrdersTool bondOrderTool = new FixBondOrdersTool(); long timestamp = System.currentTimeMillis(); long before = timestamp; int current = 0; int last = 0; int failedCount = 0; while (iterator.hasNext()) { try { ICDKMolecule mol = iterator.next(); boolean filterout = false; for (IAtom atom : mol.getAtomContainer().atoms()) { if (atom.getAtomTypeName() == null || atom.getAtomTypeName().equals("X")) { filterout = true; } } if (!filterout) { // IMolecule newAC = // bondOrderTool.kekuliseAromaticRings((IMolecule) // mol.getAtomContainer()); IAtomContainer newAC = mol.getAtomContainer(); AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(newAC); ataSatChecker.decideBondOrder(newAC); mol = new CDKMolecule(newAC); cdk.appendToSDF(output, mol); } else { ++failedCount; } } catch (BioclipseException e) { ++failedCount; logger.error(e.getMessage(), e); } catch (CDKException e) { ++failedCount; logger.warn("Could not deduce bond orders"); } current++; if (System.currentTimeMillis() - timestamp > 1000) { monitor.subTask( "Done: " + current + "/" + count + " (" + TimeCalculator.generateTimeRemainEst(before, current, count) + " )"); monitor.worked(current - last); last = current; synchronized (monitor) { if (monitor.isCanceled()) { return Status.CANCEL_STATUS; } } timestamp = System.currentTimeMillis(); } } monitor.done(); logger.debug("Wrote file" + output); if (failedCount != 0) { setProperty(IProgressConstants.KEEP_PROPERTY, true); } return new Status( IStatus.OK, net.bioclipse.cdk.ui.Activator.PLUGIN_ID, "Failed to convert " + failedCount + " molecules"); } }; job.setUser(true); job.schedule(); return null; }