use of org.obiba.mica.dataset.domain.HarmonizationDataset in project mica2 by obiba.
the class PublishedDataschemaDatasetVariableResource method getVariableAggregations.
@GET
@Path("/aggregation")
@Timed
public Mica.DatasetVariableAggregationsDto getVariableAggregations() {
ImmutableList.Builder<Mica.DatasetVariableAggregationDto> builder = ImmutableList.builder();
HarmonizationDataset dataset = getDataset(HarmonizationDataset.class, datasetId);
Mica.DatasetVariableAggregationsDto.Builder aggDto = Mica.DatasetVariableAggregationsDto.newBuilder();
List<Future<Math.SummaryStatisticsDto>> results = Lists.newArrayList();
dataset.getBaseStudyTables().forEach(table -> results.add(helper.getVariableFacet(dataset, variableName, table)));
for (int i = 0; i < dataset.getBaseStudyTables().size(); i++) {
BaseStudyTable opalTable = dataset.getBaseStudyTables().get(i);
Future<Math.SummaryStatisticsDto> futureResult = results.get(i);
try {
builder.add(dtos.asDto(opalTable, futureResult.get()).build());
} catch (Exception e) {
log.warn("Unable to retrieve statistics: " + e.getMessage(), e);
builder.add(dtos.asDto(opalTable, null).build());
}
}
List<Mica.DatasetVariableAggregationDto> aggsDto = builder.build();
Mica.DatasetVariableAggregationDto allAggDto = CombinedStatistics.mergeAggregations(aggsDto);
aggDto.setN(allAggDto.getN());
aggDto.setTotal(allAggDto.getTotal());
if (allAggDto.hasStatistics())
aggDto.setStatistics(allAggDto.getStatistics());
aggDto.addAllFrequencies(allAggDto.getFrequenciesList());
aggDto.addAllAggregations(aggsDto);
return aggDto.build();
}
use of org.obiba.mica.dataset.domain.HarmonizationDataset in project mica2 by obiba.
the class PublishedDataschemaDatasetVariableResource method getDatasetVariableContingenciesDto.
private Mica.DatasetVariableContingenciesDto getDatasetVariableContingenciesDto(DatasetVariable var, DatasetVariable crossVar) {
HarmonizationDataset dataset = getDataset(HarmonizationDataset.class, datasetId);
Mica.DatasetVariableContingenciesDto.Builder crossDto = Mica.DatasetVariableContingenciesDto.newBuilder();
List<Future<Search.QueryResultDto>> results = Lists.newArrayList();
dataset.getBaseStudyTables().forEach(table -> results.add(helper.getContingencyTable(dataset, var, crossVar, table)));
Multimap<String, Mica.DatasetVariableAggregationDto> termAggregations = LinkedListMultimap.create();
for (int i = 0; i < dataset.getBaseStudyTables().size(); i++) {
BaseStudyTable opalTable = dataset.getBaseStudyTables().get(i);
Future<Search.QueryResultDto> futureResult = results.get(i);
try {
Mica.DatasetVariableContingencyDto studyTableCrossDto = dtos.asContingencyDto(opalTable, var, crossVar, futureResult.get()).build();
termAggregations.put(null, studyTableCrossDto.getAll());
studyTableCrossDto.getAggregationsList().forEach(termAggDto -> termAggregations.put(termAggDto.getTerm(), termAggDto));
crossDto.addContingencies(studyTableCrossDto);
} catch (Exception e) {
log.warn("Unable to retrieve contingency table: " + e.getMessage(), e);
crossDto.addContingencies(dtos.asContingencyDto(opalTable, var, crossVar, null));
}
}
// Merge aggregations by term (=variable category) + all terms aggregation.
Mica.DatasetVariableContingencyDto.Builder allContingencies = Mica.DatasetVariableContingencyDto.newBuilder();
termAggregations.asMap().entrySet().forEach(entry -> {
Mica.DatasetVariableAggregationDto merged = CombinedStatistics.mergeAggregations(entry.getValue());
if (entry.getKey() == null) {
allContingencies.setAll(merged);
} else {
allContingencies.addAggregations(merged);
}
});
crossDto.setAll(allContingencies);
return crossDto.build();
}
use of org.obiba.mica.dataset.domain.HarmonizationDataset in project mica2 by obiba.
the class PublishedHarmonizedDatasetResource method getVariableHarmonizationsAsCsv.
@GET
@Path("/variables/harmonizations/_export")
@Produces("text/csv")
@Timed
public Response getVariableHarmonizationsAsCsv(@PathParam("id") String id, @QueryParam("sort") @DefaultValue("index") String sort, @QueryParam("order") @DefaultValue("asc") String order, @QueryParam("locale") @DefaultValue("en") String locale) throws IOException {
HarmonizationDataset dataset = getDataset(HarmonizationDataset.class, id);
Mica.DatasetVariablesHarmonizationsDto harmonizationVariables = getVariableHarmonizationsInternal(id, 0, 999999, sort, order, false);
CsvHarmonizationVariablesWriter writer = new CsvHarmonizationVariablesWriter(Lists.newArrayList("maelstrom", "Mlstr_harmo"));
ByteArrayOutputStream values = writer.write(dataset, harmonizationVariables, locale);
return Response.ok(values.toByteArray(), "text/csv").header("Content-Disposition", "attachment; filename=\"" + id + ".csv\"").build();
}
use of org.obiba.mica.dataset.domain.HarmonizationDataset in project mica2 by obiba.
the class PublishedHarmonizedDatasetResource method getVariable.
@Path("/study/{study}/population/{population}/data-collection-event/{dce}/variable/{variable}")
public PublishedHarmonizedDatasetVariableResource getVariable(@PathParam("id") String id, @PathParam("study") String studyId, @PathParam("population") String populationId, @PathParam("dce") String dceId, @PathParam("variable") String variable) {
checkAccess(id);
PublishedHarmonizedDatasetVariableResource resource = applicationContext.getBean(PublishedHarmonizedDatasetVariableResource.class);
resource.setDatasetId(id);
resource.setVariableName(variable);
resource.setStudyId(studyId);
HarmonizationDataset dataset = getDataset(HarmonizationDataset.class, id);
dataset.getStudyTables().stream().filter(t -> t.appliesTo(studyId, populationId, dceId)).forEach(t -> {
resource.setTableType(t instanceof StudyTable ? DatasetVariable.OPAL_STUDY_TABLE_PREFIX : DatasetVariable.OPAL_HARMONIZATION_TABLE_PREFIX);
});
return resource;
}
use of org.obiba.mica.dataset.domain.HarmonizationDataset in project mica2 by obiba.
the class RQLCriteriaOpalConverter method parseField.
private RQLFieldReferences parseField(String path) {
DatasetVariable.IdResolver resolver = DatasetVariable.IdResolver.from(path);
if (resolver.getType() == null || DatasetVariable.Type.Collected.equals(resolver.getType())) {
StudyDataset ds = collectedDatasetService.findById(resolver.getDatasetId());
BaseStudyTable studyTable = ds.getStudyTable();
BaseStudy study = studyService.findStudy(studyTable.getStudyId());
return new RQLFieldReferences(path, ds, studyTable, study, getDatasetVariableInternal(Indexer.VARIABLE_TYPE, path));
} else if (DatasetVariable.Type.Dataschema.equals(resolver.getType())) {
HarmonizationDataset ds = harmonizedDatasetService.findById(resolver.getDatasetId());
BaseStudy study = studyService.findStudy(ds.getHarmonizationTable().getStudyId());
return new RQLFieldReferences(path, ds, ds.getBaseStudyTables(), study, getDatasetVariableInternal(Indexer.VARIABLE_TYPE, path));
} else if (DatasetVariable.Type.Harmonized.equals(resolver.getType())) {
HarmonizationDataset ds = harmonizedDatasetService.findById(resolver.getDatasetId());
Optional<BaseStudyTable> studyTable = ds.getBaseStudyTables().stream().filter(st -> st.getStudyId().equals(resolver.getStudyId()) && st.getProject().equals(resolver.getProject()) && st.getTable().equals(resolver.getTable())).findFirst();
if (!studyTable.isPresent())
throw new IllegalArgumentException("Not a valid variable: " + path);
BaseStudy study = studyService.findStudy(studyTable.get().getStudyId());
return new RQLFieldReferences(path, ds, studyTable.get(), study, getDatasetVariableInternal(Indexer.HARMONIZED_VARIABLE_TYPE, path));
}
throw new IllegalArgumentException("Not a valid variable: " + path);
}
Aggregations