Skip to content

Commit

Permalink
Reduce DB queries queries by reusing study visits, NAb runs and sampl…
Browse files Browse the repository at this point in the history
…e properties
  • Loading branch information
labkey-jeckels committed Sep 15, 2023
1 parent 1e20ade commit 34843a9
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 79 deletions.
2 changes: 1 addition & 1 deletion nab/src/org/labkey/nab/NabAssayRun.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

public abstract class NabAssayRun extends DilutionAssayRun
{
public NabAssayRun(DilutionAssayProvider provider, ExpRun run,
public NabAssayRun(DilutionAssayProvider<?> provider, ExpRun run,
User user, List<Integer> cutoffs, StatsService.CurveFitType renderCurveFitType)
{
super(provider, run, user, cutoffs, renderCurveFitType);
Expand Down
28 changes: 13 additions & 15 deletions nab/src/org/labkey/nab/SinglePlateNabAssayRun.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.labkey.api.assay.plate.WellGroup;
import org.labkey.api.collections.CaseInsensitiveHashMap;
import org.labkey.api.data.ColumnInfo;
import org.labkey.api.data.Container;
import org.labkey.api.data.SimpleFilter;
import org.labkey.api.data.TableInfo;
import org.labkey.api.data.TableSelector;
Expand Down Expand Up @@ -52,20 +53,20 @@
*/
public class SinglePlateNabAssayRun extends NabAssayRun
{
protected Plate _plate;
private DilutionSummary[] _dilutionSummaries;
protected final Plate _plate;
private final DilutionSummary[] _dilutionSummaries;
public static final String SAMPLE_WELL_GROUP_NAME = "SampleWellGroupName";

private Map<Position, WellGroup> _positionToVirusMap = new HashMap<>();
private Map<String, Map<WellGroup, WellGroup>> _virusGroupToControlMap = new HashMap<>();
private final Map<Position, WellGroup> _positionToVirusMap = new HashMap<>();
private final Map<String, Map<WellGroup, WellGroup>> _virusGroupToControlMap = new HashMap<>();

public SinglePlateNabAssayRun(DilutionAssayProvider provider, ExpRun run, Plate plate,
public SinglePlateNabAssayRun(DilutionAssayProvider<?> provider, ExpRun run, Plate plate,
User user, List<Integer> cutoffs, StatsService.CurveFitType renderCurveFitType)
{
super(provider, run, user, cutoffs, renderCurveFitType);
_plate = plate;
List<? extends WellGroup> specimenGroups = _plate.getWellGroups(WellGroup.Type.SPECIMEN);
_dilutionSummaries = getDilutionSumariesForWellGroups(specimenGroups);
_dilutionSummaries = getDilutionSummariesForWellGroups(specimenGroups, run.getContainer());
}

@Override
Expand All @@ -81,7 +82,7 @@ public List<Plate> getPlates()
}

@Override
protected DilutionSummary[] getDilutionSumariesForWellGroups(List<? extends WellGroup> specimenGroups)
protected DilutionSummary[] getDilutionSummariesForWellGroups(List<? extends WellGroup> specimenGroups, Container container)
{
List<WellGroup> specimenVirusGroups = new ArrayList<>();
for (WellGroup specimenGroup : specimenGroups)
Expand Down Expand Up @@ -116,7 +117,7 @@ protected DilutionSummary[] getDilutionSumariesForWellGroups(List<? extends Well
specimenVirusGroups.add(specimenGroup);
}

return super.getDilutionSumariesForWellGroups(specimenVirusGroups);
return super.getDilutionSummariesForWellGroups(specimenVirusGroups, container);
}


Expand Down Expand Up @@ -205,9 +206,6 @@ public WellGroup getVirusControlWells(Plate plate, @Nullable List<Position> data

/**
* Returns the virus well group that contains the specified positions.
* @param plate
* @param positions
* @return
*/
@Nullable
private WellGroup getVirusGroupForPositions(Plate plate, List<Position> positions)
Expand Down Expand Up @@ -267,7 +265,7 @@ private WellGroup getControlWellsForVirus(Plate plate, WellGroup virusGroup, Str
if (virus.contains(pos))
{
if (!virusToControlPositions.containsKey(virus))
virusToControlPositions.put(virus, new ArrayList<Position>());
virusToControlPositions.put(virus, new ArrayList<>());

virusToControlPositions.get(virus).add(pos);
break;
Expand Down Expand Up @@ -322,7 +320,7 @@ public Map<String, Object> getVirusNames()
protected String getVirusName(String virusWellGroupName)
{
List<? extends ExpData> outputDatas = _run.getOutputDatas(null);
if (outputDatas.size() > 0)
if (!outputDatas.isEmpty())
{
Lsid virusLsid = DilutionDataHandler.createVirusWellGroupLsid(outputDatas.get(0), virusWellGroupName);
AssayProtocolSchema schema = _provider.createProtocolSchema(_user, _run.getContainer(), _protocol, null);
Expand Down Expand Up @@ -356,8 +354,8 @@ private WellGroup getControlWellGroupForVirus(Plate plate, String virusWellGroup
return plate.getWellGroup(WellGroup.Type.CONTROL, controlGroupName);
}

private CaseInsensitiveHashMap<WellGroup> _cellControls = new CaseInsensitiveHashMap<>();
private CaseInsensitiveHashMap<WellGroup> _virusControls = new CaseInsensitiveHashMap<>();
private final CaseInsensitiveHashMap<WellGroup> _cellControls = new CaseInsensitiveHashMap<>();
private final CaseInsensitiveHashMap<WellGroup> _virusControls = new CaseInsensitiveHashMap<>();

private boolean ensureCellControl(Plate plate, String virusWellGroupName)
{
Expand Down
12 changes: 6 additions & 6 deletions nab/src/org/labkey/nab/SinglePlateNabDataHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public DataType getDataType()
}

@Override
protected DilutionAssayRun createDilutionAssayRun(DilutionAssayProvider provider, ExpRun run, List<Plate> plates, User user,
protected DilutionAssayRun createDilutionAssayRun(DilutionAssayProvider<?> provider, ExpRun run, List<Plate> plates, User user,
List<Integer> sortedCutoffs, StatsService.CurveFitType fit)
{
return new SinglePlateNabAssayRun(provider, run, plates.get(0), user, sortedCutoffs, fit);
Expand Down Expand Up @@ -175,7 +175,7 @@ DataLoader createGrid() throws IOException, ExperimentException

// Excel can only handle single sheets so this helper class abstracts away creating the loader for a specific sheet.
// In addition we need to configure the loaders differently for parsing a list of data versus a grid of data without headers.
protected abstract class Load
protected abstract static class Load
{
abstract DataLoader createList() throws IOException, ExperimentException;
abstract DataLoader createGrid() throws IOException, ExperimentException;
Expand Down Expand Up @@ -313,11 +313,11 @@ protected void prepareWellGroups(List<WellGroup> groups, ExpMaterial sampleInput
virusData.add(well);
}
}
applyDilution(virusData, sampleInput, properties, reverseDirection);
applyDilution(virusData, sampleInput, properties, reverseDirection, sampleProperties);
}
}
else
applyDilution(wells, sampleInput, properties, reverseDirection);
applyDilution(wells, sampleInput, properties, reverseDirection, sampleProperties);
}

@Override
Expand Down Expand Up @@ -363,11 +363,11 @@ public void beforeDeleteData(List<ExpData> datas, User user) throws ExperimentEx
AssayProvider provider = AssayService.get().getProvider(protocol);
AssayProtocolSchema protocolSchema = provider.createProtocolSchema(null, protocol.getContainer(), protocol, null);
TableInfo virusTable = protocolSchema.createTable(DilutionManager.VIRUS_TABLE_NAME, null);
if (virusTable instanceof FilteredTable)
if (virusTable instanceof FilteredTable ft)
{
if (virusTable.getColumn(FieldKey.fromParts(NabVirusDomainKind.DATLSID_COLUMN_NAME)) != null)
{
TableInfo table = ((FilteredTable) virusTable).getRealTable();
TableInfo table = ft.getRealTable();
SimpleFilter dataLsidFilter = new SimpleFilter(FieldKey.fromString(NabVirusDomainKind.DATLSID_COLUMN_NAME), data.getLSID());

// delete the rows in the virus table associated with this run
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
*/
public class CrossPlateDilutionNabAssayRun extends NabAssayRun
{
protected List<Plate> _plates;
private DilutionSummary[] _dilutionSummaries;
protected final List<Plate> _plates;
private final DilutionSummary[] _dilutionSummaries;

public CrossPlateDilutionNabAssayRun(DilutionAssayProvider provider, ExpRun run, List<Plate> plates,
public CrossPlateDilutionNabAssayRun(DilutionAssayProvider<?> provider, ExpRun run, List<Plate> plates,
User user, List<Integer> cutoffs, StatsService.CurveFitType renderCurveFitType)
{
super(provider, run, user, cutoffs, renderCurveFitType);
Expand All @@ -52,18 +52,13 @@ public CrossPlateDilutionNabAssayRun(DilutionAssayProvider provider, ExpRun run,
{
for (WellGroup sample : plate.getWellGroups(WellGroup.Type.SPECIMEN))
{
List<WellGroup> groups = sampleGroups.get(sample.getName());
if (groups == null)
{
groups = new ArrayList<>();
sampleGroups.put(sample.getName(), groups);
}
List<WellGroup> groups = sampleGroups.computeIfAbsent(sample.getName(), k -> new ArrayList<>());
groups.add(sample);
}
}
int index = 0;
for (Map.Entry<String, List<WellGroup>> sample : sampleGroups.entrySet())
_dilutionSummaries[index++] = new DilutionSummary(this, sample.getValue(), null, _renderedCurveFitType);
_dilutionSummaries[index++] = new DilutionSummary(this, sample.getValue(), null, _renderedCurveFitType, run.getContainer());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public DataType getDataType()
}

@Override
protected Map<ExpMaterial, List<WellGroup>> getMaterialWellGroupMapping(DilutionAssayProvider provider, List<Plate> plates, Map<ExpMaterial,String> sampleInputs) throws ExperimentException
protected Map<ExpMaterial, List<WellGroup>> getMaterialWellGroupMapping(DilutionAssayProvider<?> provider, List<Plate> plates, Map<ExpMaterial,String> sampleInputs) throws ExperimentException
{
Map<String, ExpMaterial> nameToMaterial = new HashMap<>();
for (Map.Entry<ExpMaterial,String> e : sampleInputs.entrySet())
Expand Down Expand Up @@ -81,7 +81,7 @@ protected Map<ExpMaterial, List<WellGroup>> getMaterialWellGroupMapping(Dilution
}

@Override
protected DilutionAssayRun createDilutionAssayRun(DilutionAssayProvider provider, ExpRun run, List<Plate> plates, User user, List<Integer> sortedCutoffs, StatsService.CurveFitType fit)
protected DilutionAssayRun createDilutionAssayRun(DilutionAssayProvider<?> provider, ExpRun run, List<Plate> plates, User user, List<Integer> sortedCutoffs, StatsService.CurveFitType fit)
{
return new CrossPlateDilutionNabAssayRun(provider, run, plates, user, sortedCutoffs, fit);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected List<Plate> createPlates(File dataFile, Plate template) throws Experim
final int expectedCols = template.getColumns();

List<double[][]> matrices = parse(dataFile, loader.getColumns(), loader.load(), expectedRows, expectedCols);
if (matrices == null || matrices.size() == 0)
if (matrices == null || matrices.isEmpty())
throw createParseError(dataFile, "No plate data found");

int plateNumber = 1;
Expand Down Expand Up @@ -184,11 +184,13 @@ protected void prepareWellGroups(List<WellGroup> groups, ExpMaterial sampleInput
List<WellData> wells = new ArrayList<>();
// All well groups use the same plate template, so it's okay to just check the dilution direction of the first group:
boolean reverseDirection = Boolean.parseBoolean((String) groups.get(0).getProperty(SampleProperty.ReverseDilutionDirection.name()));

Map<PropertyDescriptor,Object> sampleProperties = sampleInput.getPropertyValues();

for (WellGroup group : groups)
{
Map<PropertyDescriptor,Object> map = sampleInput.getPropertyValues();
for (DomainProperty property : properties.values())
group.setProperty(property.getName(), map.get(property.getPropertyDescriptor()));
group.setProperty(property.getName(), sampleProperties.get(property.getPropertyDescriptor()));

boolean hasExplicitOrder = true;
List<? extends WellData> wellData = group.getWellData(true);
Expand Down Expand Up @@ -230,7 +232,7 @@ protected void prepareWellGroups(List<WellGroup> groups, ExpMaterial sampleInput
wells.addAll(wellData);
}

applyDilution(wells, sampleInput, properties, reverseDirection);
applyDilution(wells, sampleInput, properties, reverseDirection, sampleProperties);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.labkey.api.assay.dilution.DilutionSummary;
import org.labkey.api.assay.nab.Luc5Assay;
import org.labkey.api.assay.plate.WellGroup;
import org.labkey.api.data.Container;
import org.labkey.api.data.statistics.StatsService;
import org.labkey.api.exp.PropertyDescriptor;
import org.labkey.api.exp.api.ExpMaterial;
Expand All @@ -40,10 +41,10 @@
*/
public class SinglePlateDilutionNabAssayRun extends NabAssayRun
{
protected List<Plate> _plates;
private DilutionSummary[] _dilutionSummaries;
protected final List<Plate> _plates;
private final DilutionSummary[] _dilutionSummaries;

public SinglePlateDilutionNabAssayRun(DilutionAssayProvider provider, ExpRun run, List<Plate> plates,
public SinglePlateDilutionNabAssayRun(DilutionAssayProvider<?> provider, ExpRun run, List<Plate> plates,
User user, List<Integer> cutoffs, StatsService.CurveFitType renderCurveFitType)
{
super(provider, run, user, cutoffs, renderCurveFitType);
Expand All @@ -56,21 +57,16 @@ public SinglePlateDilutionNabAssayRun(DilutionAssayProvider provider, ExpRun run
{
String virusName = plate.getProperty(NabAssayProvider.VIRUS_NAME_PROPERTY_NAME).toString();
String key = SinglePlateDilutionSamplePropertyHelper.getKey(virusName, sample.getName());
List<WellGroup> groups = sampleGroups.get(key);
if (groups == null)
{
groups = new ArrayList<>();
sampleGroups.put(key, groups);
}
List<WellGroup> groups = sampleGroups.computeIfAbsent(key, k -> new ArrayList<>());
groups.add(sample);
}
}
List<DilutionSummary> dilutionSummaries = new ArrayList<>();

for (Map.Entry<String, List<WellGroup>> sample : sampleGroups.entrySet())
dilutionSummaries.add(new MultiVirusDilutionSummary(this, sample.getValue(), null, _renderedCurveFitType));
dilutionSummaries.add(new MultiVirusDilutionSummary(this, sample.getValue(), null, _renderedCurveFitType, _run.getContainer()));

_dilutionSummaries = dilutionSummaries.toArray(new DilutionSummary[dilutionSummaries.size()]);
_dilutionSummaries = dilutionSummaries.toArray(new DilutionSummary[0]);
}

@Override
Expand All @@ -87,8 +83,6 @@ public List<Plate> getPlates()

/**
* Generate a key for the sample level property map
* @param material
* @return
*/
@Override
protected String getSampleKey(ExpMaterial material)
Expand All @@ -105,8 +99,6 @@ protected String getSampleKey(ExpMaterial material)

/**
* Generate a key for the sample level property map
* @param summary
* @return
*/
@Override
protected String getSampleKey(DilutionSummary summary)
Expand All @@ -119,9 +111,9 @@ protected String getSampleKey(DilutionSummary summary)

private static class MultiVirusDilutionSummary extends DilutionSummary
{
public MultiVirusDilutionSummary(Luc5Assay assay, List<WellGroup> sampleGroups, String lsid, StatsService.CurveFitType curveFitType)
public MultiVirusDilutionSummary(Luc5Assay assay, List<WellGroup> sampleGroups, String lsid, StatsService.CurveFitType curveFitType, Container container)
{
super(assay, sampleGroups, lsid, curveFitType);
super(assay, sampleGroups, lsid, curveFitType, container);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@
import org.labkey.api.assay.plate.WellGroup;
import org.labkey.api.data.statistics.StatsService;
import org.labkey.api.exp.ExperimentException;
import org.labkey.api.exp.Lsid;
import org.labkey.api.exp.PropertyDescriptor;
import org.labkey.api.exp.api.DataType;
import org.labkey.api.exp.api.ExpData;
import org.labkey.api.exp.api.ExpMaterial;
import org.labkey.api.exp.api.ExpRun;
import org.labkey.api.exp.api.ExperimentService;
Expand Down Expand Up @@ -167,7 +165,7 @@ protected List<Plate> createPlates(File dataFile, Plate template) throws Experim
}

@Override
protected Map<ExpMaterial, List<WellGroup>> getMaterialWellGroupMapping(DilutionAssayProvider provider, List<Plate> plates, Map<ExpMaterial,String> sampleInputs) throws ExperimentException
protected Map<ExpMaterial, List<WellGroup>> getMaterialWellGroupMapping(DilutionAssayProvider<?> provider, List<Plate> plates, Map<ExpMaterial,String> sampleInputs) throws ExperimentException
{
Map<String, ExpMaterial> nameToMaterial = new HashMap<>();
for (Map.Entry<ExpMaterial,String> e : sampleInputs.entrySet())
Expand All @@ -189,38 +187,19 @@ protected Map<ExpMaterial, List<WellGroup>> getMaterialWellGroupMapping(Dilution
throw new ExperimentException("Unable to find sample metadata for sample well group \"" + name +
"\": your sample metadata file may contain incorrect well group names, or it may not list all required samples.");
}
List<WellGroup> materialWellGroups = mapping.get(material);
if (materialWellGroups == null)
{
materialWellGroups = new ArrayList<>();
mapping.put(material, materialWellGroups);
}
List<WellGroup> materialWellGroups = mapping.computeIfAbsent(material, k -> new ArrayList<>());
materialWellGroups.add(specimenGroup);
}
}
return mapping;
}

@Override
protected DilutionAssayRun createDilutionAssayRun(DilutionAssayProvider provider, ExpRun run, List<Plate> plates, User user, List<Integer> sortedCutoffs, StatsService.CurveFitType fit)
protected DilutionAssayRun createDilutionAssayRun(DilutionAssayProvider<?> provider, ExpRun run, List<Plate> plates, User user, List<Integer> sortedCutoffs, StatsService.CurveFitType fit)
{
return new SinglePlateDilutionNabAssayRun(provider, run, plates, user, sortedCutoffs, fit);
}

public Lsid getDataRowLSID(ExpData data, String wellGroupName, Map<PropertyDescriptor, Object> sampleProperties)
{
String virusName = "";
for (Map.Entry<PropertyDescriptor, Object> entry : sampleProperties.entrySet())
{
if (entry.getKey().getName().equals(NabAssayProvider.VIRUS_NAME_PROPERTY_NAME))
virusName = entry.getValue().toString();
}
Lsid.LsidBuilder dataRowLsid = new Lsid.LsidBuilder(data.getLSID());
dataRowLsid.setNamespacePrefix(NAB_DATA_ROW_LSID_PREFIX);
dataRowLsid.setObjectId(dataRowLsid.getObjectId() + "-" + virusName + "-" + wellGroupName);
return dataRowLsid.build();
}

@Override
public Map<DilutionSummary, DilutionAssayRun> getDilutionSummaries(User user, StatsService.CurveFitType fit, int... dataObjectIds) throws ExperimentException
{
Expand Down

0 comments on commit 34843a9

Please sign in to comment.