com.hfg.automation.platelayer.LayerPlate Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.automation.platelayer;
import java.util.*;
import com.hfg.automation.*;
import com.hfg.units.Quantity;
import com.hfg.util.CompareUtil;
import com.hfg.util.StringUtil;
import com.hfg.util.collection.CollectionUtil;
import com.hfg.util.collection.OrderedSet;
import com.hfg.xml.XMLNode;
import com.hfg.xml.XMLTag;
//------------------------------------------------------------------------------
/**
Plate object with contents stored as layers.
@author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
public class LayerPlate extends PlateImpl
{
private List mLayers;
//###########################################################################
// CONSTRUCTORS
//###########################################################################
//---------------------------------------------------------------------------
public LayerPlate(PlateType inType)
{
this(inType, null);
}
//---------------------------------------------------------------------------
public LayerPlate(PlateType inType, String inName)
{
super(inType, inName);
init();
}
//---------------------------------------------------------------------------
protected LayerPlate()
{
init();
}
//--------------------------------------------------------------------------
public LayerPlate(XMLNode inXML)
{
super(inXML);
init();
XMLNode layersTag = inXML.getOptionalSubtagByName(AutomationXML.PLATE_LAYERS);
if (layersTag != null)
{
for (XMLNode layerTag : layersTag.getSubtagsByName(AutomationXML.PLATE_LAYER))
{
addLayer(PlateLayer.instantiate(layerTag));
}
}
}
//--------------------------------------------------------------------------
private void init()
{
setWellFactory((WellFactory) new LayerWellFactory());
}
//###########################################################################
// PUBLIC METHODS
//###########################################################################
//--------------------------------------------------------------------------
public XMLNode toXMLNode()
{
XMLNode node = super.toXMLNode();
if (CollectionUtil.hasValues(getLayers()))
{
XMLTag layersTag = new XMLTag(AutomationXML.PLATE_LAYERS);
node.addSubtag(layersTag);
for (PlateLayer layer : getLayers())
{
layersTag.addSubtag(layer.toXMLNode());
}
}
return node;
}
//--------------------------------------------------------------------------
@Override
public LayerPlate clone()
{
LayerPlate theClone = (LayerPlate) super.clone();
if (CollectionUtil.hasValues(getLayers()))
{
theClone.mLayers = new ArrayList<>(getLayers().size());
for (PlateLayer layer : getLayers())
{
theClone.addLayer(layer.clone());
}
}
return theClone;
}
//---------------------------------------------------------------------------
@Override
public LayerPlate setLayout(PlateLayout inValue)
{
return (LayerPlate) super.setLayout(inValue);
}
//---------------------------------------------------------------------------
public boolean isFull()
{
int totalWells = getType().size();
int unoccupiedWells = totalWells - getOccupiedWellRefs().size();
// Are there wells that were designated to be kept empty?
if (CollectionUtil.hasValues(getLayout().getWellsToKeepEmpty()))
{
unoccupiedWells -= getLayout().getWellsToKeepEmpty().size();
}
return 0 == unoccupiedWells;
}
//---------------------------------------------------------------------------
public List getLayers()
{
return mLayers;
}
//---------------------------------------------------------------------------
public LayerPlate addLayer(PlateLayer inValue)
{
if (null == mLayers)
{
mLayers = new ArrayList<>(4);
}
mLayers.add(inValue);
inValue.setPlate(this);
return this;
}
//---------------------------------------------------------------------------
public void clear()
{
mLayers = null;
}
//---------------------------------------------------------------------------
public LayerPlate setWell(W inWell)
{
inWell.setPlate(this);
return this;
}
//---------------------------------------------------------------------------
public LayerPlate setWells(Collection inWells)
{
if (getLayers() != null)
{
getLayers().clear();
}
if (CollectionUtil.hasValues(inWells))
{
for (W well : inWells)
{
setWell(well);
}
}
return this;
}
//---------------------------------------------------------------------------
public W getWell(String inWellRef)
{
return getWell(new WellRef(inWellRef));
}
//---------------------------------------------------------------------------
public W getWell(WellRef inRef)
{
W well = allocateWell(inRef);
if (CollectionUtil.hasValues(getLayers()))
{
for (PlateLayer layer : getLayers())
{
WellLayer wellLayer = layer.getWellLayer(inRef);
if (wellLayer != null)
{
well.addLayer(wellLayer);
}
}
}
return well;
}
//---------------------------------------------------------------------------
public Collection getOccupiedWells()
{
List wells = null;
if (CollectionUtil.hasValues(mLayers))
{
Map wellMap = new HashMap<>();
for (PlateLayer layer : getLayers())
{
Collection wellRefs = layer.getOccupiedWellRefs();
if (CollectionUtil.hasValues(wellRefs))
{
for (WellRef wellRef : wellRefs)
{
W well = wellMap.get(wellRef);
if (null == well)
{
well = allocateWell(wellRef);
wellMap.put(wellRef, well);
}
well.addLayerFromPlateLayer(layer);
}
}
}
wells = new ArrayList<>(wellMap.values());
Collections.sort(wells, getLayout());
}
return wells;
}
//---------------------------------------------------------------------------
public Collection getOccupiedWellRefs()
{
OrderedSet wellRefs = null;
if (CollectionUtil.hasValues(mLayers))
{
wellRefs = new OrderedSet<>();
for (PlateLayer layer : getLayers())
{
wellRefs.addAll(layer.getOccupiedWellRefs());
}
Collections.sort(wellRefs);
}
return wellRefs;
}
//---------------------------------------------------------------------------
public WellRef addSample(Comparable inSample)
{
WellRef wellRef = getNextAvailableSampleWellRef(inSample.getClass(), null);
allocateWell(wellRef).addSample(inSample);
return wellRef;
}
//---------------------------------------------------------------------------
public WellRef addSample(Comparable inSample, Quantity inQuantity)
{
WellRef wellRef = getNextAvailableSampleWellRef(inSample.getClass(), null);
allocateWell(wellRef).addSample(inSample, inQuantity);
return wellRef;
}
//---------------------------------------------------------------------------
public WellRef getNextAvailableSampleWellRef(Class inSampleClass, String inSampleLayerName)
{
PlateSampleLayer sampleLayer;
if (StringUtil.isSet(inSampleLayerName))
{
sampleLayer = getSampleLayer(inSampleLayerName);
}
else
{
sampleLayer = getSampleLayer(inSampleClass);
}
if (null == sampleLayer)
{
sampleLayer = new PlateSampleLayer<>(inSampleClass);
sampleLayer.setName(inSampleLayerName);
addLayer(sampleLayer);
}
return sampleLayer.getNextAvailableWellRef();
}
//---------------------------------------------------------------------------
public WellRef getNextAvailableSampleWellRef(Class inSampleClass)
{
PlateSampleLayer sampleLayer = getSampleLayer(inSampleClass);
if (null == sampleLayer)
{
sampleLayer = new PlateSampleLayer<>(inSampleClass);
addLayer(sampleLayer);
}
return sampleLayer.getNextAvailableWellRef();
}
//---------------------------------------------------------------------------
/**
Flags all occupied wells as selected.
*/
public void selectAllWells()
{
PlateSelectionLayer selectionLayer = getSelectionLayer();
if (null == selectionLayer)
{
selectionLayer = new PlateSelectionLayer();
addLayer(selectionLayer);
}
Collection occupiedWells = getOccupiedWells();
if (CollectionUtil.hasValues(occupiedWells))
{
for (Well well : occupiedWells)
{
selectionLayer.setSelected(well.getRef(), true);
}
}
}
//---------------------------------------------------------------------------
/**
Returns wells which have been flagged as selected.
@return wells which have been flagged as selected.
*/
public Collection getSelectedWells()
{
List selectedWells = null;
PlateSelectionLayer selectionLayer = getSelectionLayer();
if (selectionLayer != null)
{
Collection wellRefs = selectionLayer.getOccupiedWellRefs();
if (CollectionUtil.hasValues(wellRefs))
{
selectedWells = new ArrayList<>();
for (WellRef wellRef : wellRefs)
{
if (selectionLayer.isSelected(wellRef))
{
selectedWells.add(getWell(wellRef));
}
}
}
if (CollectionUtil.hasValues(selectedWells))
{
Collections.sort(selectedWells, getLayout());
}
}
return selectedWells;
}
//---------------------------------------------------------------------------
public PlateSelectionLayer getSelectionLayer()
{
PlateSelectionLayer selectionLayer = null;
if (CollectionUtil.hasValues(getLayers()))
{
for (PlateLayer plateLayer : getLayers())
{
if (plateLayer instanceof SelectionLayer)
{
selectionLayer = (PlateSelectionLayer) plateLayer;
break;
}
}
}
return selectionLayer;
}
//---------------------------------------------------------------------------
public PlateVolumeAdjustmentLayer getVolumeAdjustmentLayer()
{
PlateVolumeAdjustmentLayer requestedVolumeAdjustmentLayer = null;
if (CollectionUtil.hasValues(getLayers()))
{
for (PlateLayer plateLayer : getLayers())
{
if (plateLayer instanceof PlateVolumeAdjustmentLayer)
{
requestedVolumeAdjustmentLayer = (PlateVolumeAdjustmentLayer) plateLayer;
break;
}
}
}
return requestedVolumeAdjustmentLayer;
}
//---------------------------------------------------------------------------
public Collection getSampleLayers()
{
Collection sampleLayers = null;
if (CollectionUtil.hasValues(getLayers()))
{
for (PlateLayer plateLayer : getLayers())
{
if (plateLayer instanceof PlateSampleLayer)
{
if (null == sampleLayers)
{
sampleLayers = new ArrayList<>(3);
}
sampleLayers.add((PlateSampleLayer) plateLayer);
}
}
}
return sampleLayers;
}
//---------------------------------------------------------------------------
public PlateSampleLayer getSampleLayer(String inName)
{
PlateSampleLayer requestedSampleLayer = null;
if (CollectionUtil.hasValues(getLayers()))
{
for (PlateLayer plateLayer : getLayers())
{
if (plateLayer instanceof SampleLayer)
{
PlateSampleLayer sampleLayer = (PlateSampleLayer) plateLayer;
if (0 == CompareUtil.compare(sampleLayer.name(), inName))
{
requestedSampleLayer = sampleLayer;
break;
}
}
}
}
return requestedSampleLayer;
}
//---------------------------------------------------------------------------
public PlateSampleLayer getSampleLayer(Class inSampleClass)
{
PlateSampleLayer requestedSampleLayer = null;
if (CollectionUtil.hasValues(getLayers()))
{
for (PlateLayer plateLayer : getLayers())
{
if (plateLayer instanceof SampleLayer)
{
PlateSampleLayer sampleLayer = (PlateSampleLayer) plateLayer;
if (sampleLayer.getSampleClass().equals(inSampleClass))
{
requestedSampleLayer = sampleLayer;
break;
}
}
}
}
return requestedSampleLayer;
}
//---------------------------------------------------------------------------
public PlateReagentLayer getReagentLayer(R inReagent)
{
PlateReagentLayer requestedReagentLayer = null;
if (CollectionUtil.hasValues(getLayers()))
{
for (PlateLayer plateLayer : getLayers())
{
if (plateLayer instanceof ReagentLayer)
{
PlateReagentLayer reagentLayer = (PlateReagentLayer) plateLayer;
if (reagentLayer.getReagent().equals(inReagent))
{
requestedReagentLayer = reagentLayer;
break;
}
}
}
}
return requestedReagentLayer;
}
//---------------------------------------------------------------------------
public PlateDataLayer getDataLayer(String inName)
{
PlateDataLayer requestedDataLayer = null;
if (CollectionUtil.hasValues(getLayers()))
{
for (PlateLayer plateLayer : getLayers())
{
if (plateLayer instanceof PlateDataLayer)
{
PlateDataLayer dataLayer = (PlateDataLayer) plateLayer;
if (0 == CompareUtil.compare(dataLayer.name(), inName))
{
requestedDataLayer = dataLayer;
break;
}
}
}
}
return requestedDataLayer;
}
}