Changeset 1206


Ignore:
Timestamp:
Mar 19, 2010, 3:09:43 PM (12 years ago)
Author:
Jari Häkkinen
Message:

Addresses #248 and #252. File contains relevant information now. Mean is fixed, median needs to be fixed.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • plugins/base2/net.sf.basedb.illumina/trunk/src/net/sf/basedb/illumina/plugins/BackgroundCorrection.java

    r1205 r1206  
    7878import java.util.Collection;
    7979import java.util.EnumSet;
     80import java.util.HashMap;
    8081import java.util.HashSet;
     82import java.util.Iterator;
    8183import java.util.List;
    8284import java.util.Set;
     85import java.util.TreeSet;
     86import java.util.Vector;
    8387
    8488
     
    168172  private ThreadSignalHandler signalHandler;
    169173
     174  /*
     175    Variables to keep track of spot data for negative controls. The
     176    information must be collected before writing it to file in matrix
     177    format.
     178
     179    Spot data collected in the negative control queries are stored in
     180    the SData class.
     181
     182    The allNegPos TreeSet is used to collect positions of all negative
     183    controls occuring in the queries (bioassays may have different
     184    sets of negative control probes due to filtering and
     185    deletion). The key is the position of the spot.
     186
     187    The negSData Vector keeps track of spot data for negative control
     188    spots per bioassay. The HashMap is just a map from the position to
     189    the data stored about that position.
     190  */
     191  private class SData
     192  {
     193    public String externalId;
     194    public float intensity;
     195
     196    private int ch1Index;
     197    private int externalIdIndex;
     198    private int positionIndex;
     199
     200    /**
     201       This constructor will not create a fully functional SData
     202       object, i.e., extractData cannot be called from objects created
     203       with this constructor. For SData objects to be usable for
     204       SqlResult you must use the SData(DynamicResultIterator)
     205       constructor.
     206     */
     207    public SData(SData sd)
     208    {
     209      this.externalId=sd.externalId;
     210      this.intensity=sd.intensity;
     211    }
     212    public SData(DynamicResultIterator dri) throws SQLException
     213    {
     214      ch1Index = dri.getIndex(VirtualColumn.channelIntensity(1).getName());
     215      externalIdIndex = dri.getIndex("externalId");
     216      positionIndex = dri.getIndex(VirtualColumn.POSITION.getName());
     217    }
     218    public int extractData(SqlResult sr) throws SQLException
     219    {
     220      intensity  = sr.getFloat(ch1Index);
     221      externalId = new String(sr.getString(externalIdIndex));
     222      return sr.getInt(positionIndex);
     223    }
     224  }
     225  private TreeSet<Integer> allNegPos=null;
     226  private Vector<HashMap<Integer,SData>> negSData=null;
    170227
    171228  // From the InteractivePlugin interface
     
    346403
    347404
    348   private float mean(DynamicResultIterator dri, int index) throws SQLException
     405  private float mean(DynamicResultIterator dri, int index, Boolean saveBG)
     406    throws SQLException
    349407  {
    350408    float mean=0;
    351409    int nofSpots=0;
     410    HashMap<Integer,SData> hmap=(saveBG ? new HashMap<Integer,SData>() : null);
     411    SData sdata=new SData(dri);
     412
    352413    while (dri.hasNext())
    353414    {
    354       mean+=dri.next().getFloat(index);
     415      SqlResult sr = dri.next();
     416      mean+=sr.getFloat(index);
    355417      ++nofSpots;
    356     }
     418      if (saveBG)
     419      {
     420        Integer position=sdata.extractData(sr);
     421        allNegPos.add(position);
     422        hmap.put(position,new SData(sdata));
     423      }
     424    }
     425
     426    if (saveBG) negSData.add(hmap);
    357427    dri.close();
     428
    358429    return (nofSpots!=0 ? mean/nofSpots : 0);
    359430  }
    360431
    361432
    362   private float median(DynamicResultIterator dri, int index) throws SQLException
     433  private float median(DynamicResultIterator dri, int index, Boolean saveBG)
     434    throws SQLException
    363435  {
    364436    long count=dri.getTotalCount();
     
    408480      dc.saveItem(child);
    409481
    410       PrintWriter bgWriter = null;
    411       Boolean saveBG = (Boolean)job.getValue(saveBgParameter.getName());
    412       if (saveBG)
    413       {
    414         Directory expDir=source.getExperiment().getDirectory();
    415         File bgData=File.getFile(dc, expDir,
    416                                  about.getName() + "_backgroundProbes_" +
    417                                  thisJob.getId() + ".txt", true);
    418         bgData.setLocation(Location.PRIMARY);
    419         bgData.setMimeType("text/plain");
    420         dc.saveItem(bgData);
    421 
    422         // Associate files in a directory with transformation
    423         String root = expDir.getPath().toString();
    424         AnyToAny ata = AnyToAny.getNew(dc, t, bgData,
    425                                        bgData.getPath().toString(), false);
    426         dc.saveItem(ata);
    427 
    428         bgWriter = new PrintWriter(bgData.getUploadStream(false,true));
    429       }
    430 
    431482      // Create restriction: negative control beads only (background
    432483      // spots)
     
    456507      }
    457508
     509      // setup for optionally saving background information in a file
     510      PrintWriter bgWriter = null;
     511      Boolean saveBG = (Boolean)job.getValue(saveBgParameter.getName());
     512      if (saveBG)
     513      {
     514        Directory expDir=source.getExperiment().getDirectory();
     515        File bgData=File.getFile(dc, expDir,
     516                                 about.getName() + "_backgroundProbes_" +
     517                                 thisJob.getId() + ".txt", true);
     518        bgData.setLocation(Location.PRIMARY);
     519        bgData.setMimeType("text/plain");
     520        dc.saveItem(bgData);
     521
     522        // Associate files in a directory with transformation
     523        String root = expDir.getPath().toString();
     524        String ataName=bgData.getPath().toString().replace(root,"linkTo");
     525        AnyToAny ata = AnyToAny.getNew(dc, t, bgData, ataName, false);
     526        dc.saveItem(ata);
     527
     528        bgWriter = new PrintWriter(bgData.getUploadStream(false,true));
     529        bgWriter.println("# File generated by plug-in: " + about.getName());
     530        bgWriter.println("#\n# Background based on " +
     531                         ( method.equals("Illumina BeadStudio median") ?
     532                           "median" : "mean" ) +
     533                         " expression of background probes.");
     534        bgWriter.println("# The probes considered to be background probes are");
     535        bgWriter.println("# listed below in the expression matrix.");
     536        bgWriter.println("#\n# Assay name: Background expression level");
     537
     538        // Initialiaze negative control spot data collection support
     539        allNegPos=new TreeSet<Integer>();
     540        //  negSData=new Vector<HashMap<Integer,SData>>(size); done below
     541        //  when number of assays is known.
     542
     543        // add reporter externalId to the query
     544        query.joinReporters(JoinType.LEFT);
     545        query.select(Dynamic.selectReporter("externalId"));
     546      }
     547
    458548      try
    459549      {
     
    461551        // Caluclate one bioassay at a time
    462552        List<BioAssay> assays = source.getBioAssays().list(dc);
     553        // Initialiaze negative control spot data collection support
     554        if (saveBG) negSData=new Vector<HashMap<Integer,SData>>(assays.size());
    463555        float[] backgroundCh1=new float[assays.size()];
    464556        int assayNo=0;
     
    473565          backgroundCh1[assayNo]=
    474566            ( method.equals("Illumina BeadStudio median") ?
    475               median(dri,ch1Index) : mean(dri,ch1Index) );
    476 
    477           if (saveBG) bgWriter.println(assay.getName() + ": " + backgroundCh1[assayNo]);
     567              median(dri,ch1Index,saveBG) : mean(dri,ch1Index,saveBG) );
     568
     569          if (saveBG) bgWriter.println("# " + assay.getName() + ": " +
     570                                       backgroundCh1[assayNo]);
    478571
    479572          ++assayNo;
    480573        }
    481574        query.reset();
    482         if (saveBG) bgWriter.close();
     575
     576        // Optionally write information to negative control file.
     577        if (saveBG)
     578        {
     579          bgWriter.println("#\n# Number of assays: " + negSData.size());
     580          bgWriter.println("# Number of spots : " + allNegPos.size());
     581          bgWriter.print("#\n# Position\tExternalId");
     582          for (BioAssay assay : assays) bgWriter.print("\t"+assay.getName());
     583          bgWriter.println();
     584
     585          // write expression matrix
     586          Iterator<Integer> position=allNegPos.iterator();
     587          while (position.hasNext())
     588          {
     589            int pos=position.next();
     590            String externalId=null;
     591            for (int i=0; i<assays.size(); ++i)
     592            {
     593              SData sdata=negSData.get(i).get(pos);
     594              if (sdata!=null)
     595              {
     596                externalId=sdata.externalId;
     597                break;
     598              }
     599            }
     600            bgWriter.print(pos + "\t" + externalId);
     601            for (int i=0; i<assays.size(); ++i)
     602            {
     603              SData sdata=negSData.get(i).get(pos);
     604              bgWriter.print("\t" + ((sdata!=null) ? sdata.intensity : ""));
     605            }
     606            bgWriter.println();
     607          }
     608
     609          // cleanup negative control file creation support
     610          bgWriter.close();
     611          allNegPos=null;   // tell GC that allNegPos   is not needed anymore
     612          negSData=null; // tell GC that negSpotdata is not needed anymore
     613        }
    483614
    484615        int nofAssays=assayNo;
Note: See TracChangeset for help on using the changeset viewer.