<commit_before>#include "aligner.h"

#include <list>

#include "dataStructures/optional_tag.h"
#include "utilities/bam/bam_constant.h"
#include "utilities/bam/bam_utilities.h"
#include "utilities/miscellaneous/alignment_filter.h"
#include "utilities/miscellaneous/hashes_collection.h"

namespace {
void SetTargetSequence(const SearchRegionType::RegionType& region_type, 
                       SR_QueryRegion* query_region) {
  //const bool forward    = !bam1_strand(query_region->pOrphan);
  //const bool inverse    = (forward && (query_region->isOrphanInversed == TRUE))
  //                      ||(!forward && (query_region->isOrphanInversed == FALSE));
  //const bool complement = !forward;
  
  if (region_type.sequence_inverse && region_type.sequence_complement) {
   // if (!inverse && !complement)
      SR_QueryRegionChangeSeq(query_region, SR_REVERSE_COMP);
   // else if (!inverse)
   //   SR_QueryRegionChangeSeq(query_region, SR_INVERSE);
   // else if (!complement)
   //   SR_QueryRegionChangeSeq(query_region, SR_COMP);

    SR_SetStrand(query_region->pOrphan, SR_REVERSE_COMP);
    query_region->isOrphanInversed = FALSE;

  } else if (region_type.sequence_inverse && !region_type.sequence_complement) {
   // if (!inverse && complement)
   //   SR_QueryRegionChangeSeq(query_region, SR_REVERSE_COMP);
   // else if (!inverse)
      SR_QueryRegionChangeSeq(query_region, SR_INVERSE);
   // else if (complement)
   //   SR_QueryRegionChangeSeq(query_region, SR_COMP);
      
    SR_SetStrand(query_region->pOrphan, SR_INVERSE);
    query_region->isOrphanInversed = TRUE;
 
  } else if (!region_type.sequence_inverse && region_type.sequence_complement) {
   // if (inverse && !complement)
   //   SR_QueryRegionChangeSeq(query_region, SR_REVERSE_COMP);
   // else if (inverse)
   //   SR_QueryRegionChangeSeq(query_region, SR_INVERSE);
   // else if (!complement)
      SR_QueryRegionChangeSeq(query_region, SR_COMP);

    SR_SetStrand(query_region->pOrphan, SR_REVERSE_COMP);
    query_region->isOrphanInversed = TRUE;
  
  } else {
   // if (inverse && complement)
   //   SR_QueryRegionChangeSeq(query_region, SR_REVERSE_COMP);
   // else if (inverse)
   //   SR_QueryRegionChangeSeq(query_region, SR_INVERSE);
   // else if (complement)
   //   SR_QueryRegionChangeSeq(query_region, SR_COMP);

    SR_QueryRegionChangeSeq(query_region, SR_FORWARD);
    SR_SetStrand(query_region->pOrphan, SR_FORWARD);
    query_region->isOrphanInversed = FALSE;
  } // end if-else
  
}

void AdjustBamFlag(bam1_t* al_bam_anchor, bam1_t* partial_al1, bam1_t* partial_al2) {
  namespace Constant = BamFlagConstant;
  bool anchor_mate1 = al_bam_anchor->core.flag & Constant::kBamFMate1;
  bool anchor_reverse = al_bam_anchor->core.flag & Constant::kBamFReverse;

  // set anchor's flag
  al_bam_anchor->core.flag &= 0xfff7; // mate is not unmapped
  if (anchor_reverse) al_bam_anchor->core.flag &= 0xffdf; // mate is not reverse
  else al_bam_anchor->core.flag |= Constant::kBamFReverseMate;

  //set partial alignments flags
  int16_t partial_flag = 0;
  partial_flag |= Constant::kBamFPaired;
  if (anchor_reverse) partial_flag |= Constant::kBamFReverseMate;
  else partial_flag |= Constant::kBamFReverse;
  if (anchor_mate1) partial_flag |= Constant::kBamFMate2;
  else partial_flag |= Constant::kBamFMate1;
  partial_al1->core.flag = partial_flag;
  partial_al2->core.flag = partial_flag;
}
}

Aligner::Aligner(const SR_Reference* reference, 
                 const SR_InHashTable* hash_table,
		 const SR_Reference* reference_special,
		 const SR_InHashTable* hash_table_special,
		 const SR_RefHeader* reference_header,
		 const int& fragment_length)
    : search_region_type_()
    , anchor_region_()
    , reference_(reference)
    , hash_table_(hash_table)
    , reference_special_(reference_special)
    , hash_table_special_(hash_table_special)
    , reference_header_(reference_header)
    , sw_aligner_(){
  
  query_region_     = SR_QueryRegionAlloc();
  hashes_           = HashRegionTableAlloc();
  hashes_special_   = HashRegionTableAlloc();
  special_ref_view_ = SR_RefViewAlloc();

  hash_length_.fragLen = fragment_length;
  hash_length_.closeRange = 2000;
  hash_length_.farRange   = 10000;
}

Aligner::~Aligner() {
  SR_QueryRegionFree(query_region_);
  HashRegionTableFree(hashes_);
  HashRegionTableFree(hashes_special_);
  SR_RefViewFree(special_ref_view_);
}

void Aligner::LoadRegionType(const bam1_t& anchor) {
  uint32_t* cigar = bam1_cigar(&anchor);
  bool is_new_region = anchor_region_.IsNewRegion(cigar,
                                                  anchor.core.n_cigar, 
                                                  anchor.core.pos);
  
  if (is_new_region)
    search_region_type_.ResetRegionTypeList();
  else
    search_region_type_.RewindRegionTypeList();
}

void Aligner::AlignCandidate(const bool& detect_special,
                             const AlignmentFilter& alignment_filter,
			     SR_BamInStreamIter* al_ite,
                             vector<bam1_t*>* alignments) {
    while (SR_QueryRegionLoadPair(query_region_, al_ite) == SR_OK) {
      // TODO@WP: it may be removed later
      if (query_region_->algnType != SR_UNIQUE_ORPHAN) continue;

      Align(detect_special, alignment_filter, query_region_, alignments);
    } // end while

    al_ite = NULL;
}

void Aligner::AlignCandidate(const bool& detect_special,
                    const AlignmentFilter& alignment_filter,
		    const bam1_t& anchor,
		    const bam1_t& target,
		    vector<bam1_t*>* alignments) {

  SR_QueryRegion* query_region = SR_QueryRegionAlloc();
  query_region->pAnchor = (bam1_t*) &anchor;
  query_region->pOrphan = (bam1_t*) &target;
  Align(detect_special, alignment_filter, query_region, alignments);
  SR_QueryRegionFree(query_region);
}<commit_msg>complete aligner api demo code<commit_after>#include "aligner.h"

#include <list>

#include "dataStructures/optional_tag.h"
#include "utilities/bam/bam_constant.h"
#include "utilities/bam/bam_utilities.h"
#include "utilities/miscellaneous/alignment_filter.h"
#include "utilities/miscellaneous/hashes_collection.h"

namespace {
void SetTargetSequence(const SearchRegionType::RegionType& region_type, 
                       SR_QueryRegion* query_region) {
  //const bool forward    = !bam1_strand(query_region->pOrphan);
  //const bool inverse    = (forward && (query_region->isOrphanInversed == TRUE))
  //                      ||(!forward && (query_region->isOrphanInversed == FALSE));
  //const bool complement = !forward;
  
  if (region_type.sequence_inverse && region_type.sequence_complement) {
   // if (!inverse && !complement)
      SR_QueryRegionChangeSeq(query_region, SR_REVERSE_COMP);
   // else if (!inverse)
   //   SR_QueryRegionChangeSeq(query_region, SR_INVERSE);
   // else if (!complement)
   //   SR_QueryRegionChangeSeq(query_region, SR_COMP);

    SR_SetStrand(query_region->pOrphan, SR_REVERSE_COMP);
    query_region->isOrphanInversed = FALSE;

  } else if (region_type.sequence_inverse && !region_type.sequence_complement) {
   // if (!inverse && complement)
   //   SR_QueryRegionChangeSeq(query_region, SR_REVERSE_COMP);
   // else if (!inverse)
      SR_QueryRegionChangeSeq(query_region, SR_INVERSE);
   // else if (complement)
   //   SR_QueryRegionChangeSeq(query_region, SR_COMP);
      
    SR_SetStrand(query_region->pOrphan, SR_INVERSE);
    query_region->isOrphanInversed = TRUE;
 
  } else if (!region_type.sequence_inverse && region_type.sequence_complement) {
   // if (inverse && !complement)
   //   SR_QueryRegionChangeSeq(query_region, SR_REVERSE_COMP);
   // else if (inverse)
   //   SR_QueryRegionChangeSeq(query_region, SR_INVERSE);
   // else if (!complement)
      SR_QueryRegionChangeSeq(query_region, SR_COMP);

    SR_SetStrand(query_region->pOrphan, SR_REVERSE_COMP);
    query_region->isOrphanInversed = TRUE;
  
  } else {
   // if (inverse && complement)
   //   SR_QueryRegionChangeSeq(query_region, SR_REVERSE_COMP);
   // else if (inverse)
   //   SR_QueryRegionChangeSeq(query_region, SR_INVERSE);
   // else if (complement)
   //   SR_QueryRegionChangeSeq(query_region, SR_COMP);

    SR_QueryRegionChangeSeq(query_region, SR_FORWARD);
    SR_SetStrand(query_region->pOrphan, SR_FORWARD);
    query_region->isOrphanInversed = FALSE;
  } // end if-else
  
}

void AdjustBamFlag(bam1_t* al_bam_anchor, bam1_t* partial_al1, bam1_t* partial_al2) {
  namespace Constant = BamFlagConstant;
  bool anchor_mate1 = al_bam_anchor->core.flag & Constant::kBamFMate1;
  bool anchor_reverse = al_bam_anchor->core.flag & Constant::kBamFReverse;

  // set anchor's flag
  al_bam_anchor->core.flag &= 0xfff7; // mate is not unmapped
  if (anchor_reverse) al_bam_anchor->core.flag &= 0xffdf; // mate is not reverse
  else al_bam_anchor->core.flag |= Constant::kBamFReverseMate;

  //set partial alignments flags
  int16_t partial_flag = 0;
  partial_flag |= Constant::kBamFPaired;
  if (anchor_reverse) partial_flag |= Constant::kBamFReverseMate;
  else partial_flag |= Constant::kBamFReverse;
  if (anchor_mate1) partial_flag |= Constant::kBamFMate2;
  else partial_flag |= Constant::kBamFMate1;
  partial_al1->core.flag = partial_flag;
  partial_al2->core.flag = partial_flag;
}
}

Aligner::Aligner(const SR_Reference* reference, 
                 const SR_InHashTable* hash_table,
		 const SR_Reference* reference_special,
		 const SR_InHashTable* hash_table_special,
		 const SR_RefHeader* reference_header,
		 const int& fragment_length)
    : search_region_type_()
    , anchor_region_()
    , reference_(reference)
    , hash_table_(hash_table)
    , reference_special_(reference_special)
    , hash_table_special_(hash_table_special)
    , reference_header_(reference_header)
    , sw_aligner_(){
  
  query_region_     = SR_QueryRegionAlloc();
  hashes_           = HashRegionTableAlloc();
  hashes_special_   = HashRegionTableAlloc();
  special_ref_view_ = SR_RefViewAlloc();

  hash_length_.fragLen = fragment_length;
  hash_length_.closeRange = 2000;
  hash_length_.farRange   = 10000;
}

Aligner::~Aligner() {
  SR_QueryRegionFree(query_region_);
  HashRegionTableFree(hashes_);
  HashRegionTableFree(hashes_special_);
  SR_RefViewFree(special_ref_view_);
}

void Aligner::LoadRegionType(const bam1_t& anchor) {
  uint32_t* cigar = bam1_cigar(&anchor);
  bool is_new_region = anchor_region_.IsNewRegion(cigar,
                                                  anchor.core.n_cigar, 
                                                  anchor.core.pos);
  
  if (is_new_region)
    search_region_type_.ResetRegionTypeList();
  else
    search_region_type_.RewindRegionTypeList();
}

void Aligner::AlignCandidate(const bool& detect_special,
                             const AlignmentFilter& alignment_filter,
			     SR_BamInStreamIter* al_ite,
                             vector<bam1_t*>* alignments) {
    while (SR_QueryRegionLoadPair(query_region_, al_ite) == SR_OK) {
      // TODO@WP: it may be removed later
      if (query_region_->algnType != SR_UNIQUE_ORPHAN) continue;

      Align(detect_special, alignment_filter, query_region_, alignments);
    } // end while

    al_ite = NULL;
}

void Aligner::AlignCandidate(const bool& detect_special,
                    const AlignmentFilter& alignment_filter,
		    const bam1_t& anchor,
		    const bam1_t& target,
		    vector<bam1_t*>* alignments) {

  query_region_->pAnchor = (bam1_t*) &anchor;
  query_region_->pOrphan = (bam1_t*) &target;
  Align(detect_special, alignment_filter, query_region_, alignments);
}
{'fraction_non_alphanumeric': 0.10515356585111921, 'fraction_numerical': 0.00659378795766094, 'mean_word_length': 3.8351510067114094, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3734027', 'n_tokens_mistral': 3957, 'n_tokens_neox': 3753, 'n_words': 638}