Skip to content

Commit

Permalink
Fix measurement registration (#854)
Browse files Browse the repository at this point in the history
* Fix measurement registration

* Fix mandatory property for ngs measurements

* fix column header names

* Separate register from edit case

* Fix analysisMethod storage to use the enum name again

* fix wrong method call

Co-authored-by: steffengreiner <[email protected]>

* fix inverted boolean flag

---------

Co-authored-by: steffengreiner <[email protected]>
  • Loading branch information
KochTobi and Steffengreiner authored Oct 17, 2024
1 parent f9393aa commit 4a5aabc
Show file tree
Hide file tree
Showing 14 changed files with 355 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ public String convertToDatabaseColumn(AnalysisMethod attribute) {
if (attribute == null) {
return null;
}
return attribute.abbreviation();
return attribute.name();
}

@Override
public AnalysisMethod convertToEntityAttribute(String dbData) {
if (dbData == null) {
return null;
}
return AnalysisMethod.forAbbreviation(dbData).orElse(null);
return AnalysisMethod.valueOf(dbData);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ static class AnalysisMethodConverter implements AttributeConverter<AnalysisMetho

@Override
public String convertToDatabaseColumn(AnalysisMethod analysisMethod) {
if (analysisMethod == null) {
return null;
}
return analysisMethod.name();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@ public interface MeasurementMetadataConverter {
* </ul>
*
* @param parsingResult the parsing result to take as input for the conversion.
* @param ignoreMeasurementId weather to ignore the measurement identifier or not
* @return a list of converted implementations of {@link MeasurementMetadata}.
* @throws UnknownMetadataTypeException if no matching implementation of
* {@link MeasurementMetadata} can be associated from the
* provided {@link ParsingResult#columnMap()}.
* @since 1.4.0
*/
List<? extends MeasurementMetadata> convert(ParsingResult parsingResult,
boolean ignoreMeasurementId)
List<? extends MeasurementMetadata> convertRegister(ParsingResult parsingResult)
throws UnknownMetadataTypeException;

List<MeasurementMetadata> convertEdit(ParsingResult parsingResult)
throws UnknownMetadataTypeException, MissingSampleIdException;

class UnknownMetadataTypeException extends RuntimeException {

public UnknownMetadataTypeException(String message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import java.util.function.Function;
import java.util.stream.Collectors;
import life.qbic.datamanager.parser.measurement.NGSMeasurementEditColumn;
import life.qbic.datamanager.parser.measurement.NGSMeasurementRegisterColumn;
import life.qbic.datamanager.parser.measurement.ProteomicsMeasurementEditColumn;
import life.qbic.datamanager.parser.measurement.ProteomicsMeasurementRegisterColumn;
import life.qbic.logging.api.Logger;
import life.qbic.projectmanagement.application.measurement.Labeling;
import life.qbic.projectmanagement.application.measurement.MeasurementMetadata;
Expand Down Expand Up @@ -82,14 +84,30 @@ private static Map<String, Integer> countHits(Collection<String> target, Set<Str
}

@Override
public List<MeasurementMetadata> convert(ParsingResult parsingResult, boolean ignoreMeasurementId)
public List<MeasurementMetadata> convertRegister(ParsingResult parsingResult)
throws UnknownMetadataTypeException, MissingSampleIdException {
Objects.requireNonNull(parsingResult);
var properties = parsingResult.columnMap().keySet();
if (looksLikeNgsMeasurement(properties, ignoreMeasurementId)) {
return tryConversion(this::convertNGSMeasurement, parsingResult);
} else if (looksLikeProteomicsMeasurement(properties, ignoreMeasurementId)) {
return tryConversion(this::convertProteomicsMeasurement, parsingResult);
if (looksLikeNgsMeasurement(properties, true)) {
return tryConversion(this::convertNewNGSMeasurement, parsingResult);
} else if (looksLikeProteomicsMeasurement(properties, true)) {
return tryConversion(this::convertNewProteomicsMeasurement, parsingResult);
} else {
throw new UnknownMetadataTypeException(
"Unknown metadata type: cannot match properties to any known metadata type. Provided [%s]".formatted(
String.join(", ", properties)));
}
}

@Override
public List<MeasurementMetadata> convertEdit(ParsingResult parsingResult)
throws UnknownMetadataTypeException, MissingSampleIdException {
Objects.requireNonNull(parsingResult);
var properties = parsingResult.columnMap().keySet();
if (looksLikeNgsMeasurement(properties, false)) {
return tryConversion(this::convertExistingNGSMeasurement, parsingResult);
} else if (looksLikeProteomicsMeasurement(properties, false)) {
return tryConversion(this::convertExistingProteomicsMeasurement, parsingResult);
} else {
throw new UnknownMetadataTypeException(
"Unknown metadata type: cannot match properties to any known metadata type. Provided [%s]".formatted(
Expand All @@ -106,7 +124,65 @@ private List<MeasurementMetadata> tryConversion(
}
}

private List<MeasurementMetadata> convertProteomicsMeasurement(ParsingResult parsingResult) {
private List<MeasurementMetadata> convertNewProteomicsMeasurement(ParsingResult parsingResult) {
var result = new ArrayList<MeasurementMetadata>();
for (int i = 0; i < parsingResult.rows().size(); i++) {
var sampleCode = SampleCode.create(parsingResult.getValueOrDefault(i,
ProteomicsMeasurementRegisterColumn.SAMPLE_ID.headerName(), ""));
var technicalReplicateName = parsingResult.getValueOrDefault(i,
ProteomicsMeasurementRegisterColumn.TECHNICAL_REPLICATE_NAME.headerName(), "");
var organisationId = parsingResult.getValueOrDefault(i,
ProteomicsMeasurementRegisterColumn.ORGANISATION_ID.headerName(), "");
var msDevice = parsingResult.getValueOrDefault(i,
ProteomicsMeasurementRegisterColumn.MS_DEVICE.headerName(), "");
var samplePoolGroup = parsingResult.getValueOrDefault(i,
ProteomicsMeasurementRegisterColumn.POOL_GROUP.headerName(), "");
var facility = parsingResult.getValueOrDefault(i,
ProteomicsMeasurementRegisterColumn.FACILITY.headerName(), "");
var fractionName = parsingResult.getValueOrDefault(i,
ProteomicsMeasurementRegisterColumn.CYCLE_FRACTION_NAME.headerName(), "");
var digestionEnzyme = parsingResult.getValueOrDefault(i,
ProteomicsMeasurementRegisterColumn.DIGESTION_ENZYME.headerName(), "");
var digestionMethod = parsingResult.getValueOrDefault(i,
ProteomicsMeasurementRegisterColumn.DIGESTION_METHOD.headerName(), "");
var enrichmentMethod = parsingResult.getValueOrDefault(i,
ProteomicsMeasurementRegisterColumn.ENRICHMENT_METHOD.headerName(), "");
var injectionVolume = parsingResult.getValueOrDefault(i,
ProteomicsMeasurementRegisterColumn.INJECTION_VOLUME.headerName(), "");
var lcColumn = parsingResult.getValueOrDefault(i,
ProteomicsMeasurementRegisterColumn.LC_COLUMN.headerName(), "");
var lcmsMethod = parsingResult.getValueOrDefault(i,
ProteomicsMeasurementRegisterColumn.LCMS_METHOD.headerName(), "");
var labelingType = parsingResult.getValueOrDefault(i,
ProteomicsMeasurementRegisterColumn.LABELING_TYPE.headerName(), "");
var label = parsingResult.getValueOrDefault(i,
ProteomicsMeasurementRegisterColumn.LABEL.headerName(), "");
var comment = parsingResult.getValueOrDefault(i,
ProteomicsMeasurementRegisterColumn.COMMENT.headerName(), "");
var pxpMetaDaturm = new ProteomicsMeasurementMetadata(
"",
sampleCode,
technicalReplicateName,
organisationId,
msDevice,
samplePoolGroup,
facility,
fractionName,
digestionEnzyme,
digestionMethod,
enrichmentMethod,
injectionVolume,
lcColumn,
lcmsMethod,
new Labeling(labelingType, label),
comment);
result.add(pxpMetaDaturm);
}
return result;
}

private List<MeasurementMetadata> convertExistingProteomicsMeasurement(
ParsingResult parsingResult) {
var result = new ArrayList<MeasurementMetadata>();
for (int i = 0; i < parsingResult.rows().size(); i++) {
var measurementId = parsingResult.getValueOrDefault(i,
Expand Down Expand Up @@ -164,7 +240,7 @@ private List<MeasurementMetadata> convertProteomicsMeasurement(ParsingResult par
return result;
}

private List<MeasurementMetadata> convertNGSMeasurement(ParsingResult parsingResult) {
private List<MeasurementMetadata> convertExistingNGSMeasurement(ParsingResult parsingResult) {
var result = new ArrayList<MeasurementMetadata>();

for (int i = 0; i < parsingResult.rows().size(); i++) {
Expand All @@ -182,7 +258,7 @@ private List<MeasurementMetadata> convertNGSMeasurement(ParsingResult parsingRes
var facility = parsingResult.getValueOrDefault(i,
NGSMeasurementEditColumn.FACILITY.headerName(), "");
var sequencingReadType = parsingResult.getValueOrDefault(i,
NGSMeasurementEditColumn.SEQUENCING_RUN_PROTOCOL.headerName(), "");
NGSMeasurementEditColumn.SEQUENCING_READ_TYPE.headerName(), "");
var libraryKit = parsingResult.getValueOrDefault(i,
NGSMeasurementEditColumn.LIBRARY_KIT.headerName(), "");
var flowCell = parsingResult.getValueOrDefault(i,
Expand Down Expand Up @@ -217,22 +293,74 @@ private List<MeasurementMetadata> convertNGSMeasurement(ParsingResult parsingRes
return result;
}

private List<MeasurementMetadata> convertNewNGSMeasurement(ParsingResult parsingResult) {
var result = new ArrayList<MeasurementMetadata>();

for (int i = 0; i < parsingResult.rows().size(); i++) {
var sampleCodes = List.of(
SampleCode.create(
parsingResult.getValueOrDefault(i,
NGSMeasurementRegisterColumn.SAMPLE_ID.headerName(),
""))
);
var organisationId = parsingResult.getValueOrDefault(i,
NGSMeasurementRegisterColumn.ORGANISATION_ID.headerName(), "");
var instrument = parsingResult.getValueOrDefault(i,
NGSMeasurementRegisterColumn.INSTRUMENT.headerName(), "");
var facility = parsingResult.getValueOrDefault(i,
NGSMeasurementRegisterColumn.FACILITY.headerName(), "");
var sequencingReadType = parsingResult.getValueOrDefault(i,
NGSMeasurementRegisterColumn.SEQUENCING_READ_TYPE.headerName(), "");
var libraryKit = parsingResult.getValueOrDefault(i,
NGSMeasurementRegisterColumn.LIBRARY_KIT.headerName(), "");
var flowCell = parsingResult.getValueOrDefault(i,
NGSMeasurementRegisterColumn.FLOW_CELL.headerName(), "");
var runProtocol = parsingResult.getValueOrDefault(i,
NGSMeasurementRegisterColumn.SEQUENCING_RUN_PROTOCOL.headerName(), "");
var poolGroup = parsingResult.getValueOrDefault(i,
NGSMeasurementRegisterColumn.POOL_GROUP.headerName(), "");
var indexI7 = parsingResult.getValueOrDefault(i,
NGSMeasurementRegisterColumn.INDEX_I7.headerName(), "");
var indexI5 = parsingResult.getValueOrDefault(i,
NGSMeasurementRegisterColumn.INDEX_I5.headerName(), "");
var comment = parsingResult.getValueOrDefault(i,
NGSMeasurementRegisterColumn.COMMENT.headerName(), "");
var metadatum = new NGSMeasurementMetadata(
"",
sampleCodes,
organisationId,
instrument,
facility,
sequencingReadType,
libraryKit,
flowCell,
runProtocol,
poolGroup,
indexI7,
indexI5,
comment
);
result.add(metadatum);
}
return result;
}

private boolean looksLikeNgsMeasurement(Collection<String> properties, boolean ignoreID) {
var formattedProperties = properties.stream().map(String::toLowerCase)
.collect(Collectors.toList());
Map<String, Integer> hitMap;
if (ignoreID) {
formattedProperties.remove(NGSMeasurementEditColumn.MEASUREMENT_ID.headerName());
hitMap = countHits(formattedProperties,
Arrays.stream(NGSMeasurementEditColumn.values())
.map(NGSMeasurementEditColumn::headerName)
.collect(
Collectors.toSet()), NGSMeasurementEditColumn.MEASUREMENT_ID.headerName());
Arrays.stream(NGSMeasurementRegisterColumn.values())
.map(NGSMeasurementRegisterColumn::headerName)
.map(Sanitizer::headerEncoder)
.collect(Collectors.toSet()), NGSMeasurementEditColumn.MEASUREMENT_ID.headerName());
} else {
hitMap = countHits(formattedProperties,
Arrays.stream(NGSMeasurementEditColumn.values())
.map(NGSMeasurementEditColumn::headerName).collect(
Collectors.toSet()));
.map(NGSMeasurementEditColumn::headerName)
.map(Sanitizer::headerEncoder)
.collect(Collectors.toSet()));
}
var missingProperties = new ArrayList<>();
for (Entry<String, Integer> entry : hitMap.entrySet()) {
Expand All @@ -253,16 +381,18 @@ private boolean looksLikeProteomicsMeasurement(Collection<String> properties, bo
.collect(Collectors.toList());
Map<String, Integer> hitMap;
if (ignoreID) {
formattedProperties.remove(ProteomicsMeasurementEditColumn.MEASUREMENT_ID.headerName());
hitMap = countHits(formattedProperties,
Arrays.stream(ProteomicsMeasurementEditColumn.values())
.map(ProteomicsMeasurementEditColumn::headerName).collect(
Collectors.toSet()), ProteomicsMeasurementEditColumn.MEASUREMENT_ID.headerName());
Arrays.stream(ProteomicsMeasurementRegisterColumn.values())
.map(ProteomicsMeasurementRegisterColumn::headerName)
.map(Sanitizer::headerEncoder)
.collect(Collectors.toSet()),
ProteomicsMeasurementEditColumn.MEASUREMENT_ID.headerName());
} else {
hitMap = countHits(formattedProperties,
Arrays.stream(ProteomicsMeasurementEditColumn.values())
.map(ProteomicsMeasurementEditColumn::headerName).collect(
Collectors.toSet()));
.map(ProteomicsMeasurementEditColumn::headerName)
.map(Sanitizer::headerEncoder)
.collect(Collectors.toSet()));
}
var missingProperties = new ArrayList<>();
for (Entry<String, Integer> entry : hitMap.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,35 @@
/**
* <b>NGS Measurement Columns</b>
*
* <p>Enumeration of the columns shown in the file used for NGS measurement registration and edit
* <p>Enumeration of the columns shown in the file used for NGS measurement edit
* in the context of measurement file based upload. Provides the name of the header column, the
* column index and if the column should be set to readOnly in the generated sheet
* </p>
*/
public enum NGSMeasurementEditColumn {

MEASUREMENT_ID("Measurement ID", 0, true),
SAMPLE_ID("QBiC Sample Id", 1, true),
SAMPLE_NAME("Sample Name", 2, true),
POOL_GROUP("Sample Pool Group", 3, true),
ORGANISATION_ID("Organisation ID", 4, false),
ORGANISATION_NAME("Organisation Name", 5, true),
FACILITY("Facility", 6, false),
INSTRUMENT("Instrument", 7, false),
INSTRUMENT_NAME("Instrument Name", 8, true),
SEQUENCING_READ_TYPE("Sequencing Read Type", 9, false),
LIBRARY_KIT("Library Kit", 10, false),
FLOW_CELL("Flow Cell", 11, false),
SEQUENCING_RUN_PROTOCOL("Sequencing Run Protocol", 12, false),
INDEX_I7("Index i7", 13, false),
INDEX_I5("Index i5", 14, false),
COMMENT("Comment", 15, false),
MEASUREMENT_ID("Measurement ID", 0, true, true),
SAMPLE_ID("QBiC Sample Id", 1, true, true),
SAMPLE_NAME("Sample Name", 2, true, false),
POOL_GROUP("Sample Pool Group", 3, true, false),
ORGANISATION_ID("Organisation ID", 4, false, true),
ORGANISATION_NAME("Organisation Name", 5, true, false),
FACILITY("Facility", 6, false, true),
INSTRUMENT("Instrument", 7, false, true),
INSTRUMENT_NAME("Instrument Name", 8, true, false),
SEQUENCING_READ_TYPE("Sequencing Read Type", 9, false, true),
LIBRARY_KIT("Library Kit", 10, false, false),
FLOW_CELL("Flow Cell", 11, false, false),
SEQUENCING_RUN_PROTOCOL("Sequencing Run Protocol", 12, false, false),
INDEX_I7("Index i7", 13, false, false),
INDEX_I5("Index i5", 14, false, false),
COMMENT("Comment", 15, false, false),
;

private final String headerName;
private final int columnIndex;
private final boolean readOnly;
private final boolean mandatory;

static int maxColumnIndex() {
return Arrays.stream(values())
Expand All @@ -44,11 +45,14 @@ static int maxColumnIndex() {
* @param headerName the name in the header
* @param columnIndex the index of the column this property is in
* @param readOnly is the property read only
* @param mandatory
*/
NGSMeasurementEditColumn(String headerName, int columnIndex, boolean readOnly) {
NGSMeasurementEditColumn(String headerName, int columnIndex, boolean readOnly,
boolean mandatory) {
this.headerName = headerName;
this.columnIndex = columnIndex;
this.readOnly = readOnly;
this.mandatory = mandatory;
}

public String headerName() {
Expand All @@ -59,8 +63,12 @@ public int columnIndex() {
return columnIndex;
}

public boolean readOnly() {
public boolean isReadOnly() {
return readOnly;
}

public boolean isMandatory() {
return mandatory;
}

}
Loading

0 comments on commit 4a5aabc

Please sign in to comment.