From 04c723458028a6ddfddf927ecd613abff83e2ca9 Mon Sep 17 00:00:00 2001 From: cramakri <cramakri> Date: Tue, 19 Jul 2011 09:38:00 +0000 Subject: [PATCH] LMS-2392 Refactored and added data set search. SVN: 22198 --- .../cisd/client/examples/BaSynthecClient.java | 15 +- .../cisd/client/examples/DataSetSearch.java | 249 ++++++++++++++++++ .../client/examples/ExperimentLister.java | 24 +- 3 files changed, 280 insertions(+), 8 deletions(-) create mode 100644 eu_basynthec/source/java/eu/basynthec/cisd/client/examples/DataSetSearch.java diff --git a/eu_basynthec/source/java/eu/basynthec/cisd/client/examples/BaSynthecClient.java b/eu_basynthec/source/java/eu/basynthec/cisd/client/examples/BaSynthecClient.java index 6ffaee9b05b..f77ff86557d 100644 --- a/eu_basynthec/source/java/eu/basynthec/cisd/client/examples/BaSynthecClient.java +++ b/eu_basynthec/source/java/eu/basynthec/cisd/client/examples/BaSynthecClient.java @@ -16,6 +16,8 @@ package eu.basynthec.cisd.client.examples; +import java.util.Arrays; +import java.util.List; import java.util.Properties; import org.apache.commons.lang.time.DateUtils; @@ -122,9 +124,18 @@ public class BaSynthecClient extends AbstractBaSynthecClient */ public void run() { - logInfo("Listing experiments..."); + List<String> projectIdentifiers = Arrays.asList("/PRIVATE/TEST"); + logInfo("Listing experiments in projects " + projectIdentifiers + "..."); ExperimentLister experimentLister = new ExperimentLister(openBis); - experimentLister.run(); + experimentLister.run(projectIdentifiers); + + println("\n"); + + List<String> strainNames = Arrays.asList("MGP100"); + logInfo("Listing data sets containing data for strains " + strainNames + "..."); + DataSetSearch dataSetSearch = new DataSetSearch(openBis); + dataSetSearch.run(strainNames); + } private void logout() diff --git a/eu_basynthec/source/java/eu/basynthec/cisd/client/examples/DataSetSearch.java b/eu_basynthec/source/java/eu/basynthec/cisd/client/examples/DataSetSearch.java new file mode 100644 index 00000000000..7e896a29dde --- /dev/null +++ b/eu_basynthec/source/java/eu/basynthec/cisd/client/examples/DataSetSearch.java @@ -0,0 +1,249 @@ +/* + * Copyright 2011 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 eu.basynthec.cisd.client.examples; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import ch.systemsx.cisd.openbis.dss.client.api.v1.DataSet; +import ch.systemsx.cisd.openbis.dss.client.api.v1.IOpenbisServiceFacade; +import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.EntityRegistrationDetails; +import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.Experiment; +import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.SearchCriteria; +import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.SearchCriteria.MatchClause; +import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.SearchCriteria.SearchOperator; + +/** + * A class that, given a live connection to openBIS, searches for data sets where one of the + * specified strains appear, downloads the data set and prints information about the data set and + * experiment. + * + * @author Chandrasekhar Ramakrishnan + */ +public class DataSetSearch extends AbstractBaSynthecClient +{ + private static final String STRAIN_NAMES_PROPERTY = "STRAIN_NAMES"; + + private static final String STRAINS_HEADER = "Strains"; + + private static final String REGISTERED_BY_HEADER = "Contact Person"; + + private static final String DATA_SET_HEADER = "Data Set"; + + /** + * An enum for keeping track of the metadata fields we are interested in, in the order we want + * to display them. + * + * @author Chandrasekhar Ramakrishnan + */ + private enum MetadataField + { + EXPERIMENT_DATE("Date"), DESCRIPTION("Description"), MEDIUM("Medium"), TEMPERATURE( + "Temperature"), MISC_GROWTH_CONDITIONS("Growth Conditions"); + + MetadataField(String headerText) + { + this.headerText = headerText; + } + + private final String headerText; + } + + // State we compute as part of the operation of this class + + // The data sets we are interested in + private List<DataSet> matchingDataSets; + + // The experiments that contain the data sets we are interested in + private List<Experiment> experiments; + + // A mapping from experiments to data sets + private HashMap<String, ArrayList<DataSet>> experimentDataSets; + + /** + * The public constructor + * + * @param facade The facade for interacting with openBIS. + */ + public DataSetSearch(IOpenbisServiceFacade facade) + { + super(facade); + } + + /** + * Using the connection to openBIS, get search for data sets that contain data on the specified + * strains. Download the data set and print information about the data set and its experiment.<br> + * <ul> + * <li>Experiment Date</li> + * <li>Description</li> + * <li>Medium</li> + * <li>Temperature</li> + * <li>Growth Conditions</li> + * <li>Data Set Strains</li> + * <li>Data Set Path</li> + * </ul> + */ + public void run(List<String> strainNames) + { + printHeader(); + retrieveInformationFromOpenBis(strainNames); + + for (Experiment experiment : experiments) + { + printExperiment(experiment); + } + } + + /** + * Connect to openBis to get the information we need. + */ + private void retrieveInformationFromOpenBis(List<String> strainNames) + { + // Find all data sets that match the strain names + matchingDataSets = retrieveDataSetsReferencingStrains(strainNames); + + // Get the experiments for the specified data sets + List<String> experimentIdentifiers = new ArrayList<String>(); + for (DataSet dataSet : matchingDataSets) + { + experimentIdentifiers.add(dataSet.getExperimentIdentifier()); + } + experiments = openBis.getExperiments(experimentIdentifiers); + + // Create a map from experiment to data set + experimentDataSets = new HashMap<String, ArrayList<DataSet>>(); + for (DataSet dataSet : matchingDataSets) + { + ArrayList<DataSet> dataSetsForExperiment = + experimentDataSets.get(dataSet.getExperimentIdentifier()); + if (null == dataSetsForExperiment) + { + dataSetsForExperiment = new ArrayList<DataSet>(); + experimentDataSets.put(dataSet.getExperimentIdentifier(), dataSetsForExperiment); + } + dataSetsForExperiment.add(dataSet); + } + } + + /** + * Find data sets that contain data for the specified strains. + */ + private List<DataSet> retrieveDataSetsReferencingStrains(List<String> strainNames) + { + // Construct a search criteria that matches any of the strains specified. + SearchCriteria searchCriteria = new SearchCriteria(); + searchCriteria.setOperator(SearchOperator.MATCH_ANY_CLAUSES); + for (String strainName : strainNames) + { + searchCriteria.addMatchClause(MatchClause.createPropertyMatch(STRAIN_NAMES_PROPERTY, + strainName)); + } + println("Searching for data sets that match the following criteria: "); + println(searchCriteria.toString()); + return openBis.searchForDataSets(searchCriteria); + } + + private void printHeader() + { + StringBuffer sb = new StringBuffer(); + sb.append("Experiment"); + for (MetadataField field : MetadataField.values()) + { + sb.append("\t"); + sb.append(field.headerText); + } + sb.append("\t"); + sb.append(REGISTERED_BY_HEADER); + sb.append("\t"); + sb.append(DATA_SET_HEADER); + sb.append("\t"); + sb.append(STRAINS_HEADER); + println(sb.toString()); + } + + private void printEmptyExperimentInformationOn(StringBuffer sb) + { + // Experiment Identifier + sb.append("\t"); + for (@SuppressWarnings("unused") + MetadataField field : MetadataField.values()) + { + sb.append("\t"); + } + // REGISTERED_BY_HEADER + sb.append("\t"); + + // We have now tabbed to allign with the data set information + } + + private void printExperiment(Experiment experiment) + { + StringBuffer sb = new StringBuffer(); + sb.append(experiment.getIdentifier()); + printExperimentPropertiesOn(experiment.getProperties(), sb); + printRegisteredByOn(experiment.getRegistrationDetails(), sb); + printDataSetsOn(experiment, sb); + println(sb.toString()); + } + + private void printExperimentPropertiesOn(Map<String, String> properties, StringBuffer sb) + { + for (MetadataField field : MetadataField.values()) + { + sb.append("\t"); + String value = properties.get(field.toString()); + if (null != value) + { + sb.append(value); + } + } + } + + /** + * Get the strains for the experiment and print them to the string buffer. + */ + private void printDataSetsOn(Experiment experiment, StringBuffer sb) + { + sb.append("\t"); + + // dataSets cannot be null, because we only have experiments that have data sets here + ArrayList<DataSet> dataSets = experimentDataSets.get(experiment.getIdentifier()); + + boolean isFirstLine = true; + for (DataSet dataSet : dataSets) + { + String dataSetCode = dataSet.getCode(); + String strainNames = dataSet.getProperties().get(STRAIN_NAMES_PROPERTY); + if (false == isFirstLine) + { + sb.append("\n"); + printEmptyExperimentInformationOn(sb); + } + sb.append(dataSetCode); + sb.append("\t"); + sb.append(strainNames); + } + } + + private void printRegisteredByOn(EntityRegistrationDetails registrationDetails, StringBuffer sb) + { + sb.append("\t"); + sb.append(registrationDetails.getUserEmail()); + } +} diff --git a/eu_basynthec/source/java/eu/basynthec/cisd/client/examples/ExperimentLister.java b/eu_basynthec/source/java/eu/basynthec/cisd/client/examples/ExperimentLister.java index 7652e191e8b..421ab0f9367 100644 --- a/eu_basynthec/source/java/eu/basynthec/cisd/client/examples/ExperimentLister.java +++ b/eu_basynthec/source/java/eu/basynthec/cisd/client/examples/ExperimentLister.java @@ -17,13 +17,13 @@ package eu.basynthec.cisd.client.examples; import java.util.ArrayList; -import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import ch.systemsx.cisd.openbis.dss.client.api.v1.DataSet; import ch.systemsx.cisd.openbis.dss.client.api.v1.IOpenbisServiceFacade; +import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.EntityRegistrationDetails; import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.Experiment; /** @@ -37,6 +37,8 @@ public class ExperimentLister extends AbstractBaSynthecClient private static final String STRAINS_HEADER = "Strains"; + private static final String REGISTERED_BY_HEADER = "Contact Person"; + /** * An enum for keeping track of the metadata fields we are interested in, in the order we want * to display them. @@ -75,8 +77,8 @@ public class ExperimentLister extends AbstractBaSynthecClient } /** - * Using the connection to openBIS, get all experiments in the project <i>/PRIVATE/TEST</i> and - * print the following information about the experiment:<br> + * Using the connection to openBIS, get all experiments in the specified projects and print the + * following information about the experiment:<br> * <ul> * <li>Experiment Date</li> * <li>Description</li> @@ -85,11 +87,13 @@ public class ExperimentLister extends AbstractBaSynthecClient * <li>Growth Conditions</li> * <li>Strains</li> * </ul> + * + * @param projectIdentifiers The projects in which the experiments we are interested in are + * housed. */ - public void run() + public void run(List<String> projectIdentifiers) { printHeader(); - List<String> projectIdentifiers = Arrays.asList("/PRIVATE/TEST"); retrieveInformationFromOpenBis(projectIdentifiers); for (Experiment experiment : experiments) @@ -113,7 +117,6 @@ public class ExperimentLister extends AbstractBaSynthecClient experimentIdentifiers.add(experiment.getIdentifier()); } - experimentIdentifiers = Arrays.asList("/PRIVATE/TEST/TEST"); List<DataSet> dataSets = openBis.listDataSetsForExperiments(experimentIdentifiers); experimentDataSets = new HashMap<String, ArrayList<DataSet>>(); @@ -141,6 +144,8 @@ public class ExperimentLister extends AbstractBaSynthecClient } sb.append("\t"); sb.append(STRAINS_HEADER); + sb.append("\t"); + sb.append(REGISTERED_BY_HEADER); println(sb.toString()); } @@ -150,6 +155,7 @@ public class ExperimentLister extends AbstractBaSynthecClient sb.append(experiment.getIdentifier()); printExperimentPropertiesOn(experiment.getProperties(), sb); printExperimentStrainsOn(experiment, sb); + printRegisteredByOn(experiment.getRegistrationDetails(), sb); println(sb.toString()); } @@ -203,4 +209,10 @@ public class ExperimentLister extends AbstractBaSynthecClient sb.append(strains.get(i)); } } + + private void printRegisteredByOn(EntityRegistrationDetails registrationDetails, StringBuffer sb) + { + sb.append("\t"); + sb.append(registrationDetails.getUserEmail()); + } } -- GitLab