NonOverlapRegions.h

00001 /*
00002  *  Copyright (C) 2011  Regents of the University of Michigan
00003  *
00004  *   This program is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 //////////////////////////////////////////////////////////////////////////
00019 
00020 #ifndef __NONOVERLAP_REGIONS_H__
00021 #define __NONOVERLAP_REGIONS_H__
00022 
00023 #include <map>
00024 #include <string>
00025 #include <list>
00026 #include <stdint.h>
00027 
00028 class NonOverlapRegionPos;
00029 
00030 /// This class contains a list of non-overlapping regions.  When regions are
00031 /// added that overlap, it merges them.  After adding regions, you can check
00032 /// to see if a position is found in one of the regions.  It is designed to
00033 /// work fastest if you make calls in sequential order.
00034 class NonOverlapRegions
00035 {
00036 public:
00037     friend class NonOverlapRegionsTest;
00038 
00039     NonOverlapRegions();
00040     ~NonOverlapRegions();
00041 
00042     /// End position is not included in the region.
00043     /// If this region overlaps another region(s), they will be merged into 
00044     /// one region.
00045     void add(const char* chrom, int32_t start, int32_t end);
00046  
00047     /// Return whether or not the position was found within a region.
00048     /// If it is found within the region, myRegionIter will point to the region
00049     /// otherwise myRegionIter will point to the region after the position 
00050     /// or to the end if the position is after the last region.
00051     bool inRegion(const char* chrom, int32_t pos);
00052 
00053 private:
00054     // Copy Constructor - unimplimented.
00055     NonOverlapRegions(const NonOverlapRegions& reg);
00056 
00057     std::map<std::string, NonOverlapRegionPos> myRegions;
00058 };
00059 
00060 
00061 /// This class contains a list of non-overlapping regions, just positions, not
00062 /// including chromosomes (see NonOverlapRegions for chromosomes and positions).
00063 ///  When regions are added that overlap, it merges them.  After adding regions,
00064 /// you can check to see if a position is found in one of the regions.  It is
00065 /// designed to work fastest if you make calls in sequential order.
00066 class NonOverlapRegionPos
00067 {
00068 public:
00069     friend class NonOverlapRegionsTest;
00070     NonOverlapRegionPos();
00071     /// Copy constructor, does not copy, but initializes with an empty 
00072     /// region list.
00073     NonOverlapRegionPos(const NonOverlapRegionPos& reg);
00074 
00075     ~NonOverlapRegionPos();
00076 
00077     /// End position is not included in the region.
00078     /// If this region overlaps another region(s), they will be merged into 
00079     /// one region.
00080     void add(int32_t start, int32_t end);
00081  
00082     /// Return whether or not the position was found within a region.
00083     /// If it is found within the region, myRegionIter will point to the region
00084     /// otherwise myRegionIter will point to the region after the position 
00085     /// or to the end if the position is after the last region.
00086     bool inRegion(int32_t pos);
00087 
00088 private:
00089     // True if pos found in the region pointed to by myRegionIter or to
00090     // the right of myRegionIter.  If the position is found in a region,
00091     // myRegionIter will point to the region containing the position.
00092     // If the position is not found in a region, myRegionIter will point 
00093     // to the region after the position, or to the end if the position is
00094     // after the last region.
00095     bool findRight(int32_t pos);
00096 
00097     // True if pos found in the region pointed to by myRegionIter or to
00098     // the left of myRegionIter.  If the position is found in a region,
00099     // myRegionIter will point to the region containing the position.
00100     // If the position is not found in a region, myRegionIter will point 
00101     // to the region after the position, or to the end if the position is
00102     // after the last region.
00103     bool findLeft(int32_t pos);
00104 
00105 
00106     std::list< std::pair<int32_t, int32_t> > myRegions;
00107     std::list< std::pair<int32_t, int32_t> >::iterator myRegionIter;
00108     std::list< std::pair<int32_t, int32_t> >::iterator myTmpIter;
00109 };
00110 
00111 
00112 #endif
Generated on Mon Feb 11 13:45:18 2013 for libStatGen Software by  doxygen 1.6.3