Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added manager metod for saving a MoleculesEditorModel to CSV file

Also published createIndex() to be able to get a model to save
Solves bug 1916
  • Loading branch information...
commit aca6f2c71dc8acc458ad443231ca4c92783ead79 1 parent bf8ace0
Arvid Berg authored committed
View
49 plugins/net.bioclipse.cdk.ui.sdfeditor.tests/src/net/bioclipse/cdk/ui/sdfeditor/business/MolTableTest.java
@@ -0,0 +1,49 @@
+/* *****************************************************************************
+* Copyright (c) 2010 Arvid Berg <goglepox@users.sourceforge.net
+* All rights reserved. This program and the accompanying materials
+* are made available under the terms of the Eclipse Public License v1.0
+* which accompanies this distribution, and is available at
+* http://www.eclipse.org/legal/epl-v10.html
+*
+* Contributors:
+* Arvid Berg
+******************************************************************************/
+package net.bioclipse.cdk.ui.sdfeditor.business;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Map;
+import java.util.TreeMap;
+
+import org.junit.Test;
+
+
+/**
+ * @author arvid
+ *
+ */
+public class MolTableTest {
+
+ @Test
+ public void encodeCSVTest() {
+ MoleculeTableManager moltable = new MoleculeTableManager();
+
+ Map<String,String> testPairs = new TreeMap<String, String>();
+ testPairs.put( "Value", "Value" );
+ testPairs.put( "Value,Value", "\"Value,Value\"" );
+ testPairs.put( "Value\nValue", "\"Value\nValue\"" );
+ testPairs.put( "Value\"Value", "\"Value\"\"Value\"" );
+ testPairs.put( "\"Value\"", "\"Value\"" );
+ testPairs.put( "\"Val\"ue\"", "\"Val\"\"ue\"" );
+ testPairs.put( " Value", "\" Value\"" );
+ testPairs.put( "Value ", "\"Value \"" );
+ testPairs.put( "", "\"\"" );
+ testPairs.put( " ", "\" \"" );
+
+ for(String test:testPairs.keySet()) {
+ String result = moltable.encodeCSV(test);
+ assertEquals( testPairs.get( test ), result );
+ }
+
+ }
+}
View
7 ...ns/net.bioclipse.cdk.ui.sdfeditor/src/net/bioclipse/cdk/ui/sdfeditor/business/IMoleculeTableManager.java
@@ -15,6 +15,7 @@
import net.bioclipse.cdk.domain.ICDKMolecule;
import net.bioclipse.cdk.ui.views.IFileMoleculesEditorModel;
import net.bioclipse.cdk.ui.views.IMoleculesEditorModel;
+import net.bioclipse.core.PublishedMethod;
import net.bioclipse.core.Recorded;
import net.bioclipse.core.business.BioclipseException;
import net.bioclipse.jobs.BioclipseJob;
@@ -37,6 +38,8 @@ public void createSDFIndex( IFile file,
public BioclipseJob<SDFIndexEditorModel> createSDFIndex(IFile file,
BioclipseJobUpdateHook<SDFIndexEditorModel> hook);
@Recorded
+ @PublishedMethod( params ="String file",
+ methodSummary = "Creates a list model of molecules from file")
public SDFIndexEditorModel createSDFIndex( String file );
@Recorded
@@ -83,4 +86,8 @@ public void parseProperties( SDFIndexEditorModel model,
public void calculatePropertiesFor( IFile file,
IPropertyCalculator<?>[] calculator)
throws BioclipseException;
+
+ @PublishedMethod( params="IMoleculesEditorModel mol, String filename",
+ methodSummary="Saves the molecule list as a file as CSV")
+ public void saveAsCSV(IMoleculesEditorModel model, String file) throws Exception;
}
View
82 ...ins/net.bioclipse.cdk.ui.sdfeditor/src/net/bioclipse/cdk/ui/sdfeditor/business/MoleculeTableManager.java
@@ -89,21 +89,19 @@ public void createSDFIndex( IFile file,
//TODO refactor out file.getContent()
private SDFileIndex createIndex( IFile file,
IProgressMonitor monitor) {
-
+ if(file == null || !file.exists()) {
+ throw new IllegalArgumentException("File does not exist ");
+ }
SubMonitor progress = SubMonitor.convert( monitor );
long size = -1;
- if(file != null) {
- try {
- size = EFS.getStore( file.getLocationURI() )
- .fetchInfo().getLength();
- progress.beginTask( "Parsing SDFile",
- (int) (size/1048576));
-
- }catch (CoreException e) {
- logger.debug( "Failed to get size of file" );
- progress.beginTask("Parsing SDFile", IProgressMonitor.UNKNOWN );
- }
- }else {
+ try {
+ size = EFS.getStore( file.getLocationURI() )
+ .fetchInfo().getLength();
+ progress.beginTask( "Parsing SDFile",
+ (int) (size/1048576));
+
+ }catch (CoreException e) {
+ logger.debug( "Failed to get size of file" );
progress.beginTask("Parsing SDFile", IProgressMonitor.UNKNOWN );
}
@@ -612,4 +610,62 @@ public void partialReturn( Void object ) {
}
throw new IllegalArgumentException("Id "+id+" dose not exist");
}
+
+ public void saveAsCSV( IMoleculesEditorModel model,IFile file, IProgressMonitor monitor) throws BioclipseException, IOException, CoreException {
+ SubMonitor submon = SubMonitor.convert( monitor );
+ submon.beginTask( "Writing CSV-file", model.getNumberOfMolecules()*100+200 );
+ String del = ", ";
+ List<Object> avaiableProperties;
+ avaiableProperties = new ArrayList<Object>(model.getAvailableProperties());
+
+ StringBuilder header = new StringBuilder();
+ header.append( "SMILES" ).append( del );
+ for(Object o: avaiableProperties) {
+ header.append( o.toString() ).append( del );
+ }
+ header.append( "\n" );
+ if(submon.isCanceled()) throw new OperationCanceledException();
+ ByteArrayInputStream input = new ByteArrayInputStream( header.toString().getBytes() );
+ file.create(input, true, submon.newChild( 200 ));
+
+ submon.setWorkRemaining( model.getNumberOfMolecules()*100 );
+ for(int i=0;i<model.getNumberOfMolecules();i++) {
+ SubMonitor childMon = submon.newChild( 100 );
+ StringBuilder sb = new StringBuilder();
+ ICDKMolecule molecule = model.getMoleculeAt( i );
+ sb.append( "\"" ).append( molecule.toSMILES() ).append( "\"" );
+ sb.append( del );
+ SubMonitor childeMon2 = childMon.newChild( 70);
+ childeMon2.setWorkRemaining( avaiableProperties.size() );
+ for(Object key: avaiableProperties) {
+ Object o = molecule.getProperty( key.toString(), Property.USE_CACHED );
+ if(o != null) {
+ String value = o instanceof String?(String)o:o.toString();
+ value = encodeCSV( value );
+ sb.append( value );
+ }
+ sb.append( del );
+ if(childeMon2.isCanceled()) throw new OperationCanceledException();
+ childeMon2.worked( 1 );
+ }
+ sb.append( "\n" );
+ ByteArrayInputStream bais = new ByteArrayInputStream( sb.toString().getBytes() );
+ file.appendContents( bais, IFile.FORCE, childMon );
+ }
+ }
+
+ String encodeCSV(String value) {
+ if(value.isEmpty()) return "\"\"";
+ if(!value.matches( "[^,^\n]*" )) {
+ return "\""+value+"\"";
+ }
+ if(value.contains( "\"" )) {
+ if(value.matches( "\".+\"" )) value = value.substring( 1, value.length()-1 );
+ return "\""+value.replaceAll( "\"", "\"\"" )+"\"";
+ }
+ if(value.startsWith( " " ) || value.endsWith( " " ) ) {
+ return "\"" +value+"\"";
+ }
+ return value;
+ }
}
View
6 plugins/net.bioclipse.cdk.ui.sdfeditor/src/net/bioclipse/cdk/ui/sdfeditor/business/SDFIndexEditorModel.java
@@ -19,11 +19,9 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
-import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
-import java.util.Queue;
import java.util.Set;
import java.util.TreeMap;
@@ -100,6 +98,7 @@ private SDFIndexEditorModel() {
public SDFIndexEditorModel(SDFileIndex input) {
this();
this.input = input;
+ if(getNumberOfMolecules()>0) getMoleculeAt( 0 );
}
public IFile getResource() {
@@ -221,10 +220,10 @@ private void setLastRead( int index, ICDKMolecule mol ) {
private void readProperties(ICDKMolecule molecule) {
if(availableProperties==null) {
availableProperties = new HashSet<Object>();
+ }
availableProperties.addAll(
molecule.getAtomContainer()
.getProperties().keySet());
- }
}
public Collection<Object> getAvailableProperties() {
@@ -246,6 +245,7 @@ public void removePropertyKey(Object key) {
setDirty( true );
}
+ @SuppressWarnings("unchecked")
public <T> T getPropertyFor(int moleculeIndex,String property) {
Map<String,Object> props = molProps.get(moleculeIndex);
Class<?> c = propertyList.get( property );
Please sign in to comment.
Something went wrong with that request. Please try again.