Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages

IDFilter.h

Go to the documentation of this file.
00001 // --------------------------------------------------------------------------
00002 //                   OpenMS -- Open-Source Mass Spectrometry
00003 // --------------------------------------------------------------------------
00004 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
00005 // ETH Zurich, and Freie Universitaet Berlin 2002-2012.
00006 //
00007 // This software is released under a three-clause BSD license:
00008 //  * Redistributions of source code must retain the above copyright
00009 //    notice, this list of conditions and the following disclaimer.
00010 //  * Redistributions in binary form must reproduce the above copyright
00011 //    notice, this list of conditions and the following disclaimer in the
00012 //    documentation and/or other materials provided with the distribution.
00013 //  * Neither the name of any author or any participating institution
00014 //    may be used to endorse or promote products derived from this software
00015 //    without specific prior written permission.
00016 // For a full list of authors, refer to the file AUTHORS.
00017 // --------------------------------------------------------------------------
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
00022 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00023 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00024 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00025 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00026 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00027 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00028 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // --------------------------------------------------------------------------
00031 // $Maintainer: Mathias Walzer $
00032 // $Authors: Nico Pfeifer, Mathias Walzer$
00033 // --------------------------------------------------------------------------
00034 
00035 #ifndef OPENMS_FILTERING_ID_IDFILTER_H
00036 #define OPENMS_FILTERING_ID_IDFILTER_H
00037 
00038 #include <OpenMS/config.h>
00039 #include <OpenMS/DATASTRUCTURES/String.h>
00040 #include <OpenMS/METADATA/ProteinIdentification.h>
00041 #include <OpenMS/KERNEL/MSExperiment.h>
00042 #include <OpenMS/FORMAT/FASTAFile.h>
00043 
00044 #include <vector>
00045 #include <climits>
00046 
00047 namespace OpenMS
00048 {
00061   class OPENMS_DLLAPI IDFilter
00062   {
00063 public:
00064 
00066     IDFilter();
00067 
00069     virtual ~IDFilter();
00070 
00072     template <class IdentificationType>
00073     void filterIdentificationsByThreshold(const IdentificationType& identification, DoubleReal threshold_fraction, IdentificationType& filtered_identification)
00074     {
00075       typedef typename IdentificationType::HitType HitType;
00076       std::vector<HitType> temp_hits;
00077       std::vector<HitType> filtered_hits;
00078 
00079       filtered_identification = identification;
00080       filtered_identification.setHits(std::vector<HitType>());
00081 
00082       for (typename std::vector<HitType>::const_iterator it = identification.getHits().begin();
00083            it != identification.getHits().end();
00084            ++it)
00085       {
00086         if (it->getScore() >= threshold_fraction * identification.getSignificanceThreshold())
00087         {
00088           filtered_hits.push_back(*it);
00089         }
00090       }
00091 
00092       if (!filtered_hits.empty())
00093       {
00094         filtered_identification.setHits(filtered_hits);
00095         filtered_identification.assignRanks();
00096       }
00097     }
00098 
00106     template <class IdentificationType>
00107     void filterIdentificationsByScore(const IdentificationType& identification, DoubleReal threshold_score, IdentificationType& filtered_identification)
00108     {
00109       typedef typename IdentificationType::HitType HitType;
00110       std::vector<HitType> temp_hits;
00111       std::vector<HitType> filtered_hits;
00112 
00113       filtered_identification = identification;
00114       filtered_identification.setHits(std::vector<HitType>());
00115 
00116       for (typename std::vector<HitType>::const_iterator it = identification.getHits().begin();
00117            it != identification.getHits().end();
00118            ++it)
00119       {
00120         if (identification.isHigherScoreBetter())
00121         {
00122           if (it->getScore() >= threshold_score)
00123           {
00124             filtered_hits.push_back(*it);
00125           }
00126         }
00127         else
00128         {
00129           if (it->getScore() <= threshold_score)
00130           {
00131             filtered_hits.push_back(*it);
00132           }
00133         }
00134       }
00135 
00136       if (!filtered_hits.empty())
00137       {
00138         filtered_identification.setHits(filtered_hits);
00139         filtered_identification.assignRanks();
00140       }
00141     }
00142 
00149     template <class IdentificationType>
00150     void filterIdentificationsByBestNHits(const IdentificationType& identification, Size n, IdentificationType& filtered_identification)
00151     {
00152       typedef typename IdentificationType::HitType HitType;
00153       std::vector<HitType> temp_hits;
00154       std::vector<HitType> filtered_hits;
00155       Size count = 0;
00156 
00157       IdentificationType temp_identification = identification;
00158       temp_identification.sort(); // .. by score
00159 
00160       filtered_identification = identification;
00161       filtered_identification.setHits(std::vector<HitType>());
00162 
00163 
00164       typename std::vector<HitType>::const_iterator it = temp_identification.getHits().begin();
00165       while (it != temp_identification.getHits().end()
00166             && count < n)
00167       {
00168         filtered_hits.push_back(*it);
00169         ++it;
00170         ++count;
00171       }
00172 
00173       if (!filtered_hits.empty())
00174       {
00175         filtered_identification.setHits(filtered_hits);
00176         filtered_identification.assignRanks();
00177       }
00178     }
00179 
00187     template <class IdentificationType>
00188     void filterIdentificationsByBestNToMHits(const IdentificationType& identification, Size n, Size m, IdentificationType& filtered_identification)
00189     {
00190       if (n > m)
00191       {
00192         std::swap(n, m);
00193       }
00194 
00195       typedef typename IdentificationType::HitType HitType;
00196       std::vector<HitType> filtered_hits;
00197 
00198       IdentificationType temp_identification = identification;
00199       temp_identification.sort(); // .. by score
00200 
00201       filtered_identification = identification;
00202       filtered_identification.setHits(std::vector<HitType>());
00203 
00204       const std::vector<HitType>& hits = temp_identification.getHits();
00205       for (Size i = n - 1; n <= m - 1; ++i)
00206       {
00207         if (i >= hits.size())
00208         {
00209           break;
00210         }
00211         filtered_hits.push_back(hits[i]);
00212       }
00213 
00214       if (!filtered_hits.empty())
00215       {
00216         filtered_identification.setHits(filtered_hits);
00217         filtered_identification.assignRanks();
00218       }
00219     }
00220 
00222     void filterIdentificationsByBestHits(const PeptideIdentification& identification, PeptideIdentification& filtered_identification, bool strict = false);
00223 
00227     void filterIdentificationsByProteins(const PeptideIdentification& identification, const std::vector<FASTAFile::FASTAEntry>& proteins, PeptideIdentification& filtered_identification, bool no_protein_identifiers = false);
00228 
00232     void filterIdentificationsByProteins(const ProteinIdentification& identification, const std::vector<FASTAFile::FASTAEntry>& proteins, ProteinIdentification& filtered_identification);
00233 
00235     void filterIdentificationsByExclusionPeptides(const PeptideIdentification& identification, const std::set<String>& peptides, PeptideIdentification& filtered_identification);
00236 
00238     void filterIdentificationsByLength(const PeptideIdentification& identification, PeptideIdentification& filtered_identification, Size min_length, Size max_length = UINT_MAX);
00239 
00241     void filterIdentificationsByCharge(const PeptideIdentification& identification, Int charge, PeptideIdentification& filtered_identification);
00242 
00244     void filterIdentificationsByVariableModifications(const PeptideIdentification& identification, const std::vector<String>& fixed_modifications, PeptideIdentification& filtered_identification);
00245 
00247     void removeUnreferencedProteinHits(const ProteinIdentification& identification, const std::vector<PeptideIdentification> peptide_identifications, ProteinIdentification& filtered_identification);
00248 
00250     void filterIdentificationsUnique(const PeptideIdentification& identification, PeptideIdentification& filtered_identification);
00251 
00253     void filterIdentificationsByMzError(const PeptideIdentification& identification, DoubleReal mass_error, bool unit_ppm, PeptideIdentification& filtered_identification);
00254 
00262     void filterIdentificationsByRTPValues(const PeptideIdentification& identification, PeptideIdentification& filtered_identification, DoubleReal p_value = 0.05);
00263 
00271     void filterIdentificationsByRTFirstDimPValues(const PeptideIdentification& identification,
00272                                                   PeptideIdentification& filtered_identification,
00273                                                   DoubleReal                                      p_value = 0.05);
00274 
00276     template <class PeakT>
00277     void filterIdentificationsByThresholds(MSExperiment<PeakT>& experiment, DoubleReal peptide_threshold_fraction, DoubleReal protein_threshold_fraction)
00278     {
00279       //filter protein hits
00280       ProteinIdentification temp_protein_identification;
00281       std::vector<ProteinIdentification> filtered_protein_identifications;
00282 
00283       for (Size j = 0; j < experiment.getProteinIdentifications().size(); j++)
00284       {
00285         filterIdentificationsByThreshold(experiment.getProteinIdentifications()[j], protein_threshold_fraction, temp_protein_identification);
00286         if (!temp_protein_identification.getHits().empty())
00287         {
00288           filtered_protein_identifications.push_back(temp_protein_identification);
00289         }
00290       }
00291       experiment.setProteinIdentifications(filtered_protein_identifications);
00292 
00293       //filter peptide hits
00294       PeptideIdentification temp_identification;
00295       std::vector<PeptideIdentification> filtered_identifications;
00296 
00297       for (Size i = 0; i < experiment.size(); i++)
00298       {
00299         for (Size j = 0; j < experiment[i].getPeptideIdentifications().size(); j++)
00300         {
00301           filterIdentificationsByThreshold(experiment[i].getPeptideIdentifications()[j], peptide_threshold_fraction, temp_identification);
00302           if (!temp_identification.getHits().empty())
00303           {
00304             filtered_identifications.push_back(temp_identification);
00305           }
00306         }
00307         experiment[i].setPeptideIdentifications(filtered_identifications);
00308         filtered_identifications.clear();
00309       }
00310     }
00311 
00313     template <class PeakT>
00314     void filterIdentificationsByScores(MSExperiment<PeakT>& experiment, DoubleReal peptide_threshold_score, DoubleReal protein_threshold_score)
00315     {
00316       //filter protein hits
00317       ProteinIdentification temp_protein_identification;
00318       std::vector<ProteinIdentification> filtered_protein_identifications;
00319 
00320       for (Size j = 0; j < experiment.getProteinIdentifications().size(); j++)
00321       {
00322         filterIdentificationsByScore(experiment.getProteinIdentifications()[j], protein_threshold_score, temp_protein_identification);
00323         if (!temp_protein_identification.getHits().empty())
00324         {
00325           filtered_protein_identifications.push_back(temp_protein_identification);
00326         }
00327       }
00328       experiment.setProteinIdentifications(filtered_protein_identifications);
00329 
00330       //filter peptide hits
00331       PeptideIdentification temp_identification;
00332       std::vector<PeptideIdentification> filtered_identifications;
00333 
00334       for (Size i = 0; i < experiment.size(); i++)
00335       {
00336         for (Size j = 0; j < experiment[i].getPeptideIdentifications().size(); j++)
00337         {
00338           filterIdentificationsByScore(experiment[i].getPeptideIdentifications()[j], peptide_threshold_score, temp_identification);
00339           if (!temp_identification.getHits().empty())
00340           {
00341             filtered_identifications.push_back(temp_identification);
00342           }
00343         }
00344         experiment[i].setPeptideIdentifications(filtered_identifications);
00345         filtered_identifications.clear();
00346       }
00347     }
00348 
00350     template <class PeakT>
00351     void filterIdentificationsByBestNHits(MSExperiment<PeakT>& experiment, Size n)
00352     {
00353       //filter protein hits
00354       ProteinIdentification temp_protein_identification;
00355       std::vector<ProteinIdentification> filtered_protein_identifications;
00356 
00357       for (Size j = 0; j < experiment.getProteinIdentifications().size(); j++)
00358       {
00359         filterIdentificationsByBestNHits(experiment.getProteinIdentifications()[j], n, temp_protein_identification);
00360         if (!temp_protein_identification.getHits().empty())
00361         {
00362           filtered_protein_identifications.push_back(temp_protein_identification);
00363         }
00364       }
00365       experiment.setProteinIdentifications(filtered_protein_identifications);
00366 
00367       //filter peptide hits
00368       PeptideIdentification temp_identification;
00369       std::vector<PeptideIdentification> filtered_identifications;
00370 
00371       for (Size i = 0; i < experiment.size(); i++)
00372       {
00373         for (Size j = 0; j < experiment[i].getPeptideIdentifications().size(); j++)
00374         {
00375           filterIdentificationsByBestNHits(experiment[i].getPeptideIdentifications()[j], n, temp_identification);
00376           if (!temp_identification.getHits().empty())
00377           {
00378             filtered_identifications.push_back(temp_identification);
00379           }
00380         }
00381         experiment[i].setPeptideIdentifications(filtered_identifications);
00382         filtered_identifications.clear();
00383       }
00384     }
00385 
00387     template <class PeakT>
00388     void filterIdentificationsByProteins(MSExperiment<PeakT>& experiment,
00389                                          const std::vector<FASTAFile::FASTAEntry>& proteins)
00390     {
00391       std::vector<PeptideIdentification> temp_identifications;
00392       std::vector<PeptideIdentification> filtered_identifications;
00393       PeptideIdentification temp_identification;
00394 
00395       for (Size i = 0; i < experiment.size(); i++)
00396       {
00397         if (experiment[i].getMSLevel() == 2)
00398         {
00399           temp_identifications = experiment[i].getPeptideIdentifications();
00400           for (Size j = 0; j < temp_identifications.size(); j++)
00401           {
00402             filterIdentificationsByProteins(temp_identifications[j], proteins, temp_identification);
00403             if (!temp_identification.getHits().empty())
00404             {
00405               filtered_identifications.push_back(temp_identification);
00406             }
00407           }
00408           experiment[i].setPeptideIdentifications(filtered_identifications);
00409           filtered_identifications.clear();
00410         }
00411       }
00412     }
00413 
00414   };
00415 
00416 } // namespace OpenMS
00417 
00418 #endif // OPENMS_FILTERING_ID_IDFILTER_H

OpenMS / TOPP release 1.10.0 Documentation generated on Thu Mar 7 2013 09:42:40 using doxygen 1.7.1