OpenMS
MetaInfoInterfaceUtils.h
Go to the documentation of this file.
1 // Copyright (c) 2002-present, OpenMS Inc. -- 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 
66  template<typename T_In, typename T_Out>
67  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>())
68  {
69  // make sure min_frequency is within [0,100]
70  min_frequency = std::min(100.0f, std::max(0.0f, min_frequency));
71 
72  std::map<String, UInt> counter;
73  typedef std::vector<String> KeysType;
74  KeysType keys;
75  for (typename T_In::const_iterator it = it_start; it != it_end; ++it)
76  {
77  getter.getKeys(*it, keys);
78  for (KeysType::const_iterator itk = keys.begin(); itk != keys.end(); ++itk)
79  {
80  ++counter[*itk];
81  }
82  }
83  // pick the keys which occur often enough
84  const UInt required_counts = UInt(min_frequency / 100.0 * std::distance(it_start, it_end));
85  T_Out common_keys;
86  for (const auto& [key, count] : counter)
87  {
88  if (count >= required_counts)
89  {
90  common_keys.insert(common_keys.end(), key);
91  }
92  }
93  return common_keys;
94  }
95 
96  }; // class
97 
98 } // namespace OPENMS
99 
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:67
MetaInfoInterfaceUtils(const MetaInfoInterfaceUtils &)=delete
unsigned int UInt
Unsigned integer type.
Definition: Types.h:64
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
Definition: MetaInfoInterfaceUtils.h:25
static void getKeys(const T &object, std::vector< String > &keys)
Definition: MetaInfoInterfaceUtils.h:26