Changeset 3764


Ignore:
Timestamp:
Aug 13, 2010, 4:34:01 PM (13 years ago)
Author:
Fredrik Levander
Message:

Refs #614. Updates in feature hit matching. Allowing matching with other files of same local sample id. Corrected retention time matching with tolerance. Added form for settings.

Location:
trunk
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/client/servlet/src/org/proteios/action/feature/CreateFeatureHitMatchJob.java

    r3445 r3764  
    2828package org.proteios.action.feature;
    2929
    30 import org.proteios.Context;
    31 import org.proteios.ContextEnabled;
    32 import org.proteios.TableToolbarContext;
    3330import org.proteios.action.ProteiosAction;
    3431import org.proteios.action.job.ListJobs;
     32import org.proteios.core.BooleanParameterType;
    3533import org.proteios.core.DbControl;
    36 import org.proteios.core.Feature;
     34import org.proteios.core.FloatParameterType;
    3735import org.proteios.core.ItemFactory;
    3836import org.proteios.core.ItemParameterType;
     
    4341import se.lu.thep.waf.ActionException;
    4442import se.lu.thep.waf.constraints.InvalidParameterValue;
    45 
    46 import java.util.ArrayList;
    47 import java.util.List;
     43import se.lu.thep.waf.constraints.VBoolean;
     44import se.lu.thep.waf.constraints.VFloat;
    4845
    4946/**
     
    5249public class CreateFeatureHitMatchJob
    5350    extends ProteiosAction<CreateFeatureHitMatchJob>
    54     implements ContextEnabled
    5551{
     52
     53  public static final VFloat VTOLERANCE = new VFloat("tol", true);
     54  public static final VFloat VSTARTTOL = new VFloat("starttol", true);
     55  public static final VFloat VENDTOL = new VFloat("endtol", true);
     56  public static final VBoolean VSECONDARY = new VBoolean("isSecondary", false);
     57 
    5658  /*
    5759   * (non-Javadoc)
     
    6971    Project project = isProjectActive(dc);
    7072    ItemFactory factory = getItemFactory(dc);
     73    Float tolerance = getValidFloat(VTOLERANCE);
     74    Float startTimeTol = getValidFloat(VSTARTTOL);
     75    Float endTimeTol = getValidFloat(VENDTOL);
     76    Boolean isSecondary = getValidBoolean(VSECONDARY);
     77    if (isSecondary == null)
     78      isSecondary = new Boolean(false);
    7179    /***********************************************************************
    7280     * Create jobs
     
    96104    job.setParameterValue("project", projectParam, project);
    97105    // File parameters
     106    job.setParameterValue("tolerance", new FloatParameterType(), tolerance);
     107    job.setParameterValue("endTimeTol", new FloatParameterType(), endTimeTol);
     108    job.setParameterValue("startTimeTol", new FloatParameterType(), startTimeTol);
     109    job.setParameterValue("secondary", new BooleanParameterType(),
     110      isSecondary);
    98111    job.setName("Match features and hits");
    99     job.setDescription("Matching in project" + project);
     112    job.setDescription("Matching in project:" + project+ ", tolerance:"+tolerance + ", secondary:"+isSecondary);
    100113    dc.saveItem(job);
    101114    dc.commit();
     
    107120  }
    108121
    109   private static List<Context> contexts = new ArrayList<Context>(1);
    110   static
    111   {
    112     TableToolbarContext btn = new TableToolbarContext("match",
    113       "MatchFeaturesHits", CreateFeatureHitMatchJob.class, Feature.class);
    114     contexts.add(btn);
    115   }
    116122
    117 
    118   public List<Context> listContexts()
    119   {
    120     return contexts;
    121   }
    122123}
  • trunk/plugin/src/org/proteios/plugins/FeatureHitMatcher.java

    r3694 r3764  
    115115      int count = 0;
    116116      DbControl dc = sc.newDbControl();
     117      tolerance = ((Float) job.getValue("tolerance")).doubleValue();
     118      endTimeTol = ((Float) job.getValue("endTimeTol")).doubleValue();
     119      startTimeTol = ((Float) job.getValue("startTimeTol")).doubleValue();
    117120      Project project = (Project) job.getValue("project");
     121      boolean matchRelated = ((Boolean) job.getValue("secondary"))
     122        .booleanValue();
    118123      /* Check that the feature file hasn't been imported already */
    119124      QueryFactory qf = new QueryFactory();
     
    130135            .property("msFile"), Hql.entity(currentMsFile)));
    131136          log.debug("Matching for file:" + currentMsFile);
    132           List<Hit> hits = retrieveHits(currentMsFile, project, dc);
     137          List<Hit> hits = retrieveHits(currentMsFile, project, dc,
     138            matchRelated);
    133139          List<Feature> features = featureQuery.list(dc);
    134           for (Feature f : features)
    135           {
    136             List<Hit> potential = new ArrayList<Hit>();
    137             for (Hit h : hits)
    138             {
    139               double hitMz = h.getExperimentalMassInDaltons() / h
    140                 .getCharge() + MONOISOTOPIC_PROTON_MASS;
    141               if ((Math.abs(f.getMassToChargeRatio() - hitMz) < tolerance) && h
    142                 .getCharge().equals(f.getChargeState()))
    143               {
    144                 log.debug("m/z matching:" + f
    145                   .getMassToChargeRatio() + " hit:" + hitMz);
    146                 if (h.getRetentionTimeInMinutes() >= f
    147                   .getStartRetentionTimeInMinutes() && h
    148                   .getRetentionTimeInMinutes() <= f
    149                   .getEndRetentionTimeInMinutes())
    150                 {
    151                   /* A match! */
    152                   log
    153                     .debug("Matched hit RT:" + h
    154                       .getRetentionTimeInMinutes() + " with feature RT:" + f
    155                       .getApexRetentionTimeInMinutes());
    156                   count++;
    157                   dc.reattachItem(f);
    158                   dc.reattachItem(h);
    159                   if (f.getPeptideSequence() == null)
    160                   {
    161                     if (h.getFeature() == null || (h
    162                       .getPrecursorQuantity() < f
    163                       .getApexIntensity()))
    164                     {
    165                       h.setPrecursorQuantity(f
    166                         .getApexIntensity());
    167                     }
    168                     f
    169                       .setPeptideSequence(h
    170                         .getDescription());
    171                     f.addHit(h);
    172                   }
    173                   else
    174                   {
    175                     /* TODO chose the best match instead */
    176                     /*
    177                      * Use the feature with the highest
    178                      * intensity
    179                      */
    180                     if (h.getPrecursorQuantity() == null || (h
    181                       .getPrecursorQuantity() < f
    182                       .getApexIntensity()))
    183                     {
    184                       h.setPrecursorQuantity(f
    185                         .getApexIntensity());
    186                     }
    187                     f.addHit(h);
    188                   }
    189                 }
    190                 // Check for potential matches just outside the
    191                 // feature
    192                 else if ((f.getStartRetentionTimeInMinutes() - h
    193                   .getRetentionTimeInMinutes()) < startTimeTol || (h
    194                   .getRetentionTimeInMinutes() - f
    195                   .getEndRetentionTimeInMinutes()) < endTimeTol)
    196                 {
    197                   if (h.getFeature() == null || h
    198                     .getDescription().equals(
    199                       f.getPeptideSequence()))
    200                   {
    201                     potential.add(h);
    202                   }
    203                 }
    204 
    205               }
    206             }
    207             // Match potential matches if they have not been matched
    208             // before
    209             if (f.getPeptideSequence() == null)
    210             {
    211               for (Hit h : potential)
    212               {
    213                 if (h.getFeature() == null)
    214                 {
    215                   dc.reattachItem(h);
    216                   dc.reattachItem(f);
    217                   if (h.getPrecursorQuantity() == null || (h
    218                     .getPrecursorQuantity() < f
    219                     .getApexIntensity()))
    220                   {
    221                     h.setPrecursorQuantity(f
    222                       .getApexIntensity());
    223                     f.addHit(h);
    224                     f
    225                       .setPeptideSequence(h
    226                         .getDescription());
    227                   }
    228                 }
    229               }
    230             }
    231           }
     140          count += matchFeatures(features, hits, dc, tolerance,
     141            startTimeTol, endTimeTol);
    232142          featureQuery.reset();
    233143        }
     
    252162
    253163
    254   private List<Hit> retrieveHits(File msFile, Project project, DbControl dc)
     164  private int matchFeatures(List<Feature> features, List<Hit> hits,
     165      DbControl dc, double tolerance, double startTimeTol,
     166      double endTimeTol)
     167  {
     168    log.debug("Features to match:" + features.size());
     169    log.debug("Hits to match:" + hits.size());
     170    int count = 0;
     171    for (Feature f : features)
     172    {
     173      List<Hit> potential = new ArrayList<Hit>();
     174      for (Hit h : hits)
     175      {
     176        double hitMz = h.getExperimentalMassInDaltons() / h.getCharge() + MONOISOTOPIC_PROTON_MASS;
     177        if ((Math.abs(f.getMassToChargeRatio() - hitMz) < tolerance) && h
     178          .getCharge().equals(f.getChargeState()))
     179        {
     180          log
     181            .debug("m/z matching:" + f.getMassToChargeRatio() + " hit:" + hitMz);
     182          if (h.getRetentionTimeInMinutes() >= f
     183            .getStartRetentionTimeInMinutes() && h
     184            .getRetentionTimeInMinutes() <= f
     185            .getEndRetentionTimeInMinutes())
     186          {
     187            /* A match! */
     188            log
     189              .debug("Matched hit RT:" + h
     190                .getRetentionTimeInMinutes() + " with feature RT:" + f
     191                .getApexRetentionTimeInMinutes());
     192            count++;
     193            dc.reattachItem(f);
     194            dc.reattachItem(h);
     195            if (f.getPeptideSequence() == null)
     196            {
     197              if (h.getFeature() == null || (h
     198                .getPrecursorQuantity() < f.getApexIntensity()))
     199              {
     200                h.setPrecursorQuantity(f.getApexIntensity());
     201              }
     202              f.setPeptideSequence(h.getDescription());
     203              f.addHit(h);
     204            }
     205            else
     206            {
     207              /* TODO chose the best match instead */
     208              /*
     209               * Use the feature with the highest intensity
     210               */
     211              if (h.getPrecursorQuantity() == null || (h
     212                .getPrecursorQuantity() < f.getApexIntensity()))
     213              {
     214                h.setPrecursorQuantity(f.getApexIntensity());
     215              }
     216              f.addHit(h);
     217            }
     218          }
     219          // Check for potential matches just outside the
     220          // feature
     221          else if ((f.getStartRetentionTimeInMinutes() - startTimeTol) <= h
     222            .getRetentionTimeInMinutes() && h
     223            .getRetentionTimeInMinutes() <= (f
     224            .getEndRetentionTimeInMinutes() + endTimeTol))
     225          {
     226            if (h.getFeature() == null || h.getDescription()
     227              .equals(f.getPeptideSequence()))
     228            {
     229              potential.add(h);
     230              log
     231                .debug("Potential hit RT: " + h
     232                  .getRetentionTimeInMinutes() + " with feature RT:" + f
     233                  .getApexRetentionTimeInMinutes());
     234            }
     235          }
     236
     237        }
     238      }
     239      // Match potential matches if they have not been matched
     240      // before
     241      if (f.getPeptideSequence() == null)
     242      {
     243        for (Hit h : potential)
     244        {
     245          if (h.getFeature() == null)
     246          {
     247            dc.reattachItem(h);
     248            dc.reattachItem(f);
     249            if (h.getPrecursorQuantity() == null || (h
     250              .getPrecursorQuantity() < f.getApexIntensity()))
     251            {
     252              log
     253                .debug("Adding potential:" + h.getDescription() + " , RT:" + h
     254                  .getRetentionTimeInMinutes());
     255              h.setPrecursorQuantity(f.getApexIntensity());
     256              f.addHit(h);
     257              f.setPeptideSequence(h.getDescription());
     258              count++;
     259            }
     260          }
     261        }
     262      }
     263    }
     264    return count;
     265  }
     266
     267
     268  private List<Hit> retrieveHits(File msFile, Project project, DbControl dc,
     269      boolean matchRelated)
    255270  {
    256271    List<Hit> hits = new ArrayList<Hit>();
    257272    ItemQuery<Hit> hitQuery = Hit.getQuery(project);
    258     // First check if the same msXML file can be found inthe hits table
     273    // First check if the same msXML file can be found in the hits table
    259274    hitQuery.restrict(Restrictions.eq(Hql.property("peakListFile"), Hql
    260275      .entity(msFile)));
     
    311326    }
    312327    log.debug("Query:" + hitQuery.toString());
     328    if (matchRelated)
     329    {
     330      // Match all hits that are NOT from the particular peak list
     331      ItemResultList<Hit> hitlist = hitQuery.list(dc);
     332      if (!hitlist.isEmpty())
     333      {
     334        Hit h = hitlist.get(0);
     335        hitQuery.reset();
     336        hitQuery.restrict(Restrictions.neq(
     337          Hql.property("peakListFile"), Hql.entity(h
     338            .getPeakListFile())));
     339        hitQuery.restrict(Restrictions.eq(
     340          Hql.property("localSampleId"), Expressions
     341            .parameter("localSampleId")));
     342        hitQuery.setPermanentParameter("localSampleId", h
     343          .getLocalSampleId(), null);
     344        hitQuery.restrict(Restrictions.eq(Hql.property("fractionId"),
     345          Expressions.parameter("fractionId")));
     346        hitQuery.setPermanentParameter("fractionId", h.getFractionId(),
     347          null);
     348      }
     349    }
    313350    ItemResultList<Hit> hitlist = hitQuery.list(dc);
    314351    for (Hit h : hitlist)
Note: See TracChangeset for help on using the changeset viewer.