OpenMS
MetaInfoInterfaceUtils.h
Go to the documentation of this file.
1 // Copyright (c) 2002-2023, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Chris Bielow $
6 // $Authors: Chris Bielow $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
11 #include <OpenMS/OpenMSConfig.h>
14 
15 #include <algorithm>
16 #include <map>
17 #include <vector>
18 
19 namespace OpenMS
20 {
21  namespace Detail
22  {
23  template<typename T>
25  {
26  static void getKeys(const T& object, std::vector<String>& keys)
27  {
28  object.getKeys(keys);
29  };
30  };
31  }
32 
38  class /*OPENMS_DLLAPI -- disabled since it's template code only */ MetaInfoInterfaceUtils
39  {
40 public:
45  // no Move semantics for utils class
46 
47 
49 
50 
65  template<typename T_In, typename T_Out>
66  static T_Out findCommonMetaKeys(const typename T_In::const_iterator& it_start, const typename T_In::const_iterator& it_end, float min_frequency, typename Detail::MetaKeyGetter<typename T_In::value_type> getter = Detail::MetaKeyGetter<typename T_In::value_type>())
67  {
68  // make sure min_frequency is within [0,100]
69  min_frequency = std::min(100.0f, std::max(0.0f, min_frequency));
70 
71  std::map<String, UInt> counter;
72  typedef std::vector<String> KeysType;
73  KeysType keys;
74  for (typename T_In::const_iterator it = it_start; it != it_end; ++it)
75  {
76  getter.getKeys(*it, keys);
77  for (KeysType::const_iterator itk = keys.begin(); itk != keys.end(); ++itk)
78  {
79  ++counter[*itk];
80  }
81  }
82  // pick the keys which occur often enough
83  const UInt required_counts = UInt(min_frequency / 100.0 * std::distance(it_start, it_end));
84  T_Out common_keys;
85  for (const auto& [key, count] : counter)
86  {
87  if (count >= required_counts)
88  {
89  common_keys.insert(common_keys.end(), key);
90  }
91  }
92  return common_keys;
93  }
94 
95  }; // class
96 
97 } // namespace OPENMS
98 
Utilities operating on containers inheriting from MetaInfoInterface.
Definition: MetaInfoInterfaceUtils.h:39
MetaInfoInterfaceUtils()=delete
hide c'tors to avoid instantiation of utils class
MetaInfoInterfaceUtils & operator=(MetaInfoInterfaceUtils &)=delete
static T_Out findCommonMetaKeys(const typename T_In::const_iterator &it_start, const typename T_In::const_iterator &it_end, float min_frequency, typename Detail::MetaKeyGetter< typename T_In::value_type > getter=Detail::MetaKeyGetter< typename T_In::value_type >())
Find keys in a collection of MetaInfoInterface objects which reach a certain frequency threshold.
Definition: MetaInfoInterfaceUtils.h:66
MetaInfoInterfaceUtils(const MetaInfoInterfaceUtils &)=delete
unsigned int UInt
Unsigned integer type.
Definition: Types.h:68
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:22
Definition: MetaInfoInterfaceUtils.h:25
static void getKeys(const T &object, std::vector< String > &keys)
Definition: MetaInfoInterfaceUtils.h:26