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
|
#ifndef SHASTA_SEQAN_HPP
#define SHASTA_SEQAN_HPP
// Wrappers to simplify SeqAn calls.
// Shasta.
#include "SHASTA_ASSERT.hpp"
// Seqan.
#include <seqan/align.h>
// Standard library.
#include "utility.hpp"
#include "vector.hpp"
namespace shasta {
// Align two integer sequences using SeqAn and
// return the alignment score.
// The alignment is returned as a vector of pairs of bools,
// where false indicates a gap and true indicates a sequence element.
// For example, the following alignment
// ACTG-A
// A-TGTA
// would be returned as
// {true, true}, A,A
// {true, false} C,-
// {true, true}, T,T
// {true, true} G,G
// {false, true}, -,T
// {true, true} A,A
template<class Iterator>
int64_t seqanAlign(
Iterator begin0, Iterator end0,
Iterator begin1, Iterator end1,
int64_t matchScore,
int64_t mismatchScore,
int64_t gapScore,
bool freeOnLeft,
bool freeOnRight,
vector< pair<bool, bool> >& alignment);
// Same, banded.
template<class Iterator>
int64_t seqanAlign(
Iterator begin0, Iterator end0,
Iterator begin1, Iterator end1,
int64_t matchScore,
int64_t mismatchScore,
int64_t gapScore,
int64_t bandMin,
int64_t bandMax,
bool freeOnLeft,
bool freeOnRight,
vector< pair<bool, bool> >& alignment);
// Find out if the alignment computed by seqanAlign contains mismatches.
template<class Iterator>
bool containsMismatches(
Iterator begin0, Iterator end0,
Iterator begin1, Iterator end1,
const vector< pair<bool, bool> >& alignment);
// Given an alignment computed by seqanAlign, find positions in the
// two sequences that contain aligned identical symbols.
template<class Iterator>
void findAlignedIdentical(
Iterator begin0, Iterator end0,
Iterator begin1, Iterator end1,
const vector< pair<bool, bool> >& alignment,
vector< pair<uint64_t, uint64_t> >& alignedIdenticalPositions);
}
template<class Iterator>
int64_t shasta::seqanAlign(
Iterator begin0, Iterator end0,
Iterator begin1, Iterator end1,
int64_t matchScore,
int64_t mismatchScore,
int64_t gapScore,
bool freeOnLeft,
bool freeOnRight,
vector< pair<bool, bool> >& alignment)
{
// SeqAn does not handle empty sequences.
SHASTA_ASSERT(begin0 != end0);
SHASTA_ASSERT(begin1 != end1);
// SeqAn types used below.
using namespace seqan2;
using Int = typename Iterator::value_type;
using Sequence = String<Int>;
using StringSet = seqan2::StringSet<Sequence>;
using DepStringSet = seqan2::StringSet<Sequence, Dependent<> >;
using AlignGraph = Graph<seqan2::Alignment<DepStringSet> >;
// Fill in the sequences, adding 100 to all values
// because SeqAn uses 45 to represent gaps.
Sequence sequence0;
for(Iterator it=begin0; it!=end0; it) {
appendValue(sequence0, *it 100);
}
Sequence sequence1;
for(Iterator it=begin1; it!=end1; it) {
appendValue(sequence1, *it 100);
}
// Store them in a SeqAn string set.
StringSet sequences;
appendValue(sequences, sequence0);
appendValue(sequences, sequence1);
// Compute the alignment.
// See https://docs.seqan.de/seqan/2.1.0/class_AlignConfig.html
// for meaning of AlignConfig.
AlignGraph graph(sequences);
int64_t alignmentScore = 0;
if(freeOnLeft) {
if(freeOnRight) {
// Free on both sides.
alignmentScore = globalAlignment(
graph,
Score<int64_t, seqan2::Simple>(matchScore, mismatchScore, gapScore),
AlignConfig<true, true, true, true>(),
LinearGaps());
} else {
// Free on left only.
alignmentScore = globalAlignment(
graph,
Score<int64_t, seqan2::Simple>(matchScore, mismatchScore, gapScore),
AlignConfig<true, true, false, false>(),
LinearGaps());
}
}else {
if(freeOnRight) {
// Free on right only.
alignmentScore = globalAlignment(
graph,
Score<int64_t, seqan2::Simple>(matchScore, mismatchScore, gapScore),
AlignConfig<false, false, true, true>(),
LinearGaps());
} else {
// Free on neither side.
alignmentScore = globalAlignment(
graph,
Score<int64_t, seqan2::Simple>(matchScore, mismatchScore, gapScore),
AlignConfig<false, false, false, false>(),
LinearGaps());
}
}
// Extract the alignment from the graph.
// This creates a single sequence consisting of the two rows
// of the alignment, concatenated.
Sequence align;
convertAlignment(graph, align);
const uint64_t totalAlignmentLength = seqan2::length(align);
SHASTA_ASSERT((totalAlignmentLength % 2) == 0);
const uint64_t alignmentLength = totalAlignmentLength / 2;
// Fill in the bool pairs representing the alignment.
alignment.resize(alignmentLength);
for(uint64_t i=0; i<alignmentLength; i ) {
auto& p = alignment[i];
p.first = not (align[i] == 45);
p.second = not (align[i alignmentLength] == 45);
}
return alignmentScore;
}
// Same, banded.
template<class Iterator>
int64_t shasta::seqanAlign(
Iterator begin0, Iterator end0,
Iterator begin1, Iterator end1,
int64_t matchScore,
int64_t mismatchScore,
int64_t gapScore,
int64_t bandMin,
int64_t bandMax,
bool freeOnLeft,
bool freeOnRight,
vector< pair<bool, bool> >& alignment)
{
// SeqAn does not handle empty sequences.
SHASTA_ASSERT(begin0 != end0);
SHASTA_ASSERT(begin1 != end1);
// SeqAn types used below.
using namespace seqan2;
using Int = typename Iterator::value_type;
using Sequence = String<Int>;
using StringSet = seqan2::StringSet<Sequence>;
using DepStringSet = seqan2::StringSet<Sequence, Dependent<> >;
using AlignGraph = Graph<seqan2::Alignment<DepStringSet> >;
// Fill in the sequences, adding 100 to all values
// because SeqAn uses 45 to represent gaps.
Sequence sequence0;
for(Iterator it=begin0; it!=end0; it) {
appendValue(sequence0, *it 100);
}
Sequence sequence1;
for(Iterator it=begin1; it!=end1; it) {
appendValue(sequence1, *it 100);
}
// Store them in a SeqAn string set.
StringSet sequences;
appendValue(sequences, sequence0);
appendValue(sequences, sequence1);
// Compute the alignment.
// See https://docs.seqan.de/seqan/2.1.0/class_AlignConfig.html
// for meaning of AlignConfig.
AlignGraph graph(sequences);
int64_t alignmentScore = 0;
if(freeOnLeft) {
if(freeOnRight) {
// Free on both sides.
alignmentScore = globalAlignment(
graph,
Score<int64_t, seqan2::Simple>(matchScore, mismatchScore, gapScore),
AlignConfig<true, true, true, true>(),
int32_t(bandMin), int32_t(bandMax),
LinearGaps());
} else {
// Free on left only.
alignmentScore = globalAlignment(
graph,
Score<int64_t, seqan2::Simple>(matchScore, mismatchScore, gapScore),
AlignConfig<true, true, false, false>(),
int32_t(bandMin), int32_t(bandMax),
LinearGaps());
}
}else {
if(freeOnRight) {
// Free on right only.
alignmentScore = globalAlignment(
graph,
Score<int64_t, seqan2::Simple>(matchScore, mismatchScore, gapScore),
AlignConfig<false, false, true, true>(),
int32_t(bandMin), int32_t(bandMax),
LinearGaps());
} else {
// Free on neither side.
alignmentScore = globalAlignment(
graph,
Score<int64_t, seqan2::Simple>(matchScore, mismatchScore, gapScore),
AlignConfig<false, false, false, false>(),
int32_t(bandMin), int32_t(bandMax),
LinearGaps());
}
}
// Extract the alignment from the graph.
// This creates a single sequence consisting of the two rows
// of the alignment, concatenated.
Sequence align;
convertAlignment(graph, align);
const uint64_t totalAlignmentLength = seqan2::length(align);
SHASTA_ASSERT((totalAlignmentLength % 2) == 0);
const uint64_t alignmentLength = totalAlignmentLength / 2;
// Fill in the bool pairs representing the alignment.
alignment.resize(alignmentLength);
for(uint64_t i=0; i<alignmentLength; i ) {
auto& p = alignment[i];
p.first = not (align[i] == 45);
p.second = not (align[i alignmentLength] == 45);
}
return alignmentScore;
}
// Find out if the alignment computed by seqanAlign contains mismatches.
template<class Iterator>
bool shasta::containsMismatches(
Iterator begin0, Iterator end0,
Iterator begin1, Iterator end1,
const vector< pair<bool, bool> >& alignment)
{
Iterator it0 = begin0;
Iterator it1 = begin1;
for(const auto& p: alignment) {
if(p.first and p.second) {
if(*it0 != *it1) {
return true;
}
it0;
it1;
} else if(p.first) {
it0;
} else if(p.second) {
it1;
}
}
SHASTA_ASSERT(it0 == end0);
SHASTA_ASSERT(it1 == end1);
return false;
}
// Given an alignment computed by seqanAlign, find positions in the
// two sequences that contain aligned identical symbols.
template<class Iterator>
void shasta::findAlignedIdentical(
Iterator begin0, Iterator end0,
Iterator begin1, Iterator end1,
const vector< pair<bool, bool> >& alignment,
vector< pair<uint64_t, uint64_t> >& alignedIdenticalPositions)
{
alignedIdenticalPositions.clear();
Iterator it0 = begin0;
Iterator it1 = begin1;
uint64_t position0 = 0;
uint64_t position1 = 0;
for(const auto& p: alignment) {
if(p.first and p.second) {
if(*it0 == *it1) {
alignedIdenticalPositions.push_back(make_pair(position0, position1));
}
it0;
it1;
position0;
position1;
} else if(p.first) {
it0;
position0;
} else if(p.second) {
it1;
position1;
}
}
SHASTA_ASSERT(it0 == end0);
SHASTA_ASSERT(it1 == end1);
}
#endif
|