Skip to content

Commit

Permalink
M2597 Export to Galaxy in data explorer
Browse files Browse the repository at this point in the history
  • Loading branch information
dennishendriksen committed Jun 11, 2014
1 parent 8f63959 commit 57d229a
Show file tree
Hide file tree
Showing 8 changed files with 276 additions and 28 deletions.
5 changes: 5 additions & 0 deletions molgenis-omx-dataexplorer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,10 @@
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>com.github.jmchilton.blend4j</groupId>
<artifactId>blend4j</artifactId>
<version>0.1.0</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package org.molgenis.dataexplorer.controller;

import static org.molgenis.dataexplorer.controller.DataExplorerController.ATTR_GALAXY_API_KEY;
import static org.molgenis.dataexplorer.controller.DataExplorerController.ATTR_GALAXY_URL;
import static org.molgenis.dataexplorer.controller.DataExplorerController.URI;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Collections;
Expand All @@ -24,10 +29,13 @@
import org.molgenis.data.AttributeMetaData;
import org.molgenis.data.DataService;
import org.molgenis.data.EntityMetaData;
import org.molgenis.data.MolgenisDataAccessException;
import org.molgenis.data.csv.CsvWriter;
import org.molgenis.data.support.GenomeConfig;
import org.molgenis.data.support.QueryImpl;
import org.molgenis.dataexplorer.controller.DataRequest.ColNames;
import org.molgenis.dataexplorer.galaxy.GalaxyDataExportRequest;
import org.molgenis.dataexplorer.galaxy.GalaxyDataExporter;
import org.molgenis.framework.server.MolgenisSettings;
import org.molgenis.framework.ui.MolgenisPluginController;
import org.molgenis.security.core.MolgenisPermissionService;
Expand All @@ -45,23 +53,19 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;

/**
* Controller class for the data explorer.
*
* The implementation javascript file for the resultstable is defined in a MolgenisSettings property named
* 'dataexplorer.resultstable.js' possible values are '/js/SingleObservationSetTable.js' or
* '/js/MultiObservationSetTable.js' with '/js/MultiObservationSetTable.js' as the default
*
* @author erwin
*
*/
@Controller
@RequestMapping(URI)
@SessionAttributes(
{ ATTR_GALAXY_URL, ATTR_GALAXY_API_KEY })
public class DataExplorerController extends MolgenisPluginController
{
private static final Logger logger = Logger.getLogger(DataExplorerController.class);
Expand All @@ -74,11 +78,17 @@ public class DataExplorerController extends MolgenisPluginController
public static final String KEY_MOD_CHARTS = "plugin.dataexplorer.mod.charts";
public static final String KEY_MOD_DATA = "plugin.dataexplorer.mod.data";
public static final String KEY_MOD_DISEASEMATCHER = "plugin.dataexplorer.mod.diseasematcher";
public static final String KEY_GALAXY_ENABLED = "plugin.dataexplorer.galaxy.enabled";
public static final String KEY_GALAXY_URL = "plugin.dataexplorer.galaxy.url";
private static final boolean DEFAULT_VAL_MOD_AGGREGATES = true;
private static final boolean DEFAULT_VAL_MOD_ANNOTATORS = true;
private static final boolean DEFAULT_VAL_MOD_CHARTS = true;
private static final boolean DEFAULT_VAL_MOD_DATA = true;
private static final boolean DEFAULT_VAL_MOD_DISEASEMATCHER = false;
private static final boolean DEFAULT_VAL_GALAXY_ENABLED = false;

static final String ATTR_GALAXY_URL = "galaxyUrl";
static final String ATTR_GALAXY_API_KEY = "galaxyApiKey";

public static final String INITLOCATION = "genomebrowser.init.initLocation";
public static final String COORDSYSTEM = "genomebrowser.init.coordSystem";
Expand Down Expand Up @@ -171,7 +181,10 @@ public String getModule(@PathVariable("moduleId") String moduleId, Model model)
model.addAttribute("browserLinks", molgenisSettings.getProperty(BROWSERLINKS));
model.addAttribute("tableEditable",
molgenisSettings.getBooleanProperty(KEY_DATAEXPLORER_EDITABLE, DEFAULT_VAL_DATAEXPLORER_EDITABLE));

model.addAttribute("galaxyEnabled",
molgenisSettings.getBooleanProperty(KEY_GALAXY_ENABLED, DEFAULT_VAL_GALAXY_ENABLED));
String galaxyUrl = molgenisSettings.getProperty(KEY_GALAXY_URL);
if (galaxyUrl != null) model.addAttribute(ATTR_GALAXY_URL, galaxyUrl);
}
return "view-dataexplorer-mod-" + moduleId; // TODO bad request in case of invalid module id
}
Expand Down Expand Up @@ -280,26 +293,62 @@ public void download(@RequestParam("dataRequest") String dataRequestStr, HttpSer
DataRequest dataRequest = new GsonHttpMessageConverter().getGson().fromJson(dataRequestStr, DataRequest.class);

String entityName = dataRequest.getEntityName();
EntityMetaData entityMetaData = dataService.getEntityMetaData(entityName);
final Set<String> attributeNames = new HashSet<String>(dataRequest.getAttributeNames());
String fileName = entityName + '_' + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + ".csv";
Iterable<AttributeMetaData> attributes = Iterables.filter(entityMetaData.getAtomicAttributes(),
new Predicate<AttributeMetaData>()
{
@Override
public boolean apply(AttributeMetaData attributeMetaData)
{
return attributeNames.contains(attributeMetaData.getName());
}
});

QueryImpl query = dataRequest.getQuery();
response.setContentType("text/csv");
response.addHeader("Content-Disposition", "attachment; filename=" + fileName);
writeDataRequestCsv(dataRequest, response.getOutputStream(), ',');
}

@RequestMapping(value = "/galaxy/export", method = POST)
@ResponseStatus(HttpStatus.OK)
public void exportToGalaxy(@Valid @RequestBody GalaxyDataExportRequest galaxyDataExportRequest, Model model)
throws IOException
{
boolean galaxyEnabled = molgenisSettings.getBooleanProperty(KEY_GALAXY_ENABLED, DEFAULT_VAL_GALAXY_ENABLED);
if (!galaxyEnabled) throw new MolgenisDataAccessException("Galaxy export disabled");

CsvWriter csvWriter = new CsvWriter(response.getOutputStream());
String galaxyUrl = galaxyDataExportRequest.getGalaxyUrl();
String galaxyApiKey = galaxyDataExportRequest.getGalaxyApiKey();
GalaxyDataExporter galaxyDataSetExporter = new GalaxyDataExporter(galaxyUrl, galaxyApiKey);

DataRequest dataRequest = galaxyDataExportRequest.getDataRequest();

File csvFile = File.createTempFile("galaxydata_" + System.currentTimeMillis(), ".tsv");
try
{
writeDataRequestCsv(dataRequest, new FileOutputStream(csvFile), '\t');
galaxyDataSetExporter.export(dataRequest.getEntityName(), csvFile);
}
finally
{
csvFile.delete();
}

// store url and api key in session for subsequent galaxy export requests
model.addAttribute(ATTR_GALAXY_URL, galaxyUrl);
model.addAttribute(ATTR_GALAXY_API_KEY, galaxyApiKey);
}

private void writeDataRequestCsv(DataRequest dataRequest, OutputStream outputStream, char separator)
throws IOException
{
CsvWriter csvWriter = new CsvWriter(outputStream, separator);
try
{
String entityName = dataRequest.getEntityName();
EntityMetaData entityMetaData = dataService.getEntityMetaData(entityName);
final Set<String> attributeNames = new HashSet<String>(dataRequest.getAttributeNames());
Iterable<AttributeMetaData> attributes = Iterables.filter(entityMetaData.getAtomicAttributes(),
new Predicate<AttributeMetaData>()
{
@Override
public boolean apply(AttributeMetaData attributeMetaData)
{
return attributeNames.contains(attributeMetaData.getName());
}
});

if (dataRequest.getColNames() == ColNames.ATTRIBUTE_LABELS)
{
csvWriter.writeAttributes(attributes);
Expand All @@ -316,6 +365,7 @@ public String apply(AttributeMetaData attributeMetaData)
}));
}

QueryImpl query = dataRequest.getQuery();
csvWriter.add(dataService.findAll(entityName, query));
}
finally
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.molgenis.dataexplorer.galaxy;

import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.URL;
import org.molgenis.dataexplorer.controller.DataRequest;

public class GalaxyDataExportRequest
{
@NotNull
@URL
private String galaxyUrl;

@NotNull
@Length(min = 32, max = 32)
private String galaxyApiKey;

@NotNull
private DataRequest dataRequest;

public String getGalaxyUrl()
{
return galaxyUrl;
}

public void setGalaxyUrl(String galaxyUrl)
{
this.galaxyUrl = galaxyUrl;
}

public String getGalaxyApiKey()
{
return galaxyApiKey;
}

public void setGalaxyApiKey(String galaxyApiKey)
{
this.galaxyApiKey = galaxyApiKey;
}

public DataRequest getDataRequest()
{
return dataRequest;
}

public void setDataRequest(DataRequest dataRequest)
{
this.dataRequest = dataRequest;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.molgenis.dataexplorer.galaxy;

import java.io.File;
import java.util.List;

import com.github.jmchilton.blend4j.galaxy.GalaxyInstance;
import com.github.jmchilton.blend4j.galaxy.GalaxyInstanceFactory;
import com.github.jmchilton.blend4j.galaxy.HistoriesClient;
import com.github.jmchilton.blend4j.galaxy.ToolsClient;
import com.github.jmchilton.blend4j.galaxy.ToolsClient.FileUploadRequest;
import com.github.jmchilton.blend4j.galaxy.ToolsClient.UploadFile;
import com.github.jmchilton.blend4j.galaxy.beans.History;
import com.github.jmchilton.blend4j.galaxy.beans.OutputDataset;
import com.github.jmchilton.blend4j.galaxy.beans.ToolExecution;

public class GalaxyDataExporter
{
private static final String MOLGENIS_HISTORY_NAME = "MOLGENIS history";

private final GalaxyInstance galaxyInstance;

public GalaxyDataExporter(String galaxyUrl, String galaxyApiKey)
{
if (galaxyUrl == null) throw new IllegalArgumentException("galaxyUrl is null");
if (galaxyApiKey == null) throw new IllegalArgumentException("galaxyApiKey is null");
this.galaxyInstance = GalaxyInstanceFactory.get(galaxyUrl, galaxyApiKey);
}

public void export(String dataSetName, File tsvFile)
{
export(dataSetName, tsvFile, MOLGENIS_HISTORY_NAME);
}

public void export(String dataSetName, File tsvFile, String historyName)
{
// get/create history
HistoriesClient historiesClient = galaxyInstance.getHistoriesClient();
History molgenisHistory = null;
for (History history : historiesClient.getHistories())
{
if (history.getName().equals(historyName))
{
molgenisHistory = history;
break;
}
}
if (molgenisHistory == null)
{
molgenisHistory = new History(historyName);
molgenisHistory = historiesClient.create(molgenisHistory);
}

// upload data set file
ToolsClient toolsClient = galaxyInstance.getToolsClient();
UploadFile uploadFile = new UploadFile(tsvFile, dataSetName);
FileUploadRequest fileUploadRequest = new FileUploadRequest(molgenisHistory.getId(), uploadFile);
fileUploadRequest.setFileType("tabular");
ToolExecution toolsExecution = toolsClient.upload(fileUploadRequest);
List<OutputDataset> outputDatasets = toolsExecution.getOutputs();
if (outputDatasets == null || outputDatasets.size() != 1)
{
throw new RuntimeException("Expected one output data set instead of " + outputDatasets.size());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
display: inline;
}

#download-modal-button {
float: right;
}

#genomebrowser {
visibility: hidden
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,35 @@
download();
});

$('form[name=galaxy-export-form]').validate({
rules : {
galaxyUrl : {
required : true,
url : true
},
galaxyApiKey : {
required : true,
minlength: 32,
maxlength: 32
}
}
});
$('form[name=galaxy-export-form]').submit(function(e) {
e.preventDefault();
if($(this).valid()) {
$.ajax({
type : $(this).attr('method'),
url : $(this).attr('action'),
data : JSON.stringify($.extend({}, $(this).serializeObject(), {dataRequest : createDownloadDataRequest()})),
contentType: 'application/json',
success : function() {
$('#galaxy-export-modal').modal('hide');
molgenis.createAlert([{'message': 'Exported data set to Galaxy'}], 'success');
}
});
}
});

$('#genomebrowser-filter-button').click(function() {
setDallianceFilter();
});
Expand Down
Loading

0 comments on commit 57d229a

Please sign in to comment.