yat  0.15.2pre
BamRead.h
1 #ifndef theplu_yat_omic_bam_read
2 #define theplu_yat_omic_bam_read
3 
4 // $Id: BamRead.h 3691 2017-09-14 07:13:22Z peter $
5 
6 /*
7  Copyright (C) 2012, 2013, 2014, 2015, 2016, 2017 Peter Johansson
8 
9  This file is part of the yat library, http://dev.thep.lu.se/yat
10 
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 3 of the
14  License, or (at your option) any later version.
15 
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with this program. If not, see <http://www.gnu.org/licenses/>.
23 */
24 
25 #include "config_bam.h"
26 
27 #include "yat/utility/Aligner.h"
28 // This file has to be included to keep compatibility with yat 0.11
29 #include "yat/utility/Cigar.h"
30 #include "yat/utility/deprecate.h"
31 
32 #include YAT_SAM_HEADER
33 
34 #include <functional>
35 #include <string>
36 #include <vector>
37 
38 namespace theplu {
39 namespace yat {
40 namespace omic {
41 
53  class BamRead
54  {
55  public:
62  BamRead(void);
63 
67  BamRead(const BamRead& other);
68 
69 #ifdef YAT_HAVE_RVALUE
70 
75  BamRead(BamRead&& other) noexcept;
76 #endif
77 
81  virtual ~BamRead(void);
82 
86  const BamRead& operator=(const BamRead& rhs);
87 
88 #ifdef YAT_HAVE_RVALUE
89 
94  BamRead& operator=(BamRead&& rhs);
95 #endif
96 
102  const uint8_t* aux(void) const;
103 
115  const uint8_t* aux(const char tag[2]) const;
116 
129  void aux_append(const char tag[2], char type, int len, uint8_t* data);
130 
138  void aux_del(const char tag[2]);
139 
145  int aux_size(void) const;
146 
152  const bam1_core_t& core(void) const;
153 
159  bam1_core_t& core(void);
160 
169  const uint32_t* cigar(void) const;
170 
174  uint32_t cigar(size_t i) const;
175 
179  uint32_t cigar_op(size_t i) const;
180 
184  uint32_t cigar_oplen(size_t i) const;
185 
189  std::string cigar_str(void) const;
190 
199  void cigar(const std::vector<uint32_t>& c) YAT_DEPRECATE;
200 
208  void cigar(const utility::Aligner::Cigar& cigar);
209 
218  int32_t end(void) const;
219 
227  uint16_t flag(void) const;
228 
232  int32_t mpos(void) const;
233 
237  int32_t mtid(void) const;
238 
244  const char* name(void) const;
245 
251  void name(const std::string& n);
252 
256  int32_t pos(void) const;
257 
261  uint8_t qual(size_t i) const;
262 
271  void qual(size_t i, uint8_t q);
272 
278  std::string sequence(void) const;
279 
285  uint8_t sequence(size_t index) const;
286 
292  char sequence_str(size_t index) const;
293 
303  void sequence(size_t i, uint8_t x);
304 
310  void sequence(const std::string& seq, const std::vector<uint8_t>& qual);
311 
315  uint32_t sequence_length(void) const;
316 
322  void swap(BamRead& other);
323 
327  int32_t tid(void) const;
328 
329  private:
330  // ensure capacity of data pointer is (at least) n
331  void reserve(uint32_t n);
332 
333  bam1_t* bam_;
334 
335  friend class InBamFile;
336  friend class OutBamFile;
337  friend class BamReadIterator;
338 #ifndef YAT_HAVE_HTSLIB
339  uint32_t calend(const bam1_core_t *c, const uint32_t *cigar) const;
340 #endif
341  // access data length, current length of data
342  int& data_size(void);
343  const int& data_size(void) const;
344  uint32_t data_capacity(void) const;
345  };
346 
360  const unsigned char nt16_table(char);
361 
374  const char nt16_str(uint8_t);
375 
384  void swap(BamRead& lhs, BamRead& rhs);
385 
394  bool soft_clipped(const BamRead& bam);
395 
404  uint32_t left_soft_clipped(const BamRead& bam);
405 
414  uint32_t right_soft_clipped(const BamRead& bam);
415 
425  bool same_query_name(const BamRead& lhs, const BamRead& rhs);
426 
436  bool less_query_name(const BamRead& lhs, const BamRead& rhs);
437 
446  struct BamLessPos
447  : public std::binary_function<const BamRead&, const BamRead&, bool>
448  {
455  bool operator()(const BamRead& lhs, const BamRead& rhs) const;
456  };
457 
458 
467  struct BamLessEnd
468  : public std::binary_function<const BamRead&, const BamRead&, bool>
469  {
476  bool operator()(const BamRead& lhs, const BamRead& rhs) const;
477  };
478 
487  struct BamLessName
488  : public std::binary_function<const BamRead&, const BamRead&, bool>
489  {
493  bool operator()(const BamRead& lhs, const BamRead& rhs) const;
494  };
495 
496 
515  template<typename RandomAccessIterator>
516  double alignment_score(const BamRead& bam, RandomAccessIterator ref,
517  double mismatch, double gap=0.0, double open_gap=0.0)
518  {
519  BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIterator<RandomAccessIterator>));
520  BOOST_CONCEPT_ASSERT((boost_concepts::RandomAccessTraversal<RandomAccessIterator>));
521  double res = 0;
522  size_t q = 0;
523 
524  for (uint32_t i = 0; i<bam.core().n_cigar; ++i) {
525  switch (bam.cigar_op(i)) {
526  case (BAM_CMATCH): {
527  RandomAccessIterator end = ref + bam.cigar_oplen(i);
528  for (; ref!=end; ++ref) {
529  if (bam.sequence_str(q) == *ref)
530  res += 1.0;
531  else
532  res -= mismatch;
533  ++q;
534  }
535  break;
536  }
537  case (BAM_CEQUAL): {
538  q += bam.cigar_oplen(i);
539  ref += bam.cigar_oplen(i);
540  res += bam.cigar_oplen(i);
541  break;
542  }
543  case (BAM_CDIFF): {
544  q += bam.cigar_oplen(i);
545  ref += bam.cigar_oplen(i);
546  res -= bam.cigar_oplen(i)*mismatch;
547  break;
548  }
549  case (BAM_CSOFT_CLIP): {
550  q += bam.cigar_oplen(i);
551  break;
552  }
553  case (BAM_CINS): {
554  q += bam.cigar_oplen(i);
555  res -= open_gap + bam.cigar_oplen(i) * gap;
556  break;
557  }
558  case (BAM_CDEL): {
559  ref += bam.cigar_oplen(i);
560  res -= open_gap + bam.cigar_oplen(i) * gap;
561  break;
562  }
563  } // end switch
564  }
565  return res;
566  }
567 
568 }}}
569 #endif
void aux_del(const char tag[2])
remove a tag in aux field
virtual ~BamRead(void)
Destructor.
uint32_t sequence_length(void) const
int32_t tid(void) const
chromosome ID
BamRead(void)
default constructor
The Department of Theoretical Physics namespace as we define it.
const char nt16_str(uint8_t)
uint32_t right_soft_clipped(const BamRead &bam)
int32_t end(void) const
rightmost coordinate
const char * name(void) const
bool soft_clipped(const BamRead &bam)
const bam1_core_t & core(void) const
access core data struct
Class holding a bam query.
Definition: BamRead.h:53
uint32_t cigar_op(size_t i) const
double alignment_score(const BamRead &bam, RandomAccessIterator ref, double mismatch, double gap=0.0, double open_gap=0.0)
Definition: BamRead.h:516
const BamRead & operator=(const BamRead &rhs)
assignment operator
char sequence_str(size_t index) const
Definition: BamRead.h:446
bool less_query_name(const BamRead &lhs, const BamRead &rhs)
bool same_query_name(const BamRead &lhs, const BamRead &rhs)
int aux_size(void) const
int32_t mtid(void) const
Chromosome ID for mate.
std::string sequence(void) const
void aux_append(const char tag[2], char type, int len, uint8_t *data)
append a new tag to aux field
uint32_t left_soft_clipped(const BamRead &bam)
const uint8_t * aux(void) const
Compact Idiosyncratic Gapped Alignment Report.
Definition: Aligner.h:179
Definition: BamRead.h:487
Definition: BamFile.h:252
int32_t mpos(void) const
leftmost position for mate
Definition: BamRead.h:467
Definition: BamFile.h:132
void swap(BamRead &other)
uint32_t cigar_oplen(size_t i) const
class to iterate through a InBamFile
Definition: BamReadIterator.h:60
uint16_t flag(void) const
bitwise flag
uint8_t qual(size_t i) const
const unsigned char nt16_table(char)
int32_t pos(void) const
0-based laftmost coordinate
const uint32_t * cigar(void) const
access CIGAR array
std::string cigar_str(void) const

Generated on Fri Jul 13 2018 02:33:27 for yat by  doxygen 1.8.11