00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef OPENMS_KERNEL_CONSENSUSMAP_H
00028 #define OPENMS_KERNEL_CONSENSUSMAP_H
00029
00030 #include <OpenMS/KERNEL/ConsensusFeature.h>
00031 #include <OpenMS/KERNEL/StandardTypes.h>
00032 #include <OpenMS/KERNEL/DPeakArray.h>
00033
00034 #include <deque>
00035
00036 namespace OpenMS
00037 {
00038
00039 #define DEBUG_MERGING
00040 #undef DEBUG_MERGING
00041
00054 template < typename ConsensusElementT = ConsensusFeature < FeatureMap< > > >
00055 class ConsensusMap : public DPeakArray<ConsensusElementT >
00056 {
00057 public:
00059 typedef ConsensusElementT ConsensusElementType;
00061 typedef typename ConsensusElementType::Group::const_iterator ConsensusElementIterator;
00063 typedef DPeakArray<ConsensusElementType > Base;
00065 typedef typename Base::iterator Iterator;
00067 typedef typename Base::const_iterator ConstIterator;
00069 typedef typename Base::reverse_iterator ReverseIterator;
00071 typedef typename Base::const_reverse_iterator ConstReverseIterator;
00072
00074 inline ConsensusMap()
00075 : Base()
00076 {}
00077
00079 inline ConsensusMap(const ConsensusMap& source)
00080 : Base(source),
00081 map_vector_(source.map_vector_),
00082 filenames_(source.filenames_)
00083 { }
00084
00086 inline ~ConsensusMap()
00087 {}
00088
00090 ConsensusMap(typename Base::size_type n) : Base(n)
00091 {}
00092
00094 ConsensusMap& operator = (const ConsensusMap& source)
00095 {
00096 if (this==&source)
00097 return *this;
00098
00099 Base::operator=(source);
00100 map_vector_ = source.map_vector_;
00101 filenames_ = source.filenames_;
00102 return *this;
00103 }
00104
00106 inline const std::vector < typename ConsensusElementType::ElementContainerType* >& getMapVector() const
00107 {
00108 return map_vector_;
00109 }
00111 inline std::vector < typename ConsensusElementType::ElementContainerType* >& getMapVector()
00112 {
00113 return map_vector_;
00114 }
00116 inline void setMapVector(const std::vector < typename ConsensusElementType::ElementContainerType* >& map_vector)
00117 {
00118 map_vector_ = map_vector;
00119 }
00120
00122 inline const std::vector < String >& getFilenames() const
00123 {
00124 return filenames_;
00125 }
00127
00128 inline std::vector < String >& getFilenames()
00129 {
00130 return filenames_;
00131 }
00133 inline void setFilenames(const std::vector < String >& filenames)
00134 {
00135 filenames_ = filenames;
00136 }
00137
00139 void merge(ConsensusMap<ConsensusElementT>& new_map)
00140 {
00141 #ifdef DEBUG_MERGING
00142 std::cout << "Number of elements " << this->size() << std::endl;
00143 #endif
00144
00145 std::vector<UInt> remove_indices;
00146 UInt n = this->size();
00147 std::vector < std::pair < DoubleReal, UInt > > rt_start_end_points(2*n);
00148 UInt i,j;
00149 for (i = 0,j = 0; i < n; ++i, j+=2)
00150 {
00151 rt_start_end_points[j] = std::pair< DoubleReal, UInt> (this->operator[](i).getPositionRange().min()[0], i);
00152 rt_start_end_points[j+1] = std::pair< DoubleReal, UInt> (this->operator[](i).getPositionRange().max()[0], i);
00153 }
00154 sort(rt_start_end_points.begin(),rt_start_end_points.end());
00155
00156
00157
00158 std::map < UInt, std::vector< UInt> > active_intervalls;
00159 std::map < UInt, std::vector< UInt> > intersecting_intervalls;
00160
00161 #ifdef DEBUG_MERGING
00162 std::cout << "Merge " << std::endl;
00163 #endif
00164 n = rt_start_end_points.size();
00165
00166 for (UInt i = 0; i < n; ++i)
00167 {
00168 UInt current_index = rt_start_end_points[i].second;
00169
00170
00171
00172 std::map < UInt, std::vector< UInt> >::iterator it = active_intervalls.find(current_index);
00173 if (it == active_intervalls.end())
00174 {
00175
00176 it = active_intervalls.begin();
00177 while (it != active_intervalls.end())
00178 {
00179 it->second.push_back(current_index);
00180 ++it;
00181 }
00182
00183
00184 active_intervalls.insert(std::pair<UInt, std::vector< UInt> > (current_index, std::vector< UInt >() ));
00185 }
00186 else
00187 {
00188
00189 if (it->second.size() > 0)
00190 {
00191 intersecting_intervalls.insert(*it);
00192 }
00193
00194 active_intervalls.erase(it);
00195 }
00196 }
00197
00198
00199
00200 std::map < UInt, std::vector< UInt> >::iterator it = intersecting_intervalls.begin();
00201 while(it != intersecting_intervalls.end())
00202 {
00203
00204 DoubleReal c_mz_min = this->operator[](it->first).getPositionRange().min()[1];
00205 DoubleReal c_mz_max = this->operator[](it->first).getPositionRange().max()[1];
00206 #ifdef DEBUG_MERGING
00207 std::cout << " RT " << this->operator[](it->first).getPositionRange().min()[0]
00208 << " - " << this->operator[](it->first).getPositionRange().max()[0]
00209 << " MZ " << c_mz_min
00210 << " - " << c_mz_max
00211 << "\n Indizes " << std::endl;
00212 #endif
00213
00214 ConsensusElementType& cons_elem = this->operator[](it->first);
00215
00216 #ifdef DEBUG_MERGING
00217 for (ConsensusElementIterator it_cons = cons_elem.begin(); it_cons != cons_elem.end(); ++it_cons)
00218 {
00219 std::cout << it_cons->getMapIndex() << ' ';
00220 }
00221 std::cout << "----- " << std::endl;
00222 #endif
00223
00224 for (UInt i = 0; i < it->second.size(); ++i)
00225 {
00226 DoubleReal mz_min = this->operator[](it->second[i]).getPositionRange().min()[1];
00227 DoubleReal mz_max = this->operator[](it->second[i]).getPositionRange().max()[1];
00228
00229 UInt duplicates = 0;
00230 if (((mz_min < c_mz_min) && (c_mz_min < mz_max)) || ((c_mz_min < mz_min) && (mz_min < c_mz_max)))
00231 {
00232 #ifdef DEBUG_MERGING
00233 std::cout << "+++ RT " << this->operator[](it->second[i]).getPositionRange().min()[0]
00234 << " - " << this->operator[](it->second[i]).getPositionRange().max()[0]
00235 << " MZ " << mz_min
00236 << " - " << mz_max
00237 << "\n Indizes " << std::endl;
00238 #endif
00239
00240
00241 for (ConsensusElementIterator it_cons = (this->operator[](it->second[i])).begin(); it_cons != (this->operator[](it->second[i])).end(); ++it_cons)
00242 {
00243 #ifdef DEBUG_MERGING
00244 std::cout << it_cons->getMapIndex() << ' ';
00245 #endif
00246 IndexTuple<typename ConsensusElementType::ElementContainerType> i;
00247 i.setMapIndex(it_cons->getMapIndex());
00248 if (cons_elem.find(i) != cons_elem.end())
00249 {
00250 ++duplicates;
00251 }
00252 }
00253 #ifdef DEBUG_MERGING
00254 std::cout << "\n number of duplicates " << duplicates << std::endl;
00255 #endif
00256
00257
00258 if (duplicates > 0)
00259 {
00260 continue;
00261 }
00262 else
00263 {
00264 #ifdef DEBUG_MERGING
00265 std::cout << "MERGE" << std::endl;
00266 #endif
00267
00268 for (ConsensusElementIterator it_cons = (this->operator[](it->second[i])).begin(); it_cons != (this->operator[](it->second[i])).end(); ++it_cons)
00269 {
00270 cons_elem.insert(*it_cons);
00271 #ifdef DEBUG_MERGING
00272 std::cout << "INSERT " << it_cons->getMapIndex() << std::endl;
00273 #endif
00274 }
00275
00276
00277 #ifdef DEBUG_MERGING
00278 std::cout << "Remove " << *(this->begin() + it->second[i]) << std::endl;
00279 #endif
00280 std::cout << "Remove " << *(this->begin() + it->second[i]) << std::endl;
00281 remove_indices.push_back(it->second[i]);
00282 }
00283 }
00284 }
00285 #ifdef DEBUG_MERGING
00286 std::cout << "========================" << std::endl;
00287 #endif
00288 ++it;
00289 }
00290
00291
00292 sort(remove_indices.begin(),remove_indices.end());
00293
00294 j=0;
00295 n=this->size();
00296 UInt m=remove_indices.size();
00297 for (UInt i = 0; i < n; ++i)
00298 {
00299 if ((j<m) && (i == remove_indices[j]))
00300 {
00301 UInt curr = remove_indices[j];
00302 ++j;
00303 while (remove_indices[j] == curr)
00304 {
00305 ++j;
00306 }
00307 continue;
00308 }
00309 else
00310 {
00311 new_map.push_back(this->operator[](i));
00312 }
00313 }
00314
00315 #ifdef DEBUG_MERGING
00316
00317
00318 std::cout << "Number of elements " << this->size() << std::endl;
00319 #endif
00320 }
00321
00322 protected:
00324 std::vector < typename ConsensusElementType::ElementContainerType* > map_vector_;
00325
00327 std::vector < String > filenames_;
00328 };
00329
00331 template < typename ConsensusElementT >
00332 std::ostream& operator << (std::ostream& os, const ConsensusMap<ConsensusElementT>& cons_map)
00333 {
00334 for (UInt i = 0; i < cons_map.size(); ++i)
00335 {
00336 os << cons_map[i] << std::endl;
00337 }
00338
00339 return os;
00340 }
00341 }
00342
00343 #endif // OPENMS_KERNEL_CONSENSUSMAP_H