pub fn parse_region_string(
    region_string: Option<&str>,
    bamfile_name: &String,
) -> Result<Option<GenomicInterval>> {
    let bam = bam::Reader::from_path(bamfile_name).chain_err(|| ErrorKind::BamOpenError)?;

    match region_string {
        Some(r) if r.contains(":") && r.contains("-") => {
            let split1: Vec<&str> = r.split(":").collect();
            if split1.len() != 2 {
                bail!("Invalid format for region. Please use <chrom> or <chrom:start-stop>");
            }
            let split2: Vec<&str> = split1[1].split("-").collect();
            if split2.len() != 2 {
                bail!("Invalid format for region. Please use <chrom> or <chrom:start-stop>");
            }
            let iv_chrom = split1[0].to_string();
            let iv_start = split2[0]
                .parse::<u32>()
                .chain_err(|| "Invalid position value specified in region string.")?; // read in as 1-based inclusive range
            let iv_end = split2[1]
                .parse::<u32>()
                .chain_err(|| "Invalid position value specified in region string.")?; // read in as 1-based inclusive range

            let mut tid: u32 = 0;
            for name in bam.header().target_names() {
                if u8_to_string(name)? == iv_chrom {
                    break;
                }
                tid += 1;
            }
            if tid as usize == bam.header().target_names().len() {
                bail!("Chromosome name for region is not in BAM file.");
            }

            let tlen = bam
                .header()
                .target_len(tid)
                .chain_err(|| ErrorKind::BamHeaderTargetLenAccessError)?;

            ensure!(
                iv_start > 0,
                "--region start position must be greater than 0."
            );
            ensure!(
                iv_start < tlen as u32,
                "--region start position exceeds the length of the contig."
            );
            ensure!(
                iv_end <= tlen as u32,
                "--region end position exceeds the length of the contig."
            );

            Ok(Some(GenomicInterval {
                tid: tid,
                chrom: iv_chrom,
                start_pos: iv_start - 1, // convert to 0-based inclusive range
                end_pos: iv_end - 1,     // convert to 0-based inclusive range
            }))
        }
        Some(r) => {
            let r_str = r.to_string();
            let mut tid: u32 = 0;
            for name in bam.header().target_names() {
                if u8_to_string(name)? == r_str {
                    break;
                }
                tid += 1;
            }
            if tid as usize == bam.header().target_names().len() {
                bail!("Chromosome name for region is not in BAM file.");
            }

            let tlen = bam
                .header()
                .target_len(tid)
                .chain_err(|| ErrorKind::BamHeaderTargetLenAccessError)?;
            Ok(Some(GenomicInterval {
                tid: tid,
                chrom: r_str,
                start_pos: 0,
                end_pos: (tlen - 1) as u32,
            }))
        }
        None => Ok(None),
    }
}
// this is really ugly. TODO a less verbose implementation
this is really ugly. TODO a less verbose implementation
this is really ugly . TODO a less verbose implementation
pub fn parse_region_string(
    region_string: Option<&str>,
    bamfile_name: &String,
) -> Result<Option<GenomicInterval>> {
    let bam = bam::Reader::from_path(bamfile_name).chain_err(|| ErrorKind::BamOpenError)?;
    match region_string {
        Some(r) if r.contains(":") && r.contains("-") => {
            let split1: Vec<&str> = r.split(":").collect();
            if split1.len() != 2 {
                bail!("Invalid format for region. Please use <chrom> or <chrom:start-stop>");
            }
            let split2: Vec<&str> = split1[1].split("-").collect();
            if split2.len() != 2 {
                bail!("Invalid format for region. Please use <chrom> or <chrom:start-stop>");
            }
            let iv_chrom = split1[0].to_string();
            let iv_start = split2[0]
                .parse::<u32>()
                .chain_err(|| "Invalid position value specified in region string.")?; // read in as 1-based inclusive range
            let iv_end = split2[1]
                .parse::<u32>()
                .chain_err(|| "Invalid position value specified in region string.")?; // read in as 1-based inclusive range
            let mut tid: u32 = 0;
            for name in bam.header().target_names() {
                if u8_to_string(name)? == iv_chrom {
                    break;
                }
                tid += 1;
            }
            if tid as usize == bam.header().target_names().len() {
                bail!("Chromosome name for region is not in BAM file.");
            }
            let tlen = bam
                .header()
                .target_len(tid)
                .chain_err(|| ErrorKind::BamHeaderTargetLenAccessError)?;
            ensure!(
                iv_start > 0,
                "--region start position must be greater than 0."
            );
            ensure!(
                iv_start < tlen as u32,
                "--region start position exceeds the length of the contig."
            );
            ensure!(
                iv_end <= tlen as u32,
                "--region end position exceeds the length of the contig."
            );
            Ok(Some(GenomicInterval {
                tid: tid,
                chrom: iv_chrom,
                start_pos: iv_start - 1, // convert to 0-based inclusive range
                end_pos: iv_end - 1,     // convert to 0-based inclusive range
            }))
        }
        Some(r) => {
            let r_str = r.to_string();
            let mut tid: u32 = 0;
            for name in bam.header().target_names() {
                if u8_to_string(name)? == r_str {
                    break;
                }
                tid += 1;
            }
            if tid as usize == bam.header().target_names().len() {
                bail!("Chromosome name for region is not in BAM file.");
            }
            let tlen = bam
                .header()
                .target_len(tid)
                .chain_err(|| ErrorKind::BamHeaderTargetLenAccessError)?;
            Ok(Some(GenomicInterval {
                tid: tid,
                chrom: r_str,
                start_pos: 0,
                end_pos: (tlen - 1) as u32,
            }))
        }
        None => Ok(None),
    }
}
pub fn parse_region_string ( region_string : Option < & str > , bamfile_name : & String , ) -> Result < Option < GenomicInterval > > { let bam = bam :: Reader :: from_path ( bamfile_name ) . chain_err ( | | ErrorKind :: BamOpenError ) ? ; match region_string { Some ( r ) if r . contains ( " " ) && r . contains ( " " ) => { let split1 : Vec < & str > = r . split ( " " ) . collect ( ) ; if split1 . len ( ) != 2 { bail ! ( " " ) ; } let split2 : Vec < & str > = split1 [ 1 ] . split ( " " ) . collect ( ) ; if split2 . len ( ) != 2 { bail ! ( " " ) ; } let iv_chrom = split1 [ 0 ] . to_string ( ) ; let iv_start = split2 [ 0 ] . parse :: < u32 > ( ) . chain_err ( | | " " ) ? ; // read in as 1-based inclusive range let iv_end = split2 [ 1 ] . parse :: < u32 > ( ) . chain_err ( | | " " ) ? ; // read in as 1-based inclusive range let mut tid : u32 = 0 ; for name in bam . header ( ) . target_names ( ) { if u8_to_string ( name ) ? == iv_chrom { break ; } tid += 1 ; } if tid as usize == bam . header ( ) . target_names ( ) . len ( ) { bail ! ( " " ) ; } let tlen = bam . header ( ) . target_len ( tid ) . chain_err ( | | ErrorKind :: BamHeaderTargetLenAccessError ) ? ; ensure ! ( iv_start 0 " " ) ; ensure ! ( iv_start tlen as u32 " " ) ; ensure ! ( iv_end tlen as u32 " " ) ; Ok ( Some ( GenomicInterval { tid : tid , chrom : iv_chrom , start_pos : iv_start - 1 , // convert to 0-based inclusive range end_pos : iv_end - 1 , // convert to 0-based inclusive range } ) ) } Some ( r ) => { let r_str = r . to_string ( ) ; let mut tid : u32 = 0 ; for name in bam . header ( ) . target_names ( ) { if u8_to_string ( name ) ? == r_str { break ; } tid += 1 ; } if tid as usize == bam . header ( ) . target_names ( ) . len ( ) { bail ! ( " " ) ; } let tlen = bam . header ( ) . target_len ( tid ) . chain_err ( | | ErrorKind :: BamHeaderTargetLenAccessError ) ? ; Ok ( Some ( GenomicInterval { tid : tid , chrom : r_str , start_pos : 0 , end_pos : ( tlen - 1 ) as u32 , } ) ) } None => Ok ( None ) , } }
this is really ugly.
this is really ugly .
region_string
Option<&str>
bamfile_name
&String
region_string
Option<&str>
bamfile_name
&String