4571 |
14 Sep 17 |
nicklas |
1 |
package net.sf.basedb.relax.plugins; |
4571 |
14 Sep 17 |
nicklas |
2 |
|
5219 |
11 Jan 19 |
nicklas |
3 |
import java.util.ArrayList; |
4571 |
14 Sep 17 |
nicklas |
4 |
import java.util.List; |
4571 |
14 Sep 17 |
nicklas |
5 |
|
5219 |
11 Jan 19 |
nicklas |
6 |
import net.sf.basedb.core.BioSource; |
4571 |
14 Sep 17 |
nicklas |
7 |
import net.sf.basedb.core.DbControl; |
4571 |
14 Sep 17 |
nicklas |
8 |
import net.sf.basedb.core.DerivedBioAssay; |
5219 |
11 Jan 19 |
nicklas |
9 |
import net.sf.basedb.core.Extract; |
4571 |
14 Sep 17 |
nicklas |
10 |
import net.sf.basedb.core.Item; |
4571 |
14 Sep 17 |
nicklas |
11 |
import net.sf.basedb.core.ItemQuery; |
4571 |
14 Sep 17 |
nicklas |
12 |
import net.sf.basedb.core.ItemSubtype; |
4571 |
14 Sep 17 |
nicklas |
13 |
import net.sf.basedb.core.MeasuredBioMaterial; |
4571 |
14 Sep 17 |
nicklas |
14 |
import net.sf.basedb.core.Nameable; |
4571 |
14 Sep 17 |
nicklas |
15 |
import net.sf.basedb.core.RawBioAssay; |
5219 |
11 Jan 19 |
nicklas |
16 |
import net.sf.basedb.core.Sample; |
4571 |
14 Sep 17 |
nicklas |
17 |
import net.sf.basedb.core.Subtypable; |
4571 |
14 Sep 17 |
nicklas |
18 |
import net.sf.basedb.core.query.Hql; |
4571 |
14 Sep 17 |
nicklas |
19 |
import net.sf.basedb.core.query.Orders; |
5219 |
11 Jan 19 |
nicklas |
20 |
import net.sf.basedb.core.query.Restrictions; |
4571 |
14 Sep 17 |
nicklas |
21 |
import net.sf.basedb.relax.Relax; |
4571 |
14 Sep 17 |
nicklas |
22 |
|
4571 |
14 Sep 17 |
nicklas |
23 |
/** |
4571 |
14 Sep 17 |
nicklas |
A wrapper class that represents a single item in a cohort chain. |
4571 |
14 Sep 17 |
nicklas |
We use this wrapper to make it easier get name and subtype |
4571 |
14 Sep 17 |
nicklas |
of items. All items must be Nameable so the name is easy to get. |
4571 |
14 Sep 17 |
nicklas |
If the item is a Subtypable item, we get the subtype from the |
4769 |
18 Apr 18 |
nicklas |
name of the ItemSubtype. For RawBioAssay:s the subtype is always |
4769 |
18 Apr 18 |
nicklas |
"RawBioAssay" (since it is possible to mix different raw data types). |
4571 |
14 Sep 17 |
nicklas |
30 |
*/ |
4571 |
14 Sep 17 |
nicklas |
31 |
public class CohortItem |
4571 |
14 Sep 17 |
nicklas |
32 |
{ |
4571 |
14 Sep 17 |
nicklas |
33 |
|
4571 |
14 Sep 17 |
nicklas |
34 |
private final Nameable item; |
4571 |
14 Sep 17 |
nicklas |
35 |
private final String name; |
4571 |
14 Sep 17 |
nicklas |
36 |
private final String subtype; |
4571 |
14 Sep 17 |
nicklas |
37 |
private Nameable parentItem; |
5219 |
11 Jan 19 |
nicklas |
38 |
private List<Nameable> childItems; |
4571 |
14 Sep 17 |
nicklas |
39 |
|
4571 |
14 Sep 17 |
nicklas |
40 |
public CohortItem(Nameable item) |
4571 |
14 Sep 17 |
nicklas |
41 |
{ |
4571 |
14 Sep 17 |
nicklas |
42 |
this.item = item; |
4571 |
14 Sep 17 |
nicklas |
43 |
this.name = ((Nameable)item).getName(); |
4571 |
14 Sep 17 |
nicklas |
44 |
if (item instanceof Subtypable) |
4571 |
14 Sep 17 |
nicklas |
45 |
{ |
4571 |
14 Sep 17 |
nicklas |
46 |
ItemSubtype st = ((Subtypable)item).getItemSubtype(); |
4571 |
14 Sep 17 |
nicklas |
47 |
subtype = st != null ? st.getName() : null; |
4571 |
14 Sep 17 |
nicklas |
48 |
} |
4571 |
14 Sep 17 |
nicklas |
49 |
else if (item instanceof RawBioAssay) |
4571 |
14 Sep 17 |
nicklas |
50 |
{ |
5221 |
11 Jan 19 |
nicklas |
51 |
subtype = ((RawBioAssay)item).getRawDataType().getName(); |
4571 |
14 Sep 17 |
nicklas |
52 |
} |
4571 |
14 Sep 17 |
nicklas |
53 |
else |
4571 |
14 Sep 17 |
nicklas |
54 |
{ |
4571 |
14 Sep 17 |
nicklas |
55 |
subtype = null; |
4571 |
14 Sep 17 |
nicklas |
56 |
} |
4571 |
14 Sep 17 |
nicklas |
57 |
} |
4571 |
14 Sep 17 |
nicklas |
58 |
|
4571 |
14 Sep 17 |
nicklas |
59 |
public Nameable getItem() |
4571 |
14 Sep 17 |
nicklas |
60 |
{ |
4571 |
14 Sep 17 |
nicklas |
61 |
return item; |
4571 |
14 Sep 17 |
nicklas |
62 |
} |
4571 |
14 Sep 17 |
nicklas |
63 |
|
4571 |
14 Sep 17 |
nicklas |
64 |
public Nameable getParentItem(DbControl dc) |
4571 |
14 Sep 17 |
nicklas |
65 |
{ |
4571 |
14 Sep 17 |
nicklas |
66 |
if (parentItem == null) |
4571 |
14 Sep 17 |
nicklas |
67 |
{ |
4571 |
14 Sep 17 |
nicklas |
68 |
if (item instanceof RawBioAssay) |
4571 |
14 Sep 17 |
nicklas |
69 |
{ |
4571 |
14 Sep 17 |
nicklas |
70 |
RawBioAssay raw = (RawBioAssay)item; |
4571 |
14 Sep 17 |
nicklas |
71 |
parentItem = raw.getParentBioAssay(); |
4571 |
14 Sep 17 |
nicklas |
72 |
if (parentItem == null) parentItem = raw.getParentExtract(); |
4571 |
14 Sep 17 |
nicklas |
73 |
} |
4571 |
14 Sep 17 |
nicklas |
74 |
else if (item instanceof DerivedBioAssay) |
4571 |
14 Sep 17 |
nicklas |
75 |
{ |
4571 |
14 Sep 17 |
nicklas |
76 |
DerivedBioAssay dba = (DerivedBioAssay)item; |
4571 |
14 Sep 17 |
nicklas |
77 |
if (!dba.isRoot()) |
4571 |
14 Sep 17 |
nicklas |
78 |
{ |
4571 |
14 Sep 17 |
nicklas |
79 |
ItemQuery<DerivedBioAssay> query = dba.getParents(); |
4571 |
14 Sep 17 |
nicklas |
80 |
query.setIncludes(Relax.INCLUDE_IN_CURRENT_PROJECT); |
4571 |
14 Sep 17 |
nicklas |
81 |
query.order(Orders.asc(Hql.property("name"))); |
4571 |
14 Sep 17 |
nicklas |
82 |
List<DerivedBioAssay> result = query.list(dc); |
4571 |
14 Sep 17 |
nicklas |
83 |
if (result.size() > 0) parentItem = result.get(0); |
4571 |
14 Sep 17 |
nicklas |
84 |
} |
4571 |
14 Sep 17 |
nicklas |
85 |
if (parentItem == null) parentItem = dba.getExtract(); |
4571 |
14 Sep 17 |
nicklas |
86 |
} |
4571 |
14 Sep 17 |
nicklas |
87 |
else if (item instanceof MeasuredBioMaterial) |
4571 |
14 Sep 17 |
nicklas |
88 |
{ |
4571 |
14 Sep 17 |
nicklas |
89 |
MeasuredBioMaterial bm = (MeasuredBioMaterial)item; |
4571 |
14 Sep 17 |
nicklas |
90 |
parentItem = bm.getParent(); |
4571 |
14 Sep 17 |
nicklas |
91 |
} |
4571 |
14 Sep 17 |
nicklas |
92 |
} |
4571 |
14 Sep 17 |
nicklas |
93 |
return parentItem; |
4571 |
14 Sep 17 |
nicklas |
94 |
} |
4571 |
14 Sep 17 |
nicklas |
95 |
|
5219 |
11 Jan 19 |
nicklas |
96 |
public List<Nameable> getChildItems(DbControl dc) |
5219 |
11 Jan 19 |
nicklas |
97 |
{ |
5219 |
11 Jan 19 |
nicklas |
98 |
if (childItems == null) |
5219 |
11 Jan 19 |
nicklas |
99 |
{ |
5219 |
11 Jan 19 |
nicklas |
100 |
childItems = new ArrayList<>(); |
5219 |
11 Jan 19 |
nicklas |
101 |
if (item instanceof BioSource) |
5219 |
11 Jan 19 |
nicklas |
102 |
{ |
5219 |
11 Jan 19 |
nicklas |
103 |
BioSource bs = (BioSource)item; |
5219 |
11 Jan 19 |
nicklas |
104 |
ItemQuery<Sample> query = bs.getSamples(); |
5219 |
11 Jan 19 |
nicklas |
105 |
query.setIncludes(Relax.INCLUDE_IN_CURRENT_PROJECT); |
5219 |
11 Jan 19 |
nicklas |
106 |
query.order(Orders.asc(Hql.property("name"))); |
5219 |
11 Jan 19 |
nicklas |
107 |
childItems.addAll(query.list(dc)); |
5219 |
11 Jan 19 |
nicklas |
108 |
} |
5219 |
11 Jan 19 |
nicklas |
109 |
else if (item instanceof Sample) |
5219 |
11 Jan 19 |
nicklas |
110 |
{ |
5219 |
11 Jan 19 |
nicklas |
111 |
Sample s = (Sample)item; |
5219 |
11 Jan 19 |
nicklas |
112 |
ItemQuery<? extends Nameable> query = s.getChildSamples(); |
5219 |
11 Jan 19 |
nicklas |
113 |
query.setIncludes(Relax.INCLUDE_IN_CURRENT_PROJECT); |
5219 |
11 Jan 19 |
nicklas |
114 |
query.order(Orders.asc(Hql.property("name"))); |
5219 |
11 Jan 19 |
nicklas |
115 |
childItems.addAll(query.list(dc)); |
5219 |
11 Jan 19 |
nicklas |
116 |
|
5219 |
11 Jan 19 |
nicklas |
117 |
query = s.getExtracts(); |
5219 |
11 Jan 19 |
nicklas |
118 |
query.setIncludes(Relax.INCLUDE_IN_CURRENT_PROJECT); |
5219 |
11 Jan 19 |
nicklas |
119 |
query.order(Orders.asc(Hql.property("name"))); |
5219 |
11 Jan 19 |
nicklas |
120 |
childItems.addAll(query.list(dc)); |
5219 |
11 Jan 19 |
nicklas |
121 |
} |
5219 |
11 Jan 19 |
nicklas |
122 |
else if (item instanceof Extract) |
5219 |
11 Jan 19 |
nicklas |
123 |
{ |
5219 |
11 Jan 19 |
nicklas |
124 |
Extract e = (Extract)item; |
5219 |
11 Jan 19 |
nicklas |
125 |
ItemQuery<? extends Nameable> query = e.getChildExtracts(); |
5219 |
11 Jan 19 |
nicklas |
126 |
query.setIncludes(Relax.INCLUDE_IN_CURRENT_PROJECT); |
5219 |
11 Jan 19 |
nicklas |
127 |
query.order(Orders.asc(Hql.property("name"))); |
5219 |
11 Jan 19 |
nicklas |
128 |
childItems.addAll(query.list(dc)); |
5219 |
11 Jan 19 |
nicklas |
129 |
|
5219 |
11 Jan 19 |
nicklas |
130 |
query = DerivedBioAssay.getQuery(); |
5219 |
11 Jan 19 |
nicklas |
131 |
query.setIncludes(Relax.INCLUDE_IN_CURRENT_PROJECT); |
5219 |
11 Jan 19 |
nicklas |
132 |
query.order(Orders.asc(Hql.property("name"))); |
5219 |
11 Jan 19 |
nicklas |
133 |
query.join(Hql.innerJoin("extract", "e")); |
5219 |
11 Jan 19 |
nicklas |
134 |
query.restrict(Restrictions.eq(Hql.alias("e"), Hql.entity(e))); |
5219 |
11 Jan 19 |
nicklas |
135 |
childItems.addAll(query.list(dc)); |
5219 |
11 Jan 19 |
nicklas |
136 |
|
5219 |
11 Jan 19 |
nicklas |
137 |
query = RawBioAssay.getQuery(); |
5219 |
11 Jan 19 |
nicklas |
138 |
query.setIncludes(Relax.INCLUDE_IN_CURRENT_PROJECT); |
5219 |
11 Jan 19 |
nicklas |
139 |
query.order(Orders.asc(Hql.property("name"))); |
5219 |
11 Jan 19 |
nicklas |
140 |
query.join(Hql.innerJoin("parentExtract", "e")); |
5219 |
11 Jan 19 |
nicklas |
141 |
query.restrict(Restrictions.eq(Hql.alias("e"), Hql.entity(e))); |
5219 |
11 Jan 19 |
nicklas |
142 |
childItems.addAll(query.list(dc)); |
5219 |
11 Jan 19 |
nicklas |
143 |
} |
5229 |
14 Jan 19 |
nicklas |
144 |
else if (item instanceof DerivedBioAssay) |
5229 |
14 Jan 19 |
nicklas |
145 |
{ |
5229 |
14 Jan 19 |
nicklas |
146 |
DerivedBioAssay dba = (DerivedBioAssay)item; |
5229 |
14 Jan 19 |
nicklas |
147 |
ItemQuery<? extends Nameable> query = dba.getChildren(); |
5229 |
14 Jan 19 |
nicklas |
148 |
query.setIncludes(Relax.INCLUDE_IN_CURRENT_PROJECT); |
5229 |
14 Jan 19 |
nicklas |
149 |
query.order(Orders.asc(Hql.property("name"))); |
5229 |
14 Jan 19 |
nicklas |
150 |
childItems.addAll(query.list(dc)); |
5229 |
14 Jan 19 |
nicklas |
151 |
|
5229 |
14 Jan 19 |
nicklas |
152 |
query = dba.getRawBioAssays(); |
5229 |
14 Jan 19 |
nicklas |
153 |
query.setIncludes(Relax.INCLUDE_IN_CURRENT_PROJECT); |
5229 |
14 Jan 19 |
nicklas |
154 |
query.order(Orders.asc(Hql.property("name"))); |
5229 |
14 Jan 19 |
nicklas |
155 |
childItems.addAll(query.list(dc)); |
5229 |
14 Jan 19 |
nicklas |
156 |
} |
5219 |
11 Jan 19 |
nicklas |
157 |
} |
5219 |
11 Jan 19 |
nicklas |
158 |
return childItems; |
5219 |
11 Jan 19 |
nicklas |
159 |
} |
5219 |
11 Jan 19 |
nicklas |
160 |
|
4571 |
14 Sep 17 |
nicklas |
161 |
public String getName() |
4571 |
14 Sep 17 |
nicklas |
162 |
{ |
4571 |
14 Sep 17 |
nicklas |
163 |
return name; |
4571 |
14 Sep 17 |
nicklas |
164 |
} |
4571 |
14 Sep 17 |
nicklas |
165 |
|
4571 |
14 Sep 17 |
nicklas |
166 |
public Item getType() |
4571 |
14 Sep 17 |
nicklas |
167 |
{ |
4571 |
14 Sep 17 |
nicklas |
168 |
return item.getType(); |
4571 |
14 Sep 17 |
nicklas |
169 |
} |
4571 |
14 Sep 17 |
nicklas |
170 |
|
4571 |
14 Sep 17 |
nicklas |
171 |
public String getSubtype() |
4571 |
14 Sep 17 |
nicklas |
172 |
{ |
4571 |
14 Sep 17 |
nicklas |
173 |
return subtype; |
4571 |
14 Sep 17 |
nicklas |
174 |
} |
4571 |
14 Sep 17 |
nicklas |
175 |
|
4571 |
14 Sep 17 |
nicklas |
176 |
} |