Skip to content

Commit

Permalink
Circos does not like weird characters in chr names
Browse files Browse the repository at this point in the history
  • Loading branch information
Franklin Delehelle committed May 13, 2019
1 parent 4b0e9ee commit e39cff0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
36 changes: 16 additions & 20 deletions src/plot/circos_plot.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use ::plot::*;
use ::utils::slugify;

use std::io::prelude::*;
use std::fs::File;
Expand Down Expand Up @@ -38,39 +39,34 @@ impl Plotter for CircosPlotter {
.and_then(|_| { println!("Config written to `{}`", &config_filename); Ok(()) })
.chain_err(|| format!("Unable to write in `{}`", &config_filename))?;

println!("\nYou can now edit `{}` and/or run `circos {}` to generate the final plot.", config_filename, config_filename);
Ok(())
}
}

impl CircosPlotter {
fn plot_karyotype(&self) -> String {
fn encode_chromosome(chr: &Start) -> String {
format!("chr - {id} {label} {start} {end} {color}",
id = slugify(&chr.name),
label = slugify(&chr.name),
start = 0,
end = chr.length,
color = "grey"
)
}

let mut karyotype : String = self.result.strand1.map
.iter()
.map(|chr|
format!("chr - {id} {label} {start} {end} {color}",
id = str::replace(&chr.name.trim(), " ", "_"),
label = chr.name,
start = 0,
end = chr.length,
color = "grey"
)
)
.map(encode_chromosome)
.collect::<Vec<String>>()
.join("\n");

if self.result.strand1.name != self.result.strand2.name {
karyotype += "\n";
karyotype += &self.result.strand2.map
.iter()
.map(|chr|
format!("chr - {id} {label} {start} {end} {color}",
id = str::replace(&chr.name.trim(), " ", "_"),
label = chr.name,
start = 0,
end = chr.length,
color = "grey"
)
)
.map(encode_chromosome)
.collect::<Vec<String>>()
.join("\n");
}
Expand All @@ -82,10 +78,10 @@ impl CircosPlotter {
.iter()
.map(|sd|
format!("{chr_left} {chr_left_start} {chr_left_end} {chr_right} {chr_right_start} {chr_right_end} {color}",
chr_left = str::replace(&sd.chr_left.trim(), " ", "_"),
chr_left = slugify(&sd.chr_left),
chr_left_start = sd.chr_left_position,
chr_left_end = sd.chr_left_position + sd.length,
chr_right = str::replace(&sd.chr_right.trim(), " ", "_"),
chr_right = slugify(&sd.chr_right),
chr_right_start = sd.chr_right_position,
chr_right_end = sd.chr_right_position + sd.length,
color = if sd.reversed { "color=teal" } else { "color=orange"}
Expand Down
7 changes: 7 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@ pub fn complement_nucleotide(n: u8) -> u8 {
pub fn complemented(text: &[u8]) -> Vec<u8> {
text.iter().map(|x| complement_nucleotide(*x)).collect::<Vec<u8>>()
}

pub fn slugify(x: &str) -> String {
x .trim()
.replace(" ", "_")
.replace(":", "_")
.replace("|", "_")
}

0 comments on commit e39cff0

Please sign in to comment.