Changeset 729


Ignore:
Timestamp:
Jul 21, 2008, 4:42:06 PM (14 years ago)
Author:
Martin Svensson
Message:

References #117 Almost done with implementing the normalization algorithm for this plugin

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

Legend:

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

    r728 r729  
    4949{
    5050
    51   private static final String REFERENCE_GROUP = "reference_group";
     51  protected static final String REFERENCE_GROUP = "reference_group";
    5252 
    5353  private static final Set<Permissions> permissions = new HashSet<Permissions>();
     
    7373  }
    7474 
     75  /**
     76    Gets the plug-in parameter that holds the reference group.
     77      @param label
     78      @param description
     79      @return PluginParameter for a BioAssay
     80   */
    7581  protected PluginParameter<BioAssay> getReferenceGroupParameter(String label, String description)
    7682  {
     
    8086      if (description == null)
    8187      {
    82         description = "Select bio assays to be used as a reference group. " +
     88        description = "Select one or several bio assays to be used as a reference group. " +
    8389            "The reference group is used to calculate a virtual array. ";
    8490      }
  • plugins/base2/net.sf.basedb.normalizations/trunk/src/net/sf/basedb/plugins/AverageNormalization.java

    r728 r729  
    3030import net.sf.basedb.core.BioAssaySet;
    3131import net.sf.basedb.core.DbControl;
     32import net.sf.basedb.core.DynamicQuery;
     33import net.sf.basedb.core.DynamicResultIterator;
     34import net.sf.basedb.core.DynamicSpotQuery;
     35import net.sf.basedb.core.Job;
    3236import net.sf.basedb.core.PluginParameter;
    3337import net.sf.basedb.core.ProgressReporter;
    3438import net.sf.basedb.core.RequestInformation;
     39import net.sf.basedb.core.SpotBatcher;
     40import net.sf.basedb.core.Transformation;
     41import net.sf.basedb.core.Type;
     42import net.sf.basedb.core.VirtualColumn;
    3543import net.sf.basedb.core.plugin.About;
    3644import net.sf.basedb.core.plugin.AboutImpl;
     
    3947import net.sf.basedb.core.plugin.Request;
    4048import net.sf.basedb.core.plugin.Response;
     49import net.sf.basedb.core.query.Aggregations;
     50import net.sf.basedb.core.query.Dynamic;
     51import net.sf.basedb.core.query.Expression;
     52import net.sf.basedb.core.query.Expressions;
     53import net.sf.basedb.core.query.Orders;
     54import net.sf.basedb.core.query.Restriction;
     55import net.sf.basedb.core.query.Restrictions;
     56import net.sf.basedb.core.query.Selects;
     57import net.sf.basedb.core.query.SqlResult;
    4158import net.sf.basedb.core.signal.SignalHandler;
    4259import net.sf.basedb.core.signal.SignalReceivedException;
     
    4663import net.sf.basedb.util.Values;
    4764
     65import java.sql.SQLException;
    4866import java.util.ArrayList;
    4967import java.util.Arrays;
     
    7290  private RequestInformation configureJob = null;
    7391 
     92  /**
     93    Expressions used to get data
     94  */
     95  private Expression ch1 = Dynamic.column(VirtualColumn.channel(1));
     96 
     97  /**
     98    Create restriction: column = :bioAssayColumn
     99  */
     100  Restriction bioAssayRestriction = Restrictions.eq(
     101      Dynamic.column(VirtualColumn.COLUMN),
     102      Expressions.parameter("bioAssayColumn")
     103    );
    74104 
    75105  private final About about = new AboutImpl
     
    124154        // Child name, description and transformation
    125155        storeValue(job, request, ri.getParameter(CHILD_NAME));
    126         storeValue(job, request, ri.getParameter(CHILD_DESCRIPTION));
    127        
    128         //TODO Store Average Normalization specific parameters
     156        storeValue(job, request, ri.getParameter(CHILD_DESCRIPTION));       
     157        storeValue(job, request, ri.getParameter(REFERENCE_GROUP));
     158        //TODO More normalization specific parameters??
     159       
     160        response.setDone("Job configuration completed", Job.ExecutionTime.MEDIUM);
    129161      }
    130162      catch (SignalReceivedException signalException)
     
    181213        String childName = Values.getString((String)job.getValue(CHILD_NAME), source.getName());
    182214        String childDescription = (String)job.getValue(CHILD_DESCRIPTION);
    183        
    184         List<BioAssay> referenceGroup = getReferenceGroup(dc);
    185         //TODO Implement things specific for the algortihm.
     215        Job thisJob = getCurrentJob(dc);
     216       
     217        List<BioAssay> refGroup = getReferenceGroup(dc);
     218       
     219        //Normalize the data
     220        BioAssaySet child = normalize(dc, source, thisJob, refGroup, progress);
     221       
     222        //Finish up
     223        child.setName(childName);
     224        child.setDescription(childDescription);
     225        dc.commit();
     226        int normalizedSpots = child.getNumSpots();
     227        if (progress != null) progress.display(100, normalizedSpots + " spots normalized\n");
     228        response.setDone(normalizedSpots + " spots normalized, " + (source.getNumSpots() - normalizedSpots) + " spots removed");
    186229      }
    187230      catch (Throwable ex)
     
    198241   ---------------------------------------------------------
    199242  */
     243 
     244  public BioAssaySet normalize(DbControl dc, BioAssaySet source, Job job, List<BioAssay> refGroup, ProgressReporter progress)
     245  {
     246    if (progress != null) progress.display(0, "Preparing normalization...");
     247   
     248    // Create transformation and prepare for the normalization
     249    Transformation t = source.newTransformation(job);
     250    t.setName(about.getName());
     251    dc.saveItem(t);
     252   
     253    BioAssaySet child = t.newProduct(null, "new", true);   
     254    dc.saveItem(child);
     255   
     256    SpotBatcher spotBatcher = child.getSpotBatcher();
     257
     258    float scaleFactor = getScaleFactor(dc, refGroup, source, progress);
     259    // Get the number of spots to be normalized
     260    DynamicSpotQuery query = source.getSpotData();
     261    long totalNumSpots = query.count(dc);
     262    query.reset();
     263    long normalizedSpots = 0;
     264   
     265    // Restrictions for the ch1's value
     266    query.select(Dynamic.select(VirtualColumn.POSITION));
     267    query.select(Selects.expression(ch1, "ch1"));
     268    query.restrict(bioAssayRestriction);
     269   
     270    if (progress != null)
     271      progress.display((int)(normalizedSpots / totalNumSpots * 100),
     272                       normalizedSpots + " spots normalized");
     273   
     274    // Normalize data
     275    List<BioAssay> assays = source.getBioAssays().list(dc);
     276    try
     277    {
     278      for (BioAssay ba : assays)
     279      {
     280        checkInterrupted();
     281       
     282        short bioAssayColumn = ba.getDataCubeColumnNo();
     283        query.setParameter("bioAssayColumn", (int)bioAssayColumn, Type.INT);
     284        DynamicResultIterator it = query.iterate(dc);
     285        int positionIndex = it.getIndex(VirtualColumn.POSITION.getName());
     286        int ch1Index = it.getIndex("ch1");
     287       
     288        while (it.hasNext())
     289        {
     290          SqlResult r = it.next();
     291          float intensity = r.getFloat(ch1Index);
     292          int position = r.getInt(positionIndex);         
     293         
     294          spotBatcher.insert(bioAssayColumn, position, (intensity * scaleFactor));
     295          ++normalizedSpots;
     296        }
     297        it.close();
     298        spotBatcher.flush();
     299        if (progress != null)
     300          progress.display((int)((normalizedSpots * 100L) / totalNumSpots),
     301              normalizedSpots + " spots normalized");
     302      }
     303    }
     304    catch (SQLException ex)
     305    {
     306      throw new BaseException(ex);
     307    }
     308    return child;
     309  }
     310 
    200311 
    201312  private RequestInformation getConfiguredJobParameters()
     
    217328        //Reference group
    218329        parameters.add(getReferenceGroupParameter(null, null));
    219         //TODO Configure Average Normalization specific parameters.
     330        //TODO More normalization specific parameters???
    220331      }
    221332    }
     
    227338    return configureJob;
    228339  }
     340 
     341  private float getScaleFactor(DbControl dc, List<BioAssay> refGroup, BioAssaySet source, ProgressReporter progress)
     342  {
     343    float averageVirtualArray;
     344    float averageWholeGroup;
     345   
     346    DynamicSpotQuery query = source.getSpotData();
     347    query.selectPermanent(Selects.expression(Aggregations.sum(ch1), "sumCh1"));
     348    query.restrictPermanent(bioAssayRestriction);
     349   
     350    //Get average intensity for the bio assays in reference group.   
     351    float totSum = 0;
     352    long numOfSpots = 0;
     353    try
     354    {
     355      for (BioAssay ba : refGroup)
     356      {
     357        numOfSpots = numOfSpots + ba.getNumSpots();
     358        short bioAssayColumn = ba.getDataCubeColumnNo();
     359        query.setParameter("bioAssayColumn", (int)bioAssayColumn, Type.INT);
     360        DynamicResultIterator it = query.iterate(dc);
     361        int sumCh1Index = it.getIndex("sumCh1");
     362        SqlResult result = it.next();
     363        totSum = totSum + result.getFloat(sumCh1Index);
     364        it.close();
     365      }     
     366    }
     367    catch (SQLException e)
     368    {
     369      throw new BaseException(e);
     370    }   
     371    averageVirtualArray = totSum / numOfSpots;
     372    query.reset();
     373   
     374    // Get average intensity for the whole group
     375    List<BioAssay> assays = source.getBioAssays().list(dc);
     376    totSum = 0;
     377    numOfSpots = 0;
     378    try
     379    {
     380      for (BioAssay ba : assays)
     381      {
     382        numOfSpots = numOfSpots + ba.getNumSpots();
     383        short bioAssayColumn = ba.getDataCubeColumnNo();
     384        query.setParameter("bioAssayColumn", (int)bioAssayColumn, Type.INT);
     385        DynamicResultIterator it = query.iterate(dc);
     386        int sumCh1Index = it.getIndex("sumCh1");
     387        SqlResult result = it.next();
     388        totSum = totSum + result.getFloat(sumCh1Index);
     389        it.close();
     390      }
     391    }
     392    catch (SQLException e)
     393    {
     394      throw new BaseException(e);
     395    }
     396    averageWholeGroup = totSum / numOfSpots;
     397   
     398   
     399    float scaleFactor = averageVirtualArray / averageWholeGroup;
     400   
     401    return scaleFactor;
     402  }
    229403}
Note: See TracChangeset for help on using the changeset viewer.