Changeset 4068


Ignore:
Timestamp:
Dec 17, 2007, 9:02:44 AM (15 years ago)
Author:
Nicklas Nordborg
Message:

Fixes #880: Exclude reporters in Lowess

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/plugins/core/net/sf/basedb/plugins/LowessNormalization.java

    r4062 r4068  
    3333import net.sf.basedb.core.DynamicSpotQuery;
    3434import net.sf.basedb.core.FloatParameterType;
     35import net.sf.basedb.core.Include;
    3536import net.sf.basedb.core.IntegerParameterType;
    3637import net.sf.basedb.core.Item;
     38import net.sf.basedb.core.ItemParameterType;
     39import net.sf.basedb.core.ItemQuery;
    3740import net.sf.basedb.core.Job;
    3841import net.sf.basedb.core.Permission;
    3942import net.sf.basedb.core.PluginParameter;
    4043import net.sf.basedb.core.ProgressReporter;
     44import net.sf.basedb.core.ReporterList;
    4145import net.sf.basedb.core.RequestInformation;
    4246import net.sf.basedb.core.SpotBatcher;
     
    5559import net.sf.basedb.core.query.Expression;
    5660import net.sf.basedb.core.query.Expressions;
     61import net.sf.basedb.core.query.Hql;
     62import net.sf.basedb.core.query.JoinType;
    5763import net.sf.basedb.core.query.Orders;
    5864import net.sf.basedb.core.query.Restriction;
     
    6066import net.sf.basedb.core.query.Selects;
    6167import net.sf.basedb.core.query.SqlResult;
     68import net.sf.basedb.core.query.WhenStatement;
    6269import net.sf.basedb.util.Values;
    6370
     
    103110    "method addressing single and multiple slide systematic " +
    104111    "variation. Nucleic Acids Res 2002, 30:e15.",
    105     "2.5",
     112    "2.6",
    106113    "2006, Base 2 development team",
    107114    null,
     
    184191      permissions.add(new Permissions(Item.EXPERIMENT, null, EnumSet.of(Permission.USE)));
    185192      permissions.add(new Permissions(Item.JOB, null, EnumSet.of(Permission.READ)));
     193      permissions.add(new Permissions(Item.REPORTERLIST, null, EnumSet.of(Permission.READ)));
    186194    }
    187195    return permissions;
     
    204212        float fitFraction = (Float) job.getValue(fitFractionParameter.getName());
    205213        int iterations = (Integer) job.getValue(iterParameter.getName());
     214        ReporterList excludeReporters = (ReporterList)job.getValue("excludeReporters");
    206215        Job thisJob = getCurrentJob(dc);
    207216       
    208         BioAssaySet child = normalize(dc, source, thisJob, fitFraction, delta, iterations, blockGroupSize, progress);
     217        BioAssaySet child = normalize(dc, source, thisJob, fitFraction, delta, iterations,
     218          blockGroupSize, excludeReporters, progress);
    209219        child.setName(childName);
    210220        child.setDescription(childDescription);
     
    291301        storeValue(job, request, iterParameter);
    292302        storeValue(job, request, blockGroupParameter);
     303        storeValue(job, request, ri.getParameter("excludeReporters"));
    293304        response.setDone("Job configuration complete", Job.ExecutionTime.SHORT);
    294305      }
     
    301312  // -------------------------------------------
    302313
     314  /**
     315    Normalise the source bioassay set using LOWESS normalization.
     316    All reporters are included.
     317    @since 2.5
     318    @see #normalize(DbControl, BioAssaySet, Job, float, float, int, int, ReporterList, ProgressReporter)
     319  */
     320  public BioAssaySet normalize(DbControl dc, BioAssaySet source, Job job, float fitFraction, float delta, int iterations, int blockGroupSize, ProgressReporter progress)
     321  {
     322    return normalize(dc, source, job, fitFraction, delta, iterations, blockGroupSize, null, progress);
     323  }
     324 
     325 
    303326  /**
    304327    Normalise the source bioassay set using LOWESS normalization.
     
    311334    @param blockGroupSize The number of blocks to group and normalise at the same time,
    312335      if <=0, all blocks on a bioassay are grouped to a single dataset
     336    @param excludeReporters A reporter list containing reporters that should not be
     337      used in when normalizing, or null to use all spots
    313338    @param progress Progress reporter used by the caller to keep track of the progress. Null is allowed   
    314339    @return The normalized bioassayset
    315340    @since 2.5
    316341  */
    317   public BioAssaySet normalize(DbControl dc, BioAssaySet source, Job job, float fitFraction, float delta, int iterations, int blockGroupSize, ProgressReporter progress)
     342  public BioAssaySet normalize(DbControl dc, BioAssaySet source, Job job, float fitFraction, float delta, int iterations, int blockGroupSize, ReporterList excludeReporters, ProgressReporter progress)
    318343  {
    319344    if (progress != null) progress.display(0, "Preparing to normalize...");
     
    355380    if (progress != null) progress.display((int)(normalizedSpots / numSpots * 100), normalizedSpots + " spots normalized");
    356381   
    357     // Create query to retrieve spot data: COLUMN, POSITION, ch1, ch2, block
     382    // If some reporters should be excluded, create an expression that:
     383    // = 0 if spot's reporter is not present in list, = 1 if it is
     384    Expression isPresent = Expressions.integer(0);
     385    if (excludeReporters != null)
     386    {
     387      query.joinReporterList(excludeReporters, JoinType.LEFT);
     388      isPresent = Expressions.caseWhen(isPresent,
     389        new WhenStatement(Dynamic.isPartOf(excludeReporters), Expressions.integer(1)));
     390    }
     391   
     392    // Create query to retrieve spot data: COLUMN, POSITION, ch1, ch2, block, present
    358393    // We use a parameter to restrict the query to return data for one bioassay at a time
     394    // The 'exclude' value is 0 to not exclude and 1 to exclude a spot
    359395    query.select(Dynamic.select(VirtualColumn.POSITION));
    360396    query.select(Selects.expression(ch1, "ch1"));
    361397    query.select(Selects.expression(ch2, "ch2"));
    362398    query.select(Selects.expression(block, "block"));
     399    query.select(Selects.expression(isPresent, "present"));
    363400    query.restrict(bioAssayRestriction);
    364401    query.order(Orders.asc(block));
     
    384421        int ch2Index = it.getIndex("ch2");
    385422        int blockIndex = it.getIndex("block");
     423        int presentIndex = it.getIndex("present");
    386424       
    387425        // Copy bioassay data to SpotData objects
     
    390428          SqlResult r = it.next();
    391429          SpotData spot = new SpotData(r.getInt(positionIndex),
    392             r.getFloat(ch1Index), r.getFloat(ch2Index), r.getInt(blockIndex));
     430            r.getFloat(ch1Index), r.getFloat(ch2Index), r.getInt(blockIndex),
     431            r.getInt(presentIndex) == 1);
    393432          data.add(spot);
    394433        }
     
    471510        parameters.add(iterParameter);
    472511        parameters.add(blockGroupParameter);
     512        PluginParameter<ReporterList> excludeReporters =
     513          getExcludeReportersParameters(dc);
     514        if (excludeReporters != null) parameters.add(excludeReporters);
    473515 
    474516        configureJob = new RequestInformation
     
    488530  }
    489531
     532  private PluginParameter<ReporterList> getExcludeReportersParameters(DbControl dc)
     533  {
     534    // Load reporter types and initialise the reporterTypeParameter
     535    ItemQuery<ReporterList> query = ReporterList.getQuery();
     536    query.order(Orders.asc(Hql.property("name")));
     537    query.include(Include.MINE, Include.SHARED, Include.IN_PROJECT);
     538    List<ReporterList> reporterLists = new ArrayList<ReporterList>(query.list(dc));
     539    if (reporterLists.size() == 0) return null;
     540   
     541    ItemParameterType<ReporterList> reporterListType =
     542      new ItemParameterType<ReporterList>(ReporterList.class, null, false, 1, reporterLists);
     543    PluginParameter<ReporterList> reporterListParameter = new PluginParameter<ReporterList>(
     544      "excludeReporters",
     545      "Exclude reporters",
     546      "Spots with reporters in the selected list will not be used in the lowess calculations. " +
     547      "If no list is selected all spots are used.",
     548      reporterListType);
     549    return reporterListParameter;
     550  }
     551 
    490552  private static List<Double> lowess(List<SpotData> data, double f, int iter, double delta)
    491553  {
     
    591653    for (int j = 0; j < data.size(); j++)
    592654    {
    593       double localW = w.get(j);
    594       double localA = data.get(j).A;
    595       double localM = data.get(j).M;
    596       sumA += localA * localW;
    597       sumM += localM * localW;
    598       sumAA += localA * localA * localW;
    599       sumAM += localA * localM * localW;
    600       sumW += localW;
     655      SpotData spot = data.get(j);
     656      if (!spot.exclude)
     657      {
     658        double localW = w.get(j);
     659        double localA = spot.A;
     660        double localM = spot.M;
     661        sumA += localA * localW;
     662        sumM += localM * localW;
     663        sumAA += localA * localA * localW;
     664        sumAM += localA * localM * localW;
     665        sumW += localW;
     666      }
    601667    }
    602668    if (sumW <= 0)
     
    634700    for (int i = 0; i < data.size(); i++)
    635701    {
    636       double A2 = data.get(i).A;
    637       double distance = Math.abs(A1 - A2) * invRadius;
    638       w.add((distance < 1 ? Math.pow(1D - Math.pow(Math.abs(distance), 3), 3) : 0) * wFit.get(i));
     702      if (!data.get(i).exclude)
     703      {
     704        double A2 = data.get(i).A;
     705        double distance = Math.abs(A1 - A2) * invRadius;
     706        w.add((distance < 1 ? Math.pow(1D - Math.pow(Math.abs(distance), 3), 3) : 0) * wFit.get(i));
     707      }
     708      else
     709      {
     710        w.add(0.0);
     711      }
    639712    }
    640713    return w;
     
    650723    final double A;
    651724    final int block;
     725    final boolean exclude;
    652726    static final double INV_LN2 = 1 / Math.log(2);
    653727
    654     public SpotData(int position, float ch1, float ch2, int block)
     728    public SpotData(int position, float ch1, float ch2, int block, boolean exclude)
    655729    {
    656730      this.position = position;
     
    658732      this.ch2 = ch2;
    659733      this.block = block;
     734      this.exclude = exclude;
    660735      this.M = Math.log(ch1 / ch2) * INV_LN2;
    661736      this.A = Math.log10(Math.sqrt(ch1 * ch2));
Note: See TracChangeset for help on using the changeset viewer.