File: benchmark_utils.c

package info (click to toggle)
libwfa2 2.3.3-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,072 kB
  • sloc: ansic: 13,812; python: 540; cpp: 500; makefile: 268; sh: 176; lisp: 41
file content (209 lines) | stat: -rw-r--r-- 7,774 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 *                             The MIT License
 *
 * Wavefront Alignment Algorithms
 * Copyright (c) 2017 by Santiago Marco-Sola  <santiagomsola@gmail.com>
 *
 * This file is part of Wavefront Alignment Algorithms.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * PROJECT: Wavefront Alignment Algorithms
 * AUTHOR(S): Santiago Marco-Sola <santiagomsola@gmail.com>
 * DESCRIPTION: Benchmark utils
 */

#include "benchmark/benchmark_utils.h"
#include "alignment/score_matrix.h"
#include "edit/edit_dp.h"
#include "gap_linear/nw.h"
#include "gap_affine/affine_matrix.h"
#include "gap_affine/swg.h"

/*
 * Setup
 */
void benchmark_align_input_clear(
    align_input_t* const align_input) {
  // Alignment form
  align_input->ends_free = false;
  align_input->pattern_begin_free = 0;
  align_input->text_begin_free = 0;
  align_input->pattern_end_free = 0;
  align_input->text_end_free = 0;
  // Output
  align_input->output_file = NULL;
  align_input->output_full = false;
  // Accuracy Stats
  counter_reset(&(align_input->align));
  counter_reset(&(align_input->align_correct));
  counter_reset(&(align_input->align_score));
  counter_reset(&(align_input->align_score_total));
  counter_reset(&(align_input->align_score_diff));
  counter_reset(&(align_input->align_cigar));
  counter_reset(&(align_input->align_bases));
  counter_reset(&(align_input->align_matches));
  counter_reset(&(align_input->align_mismatches));
  counter_reset(&(align_input->align_del));
  counter_reset(&(align_input->align_ins));
}
/*
 * Display
 */
void benchmark_print_alignment(
    FILE* const stream,
    align_input_t* const align_input,
    const int score_computed,
    cigar_t* const cigar_computed,
    const int score_correct,
    cigar_t* const cigar_correct) {
  // Print Sequence
  fprintf(stream,"ALIGNMENT (#%d)\n",align_input->sequence_id);
  fprintf(stream,"  PATTERN  %s\n",align_input->pattern);
  fprintf(stream,"  TEXT     %s\n",align_input->text);
  // Print CIGARS
  if (cigar_computed != NULL && score_computed != -1) {
    fprintf(stream,"    COMPUTED\tscore=%d\t",score_computed);
    cigar_print(stream,cigar_computed,true);
    fprintf(stream,"\n");
  }
  if (cigar_computed != NULL) {
    cigar_print_pretty(stream,
        align_input->pattern,align_input->pattern_length,
        align_input->text,align_input->text_length,
        cigar_computed,align_input->mm_allocator);
  }
  if (cigar_correct != NULL && score_correct != -1) {
    fprintf(stream,"    CORRECT \tscore=%d\t",score_correct);
    cigar_print(stream,cigar_correct,true);
    fprintf(stream,"\n");
  }
  if (cigar_correct != NULL) {
    cigar_print_pretty(stream,
        align_input->pattern,align_input->pattern_length,
        align_input->text,align_input->text_length,
        cigar_correct,align_input->mm_allocator);
  }
}
void benchmark_print_output_lite(
    FILE* const stream,
    align_input_t* const align_input,
    const int score,
    cigar_t* const cigar) {
  // Retrieve CIGAR
  const bool cigar_null = (cigar->begin_offset >= cigar->end_offset);
  char* cigar_str = NULL;
  if (!cigar_null) {
    cigar_str = malloc(2*(cigar->end_offset-cigar->begin_offset)+10);
    cigar_sprint(cigar_str,cigar,true);
  }
  // Print
  fprintf(stream,"%d\t%s\n",score,(cigar_null) ? "-" : cigar_str);
  // Free
  if (!cigar_null) free(cigar_str);
}
void benchmark_print_output_full(
    FILE* const stream,
    align_input_t* const align_input,
    const int score,
    cigar_t* const cigar) {
  // Retrieve CIGAR
  const bool cigar_null = (cigar->begin_offset >= cigar->end_offset);
  char* cigar_str = NULL;
  if (!cigar_null) {
    cigar_str = malloc(2*(cigar->end_offset-cigar->begin_offset));
    cigar_sprint(cigar_str,cigar,true);
  }
  // Print
  fprintf(stream,"%d\t%d\t%d\t%s\t%s\t%s\n",
      align_input->pattern_length,     // Pattern length
      align_input->text_length,        // Text length
      score,                           // Alignment score
      align_input->pattern,            // Pattern sequence
      align_input->text,               // Text sequence
      (cigar_null) ? "-" : cigar_str); // CIGAR
  // Free
  if (!cigar_null) free(cigar_str);
}
void benchmark_print_output(
    align_input_t* const align_input,
    const distance_metric_t distance_metric,
    const bool score_only,
    cigar_t* const cigar) {
  if (align_input->output_file) {
    // Compute score
    int score = -1;
    if (score_only) {
      score = cigar->score;
    } else {
      switch (distance_metric) {
        case indel:
        case edit:
          score = cigar_score_edit(cigar);
          break;
        case gap_linear:
          score = cigar_score_gap_linear(cigar,&align_input->linear_penalties);
          break;
        case gap_affine:
          score = cigar_score_gap_affine(cigar,&align_input->affine_penalties);
          break;
        case gap_affine_2p:
          score = cigar_score_gap_affine2p(cigar,&align_input->affine2p_penalties);
          break;
        default:
          break;
      }
    }
    // Print summary
    if (align_input->output_full) {
      benchmark_print_output_full(align_input->output_file,align_input,score,cigar);
    } else {
      benchmark_print_output_lite(align_input->output_file,align_input,score,cigar);
    }
  }
}
/*
 * Stats
 */
void benchmark_print_stats(
    FILE* const stream,
    align_input_t* const align_input,
    const bool print_wf_stats) {
  // General stats
  fprintf(stream,"[Accuracy]\n");
  fprintf(stream," => Alignments.Correct     ");
  counter_print(stream,&align_input->align_correct,&align_input->align,"alg       ",true);
  fprintf(stream," => Score.Correct          ");
  counter_print(stream,&align_input->align_score,&align_input->align,"alg       ",true);
  fprintf(stream,"   => Score.Total          ");
  counter_print(stream,&align_input->align_score_total,NULL,"score uds.",true);
  fprintf(stream,"     => Score.Diff         ");
  counter_print(stream,&align_input->align_score_diff,&align_input->align_score_total,"score uds.",true);
  fprintf(stream," => CIGAR.Correct          ");
  counter_print(stream,&align_input->align_cigar,&align_input->align,"alg       ",true);
  fprintf(stream,"   => CIGAR.Matches        ");
  counter_print(stream,&align_input->align_matches,&align_input->align_bases,"bases     ",true);
  fprintf(stream,"   => CIGAR.Mismatches     ");
  counter_print(stream,&align_input->align_mismatches,&align_input->align_bases,"bases     ",true);
  fprintf(stream,"   => CIGAR.Insertions     ");
  counter_print(stream,&align_input->align_ins,&align_input->align_bases,"bases     ",true);
  fprintf(stream,"   => CIGAR.Deletions      ");
  counter_print(stream,&align_input->align_del,&align_input->align_bases,"bases     ",true);
}