Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@
import org.breedinginsight.services.TraitService;
import org.breedinginsight.services.exceptions.DoesNotExistException;
import org.breedinginsight.services.parsers.experiment.ExperimentFileColumns;
import org.breedinginsight.utilities.DatasetUtil;
import org.breedinginsight.utilities.IntOrderComparator;
import org.breedinginsight.utilities.FileUtil;
import org.breedinginsight.utilities.Utilities;
import org.breedinginsight.utilities.*;
import org.jetbrains.annotations.NotNull;

import javax.inject.Inject;
Expand Down Expand Up @@ -138,6 +135,13 @@ private long countGermplasm(UUID programId, String trialDbId) throws ApiExceptio
return obUnits.stream().map(BrAPIObservationUnit::getGermplasmDbId).distinct().count();
}

private BrAPIObservationUnitLevelRelationship getTopLevel(BrAPIObservationUnit ou) {
BrAPIObservationUnitLevelRelationship topLevel = ou.getObservationUnitPosition()
.getObservationLevelRelationships().stream()
.filter(x -> (x.getLevelOrder() != null && x.getLevelOrder().equals(0))).findFirst().orElse(null);
return topLevel;
}

public DownloadFile exportObservations(
Program program,
UUID experimentId,
Expand All @@ -154,9 +158,6 @@ public DownloadFile exportObservations(
new ArrayList<>(Arrays.asList(params.getEnvironments().split(","))) : new ArrayList<>();
FileType fileType = params.getFileExtension();

// make columns present in all exports
List<Column> columns = ExperimentFileColumns.getOrderedColumns();

// add columns for requested dataset obsvars and timestamps
log.debug(logHash + ": fetching experiment for export");
BrAPITrial experiment = getExperiment(program, experimentId);
Expand Down Expand Up @@ -188,8 +189,27 @@ public DownloadFile exportObservations(
Utilities.generateApiExceptionLogMessage(err), err);
}

boolean isSubObs = isSubEntityDataset(ous);

List<Column> columns;

//make columns present in all exports
if (isSubObs){
columns = ExperimentFileColumns.getOrderedColumnsSubEntity();
} else {
columns = ExperimentFileColumns.getOrderedColumns();
}

//add obsUnitID as dynamic column with observation level appended to header
String observationLvl = ous.get(0).getAdditionalInfo().get(BrAPIAdditionalInfoFields.OBSERVATION_LEVEL).getAsString();
if (isSubObs) {
//need to add top level obs unit ids as well
BrAPIObservationUnitLevelRelationship topLevel = getTopLevel(ous.get(0));
if (topLevel != null) {
String topObservationLvl = StringUtils.capitalize(topLevel.getLevelName());
columns = dynamicUpdateObsUnitIDLabel(columns, topObservationLvl);
}
}
String observationLvl = ous.get(0).getAdditionalInfo().get(BrAPIAdditionalInfoFields.OBSERVATION_LEVEL).getAsString();
columns = dynamicUpdateObsUnitIDLabel(columns, observationLvl);

if (params.getDatasetId() != null) {
Expand Down Expand Up @@ -220,7 +240,8 @@ public DownloadFile exportObservations(
params.isIncludeTimestamps(),
obsVars,
studyDbIdByOUId,
programGermplasmByDbId
programGermplasmByDbId,
isSubObs
);

// make export rows for OUs without observations
Expand All @@ -230,7 +251,7 @@ public DownloadFile exportObservations(
// Map Observation Unit to the Study it belongs to.
studyDbIdByOUId.put(ouId, ou.getStudyDbId());
if (!rowByOUId.containsKey(ouId)) {
rowByOUId.put(ouId, createExportRow(experiment, program, ou, studyByDbId, programGermplasmByDbId));
rowByOUId.put(ouId, createExportRow(experiment, program, ou, studyByDbId, programGermplasmByDbId, isSubObs));
}
}
}
Expand Down Expand Up @@ -591,7 +612,8 @@ private void addBrAPIObsToRecords(
boolean includeTimestamp,
List<Trait> obsVars,
Map<String, String> studyDbIdByOUId,
Map<String, BrAPIGermplasm> programGermplasmByDbId) throws ApiException, DoesNotExistException {
Map<String, BrAPIGermplasm> programGermplasmByDbId,
boolean isSubObs) throws ApiException, DoesNotExistException {
Map<String, Trait> varByDbId = new HashMap<>();
obsVars.forEach(var -> varByDbId.put(var.getObservationVariableDbId(), var));
for (BrAPIObservation obs: dataset) {
Expand All @@ -609,7 +631,7 @@ private void addBrAPIObsToRecords(
} else {

// otherwise make a new row
Map<String, Object> row = createExportRow(experiment, program, ou, studyByDbId, programGermplasmByDbId);
Map<String, Object> row = createExportRow(experiment, program, ou, studyByDbId, programGermplasmByDbId, isSubObs);
addObsVarDataToRow(row, obs, includeTimestamp, var, program);
rowByOUId.put(ouId, row);
}
Expand Down Expand Up @@ -727,7 +749,8 @@ private Map<String, Object> createExportRow(
Program program,
BrAPIObservationUnit ou,
Map<String, BrAPIStudy> studyByDbId,
Map<String, BrAPIGermplasm> programGermplasmByDbId) throws ApiException, DoesNotExistException {
Map<String, BrAPIGermplasm> programGermplasmByDbId,
boolean isSubEntity) throws ApiException, DoesNotExistException {
HashMap<String, Object> row = new HashMap<>();

// get OU id, germplasm, and study
Expand All @@ -750,7 +773,6 @@ private Map<String, Object> createExportRow(
row.put(ExperimentObservation.Columns.TEST_CHECK, testCheck);
row.put(ExperimentObservation.Columns.EXP_TITLE, Utilities.removeProgramKey(experiment.getTrialName(), program.getKey()));
row.put(ExperimentObservation.Columns.EXP_DESCRIPTION, experiment.getTrialDescription());
row.put(ExperimentObservation.Columns.EXP_UNIT, ou.getAdditionalInfo().getAsJsonObject().get(BrAPIAdditionalInfoFields.OBSERVATION_LEVEL).getAsString());
row.put(ExperimentObservation.Columns.EXP_TYPE, experiment.getAdditionalInfo().getAsJsonObject().get(BrAPIAdditionalInfoFields.EXPERIMENT_TYPE).getAsString());
row.put(ExperimentObservation.Columns.ENV, Utilities.removeProgramKeyAndUnknownAdditionalData(study.getStudyName(), program.getKey()));
row.put(ExperimentObservation.Columns.ENV_LOCATION, Utilities.removeProgramKey(study.getLocationName(), program.getKey()));
Expand All @@ -776,7 +798,6 @@ private Map<String, Object> createExportRow(

BrAPISeason season = seasonDAO.getSeasonById(study.getSeasons().get(0), program.getId());
row.put(ExperimentObservation.Columns.ENV_YEAR, season.getYear());
row.put(ExperimentObservation.Columns.EXP_UNIT_ID, Utilities.removeProgramKeyAndUnknownAdditionalData(ou.getObservationUnitName(), program.getKey()));

// get replicate number
Optional<BrAPIObservationUnitLevelRelationship> repLevel = ou.getObservationUnitPosition()
Expand Down Expand Up @@ -810,6 +831,26 @@ private Map<String, Object> createExportRow(
String observationLvl = ou.getAdditionalInfo().getAsJsonObject().get(BrAPIAdditionalInfoFields.OBSERVATION_LEVEL).getAsString();
row.put(observationLvl + " " + OBSERVATION_UNIT_ID_SUFFIX, ouId);

if (isSubEntity) {
BrAPIObservationUnitLevelRelationship topLevel = getTopLevel(ou);

if (topLevel != null) {
String topLvlName = StringUtils.capitalize(topLevel.getLevelName());
row.put(ExperimentObservation.Columns.EXP_UNIT, topLvlName);

String topLvlOuId = Utilities.removeProgramKeyAndUnknownAdditionalData(topLevel.getLevelCode(), program.getKey());
row.put(topLvlName + " " + OBSERVATION_UNIT_ID_SUFFIX, topLvlOuId);
}
row.put(ExperimentObservation.Columns.EXP_UNIT_ID, ou.getAdditionalInfo().get(BrAPIAdditionalInfoFields.EXP_UNIT_ID).getAsString());

row.put(ExperimentObservation.Columns.SUB_OBS_UNIT, ou.getAdditionalInfo().getAsJsonObject().get(BrAPIAdditionalInfoFields.OBSERVATION_LEVEL).getAsString());
row.put(ExperimentObservation.Columns.SUB_UNIT_ID, Utilities.removeProgramKeyAndUnknownAdditionalData(ou.getObservationUnitName(), program.getKey()));

} else {
row.put(ExperimentObservation.Columns.EXP_UNIT, ou.getAdditionalInfo().getAsJsonObject().get(BrAPIAdditionalInfoFields.OBSERVATION_LEVEL).getAsString());
row.put(ExperimentObservation.Columns.EXP_UNIT_ID, Utilities.removeProgramKeyAndUnknownAdditionalData(ou.getObservationUnitName(), program.getKey()));
}

return row;
}

Expand Down Expand Up @@ -888,6 +929,10 @@ private List<BrAPIObservation> filterDatasetByEnvironment(
.collect(Collectors.toList());
}

private boolean isSubEntityDataset(List<BrAPIObservationUnit> ous){
return (ous.get(0).getObservationUnitPosition().getObservationLevelRelationships().size() > 2);
}

private void sortDefaultForObservationUnit(List<BrAPIObservationUnit> ous) {
Comparator<BrAPIObservationUnit> studyNameComparator = Comparator.comparing(BrAPIObservationUnit::getStudyName, new IntOrderComparator());
Comparator<BrAPIObservationUnit> ouNameComparator = Comparator.comparing(BrAPIObservationUnit::getObservationUnitName, new IntOrderComparator());
Expand All @@ -899,7 +944,9 @@ private void sortDefaultForExportRows(@NotNull List<Map<String, Object>> exportR
Comparator<Map<String, Object>> expUnitIdComparator =
Comparator.comparing(row -> (row.get(Columns.EXP_UNIT_ID).toString()), new IntOrderComparator());

exportRows.sort(envComparator.thenComparing(expUnitIdComparator));
Comparator<Map<String,Object>> subUnitIdComparator = Comparator.comparing(row -> (row.get(Columns.SUB_UNIT_ID).toString()), new IntOrderComparator());

exportRows.sort(envComparator.thenComparing(expUnitIdComparator).thenComparing(subUnitIdComparator));
}

public BrAPIStudy getEnvironment(Program program, UUID envId) throws ApiException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.util.List;
import java.util.stream.Collectors;

import static org.breedinginsight.brapps.importer.services.processors.experiment.model.ExpImportProcessConstants.OBSERVATION_UNIT_ID_SUFFIX;
import static org.breedinginsight.brapps.importer.services.processors.experiment.model.ExpImportProcessConstants.*;

public enum ExperimentFileColumns {

Expand All @@ -34,13 +34,13 @@ public enum ExperimentFileColumns {
EXP_TITLE(ExperimentObservation.Columns.EXP_TITLE, Column.ColumnDataType.STRING),
EXP_DESCRIPTION(ExperimentObservation.Columns.EXP_DESCRIPTION, Column.ColumnDataType.STRING),
EXP_UNIT(ExperimentObservation.Columns.EXP_UNIT, Column.ColumnDataType.STRING),
//SUB_OBS_UNIT(ExperimentObservation.Columns.SUB_OBS_UNIT, Column.ColumnDataType.STRING),
SUB_OBS_UNIT(ExperimentObservation.Columns.SUB_OBS_UNIT, Column.ColumnDataType.STRING),
EXP_TYPE(ExperimentObservation.Columns.EXP_TYPE, Column.ColumnDataType.STRING),
ENV(ExperimentObservation.Columns.ENV, Column.ColumnDataType.STRING),
ENV_LOCATION(ExperimentObservation.Columns.ENV_LOCATION, Column.ColumnDataType.STRING),
ENV_YEAR(ExperimentObservation.Columns.ENV_YEAR, Column.ColumnDataType.INTEGER),
EXP_UNIT_ID(ExperimentObservation.Columns.EXP_UNIT_ID, Column.ColumnDataType.STRING),
//SUB_UNIT_ID(ExperimentObservation.Columns.SUB_UNIT_ID, Column.ColumnDataType.STRING),
SUB_UNIT_ID(ExperimentObservation.Columns.SUB_UNIT_ID, Column.ColumnDataType.STRING),
REP_NUM(ExperimentObservation.Columns.REP_NUM, Column.ColumnDataType.INTEGER),
BLOCK_NUM(ExperimentObservation.Columns.BLOCK_NUM, Column.ColumnDataType.INTEGER),
ROW(ExperimentObservation.Columns.ROW, Column.ColumnDataType.STRING),
Expand All @@ -52,6 +52,7 @@ public enum ExperimentFileColumns {
TREATMENT_FACTORS(ExperimentObservation.Columns.TREATMENT_FACTORS, Column.ColumnDataType.STRING);

private final Column column;
private static List<ExperimentFileColumns> subEntityOnlyColumns = Arrays.asList(SUB_OBS_UNIT, SUB_UNIT_ID);

ExperimentFileColumns(String value, Column.ColumnDataType dataType) {
this.column = new Column(value, dataType);
Expand All @@ -63,6 +64,14 @@ public String toString() {
}

public static List<Column> getOrderedColumns() {
//Don't include subentity columns
return Arrays.stream(ExperimentFileColumns.values())
.filter(val -> !(subEntityOnlyColumns.contains(val)))
.map(value -> value.column)
.collect(Collectors.toList());
}

public static List<Column> getOrderedColumnsSubEntity() {
return Arrays.stream(ExperimentFileColumns.values())
.map(value -> value.column)
.collect(Collectors.toList());
Expand Down