src/core/net/sf/basedb/util/BioAssaySetUtil.java

Code
Comments
Other
Rev Date Author Line
2522 15 Aug 06 enell 1 /*
2522 15 Aug 06 enell 2  $Id$
2522 15 Aug 06 enell 3
3675 16 Aug 07 jari 4  Copyright (C) 2006 Johan Enell, Nicklas Nordborg, Martin Svensson
2522 15 Aug 06 enell 5
2522 15 Aug 06 enell 6  This file is part of BASE - BioArray Software Environment.
2522 15 Aug 06 enell 7  Available at http://base.thep.lu.se/
2522 15 Aug 06 enell 8
2522 15 Aug 06 enell 9  BASE is free software; you can redistribute it and/or modify it
2522 15 Aug 06 enell 10  under the terms of the GNU General Public License as published by
4479 05 Sep 08 jari 11  the Free Software Foundation; either version 3 of the License, or
2522 15 Aug 06 enell 12  (at your option) any later version.
2522 15 Aug 06 enell 13
2522 15 Aug 06 enell 14  BASE is distributed in the hope that it will be useful, but
2522 15 Aug 06 enell 15  WITHOUT ANY WARRANTY; without even the implied warranty of
2522 15 Aug 06 enell 16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
2522 15 Aug 06 enell 17  General Public License for more details.
2522 15 Aug 06 enell 18
2522 15 Aug 06 enell 19  You should have received a copy of the GNU General Public License
4515 11 Sep 08 jari 20  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 49   This is a utility class for {@link net.sf.basedb.core.BioAssaySet}.
2669 27 Sep 06 nicklas 50
2669 27 Sep 06 nicklas 51   @base.modified $Date$
2669 27 Sep 06 nicklas 52   @author Johan
2669 27 Sep 06 nicklas 53   @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 59     Find the annotation values for a given annotation type on a bioassay.
5130 13 Oct 09 nicklas 60     This method will only check annotations, primary and inherited, on the
6909 18 May 15 nicklas 61     root raw bioassays that the bioassay is a child of. 
5130 13 Oct 09 nicklas 62     <p> 
5130 13 Oct 09 nicklas 63     Since a bioassay may
5130 13 Oct 09 nicklas 64     have more than one parent raw bioassay and each annotation may have
5130 13 Oct 09 nicklas 65     multiple values it is possible that the set contains multiple values.
5130 13 Oct 09 nicklas 66     Each value is only present once regardless of how many times it appears
5130 13 Oct 09 nicklas 67     in the annotations.
5130 13 Oct 09 nicklas 68     
5130 13 Oct 09 nicklas 69     @param dc The DbControl for database access
5130 13 Oct 09 nicklas 70     @param snapshotManager A snapshot manager that is used for loading the annotations
5130 13 Oct 09 nicklas 71       from either the database or the file cache (if null, an internal, non-reusable
5130 13 Oct 09 nicklas 72       snapshot manager is used)
5130 13 Oct 09 nicklas 73     @param bioAssay The bioassay to get the annotations for
5130 13 Oct 09 nicklas 74     @param annotationType The annotation type of the annotations
5130 13 Oct 09 nicklas 75     @return A set containing all values
5130 13 Oct 09 nicklas 76     @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 101     Find the values of parent properties for a given bioassay. This method
2704 04 Oct 06 nicklas 102     starts by loading all raw bioassays that are parents to the bioassay.
5319 20 Apr 10 nicklas 103     For each raw bioassay it uses the {@link PropertyPath} 
5319 20 Apr 10 nicklas 104     object to find the value of the given property for that raw bioassay.
2704 04 Oct 06 nicklas 105     <p> 
2704 04 Oct 06 nicklas 106     Since a bioassay may have more than one parent raw bioassay and it is possible 
2704 04 Oct 06 nicklas 107     that the set contains multiple values. Each value is only present once 
2704 04 Oct 06 nicklas 108     regardless of how many times it appears among the parents.
2704 04 Oct 06 nicklas 109     <p>
5319 20 Apr 10 nicklas 110     Examples of property paths:
2704 04 Oct 06 nicklas 111     <ul>
2704 04 Oct 06 nicklas 112     <li>spots: to get the number of spots in the raw bioassay
2704 04 Oct 06 nicklas 113     <li>software.name: to get the name of the software used to analyse the image files
2704 04 Oct 06 nicklas 114     <li>scan.hybridization.creationEvent.eventDate: to get the date when the hybridization
2704 04 Oct 06 nicklas 115       was created
2704 04 Oct 06 nicklas 116     </ul>
2704 04 Oct 06 nicklas 117     
2704 04 Oct 06 nicklas 118     @param dc The DbControl for database access
2704 04 Oct 06 nicklas 119     @param bioAssay The bioassay to get the annotations for
2704 04 Oct 06 nicklas 120     @param property The property to get the value for
2704 04 Oct 06 nicklas 121     @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 138     Parse a JEP expression and create a restrictions that is suitable to
2669 27 Sep 06 nicklas 139     be used in a dynamic query. In addition to the standard functions defined
5319 20 Apr 10 nicklas 140     by JEP (see {@link Jep#getFunctions()}) this method can use additional 
5319 20 Apr 10 nicklas 141     functions. The extra functions are defined by the query. See
5319 20 Apr 10 nicklas 142     {@link DynamicQuery#getJepFunctions(DbControl, boolean)} and it's subclasses
5319 20 Apr 10 nicklas 143     for detailed information. Here are some useful functions:
5319 20 Apr 10 nicklas 144
2669 27 Sep 06 nicklas 145     <ul>
5319 20 Apr 10 nicklas 146     <li>all functions defined by the {@link #createJepExpression(DbControl, 
5319 20 Apr 10 nicklas 147       String, DynamicQuery)} method
2669 27 Sep 06 nicklas 148     <li>inList(list): Checks if the reporter is included in the specified list
2669 27 Sep 06 nicklas 149     <li>notInList(list): Checks if the reporter isn't included in the specified list
2669 27 Sep 06 nicklas 150     </ul>
4017 28 Nov 07 martin 151      @param dc DbControl to use when accessing the database. 
4017 28 Nov 07 martin 152      @param formula The formula to parse. Must not be null.
5319 20 Apr 10 nicklas 153      @param query The query where the restriction will be used, it is not
5319 20 Apr 10 nicklas 154        recommended to use the expression in a different query since it may
5319 20 Apr 10 nicklas 155        generate incorrect SQL
4017 28 Nov 07 martin 156      @return A restriction to be used on a query.
4017 28 Nov 07 martin 157      @throws InvalidDataException  If any of required parameters are null.
4017 28 Nov 07 martin 158      @throws BaseException If creating the restriction fails.
2669 27 Sep 06 nicklas 159     @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 169     Parse a JEP expression and create a restrictions that is suitable to
2669 27 Sep 06 nicklas 170     be used in a dynamic query. In addition to the standard functions defined
5319 20 Apr 10 nicklas 171     by JEP (see {@link Jep#getFunctions()}) this method can use additional 
5319 20 Apr 10 nicklas 172     functions. The extra functions are defined by the query. See
5319 20 Apr 10 nicklas 173     {@link DynamicQuery#getJepFunctions(DbControl, boolean)} and it's subclasses
5319 20 Apr 10 nicklas 174     for detailed information. Here are some useful functions:
5319 20 Apr 10 nicklas 175     
2669 27 Sep 06 nicklas 176     <ul>
2993 01 Dec 06 nicklas 177     <li>raw(property): The value of the specified raw data property. The use of this function
5319 20 Apr 10 nicklas 178       can be denied if the bioassayset contains merged spot data. The 
5319 20 Apr 10 nicklas 179       {@link DynamicSpotQuery#setDisableRawJoinOnMergedData(boolean)}
5319 20 Apr 10 nicklas 180       flag controls this.
2669 27 Sep 06 nicklas 181     <li>rep(property): The value of the specified reporter property
5319 20 Apr 10 nicklas 182     <li>pos(): The position of a spot. Note that this is not the same as raw(position)
4912 29 Apr 09 nicklas 183     <li>ch(n): The intensity in channel n (un-transformed)
4912 29 Apr 09 nicklas 184     <li>rawCh(n): The possibly transformed intensity in channel n
2981 30 Nov 06 nicklas 185     <li>score(list): The score of the {@link net.sf.basedb.core.data.ReporterData} in the 
2981 30 Nov 06 nicklas 186       specified {@link net.sf.basedb.core.ReporterList} (numeric or external ID)
2981 30 Nov 06 nicklas 187     <li>xtra(extraValue): The value of the specified {@link net.sf.basedb.core.ExtraValue} 
2981 30 Nov 06 nicklas 188       (numeric ID or the external ID of the {@link net.sf.basedb.core.ExtraValueType})
2669 27 Sep 06 nicklas 189     </ul>
4017 28 Nov 07 martin 190       @param dc The DbControl to use when accessing the database. 
4017 28 Nov 07 martin 191      @param formula The formula that should be parsed. Must not be null.
5319 20 Apr 10 nicklas 192      @param query The query where the expression will be used, it is not
5319 20 Apr 10 nicklas 193        recommended to use the expression in a different query since it may
5319 20 Apr 10 nicklas 194        generate incorrect SQL
4017 28 Nov 07 martin 195      @return An expression to be used with a query.
4017 28 Nov 07 martin 196      @throws InvalidDataException If any of required parameters are null.
4017 28 Nov 07 martin 197      @throws BaseException If creating the expression fails.
2669 27 Sep 06 nicklas 198     @see net.sf.basedb.util.jep.Jep
5319 20 Apr 10 nicklas 199     @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 }