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
// Copyright (c) 2018 10x Genomics, Inc. All rights reserved.

// Read a fasta file.

extern crate debruijn;
extern crate flate2;
extern crate io_utils;
extern crate string_utils;

use debruijn::dna_string::*;
use flate2::read::MultiGzDecoder;
use io_utils::*;
use std::process::Command;
use std::{
    fs::File,
    io::{prelude::*, BufReader},
};
use string_utils::*;

// This allows either a fasta file or a gzipped one.  This APPENDS to the
// dv and headers vectors.
// ◼ Kill the code duplication below.

pub fn read_fasta_into_vec_dna_string_plus_headers(
    f: &String,
    dv: &mut Vec<DnaString>,
    headers: &mut Vec<String>,
) {
    if !f.ends_with(".gz") {
        let fin = open_for_read![&f];
        let mut last: String = String::new();
        let mut first = true;
        for line in fin.lines() {
            let s = line.unwrap();
            if first {
                if !s.starts_with(">") {
                    panic!("fasta format failure reading {}", f);
                }
                first = false;
                headers.push(s.get(1..).unwrap().to_string());
            } else {
                if s.starts_with(">") {
                    dv.push(DnaString::from_dna_string(&last));
                    last.clear();
                    headers.push(s.get(1..).unwrap().to_string());
                } else {
                    last += &s;
                }
            }
        }
        dv.push(DnaString::from_dna_string(&last));
    } else {
        let gz = MultiGzDecoder::new(std::fs::File::open(&f).unwrap());
        let fin = std::io::BufReader::new(gz);
        let mut last: String = String::new();
        let mut first = true;
        for line in fin.lines() {
            let s = line.unwrap();
            if first {
                if !s.starts_with(">") {
                    panic!("fasta format failure");
                }
                first = false;
                headers.push(s.get(1..).unwrap().to_string());
            } else {
                if s.starts_with(">") {
                    dv.push(DnaString::from_dna_string(&last));
                    last.clear();
                    headers.push(s.get(1..).unwrap().to_string());
                } else {
                    last += &s;
                }
            }
        }
        dv.push(DnaString::from_dna_string(&last));
    }
}

pub fn read_fasta_contents_into_vec_dna_string_plus_headers(
    f: &String,
    dv: &mut Vec<DnaString>,
    headers: &mut Vec<String>,
) {
    let mut last: String = String::new();
    let mut first = true;
    let lines = f.split('\n').collect::<Vec<&str>>();
    for i in 0..lines.len() {
        let s = &lines[i];
        if first {
            if !s.starts_with(">") {
                panic!("fasta format failure reading {}", f);
            }
            first = false;
            headers.push(s.get(1..).unwrap().to_string());
        } else {
            if s.starts_with(">") {
                dv.push(DnaString::from_dna_string(&last));
                last.clear();
                headers.push(s.get(1..).unwrap().to_string());
            } else {
                last += &s;
            }
        }
    }
    dv.push(DnaString::from_dna_string(&last));
}

// This APPENDS.

pub fn read_fasta_headers(f: &String, headers: &mut Vec<String>) {
    let fin = open_for_read![&f];
    let mut last: String = String::new();
    let mut first = true;
    for line in fin.lines() {
        let s = line.unwrap();
        if first {
            if !s.starts_with(">") {
                panic!("fasta format failure reading {}", f);
            }
            first = false;
            headers.push(s.get(1..).unwrap().to_string());
        } else {
            if s.starts_with(">") {
                last.clear();
                headers.push(s.get(1..).unwrap().to_string());
            } else {
                last += &s;
            }
        }
    }
}

// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
// LOAD GENBANK ACCESSION
// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓

pub fn load_genbank_accession(accession: &String, bases: &mut DnaString) {
    let link = format!(
        "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/\
         efetch.fcgi?db=nucleotide&amp;id={}&amp;rettype=fasta",
        accession
    );
    let o = Command::new("csh")
        .arg("-c")
        .arg(format!("curl \"{}\"", link))
        .output()
        .expect("failed to execute curl command");
    let fasta = String::from_utf8(o.stdout).unwrap();
    let mut fasta = fasta.after("\n").to_string();
    // ◼ The following assert should not be necessary: the DnaString constructor
    // ◼ should gag if it gets nonsense input.
    assert!(!fasta.contains("moved"));
    fasta = fasta.replace("\n", "");
    *bases = DnaString::from_dna_string(&fasta);
}