NonOverlapRegions.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "NonOverlapRegions.h"
00021 #include <iostream>
00022
00023 NonOverlapRegions::NonOverlapRegions()
00024 : myRegions()
00025 {
00026 }
00027
00028
00029 NonOverlapRegions::~NonOverlapRegions()
00030 {
00031 myRegions.clear();
00032 }
00033
00034
00035 void NonOverlapRegions::add(const char* chrom, int32_t start, int32_t end)
00036 {
00037
00038 myRegions[chrom].add(start, end);
00039 }
00040
00041
00042 bool NonOverlapRegions::inRegion(const char* chrom, int32_t pos)
00043 {
00044
00045
00046
00047 return(myRegions[chrom].inRegion(pos));
00048 }
00049
00050
00051 NonOverlapRegionPos::NonOverlapRegionPos()
00052 : myRegions()
00053 {
00054 myRegionIter = myRegions.begin();
00055 myTmpIter = myRegions.begin();
00056 }
00057
00058
00059 NonOverlapRegionPos::NonOverlapRegionPos(const NonOverlapRegionPos& reg)
00060 : myRegions()
00061 {
00062 myRegionIter = myRegions.begin();
00063 myTmpIter = myRegions.begin();
00064 }
00065
00066
00067 NonOverlapRegionPos::~NonOverlapRegionPos()
00068 {
00069 myRegionIter = myRegions.begin();
00070 myTmpIter = myRegions.begin();
00071 myRegions.clear();
00072 }
00073
00074
00075 void NonOverlapRegionPos::add(int32_t start, int32_t end)
00076 {
00077
00078 if(start >= end)
00079 {
00080 std::cerr << "NonOverlapRegionPos::add: Invalid Range, "
00081 << "start must be < end, but " << start << " >= " << end
00082 << std::endl;
00083 return;
00084 }
00085
00086 bool added = false;
00087
00088 if(inRegion(start))
00089 {
00090
00091 if(end > myRegionIter->second)
00092 {
00093 myRegionIter->second = end;
00094 }
00095 added = true;
00096 }
00097 else
00098 {
00099
00100 if(myRegionIter != myRegions.end())
00101 {
00102
00103
00104 if(end >= myRegionIter->first)
00105 {
00106
00107 myRegionIter->first = start;
00108
00109 if(myRegionIter->second < end)
00110 {
00111 myRegionIter->second = end;
00112 }
00113 added = true;
00114 }
00115 }
00116 }
00117
00118
00119
00120
00121 if(added)
00122 {
00123
00124 myTmpIter = myRegionIter;
00125 ++myTmpIter;
00126 while(myTmpIter != myRegions.end())
00127 {
00128
00129 if(myTmpIter->first <= end)
00130 {
00131 if(myTmpIter->second > myRegionIter->second)
00132 {
00133
00134 myRegionIter->second = myTmpIter->second;
00135 }
00136
00137 myTmpIter = myRegions.erase(myTmpIter);
00138 }
00139 else
00140 {
00141
00142 break;
00143 }
00144 }
00145 }
00146 else
00147 {
00148
00149 myRegionIter = myRegions.insert(myRegionIter,
00150 std::make_pair(start, end));
00151 }
00152 }
00153
00154
00155 bool NonOverlapRegionPos::inRegion(int32_t pos)
00156 {
00157
00158
00159
00160
00161
00162
00163
00164
00165 if(myRegionIter == myRegions.end())
00166 {
00167
00168 return(findLeft(pos));
00169 }
00170 else if(pos < myRegionIter->first)
00171 {
00172
00173
00174 return(findLeft(pos));
00175 }
00176 else
00177 {
00178 return(findRight(pos));
00179 }
00180 }
00181
00182
00183 bool NonOverlapRegionPos::findRight(int32_t pos)
00184 {
00185
00186 while(myRegionIter != myRegions.end())
00187 {
00188
00189 if(pos < myRegionIter->first)
00190 {
00191
00192
00193
00194 return(false);
00195 }
00196 else if(pos < myRegionIter->second)
00197 {
00198
00199 return(true);
00200 }
00201 else
00202 {
00203
00204 ++myRegionIter;
00205 }
00206 }
00207
00208
00209 return(false);
00210 }
00211
00212
00213 bool NonOverlapRegionPos::findLeft(int32_t pos)
00214 {
00215 if(myRegionIter == myRegions.end())
00216 {
00217 if(myRegionIter == myRegions.begin())
00218 {
00219
00220 return(false);
00221 }
00222
00223 --myRegionIter;
00224 }
00225
00226 while(myRegionIter->first > pos)
00227 {
00228
00229
00230 if(myRegionIter == myRegions.begin())
00231 {
00232
00233
00234 return(false);
00235 }
00236
00237 --myRegionIter;
00238 }
00239
00240
00241
00242
00243
00244 if(pos < myRegionIter->second)
00245 {
00246
00247 return(true);
00248 }
00249
00250
00251
00252 ++myRegionIter;
00253 return(false);
00254 }