Author: mes Date: 2011-10-10 17:06:24 -0700 (Mon, 10 Oct 2011) New Revision: 27120
Added: csplugins/trunk/ucsd/mes/cytable-reader/ csplugins/trunk/ucsd/mes/cytable-reader/pom.xml csplugins/trunk/ucsd/mes/cytable-reader/src/ csplugins/trunk/ucsd/mes/cytable-reader/src/main/ csplugins/trunk/ucsd/mes/cytable-reader/src/main/assembly/ csplugins/trunk/ucsd/mes/cytable-reader/src/main/assembly/src.xml csplugins/trunk/ucsd/mes/cytable-reader/src/main/java/ csplugins/trunk/ucsd/mes/cytable-reader/src/main/java/cytoscape/ csplugins/trunk/ucsd/mes/cytable-reader/src/main/java/cytoscape/cytable/ csplugins/trunk/ucsd/mes/cytable-reader/src/main/java/cytoscape/cytable/CyTableReader.java csplugins/trunk/ucsd/mes/cytable-reader/src/main/java/cytoscape/cytable/CyTableReaderAction.java csplugins/trunk/ucsd/mes/cytable-reader/src/main/java/cytoscape/cytable/CyTableReaderPlugin.java csplugins/trunk/ucsd/mes/cytable-reader/src/main/java/cytoscape/cytable/ImportCyTableTask.java csplugins/trunk/ucsd/mes/cytable-reader/src/main/resources/ csplugins/trunk/ucsd/mes/cytable-reader/src/main/resources/cytoscape/ csplugins/trunk/ucsd/mes/cytable-reader/src/main/resources/cytoscape/cytable/ csplugins/trunk/ucsd/mes/cytable-reader/src/main/resources/cytoscape/cytable/plugin.props csplugins/trunk/ucsd/mes/cytable-reader/src/test/ csplugins/trunk/ucsd/mes/cytable-reader/src/test/java/ csplugins/trunk/ucsd/mes/cytable-reader/src/test/java/cytoscape/ csplugins/trunk/ucsd/mes/cytable-reader/src/test/java/cytoscape/cytable/ csplugins/trunk/ucsd/mes/cytable-reader/src/test/java/cytoscape/cytable/CyTableReaderTest.java Log: first pass at a cytable reader for 2.x Added: csplugins/trunk/ucsd/mes/cytable-reader/pom.xml =================================================================== --- csplugins/trunk/ucsd/mes/cytable-reader/pom.xml (rev 0) +++ csplugins/trunk/ucsd/mes/cytable-reader/pom.xml 2011-10-11 00:06:24 UTC (rev 27120) @@ -0,0 +1,113 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>cytoscape</groupId> + <artifactId>cytable-reader</artifactId> + <version>1.0-SNAPSHOT</version> + <packaging>jar</packaging> + <name>CyTable Reader Plugin</name> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>2.3.2</version> + <configuration> + <source>1.6</source> + <target>1.6</target> + <optimize>true</optimize> + <showWarnings>true</showWarnings> + <showDeprecation>true</showDeprecation> + </configuration> + </plugin> + <plugin> + <artifactId>maven-assembly-plugin</artifactId> + <version>2.2-beta-5</version> + <configuration> + <archive> + <manifestEntries> + <Cytoscape-Plugin>cytoscape.cytable.CyTableReaderPlugin</Cytoscape-Plugin> + </manifestEntries> + </archive> + <descriptors> + <descriptor>src/main/assembly/src.xml</descriptor> + </descriptors> + </configuration> + <executions> + <execution> + <id>make-assembly</id> + <phase>package</phase> + <goals> + <goal>single</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> + + <dependencies> + <dependency> + <groupId>cytoscape</groupId> + <artifactId>application</artifactId> + <version>2.8.3-SNAPSHOT</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.8.2</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-all</artifactId> + <version>1.8.5</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>cytoscape-temp</groupId> + <artifactId>opencsv</artifactId> + <version>2.1</version> + </dependency> + </dependencies> + + <repositories> + <repository> + <id>cytoscape_snapshots</id> + <snapshots> + <enabled>true</enabled> + </snapshots> + <releases> + <enabled>false</enabled> + </releases> + <name>Cytoscape Snapshots</name> + <url>http://cytoscape.wodaklab.org/nexus/content/repositories/snapshots/</url> + </repository> + <repository> + <id>cytoscape_releases</id> + <snapshots> + <enabled>false</enabled> + </snapshots> + <releases> + <enabled>true</enabled> + </releases> + <name>Cytoscape Releases</name> + <url>http://cytoscape.wodaklab.org/nexus/content/repositories/releases/</url> + </repository> + <repository> + <id>cytoscape_thirdparty</id> + <snapshots> + <enabled>false</enabled> + </snapshots> + <releases> + <enabled>true</enabled> + </releases> + <name>Cytoscape Third Party</name> + <url>http://cytoscape.wodaklab.org/nexus/content/repositories/thirdparty/</url> + </repository> + </repositories> + +</project> Added: csplugins/trunk/ucsd/mes/cytable-reader/src/main/assembly/src.xml =================================================================== --- csplugins/trunk/ucsd/mes/cytable-reader/src/main/assembly/src.xml (rev 0) +++ csplugins/trunk/ucsd/mes/cytable-reader/src/main/assembly/src.xml 2011-10-11 00:06:24 UTC (rev 27120) @@ -0,0 +1,18 @@ +<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd"> + <id>jar-with-dependencies</id> + <formats> + <format>jar</format> + </formats> + <includeBaseDirectory>false</includeBaseDirectory> + <dependencySets> + <dependencySet> + <outputDirectory>/</outputDirectory> + <useProjectArtifact>true</useProjectArtifact> + <unpack>true</unpack> + <scope>runtime</scope> + <useTransitiveDependencies>false</useTransitiveDependencies> + </dependencySet> + </dependencySets> +</assembly> Added: csplugins/trunk/ucsd/mes/cytable-reader/src/main/java/cytoscape/cytable/CyTableReader.java =================================================================== --- csplugins/trunk/ucsd/mes/cytable-reader/src/main/java/cytoscape/cytable/CyTableReader.java (rev 0) +++ csplugins/trunk/ucsd/mes/cytable-reader/src/main/java/cytoscape/cytable/CyTableReader.java 2011-10-11 00:06:24 UTC (rev 27120) @@ -0,0 +1,194 @@ + +/* + Copyright (c) 2006, 2007, The Cytoscape Consortium (www.cytoscape.org) + + The Cytoscape Consortium is: + - Institute for Systems Biology + - University of California San Diego + - Memorial Sloan-Kettering Cancer Center + - Institut Pasteur + - Agilent Technologies + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published + by the Free Software Foundation; either version 2.1 of the License, or + any later version. + + This library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF + MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. The software and + documentation provided hereunder is on an "as is" basis, and the + Institute for Systems Biology and the Whitehead Institute + have no obligations to provide maintenance, support, + updates, enhancements or modifications. In no event shall the + Institute for Systems Biology and the Whitehead Institute + be liable to any party for direct, indirect, special, + incidental or consequential damages, including lost profits, arising + out of the use of this software and its documentation, even if the + Institute for Systems Biology and the Whitehead Institute + have been advised of the possibility of such damage. See + the GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this library; if not, write to the Free Software Foundation, + Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. +*/ + +package cytoscape.cytable; + +import cytoscape.CyEdge; +import cytoscape.CyNetwork; +import cytoscape.CyNode; +import cytoscape.Cytoscape; + +import cytoscape.data.CyAttributes; +import cytoscape.data.Semantics; + +import java.net.URL; + +import java.util.*; + + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.io.Reader; + +import au.com.bytecode.opencsv.CSVReader; + + + +public class CyTableReader { + + private CyAttributes attrs; + private CSVReader reader; + private static final byte JUST_A_GUESS = Byte.MIN_VALUE; + + // for testing + CyTableReader(Reader streamReader, CyAttributes attrs) throws IOException { + this.reader = new CSVReader(streamReader); + this.attrs = attrs; + } + + public CyTableReader(URL tableUrl, CyAttributes attrs) throws IOException { + this(new InputStreamReader(tableUrl.openStream()),attrs); + } + + public void read() throws IOException { + int count = 0; + String[] attrNames = null; + byte[] types = null; + String[] values; + while ( (values = reader.readNext()) != null ) { + if ( count == 0 ) { + attrNames = values; + } else if ( count == 1 ) { + types = getTypes(values); + validateTypes(attrNames,types); + if ( typesAreAllGuesses(types) ) + setAttrValues(attrNames,types,values); + } else { + setAttrValues(attrNames, types, values); + } + count++; + } + } + + private boolean typesAreAllGuesses(byte[] types) { + boolean ret = true; + for ( int i = 0; i < types.length; i++ ) { + // we've found a type that isn't a guess, so + // assume that the rest of the row is as designed + if ( types[i] != JUST_A_GUESS ) + ret = false; + + // convert all guesses to attrs of type string + else + types[i] = CyAttributes.TYPE_STRING; + } + + return ret; + } + + + private void validateTypes(String[] attrNames, byte[] types) { + if ( attrNames == null ) + throw new NullPointerException("attrNames array is null"); + + if ( types == null ) + throw new NullPointerException("types array is null"); + + if ( attrNames.length != types.length ) + throw new RuntimeException("type array is not the same length as the attr name array"); + + for ( int i = 0; i < attrNames.length; i++ ) { + final byte existingType = attrs.getType(attrNames[i]); + if ( existingType == CyAttributes.TYPE_UNDEFINED ) + continue; + if ( existingType != types[i] ) + throw new RuntimeException("attr types for: " + attrNames[i] + " don't match (expected) " + existingType + " (found) " + types[i]); + } + } + + private byte[] getTypes(String[] typeStrings) { + byte[] types = new byte[typeStrings.length]; + for ( int i = 0; i < typeStrings.length; i++ ) + types[i] = guessType( typeStrings[i] ); + return types; + } + + private byte guessType(String typeString) { + String lc = typeString.trim().toLowerCase(); + if ( lc.equals("string") || lc.equals("java.lang.string") ) + return CyAttributes.TYPE_STRING; + else if ( lc.equals("integer") || lc.equals("java.lang.integer") || lc.equals("int") || lc.equals("java.lang.int") ) + return CyAttributes.TYPE_INTEGER; + else if ( lc.equals("float") || lc.equals("java.lang.float") || lc.equals("double") || lc.equals("java.lang.double") ) + return CyAttributes.TYPE_FLOATING; + else if ( lc.equals("boolean") || lc.equals("java.lang.boolean") || lc.equals("bool") ) + return CyAttributes.TYPE_BOOLEAN; + else + return JUST_A_GUESS; + } + + private void setAttrValues(String[] attrNames, byte[] types, String[] values) { + if ( values == null || values[0] == null ) + return; + String id = values[0]; + for ( int i = 1; i < attrNames.length; i++ ) { + // skip non-existing values + if ( values.length == i ) + return; + if ( values[i] == null ) + continue; + + if ( types[i] == CyAttributes.TYPE_STRING ) { + attrs.setAttribute(id,attrNames[i],values[i]); + } else if ( types[i] == CyAttributes.TYPE_INTEGER ) { + try { + Integer val = Integer.valueOf(values[i]); + attrs.setAttribute(id,attrNames[i],val); + } catch ( NumberFormatException nfe ) { + // ignore + } + } else if ( types[i] == CyAttributes.TYPE_FLOATING ) { + try { + Double val = Double.valueOf(values[i]); + attrs.setAttribute(id,attrNames[i],val); + } catch ( NumberFormatException nfe ) { + // ignore + } + } else if ( types[i] == CyAttributes.TYPE_BOOLEAN ) { + attrs.setAttribute(id,attrNames[i],Boolean.valueOf(values[i])); + } + // we don't support other types of attrs + } + } +} Added: csplugins/trunk/ucsd/mes/cytable-reader/src/main/java/cytoscape/cytable/CyTableReaderAction.java =================================================================== --- csplugins/trunk/ucsd/mes/cytable-reader/src/main/java/cytoscape/cytable/CyTableReaderAction.java (rev 0) +++ csplugins/trunk/ucsd/mes/cytable-reader/src/main/java/cytoscape/cytable/CyTableReaderAction.java 2011-10-11 00:06:24 UTC (rev 27120) @@ -0,0 +1,53 @@ +package cytoscape.cytable; + +import cytoscape.Cytoscape; +import cytoscape.CytoscapeInit; + +import cytoscape.task.Task; +import cytoscape.task.TaskMonitor; + +import cytoscape.task.ui.JTaskConfig; + +import cytoscape.task.util.TaskManager; + +import cytoscape.util.CyFileFilter; +import cytoscape.util.CytoscapeAction; +import cytoscape.util.FileUtil; + +import java.awt.event.ActionEvent; + +import java.io.File; + + +public class CyTableReaderAction extends CytoscapeAction { + + private final String type; + + public CyTableReaderAction(String type) { + super(type + " Attribute CyTable"); + setPreferredMenu("File.Import"); + this.type = type; + } + + public void actionPerformed(ActionEvent e) { + + CyFileFilter nf = new CyFileFilter(".cytable"); + + File[] files = FileUtil.getFiles("Import " + type + " Attribute CyTable", FileUtil.LOAD, + new CyFileFilter[] { nf }); + + if (files != null) { + ImportCyTableTask task = new ImportCyTableTask(files, type); + + JTaskConfig jTaskConfig = new JTaskConfig(); + jTaskConfig.setOwner(Cytoscape.getDesktop()); + jTaskConfig.displayCloseButton(true); + jTaskConfig.displayStatus(true); + jTaskConfig.setAutoDispose(false); + + TaskManager.executeTask(task, jTaskConfig); + } + } +} + + Added: csplugins/trunk/ucsd/mes/cytable-reader/src/main/java/cytoscape/cytable/CyTableReaderPlugin.java =================================================================== --- csplugins/trunk/ucsd/mes/cytable-reader/src/main/java/cytoscape/cytable/CyTableReaderPlugin.java (rev 0) +++ csplugins/trunk/ucsd/mes/cytable-reader/src/main/java/cytoscape/cytable/CyTableReaderPlugin.java 2011-10-11 00:06:24 UTC (rev 27120) @@ -0,0 +1,15 @@ +package cytoscape.cytable; + +import cytoscape.Cytoscape; +import cytoscape.plugin.CytoscapePlugin; + + +public class CyTableReaderPlugin extends CytoscapePlugin { + + public CyTableReaderPlugin() { + Cytoscape.getDesktop().getCyMenus().addAction( new CyTableReaderAction("Node")); + Cytoscape.getDesktop().getCyMenus().addAction( new CyTableReaderAction("Edge")); + } +} + + Added: csplugins/trunk/ucsd/mes/cytable-reader/src/main/java/cytoscape/cytable/ImportCyTableTask.java =================================================================== --- csplugins/trunk/ucsd/mes/cytable-reader/src/main/java/cytoscape/cytable/ImportCyTableTask.java (rev 0) +++ csplugins/trunk/ucsd/mes/cytable-reader/src/main/java/cytoscape/cytable/ImportCyTableTask.java 2011-10-11 00:06:24 UTC (rev 27120) @@ -0,0 +1,74 @@ + +package cytoscape.cytable; + +import cytoscape.Cytoscape; +import cytoscape.CytoscapeInit; + +import cytoscape.data.servers.BioDataServer; + +import cytoscape.task.Task; +import cytoscape.task.TaskMonitor; + +import cytoscape.task.ui.JTaskConfig; + +import cytoscape.task.util.TaskManager; + +import cytoscape.util.CyFileFilter; +import cytoscape.util.CytoscapeAction; +import cytoscape.util.FileUtil; + +import java.awt.event.ActionEvent; + +import java.io.File; + + +class ImportCyTableTask implements Task { + private TaskMonitor taskMonitor; + private File[] files; + private String type; + + ImportCyTableTask(File[] files, String type) { + this.files = files; + this.type = type; + } + + /** + * Executes Task. + */ + public void run() { + try { + taskMonitor.setPercentCompleted(-1); + taskMonitor.setStatus("Reading CyTable"); + + for (int i = 0; i < files.length; ++i) { + taskMonitor.setPercentCompleted((100 * i) / files.length); + + if (type.equals("Node") ) + new CyTableReader(files[i].toURI().toURL(), Cytoscape.getNodeAttributes() ).read(); + else if (type.equals("Edge") ) + new CyTableReader(files[i].toURI().toURL(), Cytoscape.getEdgeAttributes() ).read(); + else + throw new RuntimeException("Unknown table type: " + type); + } + + taskMonitor.setPercentCompleted(100); + Cytoscape.firePropertyChange(Cytoscape.ATTRIBUTES_CHANGED, null, null); + taskMonitor.setStatus("Done"); + } catch (Exception e) { + taskMonitor.setException(e, e.getMessage()); + } + } + + public void halt() { + // Task can not currently be halted. + } + + public void setTaskMonitor(TaskMonitor taskMonitor) throws IllegalThreadStateException { + this.taskMonitor = taskMonitor; + } + + public String getTitle() { + return "Loading " + type + " Attributes CyTable"; + } + +} Added: csplugins/trunk/ucsd/mes/cytable-reader/src/main/resources/cytoscape/cytable/plugin.props =================================================================== --- csplugins/trunk/ucsd/mes/cytable-reader/src/main/resources/cytoscape/cytable/plugin.props (rev 0) +++ csplugins/trunk/ucsd/mes/cytable-reader/src/main/resources/cytoscape/cytable/plugin.props 2011-10-11 00:06:24 UTC (rev 27120) @@ -0,0 +1,26 @@ +# This props file would be filled out and included in the plugin jar file. +# This props file will be used to put information into the Plugin Manager +# about the plugin + +# -- The following properties are REQUIRED -- # + +# The plugin name that will be displayed to users +pluginName=CyTableReaderPlugin + +# Description used to give users information about the plugin such as what it does. +# Html tags are encouraged for formatting purposes. +pluginDescription=CyTable Reader Plugin + +# Plugin version number, this must be two numbers separated by a decimlal. Ex. 0.2, 14.03 +pluginVersion=0.1 + +# Compatible Cytoscape version +cytoscapeVersion=2.8 + +# Category, use one of the categories listed on the website or create your own +pluginCategory=Network and Attribute I/O + +# List of authors. Note each author and institution pair are separated by a : (colon) +# each additional author institution pair must be separated from other pairs bye a ; (semicolon) +pluginAuthorsIntsitutions=Mike Smoot:UCSD + Added: csplugins/trunk/ucsd/mes/cytable-reader/src/test/java/cytoscape/cytable/CyTableReaderTest.java =================================================================== --- csplugins/trunk/ucsd/mes/cytable-reader/src/test/java/cytoscape/cytable/CyTableReaderTest.java (rev 0) +++ csplugins/trunk/ucsd/mes/cytable-reader/src/test/java/cytoscape/cytable/CyTableReaderTest.java 2011-10-11 00:06:24 UTC (rev 27120) @@ -0,0 +1,294 @@ + +package cytoscape.cytable; + + +import cytoscape.data.CyAttributes; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.StringReader; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; + +import static org.mockito.Mockito.*; + + +public class CyTableReaderTest { + + @Test + public void testGoodAttrs() throws Exception { + + String data = "ID,homer_string,marge_int,bart_double,lisa_boolean\n" + + "string,String,java.lang.Integer,Double,boolean\n" + + "a,homer,1,0.23,false\n" + + "b,marge,4,.24,true\n"; + + CyAttributes attrs = mock(CyAttributes.class); + when(attrs.getType("ID")).thenReturn(CyAttributes.TYPE_STRING); + when(attrs.getType("homer_string")).thenReturn(CyAttributes.TYPE_STRING); + when(attrs.getType("marge_int")).thenReturn(CyAttributes.TYPE_INTEGER); + when(attrs.getType("bart_double")).thenReturn(CyAttributes.TYPE_FLOATING); + when(attrs.getType("lisa_boolean")).thenReturn(CyAttributes.TYPE_BOOLEAN); + + new CyTableReader(new StringReader(data),attrs).read(); + + verify(attrs).setAttribute("a","homer_string","homer"); + verify(attrs).setAttribute("b","homer_string","marge"); + + verify(attrs).setAttribute("a","marge_int",Integer.valueOf("1")); + verify(attrs).setAttribute("b","marge_int",Integer.valueOf("4")); + + verify(attrs).setAttribute("a","bart_double",Double.valueOf("0.23")); + verify(attrs).setAttribute("b","bart_double",Double.valueOf(".24")); + + verify(attrs).setAttribute("a","lisa_boolean",Boolean.valueOf("false")); + verify(attrs).setAttribute("b","lisa_boolean",Boolean.valueOf("true")); + } + + + // The attributes with these names DO NOT already exist, so we + // can guess that they're strings. + @Test + public void testMissingTypeRowNoPreExistingAttrs() throws Exception { + + String data = "ID,homer_string,marge_int,bart_double,lisa_boolean\n" + + "a,homer,1,0.23,false\n" + + "b,marge,4,0.24,true\n"; + + CyAttributes attrs = mock(CyAttributes.class); + when(attrs.getType(anyString())).thenReturn(CyAttributes.TYPE_UNDEFINED); + + new CyTableReader(new StringReader(data),attrs).read(); + + verify(attrs).setAttribute("a","homer_string","homer"); + verify(attrs).setAttribute("b","homer_string","marge"); + + verify(attrs).setAttribute("a","marge_int","1"); + verify(attrs).setAttribute("b","marge_int","4"); + + verify(attrs).setAttribute("a","bart_double","0.23"); + verify(attrs).setAttribute("b","bart_double","0.24"); + + verify(attrs).setAttribute("a","lisa_boolean","false"); + verify(attrs).setAttribute("b","lisa_boolean","true"); + } + + // The attributes with these names DO already exist, so we + // CANNOT guess their type, so we expect an exception. + @Test(expected=Exception.class) + public void testMissingTypeRowWithPreExistingAttrs() throws Exception { + + String data = "ID,homer_string,marge_int,bart_double,lisa_boolean\n" + + "a,homer,1,0.23,false\n" + + "b,marge,4,0.24,true\n"; + + CyAttributes attrs = mock(CyAttributes.class); + when(attrs.getType("ID")).thenReturn(CyAttributes.TYPE_STRING); + when(attrs.getType("homer_string")).thenReturn(CyAttributes.TYPE_STRING); + when(attrs.getType("marge_int")).thenReturn(CyAttributes.TYPE_INTEGER); + when(attrs.getType("bart_double")).thenReturn(CyAttributes.TYPE_FLOATING); + when(attrs.getType("lisa_boolean")).thenReturn(CyAttributes.TYPE_BOOLEAN); + + new CyTableReader(new StringReader(data),attrs).read(); + } + + + // the second row of attrs is missing trailing values, which should be OK + @Test + public void testMissingTrailingValuesRow() throws Exception { + + String data = "ID,homer_string,marge_int,bart_double,lisa_boolean\n" + + "string,String,java.lang.Integer,Double,boolean\n" + + "a,homer,1,0.23,false\n" + + "b,marge,4,,\n"; + + CyAttributes attrs = mock(CyAttributes.class); + when(attrs.getType("ID")).thenReturn(CyAttributes.TYPE_STRING); + when(attrs.getType("homer_string")).thenReturn(CyAttributes.TYPE_STRING); + when(attrs.getType("marge_int")).thenReturn(CyAttributes.TYPE_INTEGER); + when(attrs.getType("bart_double")).thenReturn(CyAttributes.TYPE_FLOATING); + when(attrs.getType("lisa_boolean")).thenReturn(CyAttributes.TYPE_BOOLEAN); + + new CyTableReader(new StringReader(data),attrs).read(); + + verify(attrs).setAttribute("a","homer_string","homer"); + verify(attrs).setAttribute("b","homer_string","marge"); + + verify(attrs).setAttribute("a","marge_int",Integer.valueOf("1")); + verify(attrs).setAttribute("b","marge_int",Integer.valueOf("4")); + + verify(attrs).setAttribute("a","bart_double",Double.valueOf("0.23")); + + verify(attrs).setAttribute("a","lisa_boolean",Boolean.valueOf("false")); + } + + // the second row of attrs is missing values in the middle of the row, which should be OK + @Test + public void testMissingMiddleValuesRow() throws Exception { + + String data = "ID,homer_string,marge_int,bart_double,lisa_boolean\n" + + "string,String,java.lang.Integer,Double,boolean\n" + + "a,homer,1,0.23,false\n" + + "b,marge,,,true\n"; + + CyAttributes attrs = mock(CyAttributes.class); + when(attrs.getType("ID")).thenReturn(CyAttributes.TYPE_STRING); + when(attrs.getType("homer_string")).thenReturn(CyAttributes.TYPE_STRING); + when(attrs.getType("marge_int")).thenReturn(CyAttributes.TYPE_INTEGER); + when(attrs.getType("bart_double")).thenReturn(CyAttributes.TYPE_FLOATING); + when(attrs.getType("lisa_boolean")).thenReturn(CyAttributes.TYPE_BOOLEAN); + + new CyTableReader(new StringReader(data),attrs).read(); + + verify(attrs).setAttribute("a","homer_string","homer"); + verify(attrs).setAttribute("b","homer_string","marge"); + + verify(attrs).setAttribute("a","marge_int",Integer.valueOf("1")); + + verify(attrs).setAttribute("a","bart_double",Double.valueOf("0.23")); + + verify(attrs).setAttribute("a","lisa_boolean",Boolean.valueOf("false")); + verify(attrs).setAttribute("b","lisa_boolean",Boolean.valueOf("true")); + } + + // the second row of attrs is missing trailing values with no commas, which should be OK + @Test + public void testMissingTrailingValuesWithNoCommasRow() throws Exception { + + String data = "ID,homer_string,marge_int,bart_double,lisa_boolean\n" + + "string,String,java.lang.Integer,Double,boolean\n" + + "a,homer,1,0.23,false\n" + + "b,marge,4\n"; + + CyAttributes attrs = mock(CyAttributes.class); + when(attrs.getType("ID")).thenReturn(CyAttributes.TYPE_STRING); + when(attrs.getType("homer_string")).thenReturn(CyAttributes.TYPE_STRING); + when(attrs.getType("marge_int")).thenReturn(CyAttributes.TYPE_INTEGER); + when(attrs.getType("bart_double")).thenReturn(CyAttributes.TYPE_FLOATING); + when(attrs.getType("lisa_boolean")).thenReturn(CyAttributes.TYPE_BOOLEAN); + + new CyTableReader(new StringReader(data),attrs).read(); + + verify(attrs).setAttribute("a","homer_string","homer"); + verify(attrs).setAttribute("b","homer_string","marge"); + + verify(attrs).setAttribute("a","marge_int",Integer.valueOf("1")); + verify(attrs).setAttribute("b","marge_int",Integer.valueOf("4")); + + verify(attrs).setAttribute("a","bart_double",Double.valueOf("0.23")); + + verify(attrs).setAttribute("a","lisa_boolean",Boolean.valueOf("false")); + } + + // the type row is too short + @Test(expected=Exception.class) + public void testTypeRowTooShort() throws Exception { + + String data = "ID,homer_string,marge_int,bart_double,lisa_boolean\n" + + "string,String,java.lang.Integer\n" + + "a,homer,1,0.23,false\n" + + "b,marge,4,0.24,true\n" ; + + CyAttributes attrs = mock(CyAttributes.class); + when(attrs.getType("ID")).thenReturn(CyAttributes.TYPE_STRING); + when(attrs.getType("homer_string")).thenReturn(CyAttributes.TYPE_STRING); + when(attrs.getType("marge_int")).thenReturn(CyAttributes.TYPE_INTEGER); + when(attrs.getType("bart_double")).thenReturn(CyAttributes.TYPE_FLOATING); + when(attrs.getType("lisa_boolean")).thenReturn(CyAttributes.TYPE_BOOLEAN); + + new CyTableReader(new StringReader(data),attrs).read(); + } + + // Wrong int values for specified types + @Test + public void testWrongValuesForIntType() throws Exception { + + String data = "ID,homer_string,marge_int,bart_double,lisa_boolean\n" + + "string,String,java.lang.Integer,Double,boolean\n" + + "a,homer,0.1,0.23,false\n" + + "b,marge,asdf,.24,true\n" ; + + CyAttributes attrs = mock(CyAttributes.class); + when(attrs.getType("ID")).thenReturn(CyAttributes.TYPE_STRING); + when(attrs.getType("homer_string")).thenReturn(CyAttributes.TYPE_STRING); + when(attrs.getType("marge_int")).thenReturn(CyAttributes.TYPE_INTEGER); + when(attrs.getType("bart_double")).thenReturn(CyAttributes.TYPE_FLOATING); + when(attrs.getType("lisa_boolean")).thenReturn(CyAttributes.TYPE_BOOLEAN); + + new CyTableReader(new StringReader(data),attrs).read(); + + verify(attrs).setAttribute("a","homer_string","homer"); + verify(attrs).setAttribute("b","homer_string","marge"); + + verify(attrs).setAttribute("a","bart_double",Double.valueOf("0.23")); + verify(attrs).setAttribute("b","bart_double",Double.valueOf(".24")); + + verify(attrs).setAttribute("a","lisa_boolean",Boolean.valueOf("false")); + verify(attrs).setAttribute("b","lisa_boolean",Boolean.valueOf("true")); + } + + // Wrong double values for specified types + @Test + public void testWrongValuesForDoubleType() throws Exception { + + String data = "ID,homer_string,marge_int,bart_double,lisa_boolean\n" + + "string,String,java.lang.Integer,Double,boolean\n" + + "a,homer,1,23,false\n" + + "b,marge,4,asdf,true\n" ; + + CyAttributes attrs = mock(CyAttributes.class); + when(attrs.getType("ID")).thenReturn(CyAttributes.TYPE_STRING); + when(attrs.getType("homer_string")).thenReturn(CyAttributes.TYPE_STRING); + when(attrs.getType("marge_int")).thenReturn(CyAttributes.TYPE_INTEGER); + when(attrs.getType("bart_double")).thenReturn(CyAttributes.TYPE_FLOATING); + when(attrs.getType("lisa_boolean")).thenReturn(CyAttributes.TYPE_BOOLEAN); + + new CyTableReader(new StringReader(data),attrs).read(); + + verify(attrs).setAttribute("a","homer_string","homer"); + verify(attrs).setAttribute("b","homer_string","marge"); + + verify(attrs).setAttribute("a","marge_int",Integer.valueOf("1")); + verify(attrs).setAttribute("b","marge_int",Integer.valueOf("4")); + + verify(attrs).setAttribute("a","bart_double",Double.valueOf("23")); + + verify(attrs).setAttribute("a","lisa_boolean",Boolean.valueOf("false")); + verify(attrs).setAttribute("b","lisa_boolean",Boolean.valueOf("true")); + } + + // Wrong boolean values for specified types + @Test + public void testWrongValuesForBooleanType() throws Exception { + + String data = "ID,homer_string,marge_int,bart_double,lisa_boolean\n" + + "string,String,java.lang.Integer,Double,boolean\n" + + "a,homer,1,23,homer\n" + + "b,marge,4,1.32,marge\n" ; + + CyAttributes attrs = mock(CyAttributes.class); + when(attrs.getType("ID")).thenReturn(CyAttributes.TYPE_STRING); + when(attrs.getType("homer_string")).thenReturn(CyAttributes.TYPE_STRING); + when(attrs.getType("marge_int")).thenReturn(CyAttributes.TYPE_INTEGER); + when(attrs.getType("bart_double")).thenReturn(CyAttributes.TYPE_FLOATING); + when(attrs.getType("lisa_boolean")).thenReturn(CyAttributes.TYPE_BOOLEAN); + + new CyTableReader(new StringReader(data),attrs).read(); + + verify(attrs).setAttribute("a","homer_string","homer"); + verify(attrs).setAttribute("b","homer_string","marge"); + + verify(attrs).setAttribute("a","marge_int",Integer.valueOf("1")); + verify(attrs).setAttribute("b","marge_int",Integer.valueOf("4")); + + verify(attrs).setAttribute("a","bart_double",Double.valueOf("23")); + verify(attrs).setAttribute("b","bart_double",Double.valueOf("1.32")); + + verify(attrs).setAttribute("a","lisa_boolean",Boolean.FALSE); + verify(attrs).setAttribute("b","lisa_boolean",Boolean.FALSE); + } +} -- You received this message because you are subscribed to the Google Groups "cytoscape-cvs" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/cytoscape-cvs?hl=en.
