| 119 | } |
| 120 | |
| 121 | void FeatureOverlapFilter::filter(FeatureMap& fmap, |
| 122 | std::function<bool(const Feature&, const Feature&)> FeatureComparator, |
| 123 | std::function<bool(Feature&, Feature&)> FeatureOverlapCallback, |
| 124 | bool check_overlap_at_trace_level) |
| 125 | { |
| 126 | fmap.updateRanges(); |
| 127 | // Sort all features according to the comparator. After the sort, the "smallest" == best feature will be the first entry we will start processing with... |
| 128 | std::stable_sort(fmap.begin(), fmap.end(), FeatureComparator); |
| 129 | |
| 130 | const auto getBox = [](const Feature* f) |
| 131 | { |
| 132 | const auto& bb = f->getConvexHull().getBoundingBox(); |
| 133 | return quadtree::Box<float>(bb.minY(), bb.minX(), bb.maxY()-bb.minY(), bb.maxX()-bb.minX()); |
| 134 | }; |
| 135 | |
| 136 | float minMZ = fmap.getMinMZ(); |
| 137 | float maxMZ = fmap.getMaxMZ(); |
| 138 | float minRT = fmap.getMinRT(); |
| 139 | float maxRT = fmap.getMaxRT(); |
| 140 | |
| 141 | // build quadtree with all features |
| 142 | quadtree::Box<float> fullExp(minMZ-1, minRT-1, maxMZ-minMZ+2, maxRT-minRT+2); |
| 143 | auto quadtree = quadtree::Quadtree<Feature*, decltype(getBox)>(fullExp, getBox); |
| 144 | for (auto& f : fmap) |
| 145 | { |
| 146 | quadtree.add(&f); |
| 147 | } |
| 148 | |
| 149 | // if we check for overlapping traces we need a faster lookup structure |
| 150 | FeatureBoundsMap fbm; |
| 151 | if (check_overlap_at_trace_level) |
| 152 | { |
| 153 | fbm = getFeatureBounds(fmap); |
| 154 | } |
| 155 | |
| 156 | std::unordered_set<Size> removed_uids; |
| 157 | for (auto& f : fmap) |
| 158 | { |
| 159 | if (removed_uids.count(f.getUniqueId()) == 0) |
| 160 | { |
| 161 | for (auto& overlap : quadtree.query(getBox(&f))) |
| 162 | { |
| 163 | if ((overlap != &f)) |
| 164 | { |
| 165 | // Because feature boundaries might be large and lead to many overlapps, we (optionally) also can check if the boundaries of traces overlap |
| 166 | bool is_true_overlap = true; |
| 167 | if (check_overlap_at_trace_level) |
| 168 | { |
| 169 | is_true_overlap = tracesOverlap(f, *overlap, fbm); |
| 170 | } |
| 171 | |
| 172 | if (is_true_overlap) |
| 173 | { |
| 174 | // callback allows to e.g., transfer information from the to-be-removed feature to the representative feature |
| 175 | // if the callback returns false, overlap will not be removed (at least not because of an overlap with f) |
| 176 | if (FeatureOverlapCallback(f, *overlap)) |
| 177 | { |
| 178 | removed_uids.insert(overlap->getUniqueId()); |
no test coverage detected