All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.automation.platelayer.LayerWell Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.automation.platelayer;


import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

import com.hfg.automation.AutomationException;
import com.hfg.automation.Well;
import com.hfg.automation.WellRef;
import com.hfg.automation.WellType;
import com.hfg.exception.ProgrammingException;
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;

//------------------------------------------------------------------------------
/**
 Generic well in a plate that supports 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 LayerWell implements Well, Comparable { private WellRef mLocation; private WellType mType; private LayerPlate mPlate; private List mLayers; //########################################################################### // CONSTRUCTORS //########################################################################### //--------------------------------------------------------------------------- public LayerWell(WellRef inLocation) { mLocation = inLocation; } //--------------------------------------------------------------------------- public LayerWell(WellRef inLocation, WellType inType) { mLocation = inLocation; mType = inType; } //########################################################################### // PUBLIC METHODS //########################################################################### //--------------------------------------------------------------------------- @Override public int compareTo(LayerWell inLayerWell) { int result = 1; if (inLayerWell instanceof LayerWell) { LayerWell otherLayerWell = (LayerWell) inLayerWell; result = CompareUtil.compare(getRef().getColIndex(), otherLayerWell.getRef().getColIndex()); if (0 == result) { result = CompareUtil.compare(getRef().getRowIndex(), otherLayerWell.getRef().getRowIndex()); } } return result; } //--------------------------------------------------------------------------- @Override public String toString() { return getRef().toString(); } //--------------------------------------------------------------------------- public WellRef getRef() { return mLocation; } //--------------------------------------------------------------------------- public int getRow() { return mLocation.getRowIndex(); } //--------------------------------------------------------------------------- public int getCol() { return mLocation.getColIndex(); } //--------------------------------------------------------------------------- public LayerWell setType(WellType inValue) { mType = inValue; return this; } //--------------------------------------------------------------------------- public WellType getType() { return mType; } //--------------------------------------------------------------------------- public LayerWell setPlate(LayerPlate inValue) { mPlate = inValue; updatePlateLayers(); return this; } //--------------------------------------------------------------------------- public LayerPlate getPlate() { return mPlate; } //--------------------------------------------------------------------------- public LayerWell setSelected() { return setSelected(true); } //--------------------------------------------------------------------------- public LayerWell setSelected(boolean inValue) { WellSelectionLayer selectionLayer = getSelectionLayer(); if (null == selectionLayer) { selectionLayer = new WellSelectionLayer(inValue); addLayer(selectionLayer); } else { selectionLayer.setSelected(inValue); updatePlateLayers(selectionLayer); } return this; } //--------------------------------------------------------------------------- public boolean isSelected() { WellSelectionLayer selectionLayer = getSelectionLayer(); return (selectionLayer != null && selectionLayer.isSelected()); } //--------------------------------------------------------------------------- public LayerWell addSample(S inValue) { return addSample(inValue, null); } //--------------------------------------------------------------------------- /** Adds a sample to the well. * @param inValue an object designated as the sample. Cannot be null. * @param inVolume an optional volume to associate with the sample * @param the sample class * @return this Well object to allow for method chaining */ public LayerWell addSample(S inValue, Quantity inVolume) { if (inValue != null) { WellSampleLayer sampleLayer = getSampleLayer((Class) inValue.getClass()); if (null == sampleLayer) { sampleLayer = new WellSampleLayer<>((Class) inValue.getClass()); addLayer(sampleLayer); } sampleLayer.addSample(inValue, inVolume); updatePlateLayers(sampleLayer); } return this; } //--------------------------------------------------------------------------- public S[] getSamples(Class inClass) { List samples = null; List sampleLayers = getSampleLayers(); if(CollectionUtil.hasValues(sampleLayers)) { for (WellSampleLayer layer : getSampleLayers()) { if (layer.getSampleClass().equals(inClass) || inClass.isAssignableFrom(layer.getSampleClass())) { if (null == samples) { samples = new ArrayList<>(4); } for (Object sample : layer.getSamples()) { samples.add((S) sample); } } } } return samples != null ? samples.toArray((S[]) Array.newInstance(inClass, 0)) : null; } //--------------------------------------------------------------------------- public Quantity getSampleVolume() { Quantity sampleVolume = null; List sampleLayers = getSampleLayers(); if (CollectionUtil.hasValues(sampleLayers)) { for (WellSampleLayer layer : getSampleLayers()) { if (layer.getVolume() != null) { if (null == sampleVolume) { sampleVolume = layer.getVolume(); } else { sampleVolume = sampleVolume.add(layer.getVolume()); } } } } return sampleVolume; } //--------------------------------------------------------------------------- public Comparable[] getSamples() { Set samples = null; if (CollectionUtil.hasValues(getLayers())) { for (WellLayer layer : getLayers()) { if (layer instanceof SampleLayer) { if (null == samples) { samples = new OrderedSet<>(4); } samples.addAll(Arrays.asList(((WellSampleLayer) layer).getSamples())); } } } return samples != null ? samples.toArray(new Comparable[] {}) : null; } //--------------------------------------------------------------------------- // Special method for the plate to use when creating wells. Otherwise if the // standard addLayer() method is called, it will update the plate's layers // and this can cause unnecessary processing. protected LayerWell addLayerFromPlateLayer(PlateLayer inPlateLayer) { if (null == mLayers) { mLayers = new ArrayList<>(4); } WellLayer wellLayer = inPlateLayer.getWellLayer(getRef()); wellLayer.setName(inPlateLayer.name()); mLayers.add(wellLayer); return this; } //--------------------------------------------------------------------------- public LayerWell addLayer(WellLayer inValue) { if (null == mLayers) { mLayers = new ArrayList<>(4); } // TODO: Check if a layer of this type already exists mLayers.add(inValue); updatePlateLayers(inValue); return this; } //--------------------------------------------------------------------------- public List getLayers() { return mLayers; } //--------------------------------------------------------------------------- public WellLayer getLayer(String inName) { if (!StringUtil.isSet(inName)) { throw new AutomationException("No layer name was specified!"); } WellLayer layer = null; if (CollectionUtil.hasValues(mLayers)) { for (WellLayer wellLayer : mLayers) { if (0 == CompareUtil.compare(wellLayer.name(), inName)) { layer = wellLayer; break; } } } return layer; } //--------------------------------------------------------------------------- public List getSampleLayers() { List sampleLayers = null; if (CollectionUtil.hasValues(getLayers())) { for (WellLayer wellLayer : getLayers()) { if (wellLayer instanceof WellSampleLayer) { if (null == sampleLayers) { sampleLayers = new ArrayList<>(3); } sampleLayers.add((WellSampleLayer)wellLayer); } } } return sampleLayers; } //--------------------------------------------------------------------------- public WellSelectionLayer getSelectionLayer() { WellSelectionLayer selectionLayer = null; if (CollectionUtil.hasValues(getLayers())) { for (WellLayer wellLayer : getLayers()) { if (wellLayer instanceof SelectionLayer) { selectionLayer = (WellSelectionLayer) wellLayer; break; } } } return selectionLayer; } //--------------------------------------------------------------------------- public WellSampleLayer getSampleLayer(Class inSampleClass) { WellSampleLayer requestedSampleLayer = null; if (CollectionUtil.hasValues(getLayers())) { for (WellLayer wellLayer : getLayers()) { if (wellLayer instanceof SampleLayer) { WellSampleLayer sampleLayer = (WellSampleLayer) wellLayer; if (sampleLayer.getSampleClass().equals(inSampleClass)) { requestedSampleLayer = sampleLayer; break; } } } } return requestedSampleLayer; } //--------------------------------------------------------------------------- public WellVolumeAdjustmentLayer getVolumeAdjustmentLayer() { WellVolumeAdjustmentLayer requestedVolumeAdjustmentLayer = null; if (CollectionUtil.hasValues(getLayers())) { for (WellLayer wellLayer : getLayers()) { if (wellLayer instanceof WellVolumeAdjustmentLayer) { requestedVolumeAdjustmentLayer = (WellVolumeAdjustmentLayer) wellLayer; break; } } } return requestedVolumeAdjustmentLayer; } //--------------------------------------------------------------------------- public LayerWell addVolume(Quantity inValue) { if (inValue != null) { WellVolumeAdjustmentLayer volumeAdjustmentLayer = getVolumeAdjustmentLayer(); if (null == volumeAdjustmentLayer) { volumeAdjustmentLayer = new WellVolumeAdjustmentLayer(inValue); addLayer(volumeAdjustmentLayer); } else { volumeAdjustmentLayer.addVolume(inValue); } } return this; } //--------------------------------------------------------------------------- public LayerWell subtractVolume(Quantity inValue) { if (inValue != null) { Quantity negativeQuantity = new Quantity(- inValue.floatValue(), inValue.getUnit()); addVolume(negativeQuantity); } return this; } //--------------------------------------------------------------------------- public Quantity getVolume() { Quantity volume = null; if (CollectionUtil.hasValues(getLayers())) { for (WellLayer layer : getLayers()) { if (layer instanceof WellVolumeLayer) { Quantity layerVolume = ((WellVolumeLayer)layer).getVolume(); if (layerVolume != null) { if (null == volume) { volume = layerVolume; } else { volume = volume.add(layerVolume); } } } } } return volume; } //--------------------------------------------------------------------------- private void updatePlateLayers() { if (CollectionUtil.hasValues(getLayers())) { for (WellLayer wellLayer : getLayers()) { updatePlateLayers(wellLayer); } } } //--------------------------------------------------------------------------- private void updatePlateLayers(WellLayer inLayer) { if (getPlate() != null) { if (inLayer instanceof SelectionLayer) { PlateSelectionLayer selectionLayer = getPlate().getSelectionLayer(); if (null == selectionLayer) { selectionLayer = new PlateSelectionLayer(); getPlate().addLayer(selectionLayer); } selectionLayer.setSelected(getRef(), ((WellSelectionLayer) inLayer).isSelected()); } else if (inLayer instanceof ReagentLayer) { WellReagentLayer wellReagentLayer = (WellReagentLayer) inLayer; PlateReagentLayer plateReagentLayer = getPlate().getReagentLayer(wellReagentLayer.getReagent()); if (null == plateReagentLayer) { plateReagentLayer = new PlateReagentLayer<>(wellReagentLayer.getReagent()); getPlate().addLayer(plateReagentLayer); } plateReagentLayer.getVolumeMask().addVolume(getRef(), ((WellReagentLayer) inLayer).getVolume()); } else if (inLayer instanceof SampleLayer) { WellSampleLayer wellSampleLayer = (WellSampleLayer) inLayer; PlateSampleLayer plateSampleLayer = getPlate().getSampleLayer(wellSampleLayer.getSampleClass()); if (null == plateSampleLayer) { plateSampleLayer = new PlateSampleLayer(wellSampleLayer.getSampleClass()); getPlate().addLayer(plateSampleLayer); } Comparable[] samples = wellSampleLayer.getSamples(); if (samples != null) { for (Comparable sample : samples) { plateSampleLayer.addSample(getRef(), sample, wellSampleLayer.getVolume(sample)); } } } else if (inLayer instanceof WellDataLayer) { WellDataLayer wellDataLayer = (WellDataLayer) inLayer; PlateDataLayer plateDataLayer = getPlate().getDataLayer(wellDataLayer.name()); if (null == plateDataLayer) { plateDataLayer = new PlateDataLayer(wellDataLayer.getDataClass()); plateDataLayer.setName(wellDataLayer.name()); getPlate().addLayer(plateDataLayer); } Object data = wellDataLayer.getData(); if (data != null) { plateDataLayer.addData(getRef(), data); } } else if (inLayer instanceof WellVolumeAdjustmentLayer) { WellVolumeAdjustmentLayer wellVolumeAdjustmentLayer = (WellVolumeAdjustmentLayer) inLayer; PlateVolumeAdjustmentLayer plateVolumeAdjustmentLayer = getPlate().getVolumeAdjustmentLayer(); if (null == plateVolumeAdjustmentLayer) { plateVolumeAdjustmentLayer = new PlateVolumeAdjustmentLayer(); getPlate().addLayer(plateVolumeAdjustmentLayer); } plateVolumeAdjustmentLayer.setVolume(getRef(), wellVolumeAdjustmentLayer.getVolume()); } else { throw new ProgrammingException("No support for " + inLayer.getClass().getSimpleName() + " yet!"); } // TODO: Handle other plate layer types } } //########################################################################### // PROTECTED METHODS //########################################################################### //--------------------------------------------------------------------------- public void setRef(WellRef inValue) { mLocation = inValue; } }