<commit_before>/*
 * anchored.cc
 *
 *  Created on: Feb 16, 2016
 *      Author: isovic
 */

#include <alignment/alignment_wrappers.h>
#include "alignment/alignment.h"

#include "alignment/transcriptome_mod.h"

enum ClusterAlnType {
  kAlnBeginning = 0,
  kAlnCluster = 1,
  kAlnInBetween = 2,
  kAlnEnding = 3
};

int AlignFront(AlignmentFunctionType AlignmentFunctionSHW,
                const SingleSequence *read, const Index *index, const ProgramParameters *parameters,
                const PathGraphEntry *region_results, int64_t ref_index_start, int64_t region_ref_start,
                const int8_t *ref_data, int64_t ref_len, int64_t clip_count_front,
                int64_t alignment_position_start, bool align_end_to_end, AlignmentResults &aln) {
  aln.aln_mode_code = EDLIB_MODE_SHW;
  aln.query_start = 0;
  aln.query_end = clip_count_front - 1;
  aln.raw_pos_start = alignment_position_start;
  aln.raw_pos_end = alignment_position_start;
  aln.reg_pos_start = aln.raw_pos_start - region_ref_start;
  aln.reg_pos_end = aln.raw_pos_end - region_ref_start;
  aln.ref_start = aln.raw_pos_start - ref_index_start;
  aln.ref_end = aln.raw_pos_end - ref_index_start;
  aln.is_aligned = true;
  aln.raw_alignment.clear();

  int64_t absolute_reference_id = region_results->get_region_data().reference_id;
  int64_t reference_id = region_results->get_region_data().reference_id;
  int64_t reference_start = index->get_reference_starting_pos()[absolute_reference_id];
  int64_t reference_length = index->get_reference_lengths()[absolute_reference_id];

  LOG_DEBUG_SPEC("Aligning the beginning of the read (overhang).\n");

  /// Check if we need to extend the alignment to the left boundary. Also, even if the user specified it, if we are to close to the boundary, just clip it.
  if (align_end_to_end == false) { // || ((alignment_position_start - clip_count_front*2) > 0 && (alignment_position_start - clip_count_front*2) < region_ref_start)) {
    std::vector<unsigned char> insertions_front(clip_count_front, EDLIB_I);
    aln.raw_alignment.insert(aln.raw_alignment.end(), insertions_front.begin(), insertions_front.end());
    aln.edit_distance = insertions_front.size();

//    LOG_DEBUG_SPEC("Skipping alignment of the beginning. align_end_to_end = %s, (alignment_position_start - clip_count_front*2) = %ld should be less than region_ref_start = %ld\n", (align_end_to_end == false) ? "false" : "true", ((alignment_position_start - clip_count_front*2)), region_ref_start);
    LOG_DEBUG_SPEC("Skipping alignment because end_to_end == false.\n");
  } else {
    LOG_DEBUG_SPEC("Performing the alignment.\n");

    /// Reversing the sequences to make the semiglobal alignment of the trailing and leading parts.
//    int8_t *reversed_query_front = reverse_data(read->get_data(), clip_count_front);
    std::vector<int8_t> reversed_query_front;
    reverse_data2(read->get_data(), clip_count_front, reversed_query_front);

//    int8_t *reversed_ref_front = NULL;
    std::vector<int8_t> reversed_ref_front;
    int64_t reversed_ref_len = 0;
    if (clip_count_front*2  > (alignment_position_start - reference_start)) {
//      reversed_ref_front = reverse_data(ref_data + 0, (alignment_position_start - reference_start));
      reverse_data2(ref_data + 0, (alignment_position_start - reference_start), reversed_ref_front);
      reversed_ref_len = alignment_position_start - reference_start;
    } else {
//      reversed_ref_front = reverse_data(ref_data + (alignment_position_start - 1) - (clip_count_front*2 - 1), clip_count_front*2);
      reverse_data2(ref_data + (alignment_position_start - 1) - (clip_count_front*2 - 1), clip_count_front*2, reversed_ref_front);
      reversed_ref_len = clip_count_front*2;
    }

//    if (clip_count_front == 0 || reversed_ref_len == 0) { return 1; }

    int64_t bandwidth = -1;
//    bandwidth = 0.30f*read->get_sequence_length();

    int64_t leftover_left_start = 0, leftover_left_end = 0, leftover_left_edit_distance = 0;
    std::vector<unsigned char> leftover_left_alignment;
    int ret_code_right = AlignmentFunctionSHW(&reversed_query_front[0], (clip_count_front),
                                              (int8_t *) (&reversed_ref_front[0]), reversed_ref_len,<commit_msg>Fixed a bug when anchored aligning the beginning of the read and there is an overhang from the query. The starting position for the alignment was wrong. Should be ok now.<commit_after>/*
 * anchored.cc
 *
 *  Created on: Feb 16, 2016
 *      Author: isovic
 */

#include <alignment/alignment_wrappers.h>
#include "alignment/alignment.h"

#include "alignment/transcriptome_mod.h"

enum ClusterAlnType {
  kAlnBeginning = 0,
  kAlnCluster = 1,
  kAlnInBetween = 2,
  kAlnEnding = 3
};

int AlignFront(AlignmentFunctionType AlignmentFunctionSHW,
                const SingleSequence *read, const Index *index, const ProgramParameters *parameters,
                const PathGraphEntry *region_results, int64_t ref_index_start, int64_t region_ref_start,
                const int8_t *ref_data, int64_t ref_len, int64_t clip_count_front,
                int64_t alignment_position_start, bool align_end_to_end, AlignmentResults &aln) {
  aln.aln_mode_code = EDLIB_MODE_SHW;
  aln.query_start = 0;
  aln.query_end = clip_count_front - 1;
  aln.raw_pos_start = alignment_position_start;
  aln.raw_pos_end = alignment_position_start;
  aln.reg_pos_start = aln.raw_pos_start - region_ref_start;
  aln.reg_pos_end = aln.raw_pos_end - region_ref_start;
  aln.ref_start = aln.raw_pos_start - ref_index_start;
  aln.ref_end = aln.raw_pos_end - ref_index_start;
  aln.is_aligned = true;
  aln.raw_alignment.clear();

  int64_t absolute_reference_id = region_results->get_region_data().reference_id;
  int64_t reference_id = region_results->get_region_data().reference_id;
  int64_t reference_start = index->get_reference_starting_pos()[absolute_reference_id];
  int64_t reference_length = index->get_reference_lengths()[absolute_reference_id];

  LOG_DEBUG_SPEC("Aligning the beginning of the read (overhang).\n");

  /// Check if we need to extend the alignment to the left boundary. Also, even if the user specified it, if we are to close to the boundary, just clip it.
  if (align_end_to_end == false) { // || ((alignment_position_start - clip_count_front*2) > 0 && (alignment_position_start - clip_count_front*2) < region_ref_start)) {
    std::vector<unsigned char> insertions_front(clip_count_front, EDLIB_I);
    aln.raw_alignment.insert(aln.raw_alignment.end(), insertions_front.begin(), insertions_front.end());
    aln.edit_distance = insertions_front.size();

//    LOG_DEBUG_SPEC("Skipping alignment of the beginning. align_end_to_end = %s, (alignment_position_start - clip_count_front*2) = %ld should be less than region_ref_start = %ld\n", (align_end_to_end == false) ? "false" : "true", ((alignment_position_start - clip_count_front*2)), region_ref_start);
    LOG_DEBUG_SPEC("Skipping alignment because end_to_end == false.\n");
  } else {
    LOG_DEBUG_SPEC("Performing the alignment.\n");

    /// Reversing the sequences to make the semiglobal alignment of the trailing and leading parts.
//    int8_t *reversed_query_front = reverse_data(read->get_data(), clip_count_front);
    std::vector<int8_t> reversed_query_front;
    reverse_data2(read->get_data(), clip_count_front, reversed_query_front);

//    int8_t *reversed_ref_front = NULL;
    std::vector<int8_t> reversed_ref_front;
    int64_t reversed_ref_len = 0;
    if (clip_count_front*2  > (alignment_position_start - reference_start)) {
      reverse_data2(ref_data + reference_start, (alignment_position_start - reference_start), reversed_ref_front);
      reversed_ref_len = alignment_position_start - reference_start;
    } else {
      reverse_data2(ref_data + (alignment_position_start - 1) - (clip_count_front*2 - 1), clip_count_front*2, reversed_ref_front);
      reversed_ref_len = clip_count_front*2;
    }

    int64_t bandwidth = -1;
//    bandwidth = 0.30f*read->get_sequence_length();

    int64_t leftover_left_start = 0, leftover_left_end = 0, leftover_left_edit_distance = 0;
    std::vector<unsigned char> leftover_left_alignment;
    int ret_code_right = AlignmentFunctionSHW(&reversed_query_front[0], (clip_count_front),
                                              (int8_t *) (&reversed_ref_front[0]), reversed_ref_len,
{'fraction_non_alphanumeric': 0.09896956625928588, 'fraction_numerical': 0.01689432063263839, 'mean_word_length': 4.51685393258427, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1043209', 'n_tokens_mistral': 2955, 'n_tokens_neox': 2763, 'n_words': 562}