extensions/net.sf.basedb.relax/trunk/src/net/sf/basedb/relax/plugins/CohortChain.java

Code
Comments
Other
Rev Date Author Line
4571 14 Sep 17 nicklas 1 package net.sf.basedb.relax.plugins;
4571 14 Sep 17 nicklas 2
4571 14 Sep 17 nicklas 3 import java.util.ArrayList;
5219 11 Jan 19 nicklas 4 import java.util.LinkedList;
4571 14 Sep 17 nicklas 5 import java.util.List;
4571 14 Sep 17 nicklas 6
4571 14 Sep 17 nicklas 7 import net.sf.basedb.core.DbControl;
5229 14 Jan 19 nicklas 8 import net.sf.basedb.core.Item;
4571 14 Sep 17 nicklas 9 import net.sf.basedb.core.Nameable;
4571 14 Sep 17 nicklas 10
4571 14 Sep 17 nicklas 11 /**
4571 14 Sep 17 nicklas 12   Wrapper class that represents a chain of items in a cohort.
4571 14 Sep 17 nicklas 13   We create a chain by giving it a root item and then moving
4571 14 Sep 17 nicklas 14   up to find all parent and grand-parent items until the
5219 11 Jan 19 nicklas 15   top is reached and moving down to find all child items.
4571 14 Sep 17 nicklas 16   Parents are loaded as:
4571 14 Sep 17 nicklas 17   
4571 14 Sep 17 nicklas 18    * The parent to a RawBioAssay is the parent DerivedBioassay or 
4571 14 Sep 17 nicklas 19      the parent Extract.
4571 14 Sep 17 nicklas 20    * The parent to a non-root DerivedBioAssay is the first found parent DerivedBioAssay
4571 14 Sep 17 nicklas 21       or the parent Extract.
4571 14 Sep 17 nicklas 22    * The parent to a root DerivedBioassay is the parent Extract.
4571 14 Sep 17 nicklas 23    * The parent to a Sample or Extract is the single parent.
4571 14 Sep 17 nicklas 24    
4571 14 Sep 17 nicklas 25    Note! The assumption here is that an item have at most a single parent item.
5219 11 Jan 19 nicklas 26    
5219 11 Jan 19 nicklas 27    Children are loaded as:
5219 11 Jan 19 nicklas 28    
5219 11 Jan 19 nicklas 29     * Biosource: All child Samples
5219 11 Jan 19 nicklas 30     * Sample: All child Samples and all child Extracts
5219 11 Jan 19 nicklas 31     * Extract: All child Extracts, all child DerivedBioassays and all child RawBioassays
5219 11 Jan 19 nicklas 32    
5219 11 Jan 19 nicklas 33    Note! The assumption here is that all DerivedBioassay and RawBioassays are correctly
5219 11 Jan 19 nicklas 34    linked to the bottom-most extract (library) so that we don't have to follow links
5229 14 Jan 19 nicklas 35    between DerivedBioassays and RawBioassays except if the root item type is a 
5229 14 Jan 19 nicklas 36    derived bioassay.
4571 14 Sep 17 nicklas 37 */
4571 14 Sep 17 nicklas 38 public class CohortChain
4571 14 Sep 17 nicklas 39 {
4571 14 Sep 17 nicklas 40   
4571 14 Sep 17 nicklas 41   private final CohortItem rootItem;
5227 14 Jan 19 nicklas 42   private final CohortItem topItem;
4571 14 Sep 17 nicklas 43   private final List<CohortItem> parentItems;
5219 11 Jan 19 nicklas 44   private final List<CohortItem> childItems;
4571 14 Sep 17 nicklas 45   
4571 14 Sep 17 nicklas 46   public CohortChain(DbControl dc, CohortItem rootItem)
4571 14 Sep 17 nicklas 47   {
4571 14 Sep 17 nicklas 48     this.rootItem = rootItem;
4571 14 Sep 17 nicklas 49     this.parentItems = new ArrayList<>();
5219 11 Jan 19 nicklas 50     this.childItems = new ArrayList<>();
4571 14 Sep 17 nicklas 51     
4571 14 Sep 17 nicklas 52     Nameable item = rootItem.getItem();
4571 14 Sep 17 nicklas 53     do
4571 14 Sep 17 nicklas 54     {
4571 14 Sep 17 nicklas 55       CohortItem ci = new CohortItem(item);
4571 14 Sep 17 nicklas 56       parentItems.add(ci);
4571 14 Sep 17 nicklas 57       item = ci.getParentItem(dc);
4571 14 Sep 17 nicklas 58     } while (item != null);
5219 11 Jan 19 nicklas 59     
5227 14 Jan 19 nicklas 60     this.topItem = parentItems.get(parentItems.size() - 1);
5229 14 Jan 19 nicklas 61     // Derived and raw bioassays are typically loaded from extracts
5229 14 Jan 19 nicklas 62     // except if the root item type is itself a derived bioassay
5229 14 Jan 19 nicklas 63     boolean loadChildrenFromDerivedBioAssay = rootItem.getType() == Item.DERIVEDBIOASSAY;
5219 11 Jan 19 nicklas 64     List<Nameable> tmp = new LinkedList<>();
5219 11 Jan 19 nicklas 65     tmp.addAll(rootItem.getChildItems(dc));
5219 11 Jan 19 nicklas 66     while (tmp.size() > 0)
5219 11 Jan 19 nicklas 67     {
5219 11 Jan 19 nicklas 68       Nameable n = tmp.remove(0);
5219 11 Jan 19 nicklas 69       CohortItem ci = new CohortItem(n);
5219 11 Jan 19 nicklas 70       childItems.add(ci);
5229 14 Jan 19 nicklas 71       if (loadChildrenFromDerivedBioAssay || n.getType() != Item.DERIVEDBIOASSAY)
5229 14 Jan 19 nicklas 72       {
5229 14 Jan 19 nicklas 73         tmp.addAll(ci.getChildItems(dc));
5229 14 Jan 19 nicklas 74       }
5227 14 Jan 19 nicklas 75     }
4571 14 Sep 17 nicklas 76   }
4571 14 Sep 17 nicklas 77   
5229 14 Jan 19 nicklas 78   /**
5229 14 Jan 19 nicklas 79     Get the root item for this chain.
5229 14 Jan 19 nicklas 80   */
4571 14 Sep 17 nicklas 81   public CohortItem getRootItem()
4571 14 Sep 17 nicklas 82   {
4571 14 Sep 17 nicklas 83     return rootItem;
4571 14 Sep 17 nicklas 84   }
4571 14 Sep 17 nicklas 85   
5229 14 Jan 19 nicklas 86   /**
5229 14 Jan 19 nicklas 87     Get the top-most item in this chain. It is obtained by 
5229 14 Jan 19 nicklas 88     following the {@link CohortItem#getParentItem(DbControl)}
5229 14 Jan 19 nicklas 89     until the last parent is found.
5229 14 Jan 19 nicklas 90   */
5227 14 Jan 19 nicklas 91   public CohortItem getTopItem()
5227 14 Jan 19 nicklas 92   {
5227 14 Jan 19 nicklas 93     return topItem;
5227 14 Jan 19 nicklas 94   }
5227 14 Jan 19 nicklas 95   
5229 14 Jan 19 nicklas 96   /**
5229 14 Jan 19 nicklas 97     Get all parent items in this chain, including the root item.
5229 14 Jan 19 nicklas 98   */
4571 14 Sep 17 nicklas 99   public List<CohortItem> getParentItems()
4571 14 Sep 17 nicklas 100   {
4571 14 Sep 17 nicklas 101     return parentItems;
4571 14 Sep 17 nicklas 102   }
4571 14 Sep 17 nicklas 103
5229 14 Jan 19 nicklas 104   /**
5229 14 Jan 19 nicklas 105     Get all child items in this chain.
5229 14 Jan 19 nicklas 106   */
5219 11 Jan 19 nicklas 107   public List<CohortItem> getChildItems()
5219 11 Jan 19 nicklas 108   {
5219 11 Jan 19 nicklas 109     return childItems;
5219 11 Jan 19 nicklas 110   }
5244 18 Jan 19 nicklas 111
4571 14 Sep 17 nicklas 112 }