Changeset 742


Ignore:
Timestamp:
Aug 25, 2008, 8:29:42 AM (13 years ago)
Author:
Martin Svensson
Message:

References #117 Cleaned up and refactored the classes. The Avg.norm. plug-in does not yet do any normalization.

Location:
plugins/base2/net.sf.basedb.normalizers/trunk/src/net/sf/basedb/plugins
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • plugins/base2/net.sf.basedb.normalizers/trunk/src/net/sf/basedb/plugins/AbstractNormalizationPlugin.java

    r730 r742  
    2626package net.sf.basedb.plugins;
    2727
    28 import net.sf.basedb.core.BioAssay;
    29 import net.sf.basedb.core.DbControl;
    30 import net.sf.basedb.core.InvalidUseOfNullException;
    3128import net.sf.basedb.core.Item;
    32 import net.sf.basedb.core.ItemParameterType;
    3329import net.sf.basedb.core.Permission;
    34 import net.sf.basedb.core.PluginParameter;
     30import net.sf.basedb.core.VirtualColumn;
    3531import net.sf.basedb.core.plugin.AbstractAnalysisPlugin;
    3632import net.sf.basedb.core.plugin.Permissions;
     33import net.sf.basedb.core.query.Dynamic;
     34import net.sf.basedb.core.query.Expression;
     35import net.sf.basedb.core.query.Expressions;
     36import net.sf.basedb.core.query.Restriction;
     37import net.sf.basedb.core.query.Restrictions;
    3738
    3839import java.util.Collection;
    3940import java.util.EnumSet;
    4041import java.util.HashSet;
    41 import java.util.List;
    4242import java.util.Set;
    4343
    4444/**
    4545    @author Martin
     46    @version 1.0
     47   
    4648 
    4749 */
     
    4951  extends AbstractAnalysisPlugin
    5052{
    51 
    52   protected static final String REFERENCE_GROUP = "reference_group";
    5353 
    5454  private static final Set<Permissions> permissions = new HashSet<Permissions>();
    5555 
     56 
    5657  /**
    57     Parameter for the reference group of one or several assays.
    58     The first array is default if user doesn't make any choice.
     58    Expression used to get data from channel 1
     59  */
     60  protected Expression ch1 = Dynamic.column(VirtualColumn.channel(1));
     61 
     62  /**
     63    Expression used to get data from channel 2
    5964   */
    60   private PluginParameter<BioAssay> referenceGroupParameter = null;
     65  protected Expression ch2 = Dynamic.column(VirtualColumn.channel(1)); 
     66 
     67  /**
     68    Create restriction: column = :bioAssayColumn
     69  */
     70  protected Restriction bioAssayRestriction = Restrictions.eq(
     71      Dynamic.column(VirtualColumn.COLUMN),
     72      Expressions.parameter("bioAssayColumn")
     73    );
     74
    6175 
    6276  /**
    6377    Request use access to Experiment:s and read access to Job:s.
    6478  */
     79  @Override
    6580  public Collection<Permissions> getPermissions()
    6681  {
     
    7388    return permissions;
    7489  }
    75  
    76   /**
    77     Gets the plug-in parameter that holds the reference group.
    78       @param label
    79       @param description
    80       @return PluginParameter for a BioAssay
    81    */
    82   protected PluginParameter<BioAssay> getReferenceGroupParameter(List<BioAssay> assays, String label, String description)
    83   {
    84     if (referenceGroupParameter == null)
    85     {
    86       if (assays == null || assays.size() == 0) throw new InvalidUseOfNullException("assays");
    87       if (label == null) label = "Reference group";
    88       if (description == null)
    89       {
    90         description = "Select one or several bio assays to be used as a reference group. " +
    91             "The reference group is used to calculate a virtual array. ";
    92       }
    93       referenceGroupParameter = new PluginParameter<BioAssay>
    94       (
    95         REFERENCE_GROUP, label, description,
    96         new ItemParameterType<BioAssay>(BioAssay.class, null, true, 0, assays)
    97        
    98       );
    99     }
    100     return referenceGroupParameter;
    101   }
    102    
    103   /**
    104     Gets a list of those assays in the reference group
    105     which has been set in the job configuration.
    106       @param dc DbControl to use when accessing the database
    107       @return List<BioAssay>
    108    */
    109   @SuppressWarnings("unchecked")
    110   protected List<BioAssay> getReferenceGroup(DbControl dc)
    111   {
    112     List<BioAssay> refGroup = ((List<BioAssay>)job.getValues(REFERENCE_GROUP));
    113     for (BioAssay ba : refGroup)
    114     {
    115       dc.reattachItem(ba);
    116     }
    117     return refGroup;
    118   }
    11990}
  • plugins/base2/net.sf.basedb.normalizers/trunk/src/net/sf/basedb/plugins/AverageNormalization.java

    r730 r742  
    3030import net.sf.basedb.core.BioAssaySet;
    3131import net.sf.basedb.core.DbControl;
    32 import net.sf.basedb.core.DynamicResultIterator;
    3332import net.sf.basedb.core.DynamicSpotQuery;
    34 import net.sf.basedb.core.ItemQuery;
     33import net.sf.basedb.core.FloatParameterType;
     34import net.sf.basedb.core.InvalidUseOfNullException;
    3535import net.sf.basedb.core.Job;
    3636import net.sf.basedb.core.PluginParameter;
     
    3838import net.sf.basedb.core.RequestInformation;
    3939import net.sf.basedb.core.SpotBatcher;
     40import net.sf.basedb.core.StringParameterType;
    4041import net.sf.basedb.core.Transformation;
    41 import net.sf.basedb.core.Type;
    42 import net.sf.basedb.core.VirtualColumn;
    4342import net.sf.basedb.core.plugin.About;
    4443import net.sf.basedb.core.plugin.AboutImpl;
     
    4746import net.sf.basedb.core.plugin.Request;
    4847import net.sf.basedb.core.plugin.Response;
    49 import net.sf.basedb.core.query.Aggregations;
    50 import net.sf.basedb.core.query.Dynamic;
    51 import net.sf.basedb.core.query.Expression;
    52 import net.sf.basedb.core.query.Expressions;
    53 import net.sf.basedb.core.query.Restriction;
    54 import net.sf.basedb.core.query.Restrictions;
    55 import net.sf.basedb.core.query.Selects;
    56 import net.sf.basedb.core.query.SqlResult;
    5748import net.sf.basedb.core.signal.SignalHandler;
    5849import net.sf.basedb.core.signal.SignalReceivedException;
     
    6253import net.sf.basedb.util.Values;
    6354
    64 import java.sql.SQLException;
    6555import java.util.ArrayList;
    6656import java.util.Arrays;
     
    7363 
    7464    @author Martin
    75     @version 2.7
     65    @version 1.0
    7666 */
    7767public class AverageNormalization
     
    7969  implements InteractivePlugin, SignalTarget
    8070{
     71  private static final String yesOption = "yes";
     72  private static final String noOption = "no";
     73 
    8174  /**
    8275    Use to abort a running job
     
    8982  private RequestInformation configureJob = null;
    9083 
    91   /**
    92     Expressions used to get data
    93   */
    94   private Expression ch1 = Dynamic.column(VirtualColumn.channel(1));
    95  
    96   /**
    97     Create restriction: column = :bioAssayColumn
    98   */
    99   private Restriction bioAssayRestriction = Restrictions.eq(
    100       Dynamic.column(VirtualColumn.COLUMN),
    101       Expressions.parameter("bioAssayColumn")
    102     );
     84 
     85  private final PluginParameter<String> useGlobalAverageIntensityParameter = new PluginParameter<String>
     86    (
     87      "useGlobalAvgInt",
     88      "Use global average intensity",
     89      "Select if the global average intensity or an user defined value should be used as a reference value\n" +
     90      noOption + " = User defined value(defalut)\n" +
     91      yesOption + " = Global average intensity",
     92      new StringParameterType(null, noOption, true, 0, 0, 0, Arrays.asList(new String[] {noOption, yesOption}))
     93    );
     94 
     95  private final PluginParameter<Float> referenceValueParameter = new PluginParameter<Float>
     96  (
     97    "referenceValue",
     98    "Reference value",
     99    "Reference value to use when calculating the scaling factor. Default value is 100\n" +
     100    "This parameter cannot be null if 'Use global average intensity' is " + noOption + ".",
     101    new FloatParameterType(null, null, 100f, false)
     102  );
     103   
    103104 
    104105  private final About about = new AboutImpl
     
    154155        storeValue(job, request, ri.getParameter(CHILD_NAME));
    155156        storeValue(job, request, ri.getParameter(CHILD_DESCRIPTION));       
    156         storeValue(job, request, ri.getParameter(REFERENCE_GROUP));
    157         //TODO More normalization specific parameters??
     157        storeValue(job, request, ri.getParameter("useGlobalAverageIntensity"));
     158       
     159        // Reference value cannot be null if the global average intensity shouldn't be used.
     160        if (noOption.equals((String)request.getParameterValue("useGlobalAverageIntensity")) )
     161        {
     162          if (request.getParameterValue("referenceValue") != null)
     163          {
     164            storeValue(job, request, ri.getParameter("referenceValue"));
     165          }
     166          else
     167          {
     168            throw new InvalidUseOfNullException("referenceValue");
     169          }
     170        }
    158171       
    159172        response.setDone("Job configuration completed", Job.ExecutionTime.MEDIUM);
     
    211224        String childName = Values.getString((String)job.getValue(CHILD_NAME), source.getName());
    212225        String childDescription = (String)job.getValue(CHILD_DESCRIPTION);
    213         Job thisJob = getCurrentJob(dc);
    214        
    215         List<BioAssay> refGroup = getReferenceGroup(dc);
    216        
     226        Job currentJob = this.getCurrentJob(dc);
     227       
     228        boolean useGlobalAvgInt = yesOption.equals((String)job.getValue("useGlobalAverageIntensity"));
     229        float refValue;
     230       
     231        List<BioAssay> assays = source.getBioAssays().list(dc);;
     232        DynamicSpotQuery query = source.getSpotData();
     233       
     234        if (useGlobalAvgInt)
     235        {
     236          //FIXME Calculate the global average intensity
     237          refValue = 0;
     238        }
     239        else
     240        {
     241          refValue = (Float)job.getValue("referenceValue");         
     242        }
    217243        //Normalize the data
    218         int noChannels = source.getRawDataType().getChannels();
    219244        BioAssaySet child = null;
    220         switch (noChannels)
    221         {
    222           case 1:
    223             child = normalize(dc, source, thisJob, refGroup, progress);
    224             break;
    225 
    226         }
    227        
    228         if (child != null)
    229         {
    230           //Finish up
    231           child.setName(childName);
    232           child.setDescription(childDescription);
    233           dc.commit();
    234           int normalizedSpots = child.getNumSpots();
    235           if (progress != null) progress.display(100, normalizedSpots + " spots normalized\n");
    236           response.setDone(normalizedSpots + " spots normalized, " + (source.getNumSpots() - normalizedSpots) + " spots removed");
    237         }
     245       
     246        child = normalize(dc, source, currentJob, refValue, progress);
     247       
     248        //Finish up
     249        child.setName(childName);
     250        child.setDescription(childDescription);
     251        dc.commit();
     252        int normalizedSpots = child.getNumSpots();
     253        if (progress != null) progress.display(100, normalizedSpots + " spots normalized\n");
     254        response.setDone(normalizedSpots + " spots normalized, " + (source.getNumSpots() - normalizedSpots) + " spots removed");
     255       
    238256      }
    239257      catch (Throwable ex)
     
    247265    }
    248266  }
    249   /*
    250    ---------------------------------------------------------
    251   */
    252  
    253   /**
    254     Normalizes the data in a bio-assay set, using the average algorithm.
    255     Average algorithm uses a scale factor to calculate the normalized intensities.
    256     By dividing the average intensity of a virtual array(reference group) with
    257     the average intensity of all arrays in the data set.
    258     @param dc The DbControl, used for permission- and access control to the database
    259     @param source The bio-assay set with data to normalize.
    260     @param job Job that is running this plug-in.
    261     @param refGroup A list with one or several arrays, which should be used as a virtual array/reference group
    262     @param progress Progress reporter used to send back progress information back to the user.
    263    */
    264   public BioAssaySet normalize(DbControl dc, BioAssaySet source, Job job, List<BioAssay> refGroup, ProgressReporter progress)
    265   {
    266     //FIXME The normalization has no support for two-channel data.
    267     //FIXME Confirm normalization
    268     if (progress != null) progress.display(0, "Preparing normalization...");
    269    
    270     // Create transformation and prepare for the normalization
    271     Transformation t = source.newTransformation(job);
    272     t.setName(about.getName());
    273     dc.saveItem(t);
    274    
    275     BioAssaySet child = t.newProduct(null, "new", true);   
    276     dc.saveItem(child);
    277    
    278     SpotBatcher spotBatcher = child.getSpotBatcher();
    279 
    280     float scaleFactor = getScaleFactor(dc, refGroup, source, progress);
    281     // Get the number of spots to be normalized
    282     DynamicSpotQuery query = source.getSpotData();
    283     long totalNumSpots = query.count(dc);
    284     query.reset();
    285     long normalizedSpots = 0;
    286    
    287     // Restrictions for the ch1's value
    288     query.select(Dynamic.select(VirtualColumn.POSITION));
    289     query.select(Selects.expression(ch1, "ch1"));
    290     query.restrict(bioAssayRestriction);
    291    
    292     if (progress != null)
    293       progress.display((int)((normalizedSpots* 100L) / totalNumSpots),
    294                        normalizedSpots + " spots normalized");
    295    
    296     // Normalize data
    297     List<BioAssay> assays = source.getBioAssays().list(dc);
    298     try
    299     {
    300       for (BioAssay ba : assays)
    301       {
    302         checkInterrupted();
    303        
    304         short bioAssayColumn = ba.getDataCubeColumnNo();
    305         query.setParameter("bioAssayColumn", (int)bioAssayColumn, Type.INT);
    306         DynamicResultIterator it = query.iterate(dc);
    307         int positionIndex = it.getIndex(VirtualColumn.POSITION.getName());
    308         int ch1Index = it.getIndex("ch1");
    309        
    310         while (it.hasNext())
    311         {
    312           SqlResult r = it.next();
    313           float intensity = r.getFloat(ch1Index);
    314           int position = r.getInt(positionIndex);         
    315          
    316           spotBatcher.insert(bioAssayColumn, position, (intensity * scaleFactor));
    317           ++normalizedSpots;
    318         }
    319         it.close();
    320         spotBatcher.flush();
    321         if (progress != null)
    322           progress.display((int)((normalizedSpots * 100L) / totalNumSpots),
    323               normalizedSpots + " spots normalized");
    324       }
    325     }
    326     catch (SQLException ex)
    327     {
    328       throw new BaseException(ex);
    329     }
    330     return child;
    331   }
    332  
    333267 
    334268  private RequestInformation getConfiguredJobParameters()
     
    339273      if (configureJob == null)
    340274      {
    341         dc = sc.newDbControl();
    342275        BioAssaySet bas = getCurrentBioAssaySet(dc);
    343         ItemQuery<BioAssay> query = bas.getBioAssays();
    344         List<PluginParameter<?>> parameters = new ArrayList<PluginParameter<?>>(3);
     276        List<PluginParameter<?>> parameters = new ArrayList<PluginParameter<?>>();
    345277       
    346278        // Source and child bioassay set parameters
     
    349281        parameters.add(getChildDescriptionParameter(null, null, null));
    350282
    351         //Reference group
    352         parameters.add(getReferenceGroupParameter(query.list(dc)  ,null, null));
    353         //TODO More normalization specific parameters???
     283        parameters.add(useGlobalAverageIntensityParameter);
     284        parameters.add(referenceValueParameter);
    354285       
    355286        configureJob = new RequestInformation
     
    370301  }
    371302 
    372   private float getScaleFactor(DbControl dc, List<BioAssay> refGroup, BioAssaySet source, ProgressReporter progress)
    373   {
    374     float averageVirtualArray;
    375     float averageWholeGroup;
    376     float totSum = 0;
    377     long numOfSpots = 0;
    378    
    379     DynamicSpotQuery query = source.getSpotData();
    380     query.selectPermanent(Selects.expression(Aggregations.sum(ch1), "sumCh1"));
    381     query.restrictPermanent(bioAssayRestriction);
    382    
    383     //Get average intensity for the bio assays in reference group.     
    384     try
    385     {
    386       for (BioAssay ba : refGroup)
    387       {
    388         numOfSpots = numOfSpots + ba.getNumSpots();
    389         short bioAssayColumn = ba.getDataCubeColumnNo();
    390         query.setParameter("bioAssayColumn", (int)bioAssayColumn, Type.INT);
    391         DynamicResultIterator it = query.iterate(dc);
    392         int sumCh1Index = it.getIndex("sumCh1");
    393         SqlResult result = it.next();
    394         totSum = totSum + result.getFloat(sumCh1Index);
    395         it.close();
    396       }
    397     }
    398     catch (SQLException e)
    399     {
    400       throw new BaseException(e);
    401     }
    402     if (numOfSpots == 0) throw new BaseException("No spots in reference group");
    403    
    404     averageVirtualArray = totSum / numOfSpots;
    405     if (averageVirtualArray == 0) throw new BaseException("The average intensity is '0' for  the reference group");
    406     query.reset();
    407    
    408     // Get average intensity for the whole group
    409     List<BioAssay> assays = source.getBioAssays().list(dc);
    410     totSum = 0;
    411     numOfSpots = 0;
    412     try
    413     {
    414       for (BioAssay ba : assays)
    415       {
    416         numOfSpots = numOfSpots + ba.getNumSpots();
    417         short bioAssayColumn = ba.getDataCubeColumnNo();
    418         query.setParameter("bioAssayColumn", (int)bioAssayColumn, Type.INT);
    419         DynamicResultIterator it = query.iterate(dc);
    420         int sumCh1Index = it.getIndex("sumCh1");
    421         SqlResult result = it.next();
    422         totSum = totSum + result.getFloat(sumCh1Index);
    423         it.close();
    424       }
    425     }
    426     catch (SQLException e)
    427     {
    428       throw new BaseException(e);
    429     }
    430     if (numOfSpots == 0) throw new BaseException("No spots in selected bio assay set");
    431     averageWholeGroup = totSum / numOfSpots;   
    432     if (averageWholeGroup == 0) throw new BaseException("The average intensity is '0' for the whole group");
    433    
    434     float scaleFactor = averageVirtualArray / averageWholeGroup;
    435    
    436     return scaleFactor;
     303  private BioAssaySet normalize(DbControl dc, BioAssaySet source, Job job, float refValue,
     304      ProgressReporter progress)
     305  {
     306    // Create Transformation
     307    Transformation t = source.newTransformation(job);
     308    t.setName(about.getName());
     309    dc.saveItem(t);
     310   
     311    // Create the normalized bioassay set
     312    BioAssaySet child = t.newProduct(null, "new", true);
     313    dc.saveItem(child);
     314   
     315    // Batcher for inserting normalized data
     316    SpotBatcher batcher = child.getSpotBatcher();
     317   
     318    // Query to receive spot data
     319    DynamicSpotQuery query = configureQuery(source.getSpotData());
     320   
     321    // FIXME Do the normalization
     322    return null;
     323  }
     324 
     325  private DynamicSpotQuery configureQuery(DynamicSpotQuery query)
     326  {
     327    return query;
    437328  }
    438329}
Note: See TracChangeset for help on using the changeset viewer.