<commit_before>//---------------------------------------------------------
// Copyright 2015 Ontario Institute for Cancer Research
// Written by Jared Simpson (<EMAIL>)
//---------------------------------------------------------
//
// nanopolish_anchor - a collection of data types
// for representing a set of event-to-sequence
// mappings.
#include <vector>
#include <string>
#include <stdio.h>
#include <assert.h>
#include "htslib/faidx.h"
#include "nanopolish_common.h"
#include "nanopolish_anchor.h"
#include "nanopolish_squiggle_read.h"

HMMRealignmentInput build_input_for_region(const std::string& bam_filename, 
                                           const std::string& ref_filename, 
                                           const Fast5Map& read_name_map, 
                                           const std::string& contig_name,
                                           int start, 
                                           int end, 
                                           int stride)
{
    // Initialize return data
    HMMRealignmentInput ret;

    // load bam file
    htsFile* bam_fh = sam_open(bam_filename.c_str(), "r");
    assert(bam_fh != NULL);

    // load bam index file
    std::string index_filename = bam_filename + ".bai";
    hts_idx_t* bam_idx = bam_index_load(index_filename.c_str());
    assert(bam_idx != NULL);

    // read the bam header
    bam_hdr_t* hdr = sam_hdr_read(bam_fh);
    int contig_id = bam_name2id(hdr, contig_name.c_str());
    
    // load reference fai file
    faidx_t *fai = fai_load(ref_filename.c_str());

    // Adjust end position to make sure we don't go out-of-range
    end = std::min(end, faidx_seq_len(fai, contig_name.c_str()));

    // load the reference sequence for this region
    int fetched_len = 0;
    char* ref_segment = faidx_fetch_seq(fai, contig_name.c_str(), start, end, &fetched_len);
    ret.original_sequence = ref_segment;

    // Initialize iteration
    bam1_t* record = bam_init1();
    hts_itr_t* itr = sam_itr_queryi(bam_idx, contig_id, start, end);
    
    // Iterate over reads aligned here
    std::vector<HMMReadAnchorSet> read_anchors;<commit_msg>do not segfault when fai_fetch fails<commit_after>//---------------------------------------------------------
// Copyright 2015 Ontario Institute for Cancer Research
// Written by <NAME> (<EMAIL>)
//---------------------------------------------------------
//
// nanopolish_anchor - a collection of data types
// for representing a set of event-to-sequence
// mappings.
#include <vector>
#include <string>
#include <stdio.h>
#include <assert.h>
#include "htslib/faidx.h"
#include "nanopolish_common.h"
#include "nanopolish_anchor.h"
#include "nanopolish_squiggle_read.h"

HMMRealignmentInput build_input_for_region(const std::string& bam_filename, 
                                           const std::string& ref_filename, 
                                           const Fast5Map& read_name_map, 
                                           const std::string& contig_name,
                                           int start, 
                                           int end, 
                                           int stride)
{
    // Initialize return data
    HMMRealignmentInput ret;

    // load bam file
    htsFile* bam_fh = sam_open(bam_filename.c_str(), "r");
    assert(bam_fh != NULL);

    // load bam index file
    std::string index_filename = bam_filename + ".bai";
    hts_idx_t* bam_idx = bam_index_load(index_filename.c_str());
    assert(bam_idx != NULL);

    // read the bam header
    bam_hdr_t* hdr = sam_hdr_read(bam_fh);
    int contig_id = bam_name2id(hdr, contig_name.c_str());
    
    // load reference fai file
    faidx_t *fai = fai_load(ref_filename.c_str());

    // Adjust end position to make sure we don't go out-of-range
    end = std::min(end, faidx_seq_len(fai, contig_name.c_str()));

    // load the reference sequence for this region
    int fetched_len = 0;
    char* ref_segment = faidx_fetch_seq(fai, contig_name.c_str(), start, end, &fetched_len);
    if(ref_segment == NULL) {
        exit(EXIT_FAILURE);
    }
    ret.original_sequence = ref_segment;

    // Initialize iteration
    bam1_t* record = bam_init1();
    hts_itr_t* itr = sam_itr_queryi(bam_idx, contig_id, start, end);
    
    // Iterate over reads aligned here
    std::vector<HMMReadAnchorSet> read_anchors;
{'fraction_non_alphanumeric': 0.148359161349134, 'fraction_numerical': 0.004102096627164996, 'mean_word_length': 1.9201596806387227, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1768299', 'n_tokens_mistral': 1362, 'n_tokens_neox': 1285, 'n_words': 320}