yat  0.18.2pre
BamRead.h
1 #ifndef theplu_yat_omic_bam_read
2 #define theplu_yat_omic_bam_read
3 
4 // $Id: BamRead.h 3999 2020-10-08 23:22:32Z peter $
5 
6 /*
7  Copyright (C) 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 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 yat. If not, see <http://www.gnu.org/licenses/>.
23 */
24 
25 #include "yat/utility/Aligner.h"
26 // This file has to be included to keep compatibility with yat 0.11
27 #include "yat/utility/Cigar.h"
28 #include "yat/utility/deprecate.h"
29 
30 #include <htslib/sam.h>
31 
32 #include <functional>
33 #include <string>
34 #include <vector>
35 
36 namespace theplu {
37 namespace yat {
38 namespace omic {
39 
51  class BamRead
52  {
53  public:
60  BamRead(void);
61 
65  BamRead(const BamRead& other);
66 
72  BamRead(BamRead&& other) noexcept;
73 
77  virtual ~BamRead(void);
78 
82  const BamRead& operator=(const BamRead& rhs);
83 
89  BamRead& operator=(BamRead&& rhs);
90 
96  const uint8_t* aux(void) const;
97 
109  const uint8_t* aux(const char tag[2]) const;
110 
123  void aux_append(const char tag[2], char type, int len, uint8_t* data);
124 
132  void aux_del(const char tag[2]);
133 
139  int aux_size(void) const;
140 
146  const bam1_core_t& core(void) const;
147 
153  bam1_core_t& core(void);
154 
163  const uint32_t* cigar(void) const;
164 
168  uint32_t cigar(size_t i) const;
169 
173  uint32_t cigar_op(size_t i) const;
174 
178  uint32_t cigar_oplen(size_t i) const;
179 
183  std::string cigar_str(void) const;
184 
193  void cigar(const std::vector<uint32_t>& c) YAT_DEPRECATE;
194 
202  void cigar(const utility::Aligner::Cigar& cigar);
203 
212  int32_t end(void) const;
213 
221  uint16_t flag(void) const;
222 
228  bool flag_bit_is_set(uint16_t x) const;
229 
235  uint16_t get_flag_bit(uint16_t x) const;
236 
244  void set_flag_bit(uint16_t x);
245 
253  void unset_flag_bit(uint16_t x);
254 
262  void flip_flag_bit(uint16_t x);
263 
267  int32_t mpos(void) const;
268 
272  int32_t mtid(void) const;
273 
279  const char* name(void) const;
280 
286  void name(const std::string& n);
287 
291  int32_t pos(void) const;
292 
296  uint8_t qual(size_t i) const;
297 
306  void qual(size_t i, uint8_t q);
307 
313  std::string sequence(void) const;
314 
320  uint8_t sequence(size_t index) const;
321 
327  char sequence_str(size_t index) const;
328 
338  void sequence(size_t i, uint8_t x);
339 
345  void sequence(const std::string& seq, const std::vector<uint8_t>& qual);
346 
350  uint32_t sequence_length(void) const;
351 
357  void swap(BamRead& other);
358 
365  bool strand(void) const;
366 
373  bool mstrand(void) const;
374 
378  int32_t tid(void) const;
379 
380  private:
381  // ensure capacity of data pointer is (at least) n
382  void reserve(uint32_t n);
383 
393  void sequence(size_t k, uint8_t x, uint8_t y);
394 
395  bam1_t* bam_;
396 
397  friend class InBamFile;
398  friend class OutBamFile;
399  friend class BamReadIterator;
400  // access data length, current length of data
401  int& data_size(void);
402  const int& data_size(void) const;
403  uint32_t data_capacity(void) const;
404  };
405 
419  const unsigned char nt16_table(char);
420 
433  const char nt16_str(uint8_t);
434 
443  void swap(BamRead& lhs, BamRead& rhs);
444 
453  bool soft_clipped(const BamRead& bam);
454 
463  uint32_t left_soft_clipped(const BamRead& bam);
464 
473  uint32_t right_soft_clipped(const BamRead& bam);
474 
484  bool same_query_name(const BamRead& lhs, const BamRead& rhs);
485 
495  bool less_query_name(const BamRead& lhs, const BamRead& rhs);
496 
505  struct BamLessPos
506  : public std::binary_function<const BamRead&, const BamRead&, bool>
507  {
514  bool operator()(const BamRead& lhs, const BamRead& rhs) const;
515  };
516 
517 
526  struct BamLessEnd
527  : public std::binary_function<const BamRead&, const BamRead&, bool>
528  {
535  bool operator()(const BamRead& lhs, const BamRead& rhs) const;
536  };
537 
546  struct BamLessName
547  : public std::binary_function<const BamRead&, const BamRead&, bool>
548  {
552  bool operator()(const BamRead& lhs, const BamRead& rhs) const;
553  };
554 
555 
574  template<typename RandomAccessIterator>
575  double alignment_score(const BamRead& bam, RandomAccessIterator ref,
576  double mismatch, double gap=0.0, double open_gap=0.0)
577  {
578  BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIterator<RandomAccessIterator>));
579  BOOST_CONCEPT_ASSERT((boost_concepts::RandomAccessTraversal<RandomAccessIterator>));
580  double res = 0;
581  size_t q = 0;
582 
583  for (uint32_t i = 0; i<bam.core().n_cigar; ++i) {
584  switch (bam.cigar_op(i)) {
585  case (BAM_CMATCH): {
586  RandomAccessIterator end = ref + bam.cigar_oplen(i);
587  for (; ref!=end; ++ref) {
588  if (bam.sequence_str(q) == *ref)
589  res += 1.0;
590  else
591  res -= mismatch;
592  ++q;
593  }
594  break;
595  }
596  case (BAM_CEQUAL): {
597  q += bam.cigar_oplen(i);
598  ref += bam.cigar_oplen(i);
599  res += bam.cigar_oplen(i);
600  break;
601  }
602  case (BAM_CDIFF): {
603  q += bam.cigar_oplen(i);
604  ref += bam.cigar_oplen(i);
605  res -= bam.cigar_oplen(i)*mismatch;
606  break;
607  }
608  case (BAM_CSOFT_CLIP): {
609  q += bam.cigar_oplen(i);
610  break;
611  }
612  case (BAM_CINS): {
613  q += bam.cigar_oplen(i);
614  res -= open_gap + bam.cigar_oplen(i) * gap;
615  break;
616  }
617  case (BAM_CDEL): {
618  ref += bam.cigar_oplen(i);
619  res -= open_gap + bam.cigar_oplen(i) * gap;
620  break;
621  }
622  } // end switch
623  }
624  return res;
625  }
626 
627 }}}
628 #endif
int32_t end(void) const
rightmost coordinate
void aux_del(const char tag[2])
remove a tag in aux field
virtual ~BamRead(void)
Destructor.
int32_t mpos(void) const
leftmost position for mate
bool operator()(const BamRead &lhs, const BamRead &rhs) const
const uint8_t * aux(void) const
BamRead(void)
default constructor
The Department of Theoretical Physics namespace as we define it.
const char nt16_str(uint8_t)
int32_t pos(void) const
0-based leftmost coordinate
bool operator()(const BamRead &lhs, const BamRead &rhs) const
void unset_flag_bit(uint16_t x)
std::string sequence(void) const
Class holding a bam query.
Definition: BamRead.h:51
uint8_t qual(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:575
uint32_t cigar_oplen(size_t i) const
const BamRead & operator=(const BamRead &rhs)
assignment operator
uint32_t cigar_op(size_t i) const
Definition: BamRead.h:505
uint16_t flag(void) const
bitwise flag
uint16_t get_flag_bit(uint16_t x) const
void flip_flag_bit(uint16_t x)
bool strand(void) const
int32_t mtid(void) const
Chromosome ID for mate.
void aux_append(const char tag[2], char type, int len, uint8_t *data)
append a new tag to aux field
std::string cigar_str(void) const
bool operator()(const BamRead &lhs, const BamRead &rhs) const
int aux_size(void) const
Compact Idiosyncratic Gapped Alignment Report.
Definition: Aligner.h:182
Definition: BamRead.h:546
const bam1_core_t & core(void) const
access core data struct
Definition: BamFile.h:245
bool mstrand(void) const
Definition: BamRead.h:526
void set_flag_bit(uint16_t x)
Definition: BamFile.h:134
uint32_t sequence_length(void) const
void swap(BamRead &other)
const char * name(void) const
class to iterate through a InBamFile
Definition: BamReadIterator.h:59
int32_t tid(void) const
chromosome ID
const unsigned char nt16_table(char)
bool flag_bit_is_set(uint16_t x) const
char sequence_str(size_t index) const
const uint32_t * cigar(void) const
access CIGAR array

Generated on Tue Sep 7 2021 17:32:32 for yat by  doxygen 1.8.14