Search in sources :

Example 6 with HarmonizationDataset

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();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) Math(org.obiba.opal.web.model.Math) NoSuchVariableException(org.obiba.magma.NoSuchVariableException) BadRequestException(javax.ws.rs.BadRequestException) IOException(java.io.IOException) NoSuchValueTableException(org.obiba.magma.NoSuchValueTableException) HarmonizationDataset(org.obiba.mica.dataset.domain.HarmonizationDataset) Future(java.util.concurrent.Future) BaseStudyTable(org.obiba.mica.core.domain.BaseStudyTable) Mica(org.obiba.mica.web.model.Mica) Path(javax.ws.rs.Path) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET)

Example 7 with HarmonizationDataset

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();
}
Also used : NoSuchVariableException(org.obiba.magma.NoSuchVariableException) BadRequestException(javax.ws.rs.BadRequestException) IOException(java.io.IOException) NoSuchValueTableException(org.obiba.magma.NoSuchValueTableException) HarmonizationDataset(org.obiba.mica.dataset.domain.HarmonizationDataset) Search(org.obiba.opal.web.model.Search) Future(java.util.concurrent.Future) BaseStudyTable(org.obiba.mica.core.domain.BaseStudyTable) Mica(org.obiba.mica.web.model.Mica)

Example 8 with HarmonizationDataset

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();
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) Mica(org.obiba.mica.web.model.Mica) HarmonizationDataset(org.obiba.mica.dataset.domain.HarmonizationDataset) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET)

Example 9 with HarmonizationDataset

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;
}
Also used : PathParam(javax.ws.rs.PathParam) AbstractPublishedDatasetResource(org.obiba.mica.dataset.search.rest.AbstractPublishedDatasetResource) SubjectAclService(org.obiba.mica.security.service.SubjectAclService) Logger(org.slf4j.Logger) Produces(javax.ws.rs.Produces) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GET(javax.ws.rs.GET) DatasetVariable(org.obiba.mica.dataset.domain.DatasetVariable) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) IOException(java.io.IOException) Scope(org.springframework.context.annotation.Scope) Inject(javax.inject.Inject) Timed(com.codahale.metrics.annotation.Timed) Component(org.springframework.stereotype.Component) Lists(com.google.common.collect.Lists) QueryParam(javax.ws.rs.QueryParam) Response(javax.ws.rs.core.Response) Mica(org.obiba.mica.web.model.Mica) HarmonizationDataset(org.obiba.mica.dataset.domain.HarmonizationDataset) DefaultValue(javax.ws.rs.DefaultValue) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) StudyTable(org.obiba.mica.core.domain.StudyTable) StudyTable(org.obiba.mica.core.domain.StudyTable) HarmonizationDataset(org.obiba.mica.dataset.domain.HarmonizationDataset) Path(javax.ws.rs.Path)

Example 10 with HarmonizationDataset

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);
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) BaseStudyTable(org.obiba.mica.core.domain.BaseStudyTable) NoSuchVariableException(org.obiba.magma.NoSuchVariableException) Indexer(org.obiba.mica.spi.search.Indexer) ValueType(org.obiba.magma.ValueType) Scope(org.springframework.context.annotation.Scope) Inject(javax.inject.Inject) Strings(com.google.common.base.Strings) Lists(com.google.common.collect.Lists) StudyDataset(org.obiba.mica.dataset.domain.StudyDataset) Searcher(org.obiba.mica.spi.search.Searcher) HarmonizationStudy(org.obiba.mica.study.domain.HarmonizationStudy) TextType(org.obiba.magma.type.TextType) DatasetVariable(org.obiba.mica.dataset.domain.DatasetVariable) BaseStudy(org.obiba.mica.study.domain.BaseStudy) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DateTime(org.joda.time.DateTime) VariableNature(org.obiba.magma.support.VariableNature) CollectedDatasetService(org.obiba.mica.dataset.service.CollectedDatasetService) StudyService(org.obiba.mica.study.service.StudyService) RQLParser(net.jazdw.rql.parser.RQLParser) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) HarmonizedDatasetService(org.obiba.mica.dataset.service.HarmonizedDatasetService) Component(org.springframework.stereotype.Component) List(java.util.List) ASTNode(net.jazdw.rql.parser.ASTNode) HarmonizationDataset(org.obiba.mica.dataset.domain.HarmonizationDataset) Optional(java.util.Optional) Joiner(com.google.common.base.Joiner) InputStream(java.io.InputStream) DatasetVariable(org.obiba.mica.dataset.domain.DatasetVariable) BaseStudyTable(org.obiba.mica.core.domain.BaseStudyTable) BaseStudy(org.obiba.mica.study.domain.BaseStudy) StudyDataset(org.obiba.mica.dataset.domain.StudyDataset) HarmonizationDataset(org.obiba.mica.dataset.domain.HarmonizationDataset)

Aggregations

HarmonizationDataset (org.obiba.mica.dataset.domain.HarmonizationDataset)28 Path (javax.ws.rs.Path)10 NoSuchVariableException (org.obiba.magma.NoSuchVariableException)9 GET (javax.ws.rs.GET)8 NoSuchValueTableException (org.obiba.magma.NoSuchValueTableException)8 Timed (com.codahale.metrics.annotation.Timed)7 ImmutableList (com.google.common.collect.ImmutableList)7 IOException (java.io.IOException)4 List (java.util.List)4 BaseStudyTable (org.obiba.mica.core.domain.BaseStudyTable)4 DatasetVariable (org.obiba.mica.dataset.domain.DatasetVariable)4 Mica (org.obiba.mica.web.model.Mica)4 Lists (com.google.common.collect.Lists)3 Inject (javax.inject.Inject)3 HarmonizationStudyTable (org.obiba.mica.core.domain.HarmonizationStudyTable)3 StudyTable (org.obiba.mica.core.domain.StudyTable)3 Dataset (org.obiba.mica.dataset.domain.Dataset)3 Strings (com.google.common.base.Strings)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Collection (java.util.Collection)2