0

I have a DNA sequence that I have via reading in from a fasta and it is as a &[u8] variable. How can I most efficiently add in a $ at the end so that I can build a suffix array from it? I'm trying to mess around with pattern matching algorithms. Thanks in advance, I'm a bit new to this. I've tried simple + and some concat macros, but when I try it says that b"$" is &[u8; 1] which I think it means that it has a specified length and therefore can't be added, but then it might just be adding the bits anyway. Thanks for all your help.

Here's what I try to run

    use bio::data_structures::suffix_array::suffix_array;
    use bio::io::fasta;
    // use std::io;
    use std::env;
    use std::str;
    
    fn main() {
        let args: Vec<String> = env::args().collect();
        let filename: &str = &args[1];
    
        
        let reader = fasta::Reader::from_file(filename).unwrap();
        let mut nb_reads = 0;
        let mut nb_bases = 0;
    
    
        
        for result in reader.records() {
    
            let result_data = &result.unwrap();
    
            let dol: u8 = b"$"[0];
    
            nb_reads += 1;
            nb_bases += result_data.seq().len();
    
            let seq = result_data.seq() + b"$";
    
            println!("seq id: {:?}",result_data.id());
    
            let sa = suffix_array(seq);
            println!("sa: {:#?}",sa);
    
        }
    println!("Number of reads: {}", nb_reads);
    println!("Number of bases: {}", nb_bases);
}

I've figured out a bit more so that it doesn't complain, but it doesn't appear to work. I made it a vector and then pushed a variable into it, but then when I do that it results in a variable of unit type, in this instance seq_dol

fn main() {
    let args: Vec<String> = env::args().collect();
    let filename: &str = &args[1];

    
    let reader = fasta::Reader::from_file(filename).unwrap();
    let mut nb_reads = 0;
    let mut nb_bases = 0;


    
    for result in reader.records() {

        let result_data = &result.unwrap();

        let dol: u8 = b"$"[0];

        nb_reads += 1;
        nb_bases += result_data.seq().len();

        let seq_vec = result_data.seq().to_vec();
        
        let seq_dol = seq_vec.push(dol);

        println!("seq id: {:?}",result_data.id());

        let sa = suffix_array(seq_dol);
        println!("sa: {:?}",sa);

    }

    println!("Number of reads: {}", nb_reads);
    println!("Number of bases: {}", nb_bases);
}
Ryguy
  • 96
  • 8
  • 4
    you can't make a slice grown, what you want is a vector. – Stargateur Apr 15 '21 at 03:20
  • I don't really understand what you are saying. A vector for what? – Ryguy Apr 15 '21 at 03:26
  • 2
    `&[u8]` can't change its length. `Vec` can. – Cerberus Apr 15 '21 at 03:27
  • When I try to make it a vector using the function they have in bio rust as such `result_data.seq().to_vec().push(b"$")` I get the following error `expected 'u8', found '&[u8; 1]'` – Ryguy Apr 15 '21 at 03:30
  • 2
    no [mcve], no good answer – Stargateur Apr 15 '21 at 03:33
  • 3
    Consider [turning the `&[u8]` into a `Vec`](https://stackoverflow.com/q/47980023/3650362) and then using [`push(b'$')`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.push) – trent Apr 15 '21 at 03:40
  • I used apostrophes instead of quotes and the push worked without having to be made into a separate variable, thanks for that! – Ryguy Apr 15 '21 at 03:42
  • Either way it still says that the `let seq_vec = result_data.seq().to_vec().push(b'$');` is of type `()` – Ryguy Apr 15 '21 at 03:45
  • Oh yes! I just then split it by defining the variable `let mut seq_vec = result_data.seq().to_vec();` and then pushing on a separate line `seq_vec.push(b'$');`. Thanks so much! Now to use this vector in the `suffix_array` function I think I can figure out how to call the `Vec` as a `&[u8]` – Ryguy Apr 15 '21 at 04:05

0 Answers0