2098 |
23 Oct 13 |
nicklas |
1 |
package net.sf.basedb.reggie.dao; |
2098 |
23 Oct 13 |
nicklas |
2 |
|
2098 |
23 Oct 13 |
nicklas |
3 |
import java.util.ArrayList; |
2098 |
23 Oct 13 |
nicklas |
4 |
import java.util.Collection; |
2098 |
23 Oct 13 |
nicklas |
5 |
import java.util.List; |
7009 |
25 Jan 23 |
nicklas |
6 |
import java.util.Map; |
2098 |
23 Oct 13 |
nicklas |
7 |
|
2098 |
23 Oct 13 |
nicklas |
8 |
import org.json.simple.JSONObject; |
2098 |
23 Oct 13 |
nicklas |
9 |
|
6870 |
16 Nov 22 |
nicklas |
10 |
import net.sf.basedb.core.BioMaterial; |
2098 |
23 Oct 13 |
nicklas |
11 |
import net.sf.basedb.core.DbControl; |
2098 |
23 Oct 13 |
nicklas |
12 |
import net.sf.basedb.core.Extract; |
5359 |
12 Apr 19 |
nicklas |
13 |
import net.sf.basedb.core.Include; |
6870 |
16 Nov 22 |
nicklas |
14 |
import net.sf.basedb.core.Item; |
2098 |
23 Oct 13 |
nicklas |
15 |
import net.sf.basedb.core.ItemQuery; |
6870 |
16 Nov 22 |
nicklas |
16 |
import net.sf.basedb.core.Sample; |
2098 |
23 Oct 13 |
nicklas |
17 |
import net.sf.basedb.core.Type; |
2098 |
23 Oct 13 |
nicklas |
18 |
import net.sf.basedb.core.query.Expressions; |
2098 |
23 Oct 13 |
nicklas |
19 |
import net.sf.basedb.core.query.Hql; |
2917 |
11 Nov 14 |
nicklas |
20 |
import net.sf.basedb.core.query.Orders; |
2098 |
23 Oct 13 |
nicklas |
21 |
import net.sf.basedb.core.query.Restrictions; |
2098 |
23 Oct 13 |
nicklas |
22 |
import net.sf.basedb.reggie.JsonUtil; |
2917 |
11 Nov 14 |
nicklas |
23 |
import net.sf.basedb.reggie.Reggie; |
2098 |
23 Oct 13 |
nicklas |
24 |
|
2098 |
23 Oct 13 |
nicklas |
25 |
/** |
2098 |
23 Oct 13 |
nicklas |
Class for loading information that is related to DNA extracts. |
2098 |
23 Oct 13 |
nicklas |
27 |
|
2098 |
23 Oct 13 |
nicklas |
@author nicklas |
2098 |
23 Oct 13 |
nicklas |
@since 2.13 |
2098 |
23 Oct 13 |
nicklas |
30 |
*/ |
2098 |
23 Oct 13 |
nicklas |
31 |
public class Dna |
2098 |
23 Oct 13 |
nicklas |
32 |
extends ReggieItem<Extract> |
2098 |
23 Oct 13 |
nicklas |
33 |
{ |
2098 |
23 Oct 13 |
nicklas |
34 |
|
5356 |
12 Apr 19 |
nicklas |
35 |
/** |
5356 |
12 Apr 19 |
nicklas |
Value for the AutoProcessing annotation to indicate that the |
5356 |
12 Apr 19 |
nicklas |
DNA item should be pre-normalized before put into freezer. |
5356 |
12 Apr 19 |
nicklas |
@since 4.23 |
5356 |
12 Apr 19 |
nicklas |
39 |
*/ |
5356 |
12 Apr 19 |
nicklas |
40 |
public static final String AUTO_PROCESSING_PRE_NORMALIZE = "PreNormalizeDNA"; |
2098 |
23 Oct 13 |
nicklas |
41 |
|
2098 |
23 Oct 13 |
nicklas |
42 |
/** |
2098 |
23 Oct 13 |
nicklas |
Find all DNA items by case name. This method will check for {@link Subtype#DNA} |
2098 |
23 Oct 13 |
nicklas |
extracts with a name matching the case name (eg. xxx.d). |
2098 |
23 Oct 13 |
nicklas |
45 |
*/ |
2098 |
23 Oct 13 |
nicklas |
46 |
public static List<Dna> findByCaseName(DbControl dc, String name) |
2098 |
23 Oct 13 |
nicklas |
47 |
{ |
2098 |
23 Oct 13 |
nicklas |
// Get rid of suffixes in the name (eg. 'C' which is used for pre-neoadjuvant forms) |
2098 |
23 Oct 13 |
nicklas |
49 |
if (name.length() > 7) name = name.substring(0, 7); |
2098 |
23 Oct 13 |
nicklas |
50 |
|
2098 |
23 Oct 13 |
nicklas |
// Look for a dna with the given name |
2098 |
23 Oct 13 |
nicklas |
52 |
ItemQuery<Extract> dnaQuery = Extract.getQuery(); |
2098 |
23 Oct 13 |
nicklas |
53 |
Subtype.DNA.addFilter(dc, dnaQuery); |
2917 |
11 Nov 14 |
nicklas |
54 |
dnaQuery.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT); |
2098 |
23 Oct 13 |
nicklas |
55 |
dnaQuery.restrict(Restrictions.like(Hql.property("name"), Expressions.parameter("name", name+".%", Type.STRING))); |
2917 |
11 Nov 14 |
nicklas |
56 |
dnaQuery.order(Orders.asc(Hql.property("name"))); |
2098 |
23 Oct 13 |
nicklas |
57 |
|
2098 |
23 Oct 13 |
nicklas |
58 |
List<Extract> tmp = dnaQuery.list(dc); |
2098 |
23 Oct 13 |
nicklas |
59 |
List<Dna> dna = new ArrayList<Dna>(tmp.size()); |
2098 |
23 Oct 13 |
nicklas |
60 |
for (Extract e : tmp) |
2098 |
23 Oct 13 |
nicklas |
61 |
{ |
2098 |
23 Oct 13 |
nicklas |
62 |
dna.add(new Dna(e)); |
2098 |
23 Oct 13 |
nicklas |
63 |
} |
2098 |
23 Oct 13 |
nicklas |
64 |
return dna; |
2098 |
23 Oct 13 |
nicklas |
65 |
} |
2098 |
23 Oct 13 |
nicklas |
66 |
|
2098 |
23 Oct 13 |
nicklas |
67 |
|
2098 |
23 Oct 13 |
nicklas |
68 |
/** |
2098 |
23 Oct 13 |
nicklas |
Get a DNA extract when the id is known. |
2098 |
23 Oct 13 |
nicklas |
70 |
*/ |
2098 |
23 Oct 13 |
nicklas |
71 |
public static Dna getById(DbControl dc, int id) |
2098 |
23 Oct 13 |
nicklas |
72 |
{ |
2098 |
23 Oct 13 |
nicklas |
73 |
return new Dna(Extract.getById(dc, id)); |
2098 |
23 Oct 13 |
nicklas |
74 |
} |
2098 |
23 Oct 13 |
nicklas |
75 |
|
2098 |
23 Oct 13 |
nicklas |
76 |
public static Dna get(Extract extract) |
2098 |
23 Oct 13 |
nicklas |
77 |
{ |
7142 |
04 May 23 |
nicklas |
78 |
return extract == null ? null : new Dna(extract); |
2098 |
23 Oct 13 |
nicklas |
79 |
} |
2098 |
23 Oct 13 |
nicklas |
80 |
|
2098 |
23 Oct 13 |
nicklas |
81 |
public static List<Dna> toDna(Collection<Extract> extracts) |
2098 |
23 Oct 13 |
nicklas |
82 |
{ |
2098 |
23 Oct 13 |
nicklas |
83 |
List<Dna> dna = new ArrayList<Dna>(extracts.size()); |
2098 |
23 Oct 13 |
nicklas |
84 |
for (Extract e : extracts) |
2098 |
23 Oct 13 |
nicklas |
85 |
{ |
2098 |
23 Oct 13 |
nicklas |
86 |
dna.add(new Dna(e)); |
2098 |
23 Oct 13 |
nicklas |
87 |
} |
2098 |
23 Oct 13 |
nicklas |
88 |
return dna; |
2098 |
23 Oct 13 |
nicklas |
89 |
} |
2098 |
23 Oct 13 |
nicklas |
90 |
|
2098 |
23 Oct 13 |
nicklas |
91 |
private JSONObject jsonWell; |
2098 |
23 Oct 13 |
nicklas |
92 |
|
2098 |
23 Oct 13 |
nicklas |
93 |
private Dna(Extract extract) |
2098 |
23 Oct 13 |
nicklas |
94 |
{ |
2098 |
23 Oct 13 |
nicklas |
95 |
super(extract); |
2098 |
23 Oct 13 |
nicklas |
96 |
} |
2098 |
23 Oct 13 |
nicklas |
97 |
|
2098 |
23 Oct 13 |
nicklas |
98 |
|
2098 |
23 Oct 13 |
nicklas |
99 |
/** |
2098 |
23 Oct 13 |
nicklas |
Get the real extract that represents this DNA in BASE. |
2098 |
23 Oct 13 |
nicklas |
101 |
*/ |
2098 |
23 Oct 13 |
nicklas |
102 |
public Extract getExtract() |
2098 |
23 Oct 13 |
nicklas |
103 |
{ |
2098 |
23 Oct 13 |
nicklas |
104 |
return getItem(); |
2098 |
23 Oct 13 |
nicklas |
105 |
} |
2098 |
23 Oct 13 |
nicklas |
106 |
|
5133 |
21 Nov 18 |
nicklas |
107 |
/** |
5133 |
21 Nov 18 |
nicklas |
@since 4.21 |
5133 |
21 Nov 18 |
nicklas |
109 |
*/ |
5133 |
21 Nov 18 |
nicklas |
110 |
public Lysate getLysate() |
5133 |
21 Nov 18 |
nicklas |
111 |
{ |
5133 |
21 Nov 18 |
nicklas |
112 |
Extract dna = getItem(); |
5133 |
21 Nov 18 |
nicklas |
113 |
Extract lysate = (Extract)dna.getParent(); |
5133 |
21 Nov 18 |
nicklas |
114 |
return lysate == null ? null : Lysate.get(lysate); |
5133 |
21 Nov 18 |
nicklas |
115 |
} |
5133 |
21 Nov 18 |
nicklas |
116 |
|
5359 |
12 Apr 19 |
nicklas |
117 |
/** |
5359 |
12 Apr 19 |
nicklas |
Checks if this DNA item is a Pre-normalized DNA. |
5359 |
12 Apr 19 |
nicklas |
@since 4.23 |
5359 |
12 Apr 19 |
nicklas |
120 |
*/ |
5359 |
12 Apr 19 |
nicklas |
121 |
public boolean isPreNormalized(DbControl dc) |
5359 |
12 Apr 19 |
nicklas |
122 |
{ |
5359 |
12 Apr 19 |
nicklas |
123 |
return Subtype.DNA_NORMALIZED_ALIQUOT.get(dc).equals(getExtract().getItemSubtype()); |
5359 |
12 Apr 19 |
nicklas |
124 |
} |
5359 |
12 Apr 19 |
nicklas |
125 |
|
5359 |
12 Apr 19 |
nicklas |
126 |
/** |
5359 |
12 Apr 19 |
nicklas |
Return the parent DNA item if this is a pre-normalized |
5359 |
12 Apr 19 |
nicklas |
DNA, otherwise return itself. |
5359 |
12 Apr 19 |
nicklas |
@since 4.23 |
5359 |
12 Apr 19 |
nicklas |
130 |
*/ |
5359 |
12 Apr 19 |
nicklas |
131 |
public Dna getDnaIfPrenormalized(DbControl dc) |
5359 |
12 Apr 19 |
nicklas |
132 |
{ |
5359 |
12 Apr 19 |
nicklas |
133 |
Dna d = this; |
5359 |
12 Apr 19 |
nicklas |
134 |
if (isPreNormalized(dc)) |
5359 |
12 Apr 19 |
nicklas |
135 |
{ |
5359 |
12 Apr 19 |
nicklas |
136 |
d = new Dna((Extract)getItem().getParent()); |
5359 |
12 Apr 19 |
nicklas |
137 |
} |
5359 |
12 Apr 19 |
nicklas |
138 |
return d; |
5359 |
12 Apr 19 |
nicklas |
139 |
} |
7009 |
25 Jan 23 |
nicklas |
140 |
|
7009 |
25 Jan 23 |
nicklas |
141 |
/** |
7009 |
25 Jan 23 |
nicklas |
Find the top-most Dna item. |
7009 |
25 Jan 23 |
nicklas |
@since 4.44 |
7009 |
25 Jan 23 |
nicklas |
144 |
*/ |
7009 |
25 Jan 23 |
nicklas |
145 |
public Dna getTopDna(DbControl dc) |
7009 |
25 Jan 23 |
nicklas |
146 |
{ |
7009 |
25 Jan 23 |
nicklas |
147 |
Map<Subtype, BioMaterial> parents = findParentBioMaterial(dc, Subtype.DNA, Subtype.LYSATE); |
7009 |
25 Jan 23 |
nicklas |
148 |
return Dna.get((Extract)parents.get(Subtype.DNA)); |
7009 |
25 Jan 23 |
nicklas |
149 |
} |
5359 |
12 Apr 19 |
nicklas |
150 |
|
5359 |
12 Apr 19 |
nicklas |
151 |
|
5359 |
12 Apr 19 |
nicklas |
152 |
/** |
5359 |
12 Apr 19 |
nicklas |
Find the next name to give a DNANormalizedAliquot child item. This assumes that |
5359 |
12 Apr 19 |
nicklas |
all child items are using the naming convention. foo.n, foo.n2, and |
5359 |
12 Apr 19 |
nicklas |
so on. NOTE! The first child item have no number! |
5359 |
12 Apr 19 |
nicklas |
@return The next unused name |
5359 |
12 Apr 19 |
nicklas |
@since 4.23 |
5359 |
12 Apr 19 |
nicklas |
158 |
*/ |
5359 |
12 Apr 19 |
nicklas |
159 |
public String getNextNormalizedAliquotName(DbControl dc) |
5359 |
12 Apr 19 |
nicklas |
160 |
{ |
5359 |
12 Apr 19 |
nicklas |
161 |
Extract dna = getItem(); |
6193 |
30 Mar 21 |
nicklas |
162 |
ItemQuery<Extract> query = null; |
6193 |
30 Mar 21 |
nicklas |
163 |
if (dna.isInDatabase()) |
6193 |
30 Mar 21 |
nicklas |
164 |
{ |
6193 |
30 Mar 21 |
nicklas |
165 |
query = dna.getChildExtracts(); |
6193 |
30 Mar 21 |
nicklas |
166 |
Subtype.DNA_NORMALIZED_ALIQUOT.addFilter(dc, query); |
6193 |
30 Mar 21 |
nicklas |
167 |
query.setIncludes(Include.ALL); |
6193 |
30 Mar 21 |
nicklas |
168 |
} |
5359 |
12 Apr 19 |
nicklas |
169 |
return getNextChildItemName(dc, query, Subtype.DNA_NORMALIZED_ALIQUOT.getItemSuffix(), false); |
5359 |
12 Apr 19 |
nicklas |
170 |
} |
5359 |
12 Apr 19 |
nicklas |
171 |
|
5417 |
10 May 19 |
nicklas |
172 |
/** |
5417 |
10 May 19 |
nicklas |
Find the next name to give a "Library" child item. This assumes that |
5417 |
10 May 19 |
nicklas |
all child items are using the naming convention. foo.d/lib, foo.d2/lib2, and |
5417 |
10 May 19 |
nicklas |
so on. NOTE! The first child item have no number! |
5417 |
10 May 19 |
nicklas |
@return The next unused name |
5417 |
10 May 19 |
nicklas |
@since 4.23 |
5417 |
10 May 19 |
nicklas |
178 |
*/ |
5417 |
10 May 19 |
nicklas |
179 |
public String getNextLibName(DbControl dc, Subtype subtype) |
5417 |
10 May 19 |
nicklas |
180 |
{ |
5417 |
10 May 19 |
nicklas |
181 |
Extract dna = getItem(); |
6193 |
30 Mar 21 |
nicklas |
182 |
ItemQuery<Extract> query = null; |
6193 |
30 Mar 21 |
nicklas |
183 |
if (dna.isInDatabase()) |
6193 |
30 Mar 21 |
nicklas |
184 |
{ |
6193 |
30 Mar 21 |
nicklas |
185 |
query = dna.getChildExtracts(); |
6193 |
30 Mar 21 |
nicklas |
186 |
subtype.addFilter(dc, query); |
6193 |
30 Mar 21 |
nicklas |
187 |
query.setIncludes(Include.ALL); |
6193 |
30 Mar 21 |
nicklas |
188 |
} |
5417 |
10 May 19 |
nicklas |
189 |
return getNextChildItemName(dc, query, subtype.getItemSuffix(), true); |
5417 |
10 May 19 |
nicklas |
190 |
} |
5417 |
10 May 19 |
nicklas |
191 |
|
6870 |
16 Nov 22 |
nicklas |
192 |
/** |
6870 |
16 Nov 22 |
nicklas |
Load the top-most parent item that is an extract or sample. This method is primarily |
6870 |
16 Nov 22 |
nicklas |
intended to find the root item (RNA, DNA or Specimen) for an external item. |
6870 |
16 Nov 22 |
nicklas |
@since 4.41 |
6870 |
16 Nov 22 |
nicklas |
196 |
*/ |
6870 |
16 Nov 22 |
nicklas |
197 |
public BioMaterial getTopExtractOrSample(DbControl dc) |
6870 |
16 Nov 22 |
nicklas |
198 |
{ |
6870 |
16 Nov 22 |
nicklas |
199 |
Extract e = getItem(); |
6870 |
16 Nov 22 |
nicklas |
200 |
Sample s = null; |
6870 |
16 Nov 22 |
nicklas |
201 |
while (e.getParentType() == Item.EXTRACT) |
6870 |
16 Nov 22 |
nicklas |
202 |
{ |
6870 |
16 Nov 22 |
nicklas |
203 |
e = (Extract)e.getParent(); |
6870 |
16 Nov 22 |
nicklas |
204 |
} |
6870 |
16 Nov 22 |
nicklas |
205 |
if (e.getParentType() == Item.SAMPLE) |
6870 |
16 Nov 22 |
nicklas |
206 |
{ |
6870 |
16 Nov 22 |
nicklas |
207 |
s = (Sample)e.getParent(); |
6870 |
16 Nov 22 |
nicklas |
208 |
while (s.getParentType() == Item.SAMPLE) |
6870 |
16 Nov 22 |
nicklas |
209 |
{ |
6870 |
16 Nov 22 |
nicklas |
210 |
s = (Sample)s.getParent(); |
6870 |
16 Nov 22 |
nicklas |
211 |
} |
6870 |
16 Nov 22 |
nicklas |
212 |
} |
6870 |
16 Nov 22 |
nicklas |
213 |
return s == null ? e : s; |
6870 |
16 Nov 22 |
nicklas |
214 |
} |
6870 |
16 Nov 22 |
nicklas |
215 |
|
5417 |
10 May 19 |
nicklas |
216 |
|
6870 |
16 Nov 22 |
nicklas |
217 |
|
2098 |
23 Oct 13 |
nicklas |
218 |
@Override |
2098 |
23 Oct 13 |
nicklas |
219 |
protected void initJSON(JSONObject json) |
2098 |
23 Oct 13 |
nicklas |
220 |
{ |
2098 |
23 Oct 13 |
nicklas |
221 |
super.initJSON(json); |
2098 |
23 Oct 13 |
nicklas |
222 |
if (jsonWell != null) json.put("bioWell", jsonWell); |
2098 |
23 Oct 13 |
nicklas |
223 |
|
2098 |
23 Oct 13 |
nicklas |
224 |
} |
2098 |
23 Oct 13 |
nicklas |
225 |
|
2098 |
23 Oct 13 |
nicklas |
226 |
/** |
2098 |
23 Oct 13 |
nicklas |
Load information about the plate and location the current FlowThrough |
2098 |
23 Oct 13 |
nicklas |
is located on. |
2098 |
23 Oct 13 |
nicklas |
229 |
*/ |
2098 |
23 Oct 13 |
nicklas |
230 |
public JSONObject loadBioPlateLocation() |
2098 |
23 Oct 13 |
nicklas |
231 |
{ |
2098 |
23 Oct 13 |
nicklas |
232 |
if (jsonWell == null) |
2098 |
23 Oct 13 |
nicklas |
233 |
{ |
2134 |
11 Nov 13 |
nicklas |
234 |
jsonWell = JsonUtil.getBioWellAsJSON(getItem().getBioWell(), true); |
2098 |
23 Oct 13 |
nicklas |
235 |
} |
2098 |
23 Oct 13 |
nicklas |
236 |
return jsonWell; |
2098 |
23 Oct 13 |
nicklas |
237 |
} |
2098 |
23 Oct 13 |
nicklas |
238 |
|
2098 |
23 Oct 13 |
nicklas |
239 |
} |