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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
use super::{
    filter_rank_by_identity::filter_rank_by_identity,
    run_parallel_blast::ParallelBlastOutput,
};
use crate::domain::dtos::{
    blast_builder::{BlastBuilder, Taxon},
    blast_result::{
        BlastQueryConsensusResult, BlastQueryNoConsensusResult,
        BlastQueryResult, BlastResultRow, ConsensusResult, TaxonomyElement,
        TaxonomyFieldEnum, ValidTaxonomicRanksEnum,
    },
};

use clean_base::utils::errors::{execution_err, MappedErrors};
use log::{error, warn};
use polars::prelude::{CsvReader, DataFrame, DataType, Schema};
use polars_io::prelude::*;
use polars_lazy::prelude::*;
use rayon::prelude::{IntoParallelIterator, ParallelIterator};
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, path::Path, sync::Arc};

#[derive(Clone, Debug, Serialize, Deserialize, clap::ValueEnum)]
pub enum ConsensusStrategy {
    Cautious,
    Relaxed,
}

/// BUild consensus identities from BlastN output.
///
/// Join the `blast` output with reference `taxonomies` file and calculate
/// consensus taxonomies based on the `subjects` frequencies and concordance.
pub(super) fn build_consensus_identities(
    blast_output: ParallelBlastOutput,
    taxonomies_file: &Path,
    config: BlastBuilder,
    strategy: ConsensusStrategy,
) -> Result<Vec<ConsensusResult>, MappedErrors> {
    // ? ----------------------------------------------------------------------
    // ? Load blast output as lazy
    // ? ----------------------------------------------------------------------

    let blast_output_df = get_results_dataframe(&blast_output.output_file)?;

    // ? ----------------------------------------------------------------------
    // ? Load taxonomies as lazy
    // ? ----------------------------------------------------------------------

    let taxonomies_df = get_taxonomies_dataframe(taxonomies_file)?;

    // ? ----------------------------------------------------------------------
    // ? Merge files as lazy
    // ? ----------------------------------------------------------------------

    let joined_df = blast_output_df.lazy().left_join(
        taxonomies_df.lazy(),
        col("subject"),
        col("subject"),
    );

    // ? ----------------------------------------------------------------------
    // ? Build consensus vector
    // ? ----------------------------------------------------------------------

    let mut query_results = fold_results_by_query(joined_df)?;

    let mut remaining_query_results = Vec::<BlastQueryResult>::new();

    let comparing_query_results = query_results
        .iter()
        .map(|result| result.query.to_owned())
        .collect::<Vec<String>>();

    blast_output.headers.into_iter().for_each(|header| {
        if !comparing_query_results.contains(&header) {
            remaining_query_results.push(BlastQueryResult {
                query: header,
                results: None,
            });
        };
    });

    query_results.append(&mut remaining_query_results);

    query_results
        .into_par_iter()
        .map(|result| {
            if result.results.to_owned().is_none() {
                return Ok(ConsensusResult::NoConsensusFound(
                    BlastQueryNoConsensusResult {
                        query: result.query,
                    },
                ));
            }

            match find_single_query_consensus(
                result.query,
                result.results.unwrap(),
                config.to_owned(),
                strategy.to_owned(),
            ) {
                Err(err) => {
                    panic!("Unexpected error on parse blast results: {err}")
                }
                Ok(res) => Ok(res),
            }
        })
        .collect()
}

fn find_single_query_consensus(
    query: String,
    result: Vec<BlastResultRow>,
    config: BlastBuilder,
    strategy: ConsensusStrategy,
) -> Result<ConsensusResult, MappedErrors> {
    // ? -----------------------------------------------------------------------
    // ? Group results by bit-score
    // ? -----------------------------------------------------------------------

    let grouped_results = result.to_owned().into_iter().fold(
        HashMap::<i64, Vec<BlastResultRow>>::new(),
        |mut init, result| {
            init.entry(result.bit_score)
                .or_insert_with(Vec::new)
                .push(result);

            init
        },
    );

    // ? -----------------------------------------------------------------------
    // ? Evaluate results by bit-score
    // ? -----------------------------------------------------------------------

    let mut sorted_keys = grouped_results.keys().cloned().collect::<Vec<i64>>();
    sorted_keys.sort_by(|a, b| b.cmp(a));

    let no_consensus = BlastQueryNoConsensusResult {
        query: query.to_owned(),
    };

    for score in sorted_keys.to_owned().into_iter() {
        let score_results = result
            .to_owned()
            .into_iter()
            .filter(|i| i.bit_score == score)
            .map(|mut i| i.parse_taxonomy())
            .collect::<Vec<BlastResultRow>>();
        //
        // Early return case no results found.
        //
        if score_results.len() == 0 {
            return Ok(ConsensusResult::NoConsensusFound(no_consensus));
        }
        //
        // Fetch the lower taxonomic rank case only one record returned.
        //
        if score_results.len() == 1 {
            for rank in ValidTaxonomicRanksEnum::ordered_iter(None) {
                match score_results[0].taxonomy.to_owned() {
                    TaxonomyFieldEnum::Parsed(taxonomies) => {
                        match taxonomies
                            .to_owned()
                            .into_iter()
                            .find(|i| &i.rank == rank)
                        {
                            None => {
                                return Ok(ConsensusResult::NoConsensusFound(
                                    no_consensus,
                                ))
                            }
                            Some(mut res) => {
                                let rank = match filter_rank_by_identity(
                                    config.to_owned().taxon.to_owned(),
                                    score_results[0].perc_identity,
                                    res.to_owned().rank,
                                ) {
                                    Err(err) => panic!("{err}"),
                                    Ok(rank) => rank,
                                };

                                if res.to_owned().rank == rank {
                                    res.mutated = true;
                                }

                                let position =
                                    ValidTaxonomicRanksEnum::ordered_iter(
                                        Some(true),
                                    )
                                    .position(|i| i == &rank)
                                    .unwrap();

                                let filtered_taxonomy =
                                    get_taxonomy_from_position(
                                        position,
                                        taxonomies.to_owned(),
                                    );

                                let lower_taxonomy = filtered_taxonomy.last();

                                if lower_taxonomy.is_some() {
                                    let lower_taxonomy =
                                        lower_taxonomy.unwrap();

                                    res.rank = lower_taxonomy.to_owned().rank;
                                    res.taxid = lower_taxonomy.taxid;
                                    res.taxonomy = Some(
                                        filtered_taxonomy
                                            .into_iter()
                                            .map(|i| i.taxonomy_to_string())
                                            .collect::<Vec<String>>()
                                            .join(";"),
                                    );
                                }

                                return Ok(ConsensusResult::ConsensusFound(
                                    BlastQueryConsensusResult {
                                        query,
                                        taxon: Some(res),
                                    },
                                ));
                            }
                        }
                    }
                    _ => panic!("Unable to parse taxonomy."),
                };
            }

            return Ok(ConsensusResult::NoConsensusFound(no_consensus));
        }
        //
        // Fetch the lower taxonomic rank case more than one record returned.
        //
        if score_results.len() > 1 {
            match find_multi_taxa_consensus(
                score_results,
                config.to_owned().taxon,
                no_consensus.clone(),
                strategy.to_owned(),
            ) {
                Err(err) => panic!("{err}"),
                Ok(res) => return Ok(res),
            };
        }
    }

    // Execute the default option
    //
    // If consensus identity not found in the previous steps, assumes by default
    // a no consensus option.
    Ok(ConsensusResult::NoConsensusFound(no_consensus))
}

/// Find the consensus among Blast results with multiple output.
///
/// In some cases blast results returns a list if records with the same percent
/// identity and bit-score. In this cases this logic could be applied to solve
/// the problem.
fn find_multi_taxa_consensus(
    records: Vec<BlastResultRow>,
    taxon: Taxon,
    no_consensus_option: BlastQueryNoConsensusResult,
    strategy: ConsensusStrategy,
) -> Result<ConsensusResult, MappedErrors> {
    // ? -----------------------------------------------------------------------
    // ? Collect the reference taxonomy vector
    //
    // The taxonomies vector contain elements of the reference taxonomy given
    // the selected strategy. The `Cautious` strategy selects the shortest
    // taxonomic vector as a reference. Otherwise (`Relaxed` strategy), the
    // longest taxonomic vector is selected.
    //
    // ? -----------------------------------------------------------------------

    let mut sorted_records = records.to_owned();

    sorted_records.sort_by(|a, b| {
        let a_taxonomy = force_parsed_taxonomy(a.taxonomy.to_owned());
        let b_taxonomy = force_parsed_taxonomy(b.taxonomy.to_owned());
        a_taxonomy.len().cmp(&b_taxonomy.len())
    });

    let reference_taxonomy = match strategy {
        ConsensusStrategy::Cautious => {
            sorted_records.first().unwrap().to_owned()
        }
        ConsensusStrategy::Relaxed => sorted_records.last().unwrap().to_owned(),
    };

    // ? -----------------------------------------------------------------------
    // ? Build the bi-dimensional
    //
    // Each position of the vector contain a vector of the `TaxonomyElement`
    // type.
    //
    // ? -----------------------------------------------------------------------

    let taxonomies = sorted_records
        .into_iter()
        .map(|i| force_parsed_taxonomy(i.taxonomy))
        .collect::<Vec<Vec<TaxonomyElement>>>();

    let lowest_taxonomy_of_higher_rank =
        get_rank_lowest_statistics(taxonomies.first().unwrap().to_owned());

    // ? -----------------------------------------------------------------------
    // ? Initialize the final response based on high taxonomic rank
    // ? -----------------------------------------------------------------------

    let mut final_taxon = BlastQueryConsensusResult {
        query: no_consensus_option.query.to_owned(),
        taxon: Some(lowest_taxonomy_of_higher_rank),
    };

    // ? -----------------------------------------------------------------------
    // ? Try to update the final response
    // ? -----------------------------------------------------------------------

    let reference_taxonomy_elements =
        force_parsed_taxonomy(reference_taxonomy.taxonomy.to_owned());

    for (index, ref_taxonomy) in reference_taxonomy_elements.iter().enumerate()
    {
        let mut level_taxonomies = Vec::<(ValidTaxonomicRanksEnum, i64)>::new();

        for taxonomy in taxonomies.iter() {
            if index < taxonomy.len() {
                if !level_taxonomies.contains(&(
                    taxonomy[index].rank.to_owned(),
                    taxonomy[index].taxid,
                )) {
                    level_taxonomies.push((
                        taxonomy[index].rank.to_owned(),
                        taxonomy[index].taxid,
                    ));
                }
            }
        }

        let loop_reference_taxonomy_elements =
            reference_taxonomy_elements.to_owned();

        if level_taxonomies.len() > 1 {
            final_taxon = build_taxon(
                no_consensus_option.query.to_owned(),
                taxon.to_owned(),
                reference_taxonomy_elements[index - 1].to_owned(),
                loop_reference_taxonomy_elements.to_owned(),
            );

            break;
        }

        final_taxon = build_taxon(
            no_consensus_option.query.to_owned(),
            taxon.to_owned(),
            ref_taxonomy.to_owned(),
            loop_reference_taxonomy_elements,
        );
    }

    Ok(ConsensusResult::ConsensusFound(final_taxon))
}

fn build_taxon(
    query: String,
    taxon: Taxon,
    mut element: TaxonomyElement,
    taxonomy: Vec<TaxonomyElement>,
) -> BlastQueryConsensusResult {
    element.rank = match filter_rank_by_identity(
        taxon.to_owned(),
        element.perc_identity,
        element.rank,
    ) {
        Err(err) => panic!("{err}"),
        Ok(res) => res,
    };

    let ordered_taxonomies = ValidTaxonomicRanksEnum::ordered_iter(Some(true));

    let updated_taxid = taxonomy.to_owned().iter().find_map(|i| {
        if i.rank == element.rank {
            Some(i.taxid)
        } else {
            None
        }
    });

    match updated_taxid {
        Some(taxid) => {
            let desired_rank_position = taxonomy
                .to_owned()
                .into_iter()
                .position(|item| item.taxid == taxid);

            let filtered_taxonomy = get_taxonomy_from_position(
                desired_rank_position.unwrap(),
                taxonomy.to_owned(),
            );

            element.mutated = true;
            element.taxid = taxid;
            element.taxonomy = Some(
                filtered_taxonomy
                    .into_iter()
                    .map(|i| i.taxonomy_to_string())
                    .collect::<Vec<String>>()
                    .join(";"),
            );
        }
        None => {
            let desired_rank_position = ordered_taxonomies
                .to_owned()
                .position(|rank| rank == &element.rank);

            let filtered_taxonomy = get_taxonomy_from_position(
                desired_rank_position.unwrap(),
                taxonomy.to_owned(),
            );

            let lower_taxonomy = filtered_taxonomy.last();

            if lower_taxonomy.is_some() {
                element.mutated = true;
                element.taxid = lower_taxonomy.unwrap().taxid;
                element.taxonomy = Some(
                    filtered_taxonomy
                        .into_iter()
                        .map(|i| i.taxonomy_to_string())
                        .collect::<Vec<String>>()
                        .join(";"),
                );
            }
        }
    }

    BlastQueryConsensusResult {
        query,
        taxon: Some(element),
    }
}

fn get_taxonomy_from_position(
    position: usize,
    taxonomy: Vec<TaxonomyElement>,
) -> Vec<TaxonomyElement> {
    taxonomy
        .into_iter()
        .enumerate()
        .filter_map(
            |(index, rank)| {
                if index <= position {
                    Some(rank)
                } else {
                    None
                }
            },
        )
        .collect::<Vec<TaxonomyElement>>()
}

/// Get lowest Blast statistics of a single rank vector
///
/// Collect the lowest statistics guarantees that the worst case will be
/// selected, avoiding over-interpretation of the results.
fn get_rank_lowest_statistics(
    mut rank_taxonomies: Vec<TaxonomyElement>,
) -> TaxonomyElement {
    rank_taxonomies
        .sort_by(|a, b| a.perc_identity.partial_cmp(&b.perc_identity).unwrap());

    rank_taxonomies.first().unwrap().to_owned()
}

fn force_parsed_taxonomy(taxonomy: TaxonomyFieldEnum) -> Vec<TaxonomyElement> {
    match taxonomy {
        TaxonomyFieldEnum::Literal(_) => {
            panic!("Invalid format taxonomic field.")
        }
        TaxonomyFieldEnum::Parsed(res) => res,
    }
}

/// Group results by query
///
/// Each query results should be grouped into a `BlastQueryResult` struct.
fn fold_results_by_query(
    joined_df: LazyFrame,
) -> Result<Vec<BlastQueryResult>, MappedErrors> {
    let mut binding = joined_df.collect().unwrap();
    let joined_df_chunked = binding.as_single_chunk_par();

    let mut iters = joined_df_chunked
        .iter()
        .map(|s| s.iter())
        .collect::<Vec<_>>();

    let mut mapped_results = HashMap::<String, Vec<BlastResultRow>>::new();

    for _ in 0..joined_df_chunked.height() {
        let mut counter = 0;

        let mut query: String = String::new();
        let mut subject: String = String::new();
        let mut perc_identity: f64 = 0.0;
        let mut align_length: i64 = 0;
        let mut mismatches: i64 = 0;
        let mut gap_openings: i64 = 0;
        let mut q_start: i64 = 0;
        let mut q_end: i64 = 0;
        let mut s_start: i64 = 0;
        let mut s_end: i64 = 0;
        let mut e_value: f64 = 0.0;
        let mut bit_score: i64 = 0;
        let mut taxonomy: String = String::new();

        for iter in &mut iters {
            let value = iter.next().expect("Not enough rows to iterate.");

            match counter {
                0 => query = value.to_owned().to_string().replace("\"", ""),
                1 => subject = value.to_owned().to_string().replace("\"", ""),
                2 => perc_identity = value.try_extract().unwrap(),
                3 => align_length = value.try_extract().unwrap(),
                4 => mismatches = value.try_extract().unwrap(),
                5 => gap_openings = value.try_extract().unwrap(),
                6 => q_start = value.try_extract().unwrap(),
                7 => q_end = value.try_extract().unwrap(),
                8 => s_start = value.try_extract().unwrap(),
                9 => s_end = value.try_extract().unwrap(),
                10 => e_value = value.try_extract().unwrap(),
                11 => bit_score = value.try_extract().unwrap(),
                12 => taxonomy = value.to_owned().to_string().replace("\"", ""),
                _ => warn!("Unmapped value: {:?}", value),
            };

            counter = counter + 1;
        }

        mapped_results.entry(query).or_insert_with(Vec::new).push(
            BlastResultRow {
                subject,
                perc_identity,
                align_length,
                mismatches,
                gap_openings,
                q_start,
                q_end,
                s_start,
                s_end,
                e_value,
                bit_score,
                taxonomy: TaxonomyFieldEnum::Literal(taxonomy),
            },
        );
    }

    Ok(mapped_results
        .into_iter()
        .map(|(k, v)| BlastQueryResult {
            query: k,
            results: match v.len() {
                0 => None,
                _ => Some(v),
            },
        })
        .collect::<Vec<BlastQueryResult>>())
}

/// Load BlastN output dataframe.
///
/// The results dataframe is a default tabular option of the Blast results.
fn get_results_dataframe(path: &Path) -> Result<DataFrame, MappedErrors> {
    let column_definitions = vec![
        ("query".to_string(), DataType::Utf8),
        ("subject".to_string(), DataType::Utf8),
        ("perc_identity".to_string(), DataType::Float64),
        ("align_length".to_string(), DataType::Int64),
        ("mismatches".to_string(), DataType::Int64),
        ("gap_openings".to_string(), DataType::Int64),
        ("q_start".to_string(), DataType::Int64),
        ("q_end".to_string(), DataType::Int64),
        ("s_start".to_string(), DataType::Int64),
        ("s_end".to_string(), DataType::Int64),
        ("e_value".to_string(), DataType::Float64),
        ("bit_score".to_string(), DataType::Float64),
    ];

    load_named_dataframe(path, column_definitions, vec![])
}

fn get_taxonomies_dataframe(path: &Path) -> Result<DataFrame, MappedErrors> {
    let column_definitions = vec![
        ("subject".to_string(), DataType::Utf8),
        ("taxonomy".to_string(), DataType::Utf8),
    ];

    load_named_dataframe(path, column_definitions, vec![])
}

fn load_named_dataframe(
    path: &Path,
    column_definitions: Vec<(String, DataType)>,
    exclude_list: Vec<String>,
) -> Result<DataFrame, MappedErrors> {
    // initialize dataframe schema
    let mut schema = Schema::new();

    // Map definitions to schema
    for (name, column_type) in &column_definitions {
        schema.with_column(name.to_owned().into(), column_type.to_owned());
    }

    // Collect column names
    let mut columns_names: Vec<String> = [].to_vec();

    for (name, _) in &column_definitions {
        // Check if the current column exists inside the `exclude_list` vector.
        // Case `true`, ignore the current column.
        if exclude_list.contains(name) {
            continue;
        }

        // Otherwise, include it into the desired columns vector.
        columns_names.push(name.to_owned());
    }

    // Load dataframe
    match CsvReader::from_path(path) {
        Err(err) => {
            error!("Unexpected error detected on read `blast_output`: {}", err);
            return Err(execution_err(
                String::from("Unexpected error occurred on load table."),
                None,
                None,
            ));
        }
        Ok(res) => Ok(res
            .with_delimiter(b'\t')
            .has_header(false)
            .with_schema(Arc::new(schema))
            .with_columns(Some(columns_names))
            .finish()
            .unwrap()),
    }
}