Skip to content

Commit

Permalink
fixed bug: 3220
Browse files Browse the repository at this point in the history
Added method for adding molecule entry to SDF file
  • Loading branch information
jonalv committed Jun 13, 2012
1 parent b1cafdb commit a8bdf2a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
Expand Up @@ -55,6 +55,7 @@
import net.bioclipse.ui.business.IUIManager;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.resources.WorkspaceJob;
Expand Down Expand Up @@ -1377,6 +1378,35 @@ public void testSaveMDLMolfile() throws Exception {
assertEquals("CCC", mol.toSMILES(
));
}

@Test
public void testAppendToSDF() throws Exception {

IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject( "testAppendToSDF" );
try {
project.create( new NullProgressMonitor() );
project.open( new NullProgressMonitor() );
final ICDKMolecule propane = cdk.fromSMILES("CCC");

ICDKMolecule ethane = cdk.fromSMILES("CC");
String path = "/testAppendToSDF/testSaveSDF" + propane.hashCode() + ".mol";
cdk.saveSDFile( path, new ArrayList() {{add(propane);}} );

assertEquals(1, cdk.loadMolecules(path).size());

cdk.appendToSDF( path, ethane );

List<ICDKMolecule> molecules = cdk.loadMolecules(path);
assertEquals( "CCC",
cdk.calculateSMILES( molecules.get( 0 ) ) );

assertEquals( "CC",
cdk.calculateSMILES( molecules.get(1) ) );
}
finally {
project.delete( true, new NullProgressMonitor() );
}
}

@Test
public void testSaveCML() throws Exception {
Expand Down
@@ -1,6 +1,6 @@
/*******************************************************************************
* Copyright (c) 2008-2009 Ola Spjuth
* 2008-2010 Jonathan Alvarsson
* 2008-2012 Jonathan Alvarsson
* 2008-2009 Stefan Kuhn
* 2008-2009 Egon Willighagen <egonw@users.sf.net>
*
Expand All @@ -17,6 +17,8 @@
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand Down Expand Up @@ -2156,6 +2158,30 @@ public String determineFormat( String path ) throws IOException,
);
return format == null ? "Unknown" : format.getFormatName();
}

public void appendToSDF( IFile sdFile, ICDKMolecule molecule ) throws BioclipseException {
try {
SDFWriter writer
= new SDFWriter( EFS.getStore(sdFile.getLocationURI())
.openOutputStream(
EFS.APPEND,
new NullProgressMonitor() ) );
try {
writer.write( molecule.getAtomContainer() );
writer.close();
} catch ( CDKException e ) {
throw new BioclipseException(
"Failed in writing molecule to file", e );
} catch ( IOException e ) {
throw new BioclipseException(
"Failed in writing molecule to file", e );
}
} catch ( CoreException e ) {
throw new BioclipseException( "Could not open file: " + sdFile
+ " for writing.",
e );
}
}

public void saveSDFile(final IFile file, List<? extends IMolecule> entries,
IProgressMonitor monitor)
Expand Down
Expand Up @@ -1186,5 +1186,14 @@ public void saveSDFile( IFile molFile, List<? extends IMolecule> mols,
public List<List<IMolecule>> randomSplit2parts(List<IMolecule> mols_in, double firstRatio);

public List<ICDKMolecule> loadSMILESFile( IFile file,
IProgressMonitor monitor );
IProgressMonitor monitor );

@Recorded
@PublishedMethod(
params="String sdFile, ICDKMolecule molecule",
methodSummary="Append a molecule at the end of a given sd file."
)
public void appendToSDF( String sdFile, ICDKMolecule molecule ) throws BioclipseException;

public void appendToSDF( IFile sdFile, ICDKMolecule molecule ) throws BioclipseException;
}

0 comments on commit a8bdf2a

Please sign in to comment.