| 1 |
#ifndef MSA_H |
| 2 |
#define MSA_H |
| 3 |
|
| 4 |
#include <boost/multi_array.hpp> |
| 5 |
#include "needle.h" |
| 6 |
#include "gotoh.h" |
| 7 |
|
| 8 |
namespace torali { |
| 9 |
|
| 10 |
inline int32_t |
| 11 |
lcs(std::string const& s1, std::string const& s2) { |
| 12 |
uint32_t m = s1.size(); |
| 13 |
uint32_t n = s2.size(); |
| 14 |
int32_t prevdiag = 0; |
| 15 |
std::vector<int32_t> onecol(n+1, 0); |
| 16 |
for(uint32_t i = 0; i <= m; ++i) { |
| 17 |
for(uint32_t j = 0; j <= n; ++j) { |
| 18 |
if ((i==0) || (j==0)) { |
| 19 |
onecol[j] = 0; |
| 20 |
prevdiag = 0; |
| 21 |
} else { |
| 22 |
int32_t prevprevdiag = prevdiag; |
| 23 |
prevdiag = onecol[j]; |
| 24 |
if (s1[i-1] == s2[j-1]) onecol[j] = prevprevdiag + 1; |
| 25 |
else onecol[j] = (onecol[j] > onecol[j-1]) ? onecol[j] : onecol[j-1]; |
| 26 |
} |
| 27 |
} |
| 28 |
} |
| 29 |
return onecol[n]; |
| 30 |
} |
| 31 |
|
| 32 |
template<typename TSplitReadSet, typename TDistArray> |
| 33 |
inline void |
| 34 |
distanceMatrix(TSplitReadSet const& sps, TDistArray& d) { |
| 35 |
typedef typename TDistArray::index TDIndex; |
| 36 |
typename TSplitReadSet::const_iterator sIt1 = sps.begin(); |
| 37 |
for (TDIndex i = 0; sIt1 != sps.end(); ++sIt1, ++i) { |
| 38 |
typename TSplitReadSet::const_iterator sIt2 = sIt1; |
| 39 |
++sIt2; |
| 40 |
for (TDIndex j = i+1; sIt2 != sps.end(); ++sIt2, ++j) { |
| 41 |
d[i][j] = (lcs(*sIt1, *sIt2) * 100) / std::min(sIt1->size(), sIt2->size()); |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
template<typename TDistArray, typename TDIndex> |
| 47 |
inline int |
| 48 |
closestPair(TDistArray const& d, TDIndex num, TDIndex& dI, TDIndex& dJ) { |
| 49 |
int dMax = -1; |
| 50 |
for (TDIndex i = 0; i<num; ++i) { |
| 51 |
for (TDIndex j = i+1; j<num; ++j) { |
| 52 |
if (d[i][j]>dMax) { |
| 53 |
dMax = d[i][j]; |
| 54 |
dI = i; |
| 55 |
dJ = j; |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
return dMax; |
| 60 |
} |
| 61 |
|
| 62 |
template<typename TDistArray, typename TPhylogeny, typename TDIndex> |
| 63 |
inline void |
| 64 |
updateDistanceMatrix(TDistArray& d, TPhylogeny const& p, TDIndex num, TDIndex& dI, TDIndex& dJ) { |
| 65 |
for (TDIndex i = 0; i < num; ++i) |
| 66 |
if (p[i][0] == -1) |
| 67 |
d[i][num] = (((dI < i) ? d[dI][i] : d[i][dI]) + ((dJ < i) ? d[dJ][i] : d[i][dJ])) / 2; |
| 68 |
for (TDIndex i = 0; i<dI; ++i) d[i][dI] = -1; |
| 69 |
for (TDIndex i = dI+1; i<num+1; ++i) d[dI][i] = -1; |
| 70 |
for (TDIndex i = 0; i<dJ; ++i) d[i][dJ] = -1; |
| 71 |
for (TDIndex i = dJ+1; i<num+1; ++i) d[dJ][i] = -1; |
| 72 |
} |
| 73 |
|
| 74 |
template<typename TDistArray, typename TPhylogeny, typename TDIndex> |
| 75 |
inline TDIndex |
| 76 |
upgma(TDistArray& d, TPhylogeny& p, TDIndex num) { |
| 77 |
TDIndex nn = num; |
| 78 |
for(;nn<2*num+1; ++nn) { |
| 79 |
TDIndex dI = 0; |
| 80 |
TDIndex dJ = 0; |
| 81 |
if (closestPair(d, nn, dI, dJ) == -1) break; |
| 82 |
p[dI][0] = nn; |
| 83 |
p[dJ][0] = nn; |
| 84 |
p[nn][1] = dI; |
| 85 |
p[nn][2] = dJ; |
| 86 |
updateDistanceMatrix(d, p, nn, dI, dJ); |
| 87 |
} |
| 88 |
return (nn > 0) ? (nn - 1) : 0; |
| 89 |
} |
| 90 |
|
| 91 |
template<typename TConfig, typename TSplitReadSet, typename TPhylogeny, typename TDIndex, typename TAlign> |
| 92 |
inline void |
| 93 |
palign(TConfig const& c, TSplitReadSet const& sps, TPhylogeny const& p, TDIndex root, TAlign& align) { |
| 94 |
typedef typename TAlign::index TAIndex; |
| 95 |
if ((p[root][1] == -1) && (p[root][2] == -1)) { |
| 96 |
typename TSplitReadSet::const_iterator sIt = sps.begin(); |
| 97 |
if (root) std::advance(sIt, root); |
| 98 |
align.resize(boost::extents[1][sIt->size()]); |
| 99 |
TAIndex ind = 0; |
| 100 |
for(typename std::string::const_iterator str = sIt->begin(); str != sIt->end(); ++str) align[0][ind++] = *str; |
| 101 |
} else { |
| 102 |
TAlign align1; |
| 103 |
palign(c, sps, p, p[root][1], align1); |
| 104 |
TAlign align2; |
| 105 |
palign(c, sps, p, p[root][2], align2); |
| 106 |
AlignConfig<true, true> endFreeAlign; |
| 107 |
gotoh(align1, align2, align, endFreeAlign, c.aliscore); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
template<typename TConfig, typename TAlign> |
| 112 |
inline void |
| 113 |
consensus(TConfig const& c, TAlign const& align, std::string& gapped, std::string& cs) { |
| 114 |
typedef typename TAlign::index TAIndex; |
| 115 |
|
| 116 |
|
| 117 |
typedef boost::multi_array<bool, 2> TFlag; |
| 118 |
TFlag fl(boost::extents[align.shape()[0]][align.shape()[1]]); |
| 119 |
typedef std::vector<int> TCoverage; |
| 120 |
TCoverage cov; |
| 121 |
cov.resize(align.shape()[1], 0); |
| 122 |
for(TAIndex i = 0; i < (TAIndex) align.shape()[0]; ++i) { |
| 123 |
int start = 0; |
| 124 |
int end = -1; |
| 125 |
for(TAIndex j = 0; j < (TAIndex) align.shape()[1]; ++j) { |
| 126 |
fl[i][j] = false; |
| 127 |
if (align[i][j] != '-') end = j; |
| 128 |
else if (end == -1) start = j + 1; |
| 129 |
} |
| 130 |
for(TAIndex j = start; j<=end; ++j) { |
| 131 |
++cov[j]; |
| 132 |
fl[i][j] = true; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
int covThreshold = c.minCliqueSize; |
| 137 |
TAIndex j = 0; |
| 138 |
std::vector<char> cons(align.shape()[1], '-'); |
| 139 |
for(typename TCoverage::const_iterator itCov = cov.begin(); itCov != cov.end(); ++itCov, ++j) { |
| 140 |
int32_t maxIdx = 4; |
| 141 |
if (*itCov >= covThreshold) { |
| 142 |
|
| 143 |
std::vector<int32_t> count(5, 0); |
| 144 |
for(TAIndex i = 0; i < (TAIndex) align.shape()[0]; ++i) { |
| 145 |
if (fl[i][j]) { |
| 146 |
if ((align[i][j] == 'A') || (align[i][j] == 'a')) ++count[0]; |
| 147 |
else if ((align[i][j] == 'C') || (align[i][j] == 'c')) ++count[1]; |
| 148 |
else if ((align[i][j] == 'G') || (align[i][j] == 'g')) ++count[2]; |
| 149 |
else if ((align[i][j] == 'T') || (align[i][j] == 't')) ++count[3]; |
| 150 |
else ++count[4]; |
| 151 |
} |
| 152 |
} |
| 153 |
maxIdx = 0; |
| 154 |
int32_t maxCount = count[0]; |
| 155 |
for(uint32_t i = 1; i<5; ++i) { |
| 156 |
if (count[i] > maxCount) { |
| 157 |
maxCount = count[i]; |
| 158 |
maxIdx = i; |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
switch (maxIdx) { |
| 163 |
case 0: cons[j] = 'A'; break; |
| 164 |
case 1: cons[j] = 'C'; break; |
| 165 |
case 2: cons[j] = 'G'; break; |
| 166 |
case 3: cons[j] = 'T'; break; |
| 167 |
default: break; |
| 168 |
} |
| 169 |
} |
| 170 |
gapped = std::string(cons.begin(), cons.end()); |
| 171 |
for(uint32_t i = 0; i<cons.size(); ++i) { |
| 172 |
if (cons[i] != '-') cs.push_back(cons[i]); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
template<typename TConfig, typename TAlign> |
| 177 |
inline void |
| 178 |
consensus(TConfig const& c, TAlign const& align, std::string& cs) { |
| 179 |
std::string gapped; |
| 180 |
consensus(c, align, gapped, cs); |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
} |
| 185 |
|
| 186 |
template<typename TConfig, typename TSplitReadSet> |
| 187 |
inline int |
| 188 |
msa(TConfig const& c, TSplitReadSet const& sps, std::string& cs) { |
| 189 |
|
| 190 |
typedef boost::multi_array<int, 2> TDistArray; |
| 191 |
typedef typename TDistArray::index TDIndex; |
| 192 |
TDIndex num = sps.size(); |
| 193 |
TDistArray d(boost::extents[2*num+1][2*num+1]); |
| 194 |
for (TDIndex i = 0; i<(2*num+1); ++i) |
| 195 |
for (TDIndex j = i+1; j<(2*num+1); ++j) |
| 196 |
d[i][j]=-1; |
| 197 |
distanceMatrix(sps, d); |
| 198 |
|
| 199 |
|
| 200 |
typedef boost::multi_array<int, 2> TPhylogeny; |
| 201 |
TPhylogeny p(boost::extents[2*num+1][3]); |
| 202 |
for(TDIndex i = 0; i<(2*num+1); ++i) |
| 203 |
for (TDIndex j = 0; j<3; ++j) p[i][j] = -1; |
| 204 |
TDIndex root = upgma(d, p, num); |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
typedef boost::multi_array<char, 2> TAlign; |
| 221 |
TAlign align; |
| 222 |
palign(c, sps, p, root, align); |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
consensus(c, align, cs); |
| 237 |
|
| 238 |
|
| 239 |
return align.shape()[0]; |
| 240 |
} |
| 241 |
|
| 242 |
template<typename TStructuralVariant> |
| 243 |
inline void |
| 244 |
outputConsensus(bam_hdr_t* hdr, TStructuralVariant const& sv, std::string const& cons) { |
| 245 |
std::cerr << ">" << hdr->target_name[sv.chr] << ':' << sv.svStart << ',' << hdr->target_name[sv.chr2] << ':' << sv.svEnd << " SVT:" << sv.svt << " SR:" << sv.srSupport << " PE:" << sv.peSupport << std::endl; |
| 246 |
std::cerr << cons << std::endl; |
| 247 |
} |
| 248 |
|
| 249 |
} |
| 250 |
|
| 251 |
#endif |