Skip to content

Commit

Permalink
x1216: Allow section register to specify date sectioned
Browse files Browse the repository at this point in the history
Add to SectionRegisterContent and SectionRegisterFileReader.
Record the given date as a measurement.
Look up the measurement ancestrally for the section date in
the release file.
  • Loading branch information
khelwood committed Jun 26, 2024
1 parent b1d7635 commit c13fd03
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import uk.ac.sanger.sccp.stan.model.LifeStage;
import uk.ac.sanger.sccp.utils.BasicUtils;

import java.time.LocalDate;
import java.util.Objects;

/**
Expand All @@ -25,6 +26,7 @@ public class SectionRegisterContent {
private Integer sectionNumber;
private Integer sectionThickness;
private String region;
private LocalDate dateSectioned;

public SectionRegisterContent() {}

Expand Down Expand Up @@ -147,6 +149,15 @@ public void setRegion(String region) {
this.region = region;
}

/** The date the sample was sectioned */
public LocalDate getDateSectioned() {
return this.dateSectioned;
}

public void setDateSectioned(LocalDate dateSectioned) {
this.dateSectioned = dateSectioned;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -166,6 +177,7 @@ public boolean equals(Object o) {
&& Objects.equals(this.sectionNumber, that.sectionNumber)
&& Objects.equals(this.sectionThickness, that.sectionThickness)
&& Objects.equals(this.region, that.region)
&& Objects.equals(this.dateSectioned, that.dateSectioned)
);
}

Expand All @@ -191,6 +203,7 @@ public String toString() {
.add("sectionNumber", sectionNumber)
.add("sectionThickness", sectionThickness)
.add("region", region)
.add("dateSectioned", dateSectioned)
.reprStringValues()
.omitNullValues()
.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,23 @@ public Operation createOp(User user, OperationType opType, Labware lw) {
* @return the measurements created
*/
public Iterable<Measurement> createMeasurements(SectionRegisterLabware srl, Labware lw, Operation op,
UCMap<Sample> sampleMap) {
List<Measurement> measurements = srl.getContents().stream()
.filter(content -> content.getSectionThickness() != null)
.map(content -> new Measurement(
null, "Thickness", content.getSectionThickness().toString(),
sampleMap.get(content.getExternalIdentifier()).getId(), op.getId(),
lw.getSlot(content.getAddress()).getId()))
.collect(toList());
UCMap<Sample> sampleMap) {
List<Measurement> measurements = new ArrayList<>();
for (var src : srl.getContents()) {
if (src.getDateSectioned() == null && src.getSectionThickness() == null) {
continue;
}
Sample sample = sampleMap.get(src.getExternalIdentifier());
Slot slot = lw.getSlot(src.getAddress());
if (src.getSectionThickness() != null) {
measurements.add(new Measurement(null, "Thickness", src.getSectionThickness().toString(),
sample.getId(), op.getId(), slot.getId()));
}
if (src.getDateSectioned() != null) {
measurements.add(new Measurement(null, "Date sectioned", src.getDateSectioned().toString(),
sample.getId(), op.getId(), slot.getId()));
}
}
return (measurements.isEmpty() ? measurements : measurementRepo.saveAll(measurements));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import uk.ac.sanger.sccp.stan.service.ValidationException;

import java.io.IOException;
import java.time.LocalDate;
import java.util.List;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -34,6 +35,7 @@ enum Column implements IColumn {
Section_external_ID,
Section_number(Integer.class),
Section_thickness(Integer.class),
Date_sectioned(LocalDate.class, Pattern.compile("date.*sectioned|section.*date", Pattern.CASE_INSENSITIVE), false),
Section_position(Pattern.compile("(if.+)?(section\\s+)?position", Pattern.CASE_INSENSITIVE)),
;

Expand Down Expand Up @@ -64,7 +66,7 @@ public String toString() {
return this.name().replace('_',' ');
}

/** The data type (String or Integer) expected in the column. */
/** The data type (e.g. String or Integer) expected in the column. */
@Override
public Class<?> getDataType() {
return this.dataType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.io.IOException;
import java.nio.file.*;
import java.time.LocalDate;
import java.util.*;

import static java.util.stream.Collectors.toList;
Expand Down Expand Up @@ -39,7 +40,7 @@ public SectionRegisterRequest createRequest(Collection<String> problems, List<Ma
String groupx = null;
String workNumber = getUniqueString(rows.stream().map(row -> (String) row.get(Column.Work_number)),
() -> problems.add("Multiple work numbers specified."));
if(nullOrEmpty(workNumber)) {
if (nullOrEmpty(workNumber)) {
problems.add("Missing work number.");
}

Expand Down Expand Up @@ -114,6 +115,7 @@ public SectionRegisterContent createRequestContent(Collection<String> problems,
content.setTissueType((String) row.get(Column.Tissue_type));
content.setSpatialLocation((Integer) row.get(Column.Spatial_location));
content.setRegion((String) row.get(Column.Section_position));
content.setDateSectioned((LocalDate) row.get(Column.Date_sectioned));
return content;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import uk.ac.sanger.sccp.utils.tsv.TsvColumn;

import javax.persistence.EntityNotFoundException;
import java.time.LocalDate;
import java.time.format.DateTimeParseException;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -514,6 +516,65 @@ public void loadSectionDate(Collection<ReleaseEntry> entries, Ancestry ancestry)
}
});
}

Map<SlotIdSampleId, LocalDate> slotSampleSectionDates = findSlotSampleDates(
measurementRepo.findAllBySlotIdInAndName(slotIds, "Date sectioned")
);
if (!slotSampleSectionDates.isEmpty()) {
for (ReleaseEntry entry : entries) {
if (entry.getSlot() != null && entry.getSample() != null && entry.getSectionDate() == null) {
entry.setSectionDate(findEntrySectionDate(entry, slotSampleSectionDates, ancestry));
}
}
}
}

/**
* Loads the section date for the given entry from the given map using the given ancestry
* @param entry the entry to load the date for
* @param slotSampleSectionDates map of slot/sample ids to section date
* @param ancestry the ancestry for the slot/samples
* @return the section date for the given entry
*/
public LocalDate findEntrySectionDate(ReleaseEntry entry, Map<SlotIdSampleId, LocalDate> slotSampleSectionDates,
Ancestry ancestry) {
SlotSample entrySs = new SlotSample(entry.getSlot(), entry.getSample());
for (SlotSample ss : ancestry.ancestors(entrySs)) {
LocalDate date = slotSampleSectionDates.get(new SlotIdSampleId(ss.slotId(), ss.sampleId()));
if (date != null) {
return date;
}
}
return null;
}

/**
* Builds a map from slot/sample ids to the localdate in the given measurements
* @param measurements the measurements to look through
* @return a map from slot/sample ids to the relevant date in the measurements
*/
public Map<SlotIdSampleId, LocalDate> findSlotSampleDates(Collection<Measurement> measurements) {
if (measurements.isEmpty()) {
return Map.of();
}
Map<SlotIdSampleId, LocalDate> map = new HashMap<>(measurements.size());
for (Measurement measurement : measurements) {
if (measurement.getSampleId()==null || measurement.getSlotId()==null || measurement.getValue()==null) {
continue;
}
LocalDate value;
try {
value = LocalDate.parse(measurement.getValue());
} catch (DateTimeParseException e) {
continue;
}
SlotIdSampleId key = new SlotIdSampleId(measurement.getSlotId(), measurement.getSampleId());
LocalDate oldValue = map.get(key);
if (oldValue==null || oldValue.isBefore(value)) {
map.put(key, value);
}
}
return map;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ input SectionRegisterContent {
sectionThickness: Int
"""The region of this sample in this slot, if any."""
region: String
"""The date the sample was sectioned."""
dateSectioned: Date
}

"""A request to register one or more sections into one piece of labware."""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
package uk.ac.sanger.sccp.stan.service.register;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import uk.ac.sanger.sccp.stan.EntityFactory;
import uk.ac.sanger.sccp.stan.model.*;
import uk.ac.sanger.sccp.stan.repo.*;
import uk.ac.sanger.sccp.stan.request.register.RegisterResult;
import uk.ac.sanger.sccp.stan.request.register.SectionRegisterContent;
import uk.ac.sanger.sccp.stan.request.register.SectionRegisterLabware;
import uk.ac.sanger.sccp.stan.request.register.SectionRegisterRequest;
import uk.ac.sanger.sccp.stan.service.LabwareService;
import uk.ac.sanger.sccp.stan.service.OperationService;
import uk.ac.sanger.sccp.stan.service.ValidationException;
import uk.ac.sanger.sccp.stan.request.register.*;
import uk.ac.sanger.sccp.stan.service.*;
import uk.ac.sanger.sccp.stan.service.work.WorkService;
import uk.ac.sanger.sccp.utils.UCMap;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.Stream;

Expand Down Expand Up @@ -429,8 +420,10 @@ public void testCreateMeasurements() {
content(A1, xns[0], 14),
content(A1, xns[1], 15),
content(B1, xns[2]),
content(B2, xns[3], 16)
content(B2, xns[3])
);
contents.get(1).setDateSectioned(LocalDate.of(2024,1,13));
contents.get(3).setDateSectioned(LocalDate.of(2024,2,14));

UCMap<Sample> sampleMap = UCMap.from((Sample sam) -> sam.getTissue().getExternalName(), samples);
SectionRegisterLabware srl = new SectionRegisterLabware(lw.getExternalBarcode(), lt.getName(), contents);
Expand All @@ -447,7 +440,8 @@ public void testCreateMeasurements() {
verify(mockMeasurementRepo).saveAll(List.of(
new Measurement(null, "Thickness", "14", samples[0].getId(), opId, slotA1.getId()),
new Measurement(null, "Thickness", "15", samples[1].getId(), opId, slotA1.getId()),
new Measurement(null, "Thickness", "16", samples[3].getId(), opId, slotB2.getId())
new Measurement(null, "Date sectioned", "2024-01-13", samples[1].getId(), opId, slotA1.getId()),
new Measurement(null, "Date sectioned", "2024-02-14", samples[3].getId(), opId, slotB2.getId())
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ void testIndexColumns() {
Row row = mockRow("work number", "slide type", "external slide id", "xenium barcode",
"section address", "fixative", "embedding medium", "donor id", "life stage",
"species", "humfre", "tissue type", "spatial location", "replicate number",
"section external id", "section number", "section thickness", "if bla bla bla position", null, "");
"section external id", "section number", "section thickness", "date sectioned", "if bla bla bla position", null, "");
List<String> problems = new ArrayList<>();
var result = reader.indexColumns(problems, row);
assertThat(problems).isEmpty();
Expand Down
Loading

0 comments on commit c13fd03

Please sign in to comment.