From d797171dc66e3b1bbd086d889a33cb732b5c1ee7 Mon Sep 17 00:00:00 2001 From: Philipp Benner Date: Mon, 28 Oct 2024 15:30:52 +0100 Subject: [PATCH] 2024/10/28-15:30:52 (Linux VDI0092.zit.bam.de x86_64) --- src/granges_bed.rs | 14 ++++-- src/granges_gtf.rs | 2 - tests/test_granges.rs | 46 ++++++++++++++++--- .../{test_granges.bed => test_granges_1.bed} | 0 tests/test_granges_2.bed | 12 +++++ 5 files changed, 61 insertions(+), 13 deletions(-) rename tests/{test_granges.bed => test_granges_1.bed} (100%) create mode 100644 tests/test_granges_2.bed diff --git a/src/granges_bed.rs b/src/granges_bed.rs index c2e8328..15ed7ed 100644 --- a/src/granges_bed.rs +++ b/src/granges_bed.rs @@ -73,7 +73,7 @@ impl GRanges { Some(v) => v.clone(), None => comp![ self.ranges[i].from as i64 for i in 0..self.num_rows() ] }; - let thick_end = match self.meta.get_column_int("thickStart") { + let thick_end = match self.meta.get_column_int("thickEnd") { Some(v) => v.clone(), None => comp![ self.ranges[i].to as i64 for i in 0..self.num_rows() ] }; @@ -157,7 +157,7 @@ impl GRanges { pub fn bufread_bed3(&mut self, reader: &mut R) -> Result<(), Box> { let mut line = String::new(); while reader.read_line(&mut line)? > 0 { - let fields: Vec<&str> = line.trim().split('\t').collect(); + let fields: Vec<&str> = line.split_whitespace().collect(); if fields.len() < 3 { return Err(Box::new(io::Error::new(io::ErrorKind::InvalidInput, "Bed file must have at least 3 columns".to_string()))); } @@ -177,7 +177,7 @@ impl GRanges { let mut name = Vec::new(); let mut score = Vec::new(); while reader.read_line(&mut line)? > 0 { - let fields: Vec<&str> = line.trim().split('\t').collect(); + let fields: Vec<&str> = line.split_whitespace().collect(); if fields.len() < 6 { return Err(Box::new(io::Error::new(io::ErrorKind::InvalidInput, "Bed file must have at least 6 columns".to_string()))); } @@ -205,7 +205,11 @@ impl GRanges { let mut thick_end = Vec::new(); let mut item_rgb = Vec::new(); while reader.read_line(&mut line)? > 0 { - let fields: Vec<&str> = line.trim().split('\t').collect(); + let fields: Vec<&str> = line.split_whitespace().collect(); + if fields.len() > 0 && (fields[0] == "track" || fields[0] == "browser") { + line.clear(); + continue; + } if fields.len() < 9 { return Err(Box::new(io::Error::new(io::ErrorKind::InvalidInput, "Bed file must have at least 9 columns".to_string()))); } @@ -226,7 +230,7 @@ impl GRanges { self.meta.add("score" , MetaData::IntArray (score))?; self.meta.add("thickStart", MetaData::IntArray (thick_start))?; self.meta.add("thickEnd" , MetaData::IntArray (thick_end ))?; - self.meta.add("item_rgb" , MetaData::StringArray(item_rgb ))?; + self.meta.add("itemRgb" , MetaData::StringArray(item_rgb ))?; Ok(()) } diff --git a/src/granges_gtf.rs b/src/granges_gtf.rs index a349c68..71b0e05 100644 --- a/src/granges_gtf.rs +++ b/src/granges_gtf.rs @@ -513,8 +513,6 @@ mod tests { assert!(frame.is_none()); assert!(gene_id.is_some()); - println!("{:?}", gene_id.unwrap()); - assert_eq!(source.unwrap()[0], "transcribed_unprocessed_pseudogene"); assert_eq!(source.unwrap()[1], "processed_transcript"); diff --git a/tests/test_granges.rs b/tests/test_granges.rs index 3805731..b25fe0c 100644 --- a/tests/test_granges.rs +++ b/tests/test_granges.rs @@ -38,13 +38,47 @@ mod tests { // Import given granges assert!( - granges1.import_bed("tests/test_granges.bed", 3, false).is_ok()); + granges1.import_bed("tests/test_granges_1.bed", 3, false).is_ok()); + + assert_eq!( + granges1.num_rows(), 20); + + // Export to new file and import again + assert!( + granges1.export_bed3("tests/test_granges_1.bed.tmp", false).is_ok()); + assert!( + granges2.import_bed3("tests/test_granges_1.bed.tmp", false).is_ok()); + assert!( + granges1 == granges2); + + // Modify granges and test for inequality + granges2.seqnames[2] = String::from("test"); + assert!( + granges1 != granges2); + + assert!( + remove_file("tests/test_granges_1.bed.tmp").is_ok()); + + } + + #[test] + fn test_granges_bed9() { + + let mut granges1 = GRanges::default(); + let mut granges2 = GRanges::default(); + + // Import given granges + assert!( + granges1.import_bed("tests/test_granges_2.bed", 9, false).is_ok()); + + assert_eq!( + granges1.num_rows(), 9); // Export to new file and import again assert!( - granges1.export_bed3("tests/test_granges.bed.tmp", false).is_ok()); + granges1.export_bed9("tests/test_granges_2.bed.tmp", false).is_ok()); assert!( - granges2.import_bed3("tests/test_granges.bed.tmp", false).is_ok()); + granges2.import_bed9("tests/test_granges_2.bed.tmp", false).is_ok()); assert!( granges1 == granges2); @@ -54,7 +88,7 @@ mod tests { granges1 != granges2); assert!( - remove_file("tests/test_granges.bed.tmp").is_ok()); + remove_file("tests/test_granges_2.bed.tmp").is_ok()); } @@ -66,10 +100,10 @@ mod tests { // Import given granges assert!( - granges1.import_bed("tests/test_granges.bed", 3, false).is_ok() + granges1.import_bed("tests/test_granges_1.bed", 3, false).is_ok() ); assert!( - granges2.import_bed("tests/test_granges.bed", 3, false).is_ok() + granges2.import_bed("tests/test_granges_1.bed", 3, false).is_ok() ); granges1 = granges1.subset(&[0,1]); granges2 = granges1.subset(&[0,1]); diff --git a/tests/test_granges.bed b/tests/test_granges_1.bed similarity index 100% rename from tests/test_granges.bed rename to tests/test_granges_1.bed diff --git a/tests/test_granges_2.bed b/tests/test_granges_2.bed new file mode 100644 index 0000000..33fcd51 --- /dev/null +++ b/tests/test_granges_2.bed @@ -0,0 +1,12 @@ +browser position chr7:127471196-127495720 +browser hide all +track name="ItemRGBDemo" description="Item RGB demonstration" visibility=2 itemRgb="On" +chr7 127471196 127472363 Pos1 0 + 127471196 127472363 255,0,0 +chr7 127472363 127473530 Pos2 0 + 127472363 127473530 255,0,0 +chr7 127473530 127474697 Pos3 0 + 127473530 127474697 255,0,0 +chr7 127474697 127475864 Pos4 0 + 127474697 127475864 255,0,0 +chr7 127475864 127477031 Neg1 0 - 127475864 127477031 0,0,255 +chr7 127477031 127478198 Neg2 0 - 127477031 127478198 0,0,255 +chr7 127478198 127479365 Neg3 0 - 127478198 127479365 0,0,255 +chr7 127479365 127480532 Pos5 0 + 127479365 127480532 255,0,0 +chr7 127480532 127481699 Neg4 0 - 127480532 127481699 0,0,255