2522 |
15 Aug 06 |
enell |
1 |
/* |
2522 |
15 Aug 06 |
enell |
$Id$ |
2522 |
15 Aug 06 |
enell |
3 |
|
3675 |
16 Aug 07 |
jari |
Copyright (C) 2006 Johan Enell, Nicklas Nordborg, Martin Svensson |
2522 |
15 Aug 06 |
enell |
5 |
|
2522 |
15 Aug 06 |
enell |
This file is part of BASE - BioArray Software Environment. |
2522 |
15 Aug 06 |
enell |
Available at http://base.thep.lu.se/ |
2522 |
15 Aug 06 |
enell |
8 |
|
2522 |
15 Aug 06 |
enell |
BASE is free software; you can redistribute it and/or modify it |
2522 |
15 Aug 06 |
enell |
under the terms of the GNU General Public License as published by |
4479 |
05 Sep 08 |
jari |
the Free Software Foundation; either version 3 of the License, or |
2522 |
15 Aug 06 |
enell |
(at your option) any later version. |
2522 |
15 Aug 06 |
enell |
13 |
|
2522 |
15 Aug 06 |
enell |
BASE is distributed in the hope that it will be useful, but |
2522 |
15 Aug 06 |
enell |
WITHOUT ANY WARRANTY; without even the implied warranty of |
2522 |
15 Aug 06 |
enell |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
2522 |
15 Aug 06 |
enell |
General Public License for more details. |
2522 |
15 Aug 06 |
enell |
18 |
|
2522 |
15 Aug 06 |
enell |
You should have received a copy of the GNU General Public License |
4515 |
11 Sep 08 |
jari |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
2522 |
15 Aug 06 |
enell |
21 |
*/ |
2522 |
15 Aug 06 |
enell |
22 |
package net.sf.basedb.util; |
2522 |
15 Aug 06 |
enell |
23 |
|
2522 |
15 Aug 06 |
enell |
24 |
import net.sf.basedb.core.AnnotationType; |
2669 |
27 Sep 06 |
nicklas |
25 |
import net.sf.basedb.core.BaseException; |
2522 |
15 Aug 06 |
enell |
26 |
import net.sf.basedb.core.BioAssay; |
2522 |
15 Aug 06 |
enell |
27 |
import net.sf.basedb.core.DbControl; |
5319 |
20 Apr 10 |
nicklas |
28 |
import net.sf.basedb.core.DynamicQuery; |
5319 |
20 Apr 10 |
nicklas |
29 |
import net.sf.basedb.core.DynamicSpotQuery; |
2522 |
15 Aug 06 |
enell |
30 |
import net.sf.basedb.core.Include; |
2669 |
27 Sep 06 |
nicklas |
31 |
import net.sf.basedb.core.InvalidDataException; |
2669 |
27 Sep 06 |
nicklas |
32 |
import net.sf.basedb.core.InvalidUseOfNullException; |
2522 |
15 Aug 06 |
enell |
33 |
import net.sf.basedb.core.ItemQuery; |
2522 |
15 Aug 06 |
enell |
34 |
import net.sf.basedb.core.RawBioAssay; |
5319 |
20 Apr 10 |
nicklas |
35 |
import net.sf.basedb.core.Metadata.PropertyPath; |
6909 |
18 May 15 |
nicklas |
36 |
import net.sf.basedb.core.RootRawBioAssay; |
2669 |
27 Sep 06 |
nicklas |
37 |
import net.sf.basedb.core.query.Expression; |
2669 |
27 Sep 06 |
nicklas |
38 |
import net.sf.basedb.core.query.Restriction; |
5130 |
13 Oct 09 |
nicklas |
39 |
import net.sf.basedb.core.snapshot.AnnotationSnapshot; |
5131 |
14 Oct 09 |
nicklas |
40 |
import net.sf.basedb.core.snapshot.AnnotationTypeFilter; |
5130 |
13 Oct 09 |
nicklas |
41 |
import net.sf.basedb.core.snapshot.SnapshotManager; |
5131 |
14 Oct 09 |
nicklas |
42 |
import net.sf.basedb.util.filter.Filter; |
2669 |
27 Sep 06 |
nicklas |
43 |
import net.sf.basedb.util.jep.Jep; |
2522 |
15 Aug 06 |
enell |
44 |
|
2522 |
15 Aug 06 |
enell |
45 |
import java.util.Set; |
2704 |
04 Oct 06 |
nicklas |
46 |
import java.util.TreeSet; |
2522 |
15 Aug 06 |
enell |
47 |
|
2522 |
15 Aug 06 |
enell |
48 |
/** |
2669 |
27 Sep 06 |
nicklas |
This is a utility class for {@link net.sf.basedb.core.BioAssaySet}. |
2669 |
27 Sep 06 |
nicklas |
50 |
|
2669 |
27 Sep 06 |
nicklas |
@base.modified $Date$ |
2669 |
27 Sep 06 |
nicklas |
@author Johan |
2669 |
27 Sep 06 |
nicklas |
@version 2.0 |
2669 |
27 Sep 06 |
nicklas |
54 |
*/ |
2522 |
15 Aug 06 |
enell |
55 |
public class BioAssaySetUtil |
2522 |
15 Aug 06 |
enell |
56 |
{ |
5130 |
13 Oct 09 |
nicklas |
57 |
|
5130 |
13 Oct 09 |
nicklas |
58 |
/** |
5130 |
13 Oct 09 |
nicklas |
Find the annotation values for a given annotation type on a bioassay. |
5130 |
13 Oct 09 |
nicklas |
This method will only check annotations, primary and inherited, on the |
6909 |
18 May 15 |
nicklas |
root raw bioassays that the bioassay is a child of. |
5130 |
13 Oct 09 |
nicklas |
<p> |
5130 |
13 Oct 09 |
nicklas |
Since a bioassay may |
5130 |
13 Oct 09 |
nicklas |
have more than one parent raw bioassay and each annotation may have |
5130 |
13 Oct 09 |
nicklas |
multiple values it is possible that the set contains multiple values. |
5130 |
13 Oct 09 |
nicklas |
Each value is only present once regardless of how many times it appears |
5130 |
13 Oct 09 |
nicklas |
in the annotations. |
5130 |
13 Oct 09 |
nicklas |
68 |
|
5130 |
13 Oct 09 |
nicklas |
@param dc The DbControl for database access |
5130 |
13 Oct 09 |
nicklas |
@param snapshotManager A snapshot manager that is used for loading the annotations |
5130 |
13 Oct 09 |
nicklas |
from either the database or the file cache (if null, an internal, non-reusable |
5130 |
13 Oct 09 |
nicklas |
snapshot manager is used) |
5130 |
13 Oct 09 |
nicklas |
@param bioAssay The bioassay to get the annotations for |
5130 |
13 Oct 09 |
nicklas |
@param annotationType The annotation type of the annotations |
5130 |
13 Oct 09 |
nicklas |
@return A set containing all values |
5130 |
13 Oct 09 |
nicklas |
@since 2.14 |
5130 |
13 Oct 09 |
nicklas |
77 |
*/ |
5130 |
13 Oct 09 |
nicklas |
78 |
public static Set<?> getAnnotationValues(DbControl dc, SnapshotManager snapshotManager, |
5130 |
13 Oct 09 |
nicklas |
79 |
BioAssay bioAssay, AnnotationType annotationType) |
5130 |
13 Oct 09 |
nicklas |
80 |
{ |
2704 |
04 Oct 06 |
nicklas |
81 |
Set<Object> allValues = new TreeSet<Object>(); |
5130 |
13 Oct 09 |
nicklas |
82 |
if (snapshotManager == null) snapshotManager = new SnapshotManager(); |
5131 |
14 Oct 09 |
nicklas |
83 |
Filter<AnnotationSnapshot> filter = new AnnotationTypeFilter(annotationType); |
5131 |
14 Oct 09 |
nicklas |
84 |
for (AnnotationSnapshot a : snapshotManager.findAnnotations(dc, bioAssay, filter, true)) |
2892 |
10 Nov 06 |
nicklas |
85 |
{ |
6924 |
03 Jun 15 |
nicklas |
86 |
if (a != null) allValues.addAll(a.getActualValues()); |
2892 |
10 Nov 06 |
nicklas |
87 |
} |
6909 |
18 May 15 |
nicklas |
88 |
ItemQuery<RootRawBioAssay> rbaQuery = bioAssay.getRootRawBioAssays(); |
5130 |
13 Oct 09 |
nicklas |
89 |
rbaQuery.include(Include.MINE, Include.SHARED, Include.IN_PROJECT, Include.OTHERS); |
6909 |
18 May 15 |
nicklas |
90 |
for (RootRawBioAssay rb : rbaQuery.list(dc)) |
2522 |
15 Aug 06 |
enell |
91 |
{ |
5131 |
14 Oct 09 |
nicklas |
92 |
for (AnnotationSnapshot a : snapshotManager.findAnnotations(dc, rb, filter, true)) |
2522 |
15 Aug 06 |
enell |
93 |
{ |
6924 |
03 Jun 15 |
nicklas |
94 |
if (a != null) allValues.addAll(a.getActualValues()); |
2522 |
15 Aug 06 |
enell |
95 |
} |
2522 |
15 Aug 06 |
enell |
96 |
} |
2522 |
15 Aug 06 |
enell |
97 |
return allValues; |
2522 |
15 Aug 06 |
enell |
98 |
} |
2669 |
27 Sep 06 |
nicklas |
99 |
|
2669 |
27 Sep 06 |
nicklas |
100 |
/** |
2704 |
04 Oct 06 |
nicklas |
Find the values of parent properties for a given bioassay. This method |
2704 |
04 Oct 06 |
nicklas |
starts by loading all raw bioassays that are parents to the bioassay. |
5319 |
20 Apr 10 |
nicklas |
For each raw bioassay it uses the {@link PropertyPath} |
5319 |
20 Apr 10 |
nicklas |
object to find the value of the given property for that raw bioassay. |
2704 |
04 Oct 06 |
nicklas |
<p> |
2704 |
04 Oct 06 |
nicklas |
Since a bioassay may have more than one parent raw bioassay and it is possible |
2704 |
04 Oct 06 |
nicklas |
that the set contains multiple values. Each value is only present once |
2704 |
04 Oct 06 |
nicklas |
regardless of how many times it appears among the parents. |
2704 |
04 Oct 06 |
nicklas |
<p> |
5319 |
20 Apr 10 |
nicklas |
Examples of property paths: |
2704 |
04 Oct 06 |
nicklas |
<ul> |
2704 |
04 Oct 06 |
nicklas |
<li>spots: to get the number of spots in the raw bioassay |
2704 |
04 Oct 06 |
nicklas |
<li>software.name: to get the name of the software used to analyse the image files |
2704 |
04 Oct 06 |
nicklas |
<li>scan.hybridization.creationEvent.eventDate: to get the date when the hybridization |
2704 |
04 Oct 06 |
nicklas |
was created |
2704 |
04 Oct 06 |
nicklas |
</ul> |
2704 |
04 Oct 06 |
nicklas |
117 |
|
2704 |
04 Oct 06 |
nicklas |
@param dc The DbControl for database access |
2704 |
04 Oct 06 |
nicklas |
@param bioAssay The bioassay to get the annotations for |
2704 |
04 Oct 06 |
nicklas |
@param property The property to get the value for |
2704 |
04 Oct 06 |
nicklas |
@return A set containing all values |
2704 |
04 Oct 06 |
nicklas |
122 |
*/ |
5319 |
20 Apr 10 |
nicklas |
123 |
public static Set<Object> getParentProperties(DbControl dc, BioAssay bioAssay, PropertyPath<RawBioAssay, ?> property) |
2704 |
04 Oct 06 |
nicklas |
124 |
{ |
2704 |
04 Oct 06 |
nicklas |
125 |
ItemQuery<RawBioAssay> rbaQuery = bioAssay.getRawBioAssays(); |
2704 |
04 Oct 06 |
nicklas |
126 |
rbaQuery.include(Include.MINE, Include.SHARED, Include.IN_PROJECT, Include.OTHERS); |
2704 |
04 Oct 06 |
nicklas |
127 |
Set<Object> allValues = new TreeSet<Object>(); |
2704 |
04 Oct 06 |
nicklas |
128 |
for (RawBioAssay rb : rbaQuery.list(dc)) |
2704 |
04 Oct 06 |
nicklas |
129 |
{ |
5319 |
20 Apr 10 |
nicklas |
130 |
Object value = property.getValue(dc, rb); |
2704 |
04 Oct 06 |
nicklas |
131 |
if (value == null) value = "Unknown"; |
2704 |
04 Oct 06 |
nicklas |
132 |
allValues.add(value); |
2704 |
04 Oct 06 |
nicklas |
133 |
} |
2704 |
04 Oct 06 |
nicklas |
134 |
return allValues; |
2704 |
04 Oct 06 |
nicklas |
135 |
} |
5590 |
16 Mar 11 |
nicklas |
136 |
|
2704 |
04 Oct 06 |
nicklas |
137 |
/** |
2669 |
27 Sep 06 |
nicklas |
Parse a JEP expression and create a restrictions that is suitable to |
2669 |
27 Sep 06 |
nicklas |
be used in a dynamic query. In addition to the standard functions defined |
5319 |
20 Apr 10 |
nicklas |
by JEP (see {@link Jep#getFunctions()}) this method can use additional |
5319 |
20 Apr 10 |
nicklas |
functions. The extra functions are defined by the query. See |
5319 |
20 Apr 10 |
nicklas |
{@link DynamicQuery#getJepFunctions(DbControl, boolean)} and it's subclasses |
5319 |
20 Apr 10 |
nicklas |
for detailed information. Here are some useful functions: |
5319 |
20 Apr 10 |
nicklas |
144 |
|
2669 |
27 Sep 06 |
nicklas |
<ul> |
5319 |
20 Apr 10 |
nicklas |
<li>all functions defined by the {@link #createJepExpression(DbControl, |
5319 |
20 Apr 10 |
nicklas |
String, DynamicQuery)} method |
2669 |
27 Sep 06 |
nicklas |
<li>inList(list): Checks if the reporter is included in the specified list |
2669 |
27 Sep 06 |
nicklas |
<li>notInList(list): Checks if the reporter isn't included in the specified list |
2669 |
27 Sep 06 |
nicklas |
</ul> |
4017 |
28 Nov 07 |
martin |
@param dc DbControl to use when accessing the database. |
4017 |
28 Nov 07 |
martin |
@param formula The formula to parse. Must not be null. |
5319 |
20 Apr 10 |
nicklas |
@param query The query where the restriction will be used, it is not |
5319 |
20 Apr 10 |
nicklas |
recommended to use the expression in a different query since it may |
5319 |
20 Apr 10 |
nicklas |
generate incorrect SQL |
4017 |
28 Nov 07 |
martin |
@return A restriction to be used on a query. |
4017 |
28 Nov 07 |
martin |
@throws InvalidDataException If any of required parameters are null. |
4017 |
28 Nov 07 |
martin |
@throws BaseException If creating the restriction fails. |
2669 |
27 Sep 06 |
nicklas |
@see net.sf.basedb.util.jep.Jep |
2669 |
27 Sep 06 |
nicklas |
160 |
*/ |
5319 |
20 Apr 10 |
nicklas |
161 |
public static Restriction createJepRestriction(DbControl dc, String formula, DynamicQuery query) |
5319 |
20 Apr 10 |
nicklas |
162 |
{ |
5319 |
20 Apr 10 |
nicklas |
163 |
if (query == null) throw new InvalidUseOfNullException("query"); |
5319 |
20 Apr 10 |
nicklas |
164 |
if (formula == null) throw new InvalidUseOfNullException("formula"); |
5319 |
20 Apr 10 |
nicklas |
165 |
return Jep.formulaToRestriction(formula, query.getJepFunctions(dc, true)); |
5319 |
20 Apr 10 |
nicklas |
166 |
} |
5319 |
20 Apr 10 |
nicklas |
167 |
|
2669 |
27 Sep 06 |
nicklas |
168 |
/** |
2669 |
27 Sep 06 |
nicklas |
Parse a JEP expression and create a restrictions that is suitable to |
2669 |
27 Sep 06 |
nicklas |
be used in a dynamic query. In addition to the standard functions defined |
5319 |
20 Apr 10 |
nicklas |
by JEP (see {@link Jep#getFunctions()}) this method can use additional |
5319 |
20 Apr 10 |
nicklas |
functions. The extra functions are defined by the query. See |
5319 |
20 Apr 10 |
nicklas |
{@link DynamicQuery#getJepFunctions(DbControl, boolean)} and it's subclasses |
5319 |
20 Apr 10 |
nicklas |
for detailed information. Here are some useful functions: |
5319 |
20 Apr 10 |
nicklas |
175 |
|
2669 |
27 Sep 06 |
nicklas |
<ul> |
2993 |
01 Dec 06 |
nicklas |
<li>raw(property): The value of the specified raw data property. The use of this function |
5319 |
20 Apr 10 |
nicklas |
can be denied if the bioassayset contains merged spot data. The |
5319 |
20 Apr 10 |
nicklas |
{@link DynamicSpotQuery#setDisableRawJoinOnMergedData(boolean)} |
5319 |
20 Apr 10 |
nicklas |
flag controls this. |
2669 |
27 Sep 06 |
nicklas |
<li>rep(property): The value of the specified reporter property |
5319 |
20 Apr 10 |
nicklas |
<li>pos(): The position of a spot. Note that this is not the same as raw(position) |
4912 |
29 Apr 09 |
nicklas |
<li>ch(n): The intensity in channel n (un-transformed) |
4912 |
29 Apr 09 |
nicklas |
<li>rawCh(n): The possibly transformed intensity in channel n |
2981 |
30 Nov 06 |
nicklas |
<li>score(list): The score of the {@link net.sf.basedb.core.data.ReporterData} in the |
2981 |
30 Nov 06 |
nicklas |
specified {@link net.sf.basedb.core.ReporterList} (numeric or external ID) |
2981 |
30 Nov 06 |
nicklas |
<li>xtra(extraValue): The value of the specified {@link net.sf.basedb.core.ExtraValue} |
2981 |
30 Nov 06 |
nicklas |
(numeric ID or the external ID of the {@link net.sf.basedb.core.ExtraValueType}) |
2669 |
27 Sep 06 |
nicklas |
</ul> |
4017 |
28 Nov 07 |
martin |
@param dc The DbControl to use when accessing the database. |
4017 |
28 Nov 07 |
martin |
@param formula The formula that should be parsed. Must not be null. |
5319 |
20 Apr 10 |
nicklas |
@param query The query where the expression will be used, it is not |
5319 |
20 Apr 10 |
nicklas |
recommended to use the expression in a different query since it may |
5319 |
20 Apr 10 |
nicklas |
generate incorrect SQL |
4017 |
28 Nov 07 |
martin |
@return An expression to be used with a query. |
4017 |
28 Nov 07 |
martin |
@throws InvalidDataException If any of required parameters are null. |
4017 |
28 Nov 07 |
martin |
@throws BaseException If creating the expression fails. |
2669 |
27 Sep 06 |
nicklas |
@see net.sf.basedb.util.jep.Jep |
5319 |
20 Apr 10 |
nicklas |
@since 2.15 |
2669 |
27 Sep 06 |
nicklas |
200 |
*/ |
5319 |
20 Apr 10 |
nicklas |
201 |
public static Expression createJepExpression(DbControl dc, String formula, DynamicQuery query) |
2669 |
27 Sep 06 |
nicklas |
202 |
{ |
5319 |
20 Apr 10 |
nicklas |
203 |
if (query == null) throw new InvalidUseOfNullException("query"); |
2669 |
27 Sep 06 |
nicklas |
204 |
if (formula == null) throw new InvalidUseOfNullException("formula"); |
5319 |
20 Apr 10 |
nicklas |
205 |
return Jep.formulaToExpression(formula, query.getJepFunctions(dc, false)); |
2669 |
27 Sep 06 |
nicklas |
206 |
} |
2669 |
27 Sep 06 |
nicklas |
207 |
|
2522 |
15 Aug 06 |
enell |
208 |
} |