use crate::readstruct::GO;
use std::error::Error;
use std::fs::File;
use std::io::Write;
use std::io::{BufRead, BufReader};
#[tokio::main]
pub async fn molfetcheradd(pathfile: &str, searchgo: &str) -> Result<String, Box<dyn Error>> {
let file = File::open(pathfile).expect("file not found");
let fileread = BufReader::new(file);
let mut vecgo: Vec<GO> = Vec::new();
for i in fileread.lines() {
let line = i.expect("line not present");
let lines = line.split("\t").collect::<Vec<_>>();
vecgo.push(GO {
goid: lines[0].to_string(),
description: lines[1].to_string(),
ensembid: lines[2].to_string(),
})
}
let mut searchedid: Vec<GO> = Vec::new();
for i in vecgo.iter() {
if i.ensembid.contains(searchgo) {
searchedid.push(GO {
goid: i.goid.clone(),
description: i.description.clone(),
ensembid: i.ensembid.clone(),
})
}
}
let mut filewrite = File::create("go-profile.txt").expect("file not present");
writeln!(filewrite, "The searhed go are as follows:").expect("File doesnt exist");
for i in searchedid.iter() {
writeln!(filewrite, "{}\t{}\t{}", i.goid, i.description, i.ensembid)
.expect("file not present");
}
Ok("The path has been written".to_string())
}