Author: jm
Date: 2011-05-13 12:41:41 -0700 (Fri, 13 May 2011)
New Revision: 25052

Added:
   
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/read/datatable/CSVCyReader.java
   
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/read/datatable/ColumnInfo.java
   
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/read/datatable/TableInfo.java
   
core3/io-impl/trunk/src/test/java/org/cytoscape/io/internal/read/datatable/CSVCyReaderTest.java
Log:
Added preliminary support for reading CSV files with CyTable/CyColumn schema 
details

Added: 
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/read/datatable/CSVCyReader.java
===================================================================
--- 
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/read/datatable/CSVCyReader.java
                         (rev 0)
+++ 
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/read/datatable/CSVCyReader.java
 2011-05-13 19:41:41 UTC (rev 25052)
@@ -0,0 +1,150 @@
+package org.cytoscape.io.internal.read.datatable;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.cytoscape.io.read.CyTableReader;
+import org.cytoscape.model.CyRow;
+import org.cytoscape.model.CyTable;
+import org.cytoscape.model.CyTableFactory;
+import org.cytoscape.work.TaskMonitor;
+
+import au.com.bytecode.opencsv.CSVReader;
+
+public class CSVCyReader implements CyTableReader {
+
+       private Pattern classPattern = Pattern.compile("([^<>]+)(<(.*?)>)?");
+       
+       private final InputStream stream;
+       private final boolean readSchema;
+       private final CyTableFactory tableFactory;
+       
+       private boolean isCanceled;
+       private CyTable table;
+
+
+       public CSVCyReader(InputStream stream, boolean readSchema, 
CyTableFactory tableFactory) {
+               this.stream = stream;
+               this.readSchema = readSchema;
+               this.tableFactory = tableFactory;
+       }
+       
+       @Override
+       public void cancel() {
+               isCanceled = true;
+       }
+
+       @Override
+       public void run(TaskMonitor taskMonitor) throws Exception {
+               CSVReader reader = new CSVReader(new InputStreamReader(stream));
+               TableInfo info = readHeader(reader);
+               table = createTable(reader, info);
+       }
+
+       CyTable createTable(CSVReader reader, TableInfo info) throws 
IOException, SecurityException {
+               ColumnInfo[] columns = info.getColumns();
+               CyTable table = tableFactory.createTable(info.getTitle(), 
columns[0].getName(), columns[0].getType(), info.isPublic(), info.isMutable());
+               
+               for (int i = 1; i < columns.length; i++) {
+                       ColumnInfo column = columns[i];
+                       Class<?> type = column.getType();
+                       if (type.equals(List.class)) {
+                               table.createListColumn(column.getName(), 
column.getListElementType(), !column.isMutable());
+                       } else {
+                               table.createColumn(column.getName(), type, 
!column.isMutable());
+                       }
+               }
+               String[] values = reader.readNext();
+               while (values != null) {
+                       if (isCanceled) {
+                               return null;
+                       }
+                       Object key = parseValue(columns[0].getType(), null, 
values[0]);
+                       CyRow row = table.getRow(key);
+                       for (int i = 1; i < values.length; i++) {
+                               ColumnInfo column = columns[i];
+                               String name = column.getName();
+                               Object value = parseValue(column.getType(), 
column.getListElementType(), values[i]);
+                               if (value != null) {
+                                       row.set(name, value);
+                               }
+                       }
+                       values = reader.readNext();
+               }
+               return table;
+       }
+
+       Object parseValue(Class<?> type, Class<?> listElementType, String 
value) {
+               if (type.equals(List.class)) {
+                       List<Object> list = new ArrayList<Object>();
+                       String[] values = value.split("\n");
+                       for (String item : values) {
+                               list.add(parseValue(listElementType, null, 
item));
+                       }
+                       return list;
+               } else if (type.equals(String.class)) {
+                       return value;
+               } else {
+                       try {
+                               Method method = type.getMethod("valueOf", 
String.class);
+                               return method.invoke(null, value);
+                       } catch (Exception e) {
+                               return null;
+                       }
+               }
+       }
+       
+       TableInfo readHeader(CSVReader reader) throws IOException, 
ClassNotFoundException {
+               String[] values = reader.readNext();
+               TableInfo table = new TableInfo();
+               ColumnInfo[] columns = new ColumnInfo[values.length];
+               for (int i = 0; i < values.length; i++) {
+                       ColumnInfo column = new ColumnInfo();
+                       column.setName(values[i]);
+                       columns[i] = column;
+               }
+               table.setColumns(columns);
+               if (!readSchema) {
+                       return table;
+               }
+               values = reader.readNext();
+               for (int i = 0; i < values.length; i++) {
+                       Matcher matcher = classPattern.matcher(values[i]);
+                       matcher.matches();
+                       String typeName = matcher.group(1);
+                       Class<?> type = Class.forName(typeName);
+                       if (type.equals(List.class)) {
+                               String elementName = matcher.group(3);
+                               Class<?> elementType = 
Class.forName(elementName);
+                               columns[i].setListElementType(elementType);
+                       } else {
+                               columns[i].setType(type);
+                       }
+               }
+               values = reader.readNext();
+               table.setTitle(values[0]);
+               for (String option : values[1].split(",")) {
+                       if ("public".equals(option)) {
+                               table.setPublic(true);
+                       } else if ("mutable".equals(option)) {
+                               table.setMutable(true);
+                       }
+               }
+               return table;
+       }
+
+       @Override
+       public CyTable[] getCyTables() {
+               if (table == null) {
+                       return null;
+               }
+               return new CyTable[] { table };
+       }
+
+}


Property changes on: 
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/read/datatable/CSVCyReader.java
___________________________________________________________________
Added: svn:mime-type
   + text/plain

Added: 
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/read/datatable/ColumnInfo.java
===================================================================
--- 
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/read/datatable/ColumnInfo.java
                          (rev 0)
+++ 
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/read/datatable/ColumnInfo.java
  2011-05-13 19:41:41 UTC (rev 25052)
@@ -0,0 +1,44 @@
+package org.cytoscape.io.internal.read.datatable;
+
+import java.util.List;
+
+public class ColumnInfo {
+
+       private String name;
+       private Class<?> type;
+       private Class<?> elementType;
+       private boolean isMutable;
+
+       public String getName() {
+               return name;
+       }
+
+       public void setName(String newName) {
+               name = newName;
+       }
+
+       public Class<?> getType() {
+               return type;
+       }
+
+       public void setType(Class<?> type) {
+               this.type = type;
+       }
+       
+       public Class<?> getListElementType() {
+               return elementType;
+       }
+       
+       public void setListElementType(Class<?> type) {
+               this.type = List.class;
+               elementType = type;
+       }
+
+       public boolean isMutable() {
+               return isMutable;
+       }
+       
+       public void setMutable(boolean mutable) {
+               isMutable = mutable;
+       }
+}


Property changes on: 
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/read/datatable/ColumnInfo.java
___________________________________________________________________
Added: svn:mime-type
   + text/plain

Added: 
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/read/datatable/TableInfo.java
===================================================================
--- 
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/read/datatable/TableInfo.java
                           (rev 0)
+++ 
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/read/datatable/TableInfo.java
   2011-05-13 19:41:41 UTC (rev 25052)
@@ -0,0 +1,41 @@
+package org.cytoscape.io.internal.read.datatable;
+
+public class TableInfo {
+       
+       private String title;
+       private ColumnInfo[] columns;
+       private boolean isPublic;
+       private boolean isMutable;
+
+       public String getTitle() {
+               return title;
+       }
+       
+       public void setTitle(String title) {
+               this.title = title;
+       }
+       
+       public boolean isPublic() {
+               return isPublic;
+       }
+       
+       public void setPublic(boolean isPublic) {
+               this.isPublic = isPublic;
+       }
+       
+       public boolean isMutable() {
+               return isMutable;
+       }
+       
+       public void setMutable(boolean isMutable) {
+               this.isMutable = isMutable;
+       }
+       
+       public ColumnInfo[] getColumns() {
+               return columns;
+       }
+       
+       public void setColumns(ColumnInfo[] columns) {
+               this.columns = columns;
+       }
+}


Property changes on: 
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/read/datatable/TableInfo.java
___________________________________________________________________
Added: svn:mime-type
   + text/plain

Added: 
core3/io-impl/trunk/src/test/java/org/cytoscape/io/internal/read/datatable/CSVCyReaderTest.java
===================================================================
--- 
core3/io-impl/trunk/src/test/java/org/cytoscape/io/internal/read/datatable/CSVCyReaderTest.java
                             (rev 0)
+++ 
core3/io-impl/trunk/src/test/java/org/cytoscape/io/internal/read/datatable/CSVCyReaderTest.java
     2011-05-13 19:41:41 UTC (rev 25052)
@@ -0,0 +1,103 @@
+package org.cytoscape.io.internal.read.datatable;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+import java.util.List;
+
+import org.cytoscape.model.CyRow;
+import org.cytoscape.model.CyTable;
+import org.cytoscape.model.CyTable.Mutability;
+import org.cytoscape.model.CyTableFactory;
+import org.cytoscape.test.support.DataTableTestSupport;
+import org.cytoscape.work.TaskMonitor;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+public class CSVCyReaderTest {
+       @Mock TaskMonitor taskMonitor;
+       
+       private CyTableFactory tableFactory;
+
+       @Before
+       public void setUp() {
+               DataTableTestSupport tableTestSupport = new 
DataTableTestSupport();
+               tableFactory = tableTestSupport.getDataTableFactory();
+               MockitoAnnotations.initMocks(this);
+       }
+       
+       InputStream createStream(String data) throws 
UnsupportedEncodingException {
+               return new ByteArrayInputStream(data.getBytes("UTF-8"));
+       }
+       
+       @Test
+       public void testReadSimple() throws Exception {
+               String data = "SUID\r\njava.lang.Long\r\ntest 
table,\"public\\,mutable\"\r\n5\r\n6";
+               CSVCyReader reader = new CSVCyReader(createStream(data), true, 
tableFactory);
+               reader.run(taskMonitor);
+               CyTable[] tables = reader.getCyTables();
+               assertNotNull(tables);
+               assertEquals(1, tables.length);
+               CyTable table = tables[0];
+               assertNotNull(table);
+               assertEquals(2, table.getRowCount());
+               CyRow row = table.getRow(5L);
+               assertNotNull(row);
+               Long value = row.get("SUID", Long.class);
+               assertEquals((Long) 5L, value);
+               assertTrue(table.isPublic());
+               assertEquals(Mutability.MUTABLE, table.getMutability());
+               assertEquals("test table", table.getTitle());
+       }
+       
+       @Test
+       public void testReadString() throws Exception {
+               String data = 
"SUID,name\r\njava.lang.Long,java.lang.String\r\ntest 
table,\"public\\,mutable\"\r\n1,Alice\r\n2,Bob\r\n3,Carol";
+               CSVCyReader reader = new CSVCyReader(createStream(data), true, 
tableFactory);
+               reader.run(taskMonitor);
+               CyTable[] tables = reader.getCyTables();
+               CyTable table = tables[0];
+               CyRow row = table.getRow(3L);
+               assertEquals("Carol", row.get("name", String.class));
+       }
+       
+       @Test
+       public void testReadDouble() throws Exception {
+               String data = 
"SUID,weight\r\njava.lang.Long,java.lang.Double\r\ntest 
table,\"public\\,mutable\"\r\n0,0.56\r\n-5,-1.234";
+               CSVCyReader reader = new CSVCyReader(createStream(data), true, 
tableFactory);
+               reader.run(taskMonitor);
+               CyTable[] tables = reader.getCyTables();
+               CyTable table = tables[0];
+               CyRow row = table.getRow(-5L);
+               assertEquals((Double) (-1.234), row.get("weight", 
Double.class));
+       }
+       
+       @Test
+       public void testReadBoolean() throws Exception {
+               String data = 
"SUID,hidden\r\njava.lang.Long,java.lang.Boolean\r\ntest 
table,\"public\\,mutable\"\r\n30,true\r\n40,false\r\n50,true";
+               CSVCyReader reader = new CSVCyReader(createStream(data), true, 
tableFactory);
+               reader.run(taskMonitor);
+               CyTable[] tables = reader.getCyTables();
+               CyTable table = tables[0];
+               CyRow row = table.getRow(50L);
+               assertEquals(Boolean.TRUE, row.get("hidden", Boolean.class));
+       }
+
+       @Test
+       public void testReadList() throws Exception {
+               String data = 
"SUID,list\r\njava.lang.Long,java.util.List<java.lang.String>\r\ntest 
table,\"public\\,mutable\"\r\n1,\"a\rb\rc\"";
+               CSVCyReader reader = new CSVCyReader(createStream(data), true, 
tableFactory);
+               reader.run(taskMonitor);
+               CyTable[] tables = reader.getCyTables();
+               CyTable table = tables[0];
+               CyRow row = table.getRow(1L);
+               List<String> list = row.getList("list", String.class);
+               assertEquals("c", list.get(2));
+       }
+}


Property changes on: 
core3/io-impl/trunk/src/test/java/org/cytoscape/io/internal/read/datatable/CSVCyReaderTest.java
___________________________________________________________________
Added: svn:mime-type
   + text/plain

-- 
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.

Reply via email to