Debian Codegraph @

src/basemods/nanopolish_basemods.h

raw symbols SCIP
1 //---------------------------------------------------------
2 // Copyright 2021 Ontario Institute for Cancer Research
3 // Written by Jared Simpson ([email protected])
4 //---------------------------------------------------------
5 //
6 // nanopolish_basemods -- utility functions for working with
7 // base modification data
8 //
9 #ifndef NANOPOLISH_BASEMODS_H
10 #define NANOPOLISH_BASEMODS_H
11
12 #include <stdio.h>
13 #include "htslib/faidx.h"
14 #include "htslib/sam.h"
15 #include "nanopolish_squiggle_read.h"
16
17 //
18 // Structs
19 //
20 struct OutputHandles
21 {
22 FILE* site_writer = NULL;
23 htsFile* bam_writer = NULL;
24
25 void write_site_header() {
26 // Write header
27 fprintf(site_writer, "chromosome\tstrand\tstart\tend\tread_name\t"
28 "log_lik_ratio\tlog_lik_methylated\tlog_lik_unmethylated\t"
29 "num_calling_strands\tnum_motifs\tsequence\n");
30 }
31
32 void close() {
33 if(site_writer != stdout && site_writer != NULL) {
34 fclose(site_writer);
35 site_writer = NULL;
36 }
37
38 if(bam_writer != NULL) {
39 hts_close(bam_writer);
40 bam_writer = NULL;
41 }
42 }
43 };
44
45 struct ScoredSite
46 {
47 ScoredSite()
48 {
49 ll_unmethylated[0] = 0;
50 ll_unmethylated[1] = 0;
51 ll_methylated[0] = 0;
52 ll_methylated[1] = 0;
53 strands_scored = 0;
54 }
55
56 std::string chromosome;
57 int start_position;
58 int end_position;
59 int n_motif;
60 std::string sequence;
61
62 // scores per strand
63 double ll_unmethylated[2];
64 double ll_methylated[2];
65 int strands_scored;
66
67 //
68 static bool sort_by_position(const ScoredSite& a, const ScoredSite& b) { return a.start_position < b.start_position; }
69 };
70
71 struct MethylationCallingParameters
72 {
73 int min_separation = 10;
74 int min_flank = 10;
75 std::string methylation_type = "cpg";
76 const Alphabet* alphabet = NULL;
77 };
78
79 // typedefs
80 typedef std::map<const bam1_t*, std::map<int, ScoredSite> > MethylationCallingResult;
81
82 // Construct a copy of record annotated with base modification tags
83 // The returned record must be freed by the caller
84 bam1_t* create_modbam_record(const bam1_t* record,
85 const std::map<int, ScoredSite>& calls,
86 const MethylationCallingParameters& calling_parameters);
87
88
89 // Construct a new bam record where the read sequence is replaced with the reference
90 // and is annotated with methylation tags
91 // The returned record must be freed by the caller
92 bam1_t* create_reference_modbam_record(const faidx_t* fai,
93 const bam_hdr_t* hdr,
94 const bam1_t* record,
95 const std::map<int, ScoredSite>& calls,
96 const MethylationCallingParameters& calling_parameters);
97
98 // Process methylation sites in the provided read
99 void calculate_methylation_for_read(const OutputHandles& handles,
100 MethylationCallingResult& result,
101 SquiggleRead& sr,
102 const MethylationCallingParameters& calling_parameters,
103 const faidx_t* fai,
104 const bam_hdr_t* hdr,
105 const bam1_t* record,
106 size_t read_idx,
107 int region_start,
108 int region_end);
109
110 #endif