美文网首页
gff转化成bed文件 rust代码实现

gff转化成bed文件 rust代码实现

作者: 球果假水晶蓝 | 来源:发表于2023-01-18 23:04 被阅读0次
    use std::env;
    use colored::Colorize;
    
    fn help() {
        println!("
    {} {}  {}  {}
    2023/01/19
    ",  format!("usage: gff2bed").yellow(),
        format!("gene/mRNA").blue(),
        format!("ID/Name").green(),
        format!("gff_file").cyan());
    }
    
    #[warn(unused_variables)]
    fn main() -> std::io::Result<()> {
        let args:Vec<String> = env::args().collect();
        if args.len() == 1 {
            help();
        }
        if args.len() != 4 {
            help();
            return Ok(());
        }
        let gene = &args[1];
        let id = &args[2];
        let file = &args[3];
    
    
        let mut reader = my_reader::BufReader::open(file)?;
        let mut buffer = String::new();
    
        while let Some(line) = reader.read_line(&mut buffer) {
            let line = line?;
            line_transform(line,id,gene);
    
            
        }
    
        Ok(())
    }
    fn line_transform(line:& mut String,id:&String,gene:&String)  -> i32{
        let a = id;
        let a_l:Vec<&str> = line.split('\t').collect();
        if a_l[0].starts_with('#'){
            return 0;
        }
        // print!("{:?}",a_l);
        let chr = a_l[0];
        let start = a_l[3];
        let end = a_l[4];
        let direction = a_l[6];
        if a_l[2] == gene {
            let ll = a_l[8].split(';').collect::<Vec<&str>>();
            let id = ll.iter().position(| &x|x.starts_with(a)).expect("get id error");
            let mut aa = a.to_string();
            aa.push('=');
            println!("{}\t{}\t{}\t{}\t{}\t{}",chr,start,end,ll[id].trim_start_matches(&aa).trim_end_matches('\n'),'0',direction);
            return  1;
        }
        return  0;
    
    
    
    
    }
    mod my_reader {
        use std::{
            fs::File,
            io::{self, prelude::*},
        };
    
        pub struct BufReader {
            reader: io::BufReader<File>,
        }
    
        impl BufReader {
            pub fn open(path: impl AsRef<std::path::Path>) -> io::Result<Self> {
                let file = File::open(path)?;
                let reader = io::BufReader::new(file);
    
                Ok(Self { reader })
            }
    
            pub fn read_line<'buf>(
                &mut self,
                buffer: &'buf mut String,
            ) -> Option<io::Result<&'buf mut String>> {
                buffer.clear();
    
                self.reader
                    .read_line(buffer)
                    .map(|u| if u == 0 { None } else { Some(buffer) })
                    .transpose()
            }
        }
    }
    

    前一段时间一直在折腾rust语言,这语言太难了,只能刷刷力扣题目,然后在平时的生信项目中多使用rust解决工具。

    相关文章

      网友评论

          本文标题:gff转化成bed文件 rust代码实现

          本文链接:https://www.haomeiwen.com/subject/ccvfhdtx.html