diff --git a/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/TabularDataGraphServlet.java b/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/TabularDataGraphServlet.java index 0c6b2da27aa827f0e3972be1a96423bf0f9646f4..42a0ac587b56ca0afd5566d5699b43ff801e2f3a 100644 --- a/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/TabularDataGraphServlet.java +++ b/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/TabularDataGraphServlet.java @@ -70,14 +70,14 @@ public class TabularDataGraphServlet extends AbstractDatasetDownloadServlet private final String filePath; - private final String graphTypeCode; + private final String graphName; public RequestParams(HttpServletRequest request) { sessionId = getRequiredParameter(request, SESSION_ID_PARAM); datasetCode = getRequiredParameter(request, DATASET_CODE_PARAM); filePath = getRequiredParameter(request, FILE_PATH_PARAM); - graphTypeCode = getRequiredParameter(request, GRAPH_TYPE_CODE); + graphName = getRequiredParameter(request, GRAPH_TYPE_CODE); } private static String getRequiredParameter(final HttpServletRequest request, @@ -135,7 +135,7 @@ public class TabularDataGraphServlet extends AbstractDatasetDownloadServlet // Generate an image image into the stream ITabularDataGraph generator = - configuration.getGraph(params.graphTypeCode, fileLines, response + configuration.getGraph(params.graphName, fileLines, response .getOutputStream()); generator.generateImage(); diff --git a/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/AbstractTabularDataGraph.java b/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/AbstractTabularDataGraph.java index 202adf9e9a20d6eaa82886eae519c00c5933d6fc..a4e3bf4522feb34e8432ec6c512f9790fa4d0d84 100644 --- a/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/AbstractTabularDataGraph.java +++ b/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/AbstractTabularDataGraph.java @@ -63,6 +63,20 @@ abstract class AbstractTabularDataGraph<T extends TabularDataGraphConfiguration> .getImageHeight()); } + /** + * Create an image,overriding the width and height in the configuration from the file lines and + * write it to the output stream. + * + * @param imageWidth The desired width of the image + * @param imageHeight The desired height of the image + * @throws IOException + */ + public void generateImage(int imageWidth, int imageHeight) throws IOException + { + JFreeChart chart = createChart(); + ChartUtilities.writeChartAsPNG(out, chart, imageWidth, imageHeight); + } + // For Subclasses /** * Return the configuration for this graph. diff --git a/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataGraphCollectionConfiguration.java b/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataGraphCollectionConfiguration.java index 19bf44fa8b7628db2ec9ab3d207bf0559087b879..3e6bbdee632cf3fe53dc197df528a2e17553febc 100644 --- a/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataGraphCollectionConfiguration.java +++ b/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataGraphCollectionConfiguration.java @@ -29,6 +29,8 @@ import java.util.Properties; import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException; import ch.systemsx.cisd.common.utilities.PropertyUtils; import ch.systemsx.cisd.openbis.dss.generic.server.plugins.tasks.DatasetFileLines; +import ch.systemsx.cisd.openbis.dss.generic.shared.utils.PropertyParametersUtil; +import ch.systemsx.cisd.openbis.dss.generic.shared.utils.PropertyParametersUtil.SectionProperties; /** * @author Chandrasekhar Ramakrishnan @@ -39,18 +41,59 @@ public class TabularDataGraphCollectionConfiguration private static final String IGNORE_COMMENTS_PROPERTY_KEY = "ignore-comments"; - // the seperator in the file - private final char separator; + // the full width of the graphs when requested + private static final String IMAGE_WIDTH_KEY = "full-width"; + + // the full height of the graphs when requested + private static final String IMAGE_HEIGHT_KEY = "full-height"; + + // the width of the thumbnails shown in the report + private static final String THUMBNAIL_WIDTH_KEY = "column-width"; + + // the height of the thumbnails shown in the report + private static final String THUMBNAIL_HEIGHT_KEY = "column-height"; + + // the graphs to display -- each one is shown in a column. + private static final String GRAPHS_KEY = "graphs"; + + // the type of graph. See @{link GraphType} for valid types. + private static final String GRAPHS_TYPES_KEY = "graph-type"; + + // keys for the different kinds of graphs + private static final String TITLE_KEY = "title"; + + private static final String X_AXIS_KEY = "x-axis"; + + private static final String Y_AXIS_KEY = "y-axis"; + + private static final String COLUMN_KEY = "column"; + + private static final String NUMBER_OF_BINS_KEY = "number-of-bins"; + + private final char columnDelimiter; private final boolean ignoreComments; // the comment marker in the file private final char comment; - private final ArrayList<String> graphTypeCodes; + private final int imageWidth; + + private final int imageHeight; + + private final int thumbnailWidth; + + private final int thumbnailHeight; + + private final ArrayList<String> graphNames; private final HashMap<String, TabularDataGraphConfiguration> graphTypeMap; + private enum GraphType + { + SCATTERPLOT, HISTOGRAM, HEATMAP + } + /** * Create a configuration from the properties file located at path. * @@ -82,34 +125,93 @@ public class TabularDataGraphCollectionConfiguration { comment = '#'; - this.separator = PropertyUtils.getChar(properties, SEPARATOR_PROPERTY_KEY, ';'); + this.columnDelimiter = PropertyUtils.getChar(properties, SEPARATOR_PROPERTY_KEY, ';'); this.ignoreComments = PropertyUtils.getBoolean(properties, IGNORE_COMMENTS_PROPERTY_KEY, true); - graphTypeCodes = new ArrayList<String>(); + + imageWidth = PropertyUtils.getInt(properties, IMAGE_WIDTH_KEY, 800); + + imageHeight = PropertyUtils.getInt(properties, IMAGE_HEIGHT_KEY, 600); + + thumbnailWidth = PropertyUtils.getInt(properties, THUMBNAIL_WIDTH_KEY, 300); + + thumbnailHeight = PropertyUtils.getInt(properties, THUMBNAIL_HEIGHT_KEY, 200); + + graphNames = new ArrayList<String>(); + initializeGraphTypeCodes(properties); graphTypeMap = new HashMap<String, TabularDataGraphConfiguration>(); initialzeGraphTypeMap(properties); } + private void initializeGraphTypeCodes(Properties properties) + { + String graphTypeCodesString = properties.getProperty(GRAPHS_KEY, ""); + String[] typeCodeArray = graphTypeCodesString.split(","); + for (String typeCode : typeCodeArray) + { + graphNames.add(typeCode.trim()); + } + } + private void initialzeGraphTypeMap(Properties properties) { - TabularDataGraphConfiguration config = - new TabularDataGraphConfiguration("Test", "TotalCells", "InfectedCells", 300, 200); - graphTypeMap.put("scatter", config); - graphTypeCodes.add("scatter"); + SectionProperties[] pluginServicesProperties = + PropertyParametersUtil.extractSectionProperties(properties, GRAPHS_KEY, false); + + for (SectionProperties sectionProp : pluginServicesProperties) + { + TabularDataGraphConfiguration config = getConfiguration(sectionProp); + graphTypeMap.put(sectionProp.getKey(), config); + } + } + + private TabularDataGraphConfiguration getConfiguration(SectionProperties sectionProp) + { + Properties props = sectionProp.getProperties(); + String graphTypeValue = PropertyUtils.getMandatoryProperty(props, GRAPHS_TYPES_KEY); + GraphType type = GraphType.valueOf(graphTypeValue.toUpperCase()); + String title = props.getProperty(TITLE_KEY, sectionProp.getKey()); + switch (type) + { + case HEATMAP: + String xAxis = PropertyUtils.getMandatoryProperty(props, X_AXIS_KEY); + String yAxis = PropertyUtils.getMandatoryProperty(props, Y_AXIS_KEY); + String zAxis = PropertyUtils.getMandatoryProperty(props, COLUMN_KEY); + if (xAxis.equals(yAxis)) + { + return new TabularDataHeatmapConfiguration(title, xAxis, zAxis, + getThumbnailWidth(), getThumbnailHeight()); + } else + { + return new TabularDataHeatmapConfiguration(title, xAxis, yAxis, zAxis, + getThumbnailWidth(), getThumbnailHeight()); + } + case HISTOGRAM: + return new TabularDataHistogramConfiguration(title, PropertyUtils + .getMandatoryProperty(props, COLUMN_KEY), getThumbnailWidth(), + getThumbnailHeight(), PropertyUtils.getInt(props, NUMBER_OF_BINS_KEY, 10)); + case SCATTERPLOT: + return new TabularDataGraphConfiguration(title, PropertyUtils.getMandatoryProperty( + props, X_AXIS_KEY), PropertyUtils.getMandatoryProperty(props, Y_AXIS_KEY), + getThumbnailWidth(), getThumbnailHeight()); + } + + // should never get here + return null; } /** * Return the graph configuration associated with the graphTypeCode. * - * @param graphTypeCode The name of the graph type + * @param graphName The name of the graph type */ - public TabularDataGraphConfiguration getGraphConfiguration(String graphTypeCode) + public TabularDataGraphConfiguration getGraphConfiguration(String graphName) { - TabularDataGraphConfiguration config = graphTypeMap.get(graphTypeCode); + TabularDataGraphConfiguration config = graphTypeMap.get(graphName); if (null == config) { - throw new IllegalArgumentException("No graph associated with code " + graphTypeCode); + throw new IllegalArgumentException("No graph associated with code " + graphName); } return config; } @@ -118,24 +220,23 @@ public class TabularDataGraphCollectionConfiguration * Return the graph generator associated with the graphTypeCode, initialized by the fileLines * and out. * - * @param graphTypeCode The name of the graph type + * @param graphName The name of the graph type * @param fileLines The data to generate a graph from * @param out The stream to write the graph to */ - public ITabularDataGraph getGraph(String graphTypeCode, DatasetFileLines fileLines, - OutputStream out) + public ITabularDataGraph getGraph(String graphName, DatasetFileLines fileLines, OutputStream out) { - TabularDataGraphConfiguration config = graphTypeMap.get(graphTypeCode); + TabularDataGraphConfiguration config = graphTypeMap.get(graphName); if (null == config) { - throw new IllegalArgumentException("No graph associated with code " + graphTypeCode); + throw new IllegalArgumentException("No graph associated with code " + graphName); } return new TabularDataScatterplot(config, fileLines, out); } public char getColumnDelimiter() { - return separator; + return columnDelimiter; } /** @@ -153,26 +254,26 @@ public class TabularDataGraphCollectionConfiguration public int getImageWidth() { - return 800; + return imageWidth; } public int getImageHeight() { - return 600; + return imageHeight; } public int getThumbnailWidth() { - return 300; + return thumbnailWidth; } public int getThumbnailHeight() { - return 200; + return thumbnailHeight; } - public List<String> getGraphTypeCodes() + public List<String> getGraphNames() { - return graphTypeCodes; + return graphNames; } } diff --git a/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/ImageAnalysisGraphReportingPlugin.java b/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/ImageAnalysisGraphReportingPlugin.java index 6431cae5d3132ffb055035549042bc74962a896c..c28bccc8ac266c819adc16722ca48ccc5c1e14ac 100644 --- a/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/ImageAnalysisGraphReportingPlugin.java +++ b/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/ImageAnalysisGraphReportingPlugin.java @@ -131,7 +131,7 @@ public class ImageAnalysisGraphReportingPlugin extends AbstractDataMergingReport private List<String> getGraphTypeCodes() { - return configuration.getGraphTypeCodes(); + return configuration.getGraphNames(); } private int getImageWidth() diff --git a/screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataGraphCollectionConfigurationTest.java b/screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataGraphCollectionConfigurationTest.java new file mode 100644 index 0000000000000000000000000000000000000000..75ce8c95693c3ccd90530ec4b0a37df2cea4625f --- /dev/null +++ b/screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataGraphCollectionConfigurationTest.java @@ -0,0 +1,67 @@ +/* + * Copyright 2010 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.openbis.dss.generic.server.graph; + +import java.net.URL; +import java.util.List; + +import org.testng.AssertJUnit; +import org.testng.annotations.Test; + +import ch.systemsx.cisd.openbis.dss.generic.server.graph.TabularDataGraphCollectionConfiguration; +import ch.systemsx.cisd.openbis.dss.generic.server.graph.TabularDataGraphConfiguration; + +/** + * @author Chandrasekhar Ramakrishnan + */ +public class TabularDataGraphCollectionConfigurationTest extends AssertJUnit +{ + + @Test + public void testGoodConfigurationFile() + { + URL propertiesUrl = getClass().getResource("graph.properties"); + String propertiesFilePath = propertiesUrl.getFile(); + TabularDataGraphCollectionConfiguration configuration = + TabularDataGraphCollectionConfiguration.getConfiguration(propertiesFilePath); + + // test the file global stuff + assertEquals(',', configuration.getColumnDelimiter()); + assertFalse(configuration.isIgnoreComments()); + assertEquals(1024, configuration.getImageWidth()); + assertEquals(768, configuration.getImageHeight()); + assertEquals(30, configuration.getThumbnailWidth()); + assertEquals(30, configuration.getThumbnailHeight()); + + // test the graph configurations + assertEquals(4, configuration.getGraphNames().size()); + + List<String> graphNames = configuration.getGraphNames(); + assertEquals("scatter1", graphNames.get(0)); + TabularDataGraphConfiguration graphConfig = + configuration.getGraphConfiguration(graphNames.get(0)); + // If not specified, the title should default to the name + assertEquals("scatter1", graphConfig.getTitle()); + assertEquals(30, graphConfig.getImageHeight()); + assertEquals(30, graphConfig.getImageWidth()); + + assertEquals("hist", graphNames.get(1)); + assertEquals("heat", graphNames.get(2)); + assertEquals("scatter2", graphNames.get(3)); + + } +} diff --git a/screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/graph.properties b/screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/graph.properties new file mode 100644 index 0000000000000000000000000000000000000000..534639217f9e96e3825a4d4fb3bdbf8ca59d169d --- /dev/null +++ b/screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/graph.properties @@ -0,0 +1,44 @@ +# ------------------------------------------------------------------ +# Properties File For Generating Graphs From Tabular Data +# ------------------------------------------------------------------ + +# The column delimiter +separator = , + +# Should comments be ignored? +ignore-comments = false + +# The width of the full image +full-width = 1024 + +# The height of the full image +full-height = 768 + +# The width of the table column +column-width = 30 + +# The height of the table row +column-height = 30 + +# The graphs shown per row +graphs = scatter1, hist, heat, scatter2 + +# The properties for the first scatter plot +scatter1.graph-type = scatterplot +scatter1.x-axis = TotalCells +scatter1.y-axis = InfectedCells + +# The properties for the histogram +hist.graph-type = histogram +hist.column = TotalCells + +# The properties for the heatmap +heat.graph-type = heatmap +heat.x-axis = WellName +heat.y-axis = WellName +heat.column = InfectedCells + +# The properties for the second scatter plot +scatter2.graph-type = scatterplot +scatter2.x-axis = TotalCells +scatter2.y-axis = RelativeInfectionIndex \ No newline at end of file