00001 #ifndef OST_MOL_ALG_ADJACENCY_BITMAP_HH
00002 #define OST_MOL_ALG_ADJACENCY_BITMAP_HH
00003
00004 #include <cstring>
00005 #include <boost/shared_ptr.hpp>
00006 #include <ost/img/image_handle.hh>
00007 #include <ost/tri_matrix.hh>
00008 #include <ost/seq/sequence_handle.hh>
00009 #include <ost/seq/alignment_handle.hh>
00010 #include "similarity_matrix.hh"
00011 #include "contact_overlap.hh"
00012 #include "module_config.hh"
00013
00014 namespace ost { namespace mol { namespace alg {
00015
00016
00017 struct OverlapResult {
00018 OverlapResult(uint16_t nom, uint16_t denom, uint16_t def):
00019 nominator(nom), denominator(denom), defined(def) {}
00020 OverlapResult(): nominator(0), denominator(0), defined(0) {}
00021 uint16_t nominator;
00022 uint16_t denominator;
00023 uint16_t defined;
00024 };
00025
00026
00027 class AdjacencyBitmap {
00028 public:
00029 explicit AdjacencyBitmap(size_t n_vertices):
00030 num_vertices_(n_vertices),
00031 storage_size_(StorageSize(n_vertices))
00032 {
00033 data_ = static_cast<uint64_t*>(malloc(this->num_bytes()));
00034 end_ = data_+storage_size_*num_vertices_;
00035 memset(data_, 0, this->num_bytes());
00036 }
00037
00038 AdjacencyBitmap(const DistanceMatrix& dmat_a,
00039 const DistanceMatrix& dmat_b,
00040 Real threshold, Real radius,
00041 int start, int end):
00042 num_vertices_(end-start),
00043 storage_size_(StorageSize(end-start)),
00044 data_(static_cast<uint64_t*>(malloc(this->num_bytes()))),
00045 end_(data_+storage_size_*num_vertices_) {
00046
00047 for (int i = start; i < end; ++i) {
00048 this->set(i-start, i-start, false, false);
00049 for (int j = i+1; j < end; ++j) {
00050 Real da = dmat_a.Get(i, j);
00051 Real db = dmat_b.Get(i, j);
00052 bool defined = da>=0 && db>=0 && (db < radius || da < radius);
00053 bool agree = false;
00054 if (defined) {
00055 agree = std::abs(da-db)<threshold;
00056 }
00057 this->set(i-start, j-start, agree, defined);
00058 this->set(j-start, i-start, agree, defined);
00059 }
00060 }
00061 }
00062
00063 AdjacencyBitmap(const SimilarityMatrix& sim, Real threshold):
00064 num_vertices_(sim.GetSize()),
00065 storage_size_(StorageSize(sim.GetSize())),
00066 data_(static_cast<uint64_t*>(malloc(this->num_bytes()))),
00067 end_(data_+storage_size_*num_vertices_) {
00068 memset(data_, 0, this->num_bytes());
00069 for (int i = 0; i < sim.GetSize(); ++i) {
00070 this->set(i, i, false, false);
00071 for (int j = i+1; j < sim.GetSize(); ++j) {
00072 this->set(i, j, sim.Get(i, j)>threshold, sim.Get(i, j)>=0.0);
00073 this->set(j, i, sim.Get(i, j)>threshold, sim.Get(i, j)>=0.0);
00074 }
00075 }
00076 }
00077
00078 AdjacencyBitmap(const AdjacencyBitmap& rhs):
00079 num_vertices_(rhs.num_vertices_),
00080 storage_size_(rhs.storage_size_),
00081 data_(static_cast<uint64_t*>(malloc(this->num_bytes()))),
00082 end_(data_+storage_size_*num_vertices_) {
00083 memcpy(data_, rhs.data_, this->num_bytes());
00084 }
00085
00086 ~AdjacencyBitmap() {
00087 if (data_)
00088 free(data_);
00089 }
00090
00091
00092
00093 OverlapResult Overlap(size_t vert_i, size_t vert_j) const;
00094
00095 void set(size_t i, size_t j, bool val, bool defined=true) {
00096 size_t byte_index = i*storage_size_ + j/(4*sizeof(uint64_t));
00097 assert(byte_index < this->num_bytes());
00098 size_t bit_index = (2*j) % BITS;
00099
00100
00101 uint64_t one = 1;
00102 uint64_t two = 2;
00103 assert(bit_index < sizeof(uint64_t)*8);
00104
00105 if (val) {
00106 data_[byte_index] |= (one << bit_index);
00107 } else {
00108 data_[byte_index] &= ~(one << bit_index);
00109 }
00110
00111 if (defined) {
00112 data_[byte_index] |= (two << bit_index);
00113 } else {
00114 data_[byte_index] &= ~(two << bit_index);
00115 }
00116 }
00117
00118 bool get(size_t i, size_t j) const {
00119 uint64_t one = 1;
00120 size_t byte_index = i*storage_size_ + j/(4*sizeof(uint64_t));
00121 assert(byte_index < storage_size_*num_vertices_);
00122 size_t bit_index = (2*j) % BITS;
00123 assert(bit_index < sizeof(uint64_t)*8);
00124 return data_[byte_index] & (one << bit_index);
00125 }
00126
00127 bool defined(size_t i, size_t j) const {
00128 uint64_t two = 2;
00129 size_t byte_index = i*storage_size_ + j/(4*sizeof(uint64_t));
00130 assert(byte_index < storage_size_*num_vertices_);
00131 size_t bit_index = (2*j) % BITS;
00132 assert(bit_index < sizeof(uint64_t)*8);
00133 return data_[byte_index] & (two << bit_index);
00134 }
00135
00136 size_t size() const {
00137 return num_vertices_;
00138 }
00139
00140 size_t num_bytes() const {
00141 return storage_size_*num_vertices_*sizeof(uint64_t);
00142 }
00143 void dump() const;
00144 private:
00145
00146 AdjacencyBitmap();
00147 AdjacencyBitmap& operator=(const AdjacencyBitmap&);
00148
00149 const static size_t BITS=sizeof(uint64_t)*8;
00150 size_t num_vertices_;
00151 size_t storage_size_;
00152 uint64_t* data_;
00153 uint64_t* end_;
00154
00155 static const uint8_t NUMBER_OF_BITS_SET[];
00156 static const uint8_t NUMBER_OF_EDGES_SET[];
00157 static size_t StorageSize(size_t verts) {
00158 return verts/(4*sizeof(uint64_t)) + (verts % (sizeof(uint64_t)*4) ? 1 : 0);
00159 }
00160 };
00161
00162
00163 }}}
00164 #endif
00165