NCBI C++ ToolKit
alignment_sorter_by_score.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef GUI_WIDGET_SEQ_GRAPHICS___ALIGNMENT_SORTER_BY_SCORE__HPP
2 #define GUI_WIDGET_SEQ_GRAPHICS___ALIGNMENT_SORTER_BY_SCORE__HPP
3 
4 /* $Id:
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Andrei Shkeda
30  *
31  * File Description: Defines a template class, that sorts alignments by alignment scores
32  *
33  */
34 
35 
36 #include <corelib/ncbistd.hpp>
37 #include <map>
40 
42 
43 ///////////////////////////////////////////////////////////////////////////////
44 /// CAlignSorterByScore is a sorter used to sort alignments by scores
45 ///
46 template <typename T>
48  : public CObject
49  , public IAlignSorter
50 {
51 public:
52  const int NotSet = 0xffff;
53  using TScoreMapper = std::function<string(const T&)>;
54 
55  CAlignSorterByScore(std::string const& score_name, std::string const& score_id, const std::string& group_name, std::string const& description)
56  : m_ScoreName(score_name)
57  , m_ScoreId(score_id)
58  , m_GroupName(group_name)
59  , m_Description(description)
60  {
61  }
62 
63  virtual std::string GroupIdxToName(int idx) const
64  {
65  std::string name;
66  if (!m_GroupName.empty()) {
67  name = m_GroupName;
68  name += ' ';
69  }
70  if (NotSet != idx) {
71  for (auto const& group : m_SortGroups) {
72  if (group.second != idx)
73  continue;
74  name += group.first;
75  break;
76  }
77  } else {
78  name += "not set";
79  }
80  return name;
81  }
82 
83  virtual int GetGroupIdx(const objects::CSeq_align& align) const
84  {
85  try {
86  T score;
87  if (align.GetNamedScore(m_ScoreName, score)) {
88  string str_score = (m_ScoreMapper) ? m_ScoreMapper(score) : NStr::NumericToString<T>(score);
89  auto it = m_SortGroups.find(str_score);
90  if (m_SortGroups.end() != it)
91  return it->second;
92 
93  int idx = (int)m_SortGroups.size();
94  m_SortGroups[str_score] = idx;
95  return idx;
96  }
97  } catch (const CException&) {
98  }
99  return NotSet;
100  }
101 
103  {
105  }
106 
107  void SetMapper(TScoreMapper& score_mapper) {
108  m_ScoreMapper = score_mapper;
109  }
110 
111 private:
112  /// Curently intended sorting groups.
113  /// Any feature that doesn't have the field set will be assigned to an invalid group index
114  mutable std::map<string, int> m_SortGroups;
120 };
121 
122 
124 
125 #endif // GUI_WIDGET_SEQ_GRAPHICS___ALIGNMENT_SORTER_BY_SCORE__HPP
CAlignSorterByScore is a sorter used to sort alignments by scores.
virtual SSorterDescriptor GetThisDescr() const
Return a name for the sorter.
void SetMapper(TScoreMapper &score_mapper)
std::function< string(const T &)> TScoreMapper
virtual int GetGroupIdx(const objects::CSeq_align &align) const
Get the sorted group index for a given feature.
CAlignSorterByScore(std::string const &score_name, std::string const &score_id, const std::string &group_name, std::string const &description)
virtual std::string GroupIdxToName(int idx) const
Convert group index into group name.
std::map< string, int > m_SortGroups
Curently intended sorting groups.
CObject –.
Definition: ncbiobj.hpp:180
IAlignSorter is an abstract interface for sorting alignments based on any criteria.
Include a standard set of the NCBI C++ Toolkit most basic headers.
#define T(s)
Definition: common.h:230
string
Definition: cgiapp.hpp:687
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
Modified on Sat Jun 15 11:53:33 2024 by modify_doxy.py rev. 669887