|
| 1 | +package org.baderlab.csplugins.enrichmentmap.rest; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStream; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.io.Reader; |
| 7 | +import java.util.Collections; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +import javax.ws.rs.Consumes; |
| 11 | +import javax.ws.rs.POST; |
| 12 | +import javax.ws.rs.Path; |
| 13 | +import javax.ws.rs.core.MediaType; |
| 14 | +import javax.ws.rs.core.Response; |
| 15 | + |
| 16 | +import org.baderlab.csplugins.enrichmentmap.commands.tunables.FilterTunables; |
| 17 | +import org.baderlab.csplugins.enrichmentmap.model.DataSetParameters; |
| 18 | +import org.baderlab.csplugins.enrichmentmap.model.EMCreationParameters; |
| 19 | +import org.baderlab.csplugins.enrichmentmap.model.LegacySupport; |
| 20 | +import org.baderlab.csplugins.enrichmentmap.model.TableParameters; |
| 21 | +import org.baderlab.csplugins.enrichmentmap.task.CreateEnrichmentMapTaskFactory; |
| 22 | +import org.cytoscape.model.CyRow; |
| 23 | +import org.cytoscape.model.CyTable; |
| 24 | +import org.cytoscape.model.CyTableFactory; |
| 25 | +import org.cytoscape.work.SynchronousTaskManager; |
| 26 | +import org.cytoscape.work.TaskIterator; |
| 27 | + |
| 28 | +import com.google.gson.Gson; |
| 29 | +import com.google.gson.GsonBuilder; |
| 30 | +import com.google.inject.Inject; |
| 31 | +import com.google.inject.Provider; |
| 32 | + |
| 33 | +import io.swagger.annotations.Api; |
| 34 | +import io.swagger.annotations.ApiOperation; |
| 35 | +import io.swagger.annotations.ApiParam; |
| 36 | + |
| 37 | +@Api(tags="Apps: EnrichmentMap") |
| 38 | +@Path("/enrichmentmap/build") |
| 39 | +public class BuildResource { |
| 40 | + |
| 41 | + private static final String |
| 42 | + NAME = "name", |
| 43 | + GENES = "genes", |
| 44 | + PVALUE = "pvalue", |
| 45 | + QVALUE = "qvalue", |
| 46 | + NES = "nes", |
| 47 | + DESC = "description"; |
| 48 | + |
| 49 | + |
| 50 | + @Inject private SynchronousTaskManager<?> taskManager; |
| 51 | + @Inject private CreateEnrichmentMapTaskFactory.Factory taskFactoryFactory; |
| 52 | + @Inject private CyTableFactory tableFactory; |
| 53 | + @Inject private Provider<LegacySupport> legacySupport; |
| 54 | + |
| 55 | + |
| 56 | + @POST |
| 57 | + @Path("/") |
| 58 | + @Consumes(MediaType.APPLICATION_JSON) |
| 59 | + @ApiOperation(value="Create an EnrichmentMap network.", |
| 60 | + notes="" |
| 61 | + ) |
| 62 | + public Response createEnrichmentMap( |
| 63 | + @ApiParam(value="Body JSON data.") InputStream is |
| 64 | + ) { |
| 65 | + EMJsonData emJson; |
| 66 | + try(Reader reader = new InputStreamReader(is)) { |
| 67 | + Gson gson = new GsonBuilder().create(); |
| 68 | + emJson = gson.fromJson(reader, EMJsonData.class); |
| 69 | + } catch(IOException e) { |
| 70 | + throw new RuntimeException(e); |
| 71 | + } |
| 72 | + |
| 73 | + CyTable enrichmentTable = createEnrichmentTable(); |
| 74 | + fillEnrichmentTable(enrichmentTable, emJson.enrichments); |
| 75 | + |
| 76 | + TableParameters tableParams = new TableParameters(enrichmentTable, NAME, GENES, PVALUE, QVALUE, NES, DESC, null); |
| 77 | + |
| 78 | + DataSetParameters dsParams = new DataSetParameters("DataSet1", tableParams, null); |
| 79 | + List<DataSetParameters> dataSets = Collections.singletonList(dsParams); |
| 80 | + |
| 81 | + FilterTunables filter = emJson.filter; |
| 82 | + filter.setLegacySupport(legacySupport.get()); |
| 83 | + |
| 84 | + EMCreationParameters creationParams = filter.getCreationParameters(); |
| 85 | + CreateEnrichmentMapTaskFactory taskFactory = taskFactoryFactory.create(creationParams, dataSets); |
| 86 | + TaskIterator tasks = taskFactory.createTaskIterator(); |
| 87 | + taskManager.execute(tasks); |
| 88 | + |
| 89 | + return Response.ok().build(); |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + private CyTable createEnrichmentTable() { |
| 94 | + CyTable enrichmentTable = tableFactory.createTable("tempEnrichmentEM", "pk", Long.class, false, false); |
| 95 | + enrichmentTable.createColumn(NAME, String.class, false); |
| 96 | + enrichmentTable.createListColumn(GENES, String.class, false); |
| 97 | + enrichmentTable.createColumn(PVALUE, Double.class, false); |
| 98 | + enrichmentTable.createColumn(QVALUE, Double.class, false); |
| 99 | + enrichmentTable.createColumn(NES, Double.class, false); |
| 100 | + enrichmentTable.createColumn(DESC, String.class, false); |
| 101 | + return enrichmentTable; |
| 102 | + } |
| 103 | + |
| 104 | + private void fillEnrichmentTable(CyTable table, List<EnrichmentEntry> enrichments) { |
| 105 | + long i = 0; |
| 106 | + for(EnrichmentEntry enrichment : enrichments) { |
| 107 | + CyRow row = table.getRow(i++); |
| 108 | + row.set(NAME, enrichment.name); |
| 109 | + row.set(GENES, enrichment.genes); |
| 110 | + row.set(PVALUE, enrichment.pvalue); |
| 111 | + row.set(QVALUE, enrichment.qvalue); |
| 112 | + row.set(NES, enrichment.nes); |
| 113 | + row.set(DESC, enrichment.description); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments