1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
|
#include "trianglePlot.h"
TrianglePlot::TrianglePlot(unsigned long centreCoord) : totalDepth_(0), depthSum_(0), centreCoord_(centreCoord), totalFragLength_(0) {}
void TrianglePlot::shift(unsigned long n)
{
// total depth is unchanged.
centreCoord_ += n;
depthSum_ -= n * totalDepth_;
// remove fragments which end before the new centre position
while (fragments_.size())
{
multiset< pair<unsigned long, unsigned long> >::iterator p = fragments_.begin();
// remove this coord, if necessary
if (p->second < centreCoord_)
{
// translate the coords
long start = p->first - centreCoord_;
long end = p->second - centreCoord_;
// update the depths
depthSum_ += ( start * (start - 1) - end * (end + 1) ) / 2;
totalDepth_ += start - end - 1;
// update the total frag length
totalFragLength_ -= end - start + 1;
// forget about this fragment: it doesn't cover the centre position
fragments_.erase(p);
}
else
{
break;
}
}
}
void TrianglePlot::move(unsigned long n)
{
shift(n - centreCoord_);
}
bool TrianglePlot::add(pair<unsigned long, unsigned long>& fragment)
{
// add the fragment, if possible
if (fragment.first <= centreCoord_ && centreCoord_ <= fragment.second)
{
// translate the coords
long start = fragment.first - centreCoord_;
long end = fragment.second - centreCoord_;
// update the depths
depthSum_ += ( end * (end + 1) - start * (start - 1)) / 2;
totalDepth_ += end - start + 1;
totalFragLength_ += end - start + 1;
// update the list of fragments covering this plot
fragments_.insert(fragments_.end(), fragment);
return true;
}
else if (fragment.second < centreCoord_)
{
return true;
}
// if can't add the fragment, nothing to do
else
{
return false;
}
}
void TrianglePlot::add(multiset<pair<unsigned long, unsigned long> >& frags)
{
multiset<pair<unsigned long, unsigned long> >::iterator p;
while (frags.size())
{
p = frags.begin();
pair<unsigned long, unsigned long> frag = *p;
if (add(frag))
{
frags.erase(p);
}
else
{
break;
}
}
}
double TrianglePlot::mean()
{
return totalDepth_ == 0 ? 0 : 1.0 * depthSum_ / totalDepth_;
}
unsigned long TrianglePlot::centreCoord()
{
return centreCoord_;
}
unsigned long TrianglePlot::depth()
{
return fragments_.size();
}
double TrianglePlot::meanFragLength()
{
return fragments_.size() ? totalFragLength_ / fragments_.size() : 0;
}
void TrianglePlot::clear(unsigned long n)
{
fragments_.clear();
totalDepth_ = 0;
depthSum_ = 0;
centreCoord_ = n;
totalFragLength_ = 0;
}
bool TrianglePlot::comparePairBySecond(pair<unsigned long, unsigned long>& i, pair<unsigned long, unsigned long>& j)
{
return i.second < j.second;
}
void TrianglePlot::getHeights(unsigned long maxInsert, vector<double>& leftHeights, vector<double>& rightHeights)
{
if (fragments_.size() == 0)
return;
leftHeights.clear();
for (unsigned long i = 0; i <= maxInsert; i++)
{
leftHeights.push_back(0);
}
rightHeights.clear();
for (unsigned long i = 0; i <= maxInsert; i++)
{
rightHeights.push_back(0);
}
unsigned long rightHeight = 0;
for (multiset< pair<unsigned long, unsigned long> >:: iterator p = fragments_.begin(); p != fragments_.end(); p++)
{
rightHeights[p->second - centreCoord_]++;
rightHeight++;
leftHeights[centreCoord_ - p->first]++;
}
unsigned long leftHeight = fragments_.size();
for(unsigned long i = 1; i < maxInsert - 1; i++)
{
leftHeights[i-1] = 1.0 * leftHeight / fragments_.size();
leftHeight -= leftHeights[i];
rightHeights[i-1] = 1.0 * rightHeight / fragments_.size();
rightHeight -= rightHeights[i];
}
}
string TrianglePlot::toString(unsigned long maxInsert)
{
if (fragments_.size() == 0)
{
return "";
}
stringstream ss;
vector<unsigned long> leftHeights(maxInsert, 0);
vector<unsigned long> rightHeights(maxInsert, 0);
unsigned long rightHeight = 0;
for (multiset< pair<unsigned long, unsigned long> >:: iterator p = fragments_.begin(); p != fragments_.end(); p++)
{
rightHeights[p->second - centreCoord_]++;
rightHeight++;
leftHeights[centreCoord_ - p->first]++;
}
unsigned long height = 0;
for (unsigned long i = maxInsert - 1; i > 0; i--)
{
height += leftHeights[i];
ss << height << " ";
}
ss << 0 << " " << fragments_.size() << " ";
for (unsigned long i = 1; i < maxInsert; i++)
{
ss << rightHeight << " ";
rightHeight -= rightHeights[i];
}
ss << rightHeight;
return ss.str();
}
double TrianglePlot::getTheoryHeight(unsigned long gapStart, unsigned long gapEnd, unsigned long position, unsigned long meanInsert)
{
long s = (long) gapStart - (long) centreCoord_; // gap start centred on zero
long e = (long) gapEnd - (long) centreCoord_; // gap end centred on zero
long p = (long) position - (long) centreCoord_; // position centred at zero
long i = (long) meanInsert;
if (p <= -i || p >= i)
{
return 0;
}
// theory height depends on where the gap is relative to the centre of the plot.
if (s <= 0 && 0 <= e)
{
if (p <= e - i || s + i <= p)
return 0;
else if (p < s)
return 1.0 * (p - e + i) / (s + i - e);
else if (p <= e)
return 1.0;
else
return -1.0 * (p - s - i) / (s + i - e);
}
else if (s > 0 && e > i)
{
if (p < s - i)
return 1.0 * (p + i) / s;
else if (p <= 0)
return 1.0;
else if (p < s)
return -1.0 * (p - s) / s;
else
return 0;
}
else if (e < 0 && s < -i)
{
if (p <= e)
return 0;
else if (p < 0)
return -1.0 * (p - e) / e;
else if (p <= e + i)
return 1.0;
else
return 1.0 * (p - i) / e;
}
else
{
if (-i <= s && s <= e && e < 0)
{
s += (long) i;
e += (long) i;
}
if (!(0 <= s && s <= e && e <= i))
{
cerr << "Unexpected error in FCD theory height estimation. Abort!" << endl
<< "gapStart=" << gapStart << ". gapEnd=" << gapEnd << ". position=" << position << ". centre=" << centreCoord_ << ". s=" << s << ". e=" << e << ". i=" << i << endl;
exit(1);
}
if (p < s - i)
return 1.0 * (p + i) / (s + i - e);
else if (p <= e - i)
return 1.0 * s / (s + i - e);
else if (p <= 0)
return 1.0 * (1.0 + 1.0 * p / (s + i - e));
else if (p < s)
return 1.0 * (1.0 - 1.0 * p / (s + i - e));
else if (p <= e)
return 1.0 - 1.0 * s / (s + i - e);
else
return -1.0 * (p - i) / (s + i - e);
}
}
double TrianglePlot::areaError(unsigned long maxInsert, unsigned long meanInsert, bool gapCorrect, unsigned long gapStart, unsigned long gapEnd)
{
if (fragments_.size() == 0) return -1;
double area = 0;
vector<unsigned long> leftHeights(maxInsert + 1, 0);
vector<unsigned long> rightHeights(maxInsert + 1, 0);
unsigned long rightHeight = 0;
for (multiset< pair<unsigned long, unsigned long> >:: iterator p = fragments_.begin(); p != fragments_.end(); p++)
{
rightHeights[min(p->second - centreCoord_, maxInsert)]++;
rightHeight++;
leftHeights[min(centreCoord_ - p->first, maxInsert)]++;
}
unsigned long leftHeight = fragments_.size();
for (unsigned long i = 1; i < maxInsert - 1; i++)
{
leftHeight -= leftHeights[i];
rightHeight -= rightHeights[i];
double theoryLeftHeight;
double theoryRightHeight;
if (gapCorrect)
{
theoryLeftHeight = getTheoryHeight(gapStart, gapEnd, centreCoord_ - i, meanInsert);
theoryRightHeight = getTheoryHeight(gapStart, gapEnd, centreCoord_ + i, meanInsert);
}
else
{
theoryLeftHeight = theoryRightHeight = max(0.0, 1.0 - 1.0 * i / meanInsert);
}
area += abs(theoryLeftHeight - 1.0 * leftHeight / fragments_.size());
area += abs(theoryRightHeight - 1.0 * rightHeight / fragments_.size());
}
return min(5.0, area / meanInsert);
}
void TrianglePlot::optimiseGap(unsigned long maxInsert, unsigned long meanInsert, unsigned long gapStart, unsigned long gapEnd, unsigned long& bestGapLength, double& bestError)
{
// we can only do this if the centre coord of the plot is inside the gap
if (centreCoord_ < gapStart || centreCoord_ > gapEnd)
{
cerr << "Error in TrianglePlot::optimiseGap. coord=" << centreCoord_ << " is not in gap " << gapStart << "-" << gapEnd << endl;
exit(1);
}
unsigned long originalCentreCoord = centreCoord_;
multiset< pair<unsigned long, unsigned long>, fragcomp> originalFragments(fragments_);
unsigned long originalTotalDepth_ = totalDepth_;
unsigned long originalDepthSum = depthSum_;
unsigned long originalTotalFragLength = totalFragLength_;
bestGapLength = 0;
bestError = 999999;
unsigned long bigStep = 10;
for (unsigned long gapLength = 1; gapLength < maxInsert / 2; gapLength += bigStep)
{
clear();
unsigned long thisGapEnd = gapStart + gapLength - 1;
centreCoord_ = gapStart + (thisGapEnd - gapStart) / 2;
for (multiset< pair<unsigned long, unsigned long> >:: iterator p = originalFragments.begin(); p != originalFragments.end(); p++)
{
pair<unsigned long, unsigned long> fragment(p->first, thisGapEnd + p->second - gapEnd);
add(fragment);
}
double error = areaError(maxInsert, meanInsert, true, gapStart, thisGapEnd);
if (error < bestError)
{
bestError = error;
bestGapLength = gapLength;
}
}
unsigned long windowStart = bestGapLength < bigStep ? 1 : bestGapLength - bigStep;
for (unsigned long gapLength = windowStart; gapLength < min(maxInsert / 2, bestGapLength + bigStep - 1); gapLength++)
{
clear();
unsigned long thisGapEnd = gapStart + gapLength - 1;
centreCoord_ = gapStart + (thisGapEnd - gapStart) / 2;
for (multiset< pair<unsigned long, unsigned long> >:: iterator p = originalFragments.begin(); p != originalFragments.end(); p++)
{
pair<unsigned long, unsigned long> fragment(p->first, thisGapEnd + p->second - gapEnd);
add(fragment);
}
double error = areaError(maxInsert, meanInsert, true, gapStart, thisGapEnd);
if (error < bestError)
{
bestError = error;
bestGapLength = gapLength;
}
}
fragments_ = originalFragments;
centreCoord_ = originalCentreCoord;
totalDepth_ = originalTotalDepth_;
depthSum_ = originalDepthSum;
totalFragLength_ = originalTotalFragLength;
}
|