00001 #ifndef ALGO_ALIGN_UTIL_HITCOMPARATOR__HPP
00002 #define ALGO_ALIGN_UTIL_HITCOMPARATOR__HPP
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
00028
00029
00030
00031
00032
00033
00034
00035 #include <corelib/ncbiobj.hpp>
00036 #include <algo/align/util/algo_align_util_exceptions.hpp>
00037
00038 BEGIN_NCBI_SCOPE
00039
00040
00041 template<class THit>
00042 class CHitComparator: public CObject
00043 {
00044 public:
00045
00046 typedef CRef<THit> THitRef;
00047
00048
00049 enum ESortCriterion {
00050 eQueryMin,
00051 eQueryMinQueryMax,
00052 eSubjMin,
00053 eSubjMinSubjMax,
00054 eQueryMinScore,
00055 eSubjMinScore,
00056 eSubjMaxQueryMax,
00057 eQueryId,
00058 eSubjId,
00059 eSubjIdQueryId,
00060 eSubjStrand,
00061 eQueryIdSubjIdSubjStrand
00062 };
00063
00064 CHitComparator(ESortCriterion sort_type): m_SortType (sort_type) {}
00065
00066 bool operator() (const THitRef& lhs, const THitRef& rhs) const;
00067
00068 private:
00069
00070 ESortCriterion m_SortType;
00071 };
00072
00073
00074
00075
00076
00077
00078 template<class THit>
00079 bool CHitComparator<THit>::operator() (const THitRef& lhs,
00080 const THitRef& rhs) const
00081 {
00082 bool rv;
00083
00084 switch(m_SortType) {
00085
00086 case eQueryMin:
00087
00088 rv = lhs->GetQueryMin() < rhs->GetQueryMin();
00089 break;
00090
00091 case eQueryMinQueryMax:
00092 {
00093 const typename THit::TCoord qmin_lhs (lhs->GetQueryMin());
00094 const typename THit::TCoord qmin_rhs (rhs->GetQueryMin());
00095 if(qmin_lhs != qmin_rhs) {
00096 return qmin_lhs < qmin_rhs;
00097 }
00098 else {
00099 return lhs->GetQueryMax() < rhs->GetQueryMax();
00100 }
00101 }
00102 break;
00103
00104 case eSubjMin:
00105
00106 rv = lhs->GetSubjMin() < rhs->GetSubjMin();
00107 break;
00108
00109 case eSubjMinSubjMax:
00110 {
00111 const typename THit::TCoord smin_lhs = lhs->GetSubjMin();
00112 const typename THit::TCoord smin_rhs = rhs->GetSubjMin();
00113 if(smin_lhs != smin_rhs) {
00114 return smin_lhs < smin_rhs;
00115 }
00116 else {
00117 return lhs->GetSubjMax() < rhs->GetSubjMax();
00118 }
00119 }
00120 break;
00121
00122 case eQueryMinScore:
00123 {
00124 const typename THit::TCoord qmin_lhs = lhs->GetQueryMin();
00125 const typename THit::TCoord qmin_rhs = rhs->GetQueryMin();
00126 if(qmin_lhs == qmin_rhs) {
00127 return lhs->GetScore() > rhs->GetScore();
00128 }
00129 else {
00130 return qmin_lhs < qmin_rhs;
00131 }
00132 }
00133 break;
00134
00135 case eSubjMinScore:
00136 {
00137 const typename THit::TCoord smin_lhs = lhs->GetSubjMin();
00138 const typename THit::TCoord smin_rhs = rhs->GetSubjMin();
00139 if(smin_lhs == smin_rhs) {
00140 return lhs->GetScore() > rhs->GetScore();
00141 }
00142 else {
00143 return smin_lhs < smin_rhs;
00144 }
00145 }
00146 break;
00147
00148
00149 case eSubjMaxQueryMax:
00150 {
00151 const typename THit::TCoord lhs_subj_max = lhs->GetSubjMax();
00152 const typename THit::TCoord rhs_subj_max = rhs->GetSubjMax();
00153
00154 if(lhs_subj_max < rhs_subj_max) {
00155 rv = true;
00156 }
00157 else if(lhs_subj_max > rhs_subj_max) {
00158 rv = false;
00159 }
00160 else {
00161 rv = lhs->GetQueryMax() < rhs->GetQueryMax();
00162 }
00163 }
00164 break;
00165
00166 case eQueryId:
00167
00168 rv = *(lhs->GetQueryId()) < *(rhs->GetQueryId());
00169 break;
00170
00171 case eSubjId:
00172
00173 rv = *(lhs->GetSubjId()) < *(rhs->GetSubjId());
00174 break;
00175
00176 case eSubjIdQueryId:
00177 {
00178 const int co = lhs->GetSubjId()->CompareOrdered( *(rhs->GetSubjId()) );
00179 if(co == 0) {
00180 rv = *(lhs->GetQueryId()) < *(rhs->GetQueryId());
00181 }
00182 else {
00183 rv = co < 0;
00184 }
00185 }
00186 break;
00187
00188 case eSubjStrand:
00189
00190 rv = lhs->GetSubjStrand() < rhs->GetSubjStrand();
00191 break;
00192
00193
00194 case eQueryIdSubjIdSubjStrand:
00195 {
00196 const int qid = lhs->GetQueryId()->CompareOrdered(*(rhs->GetQueryId()));
00197 const int sid = lhs->GetSubjId()->CompareOrdered(*(rhs->GetSubjId()));
00198
00199 if(qid == 0) {
00200 if(sid == 0) {
00201 const bool subj_strand_lhs = lhs->GetSubjStrand();
00202 const bool subj_strand_rhs = rhs->GetSubjStrand();
00203 return subj_strand_lhs > subj_strand_rhs;
00204 }
00205 else {
00206 rv = sid < 0;
00207 }
00208 }
00209 else {
00210 rv = qid < 0;
00211 }
00212 }
00213 break;
00214
00215 default:
00216
00217 NCBI_THROW(CAlgoAlignUtilException, eInternal,
00218 "CHitComparator: Sorting criterion not supported.");
00219 };
00220
00221 return rv;
00222 }
00223
00224 END_NCBI_SCOPE
00225
00226 #endif
00227
00228