Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
“suchapalaver” committed Sep 6, 2021
0 parents commit 51ed321
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "krust"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bio = "*"
8 changes: 8 additions & 0 deletions Cargo.toml~
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "krust"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 suchapalaver

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
To make use of k-rust's multithreaded cocurrency test with the larger fasta data, which, be warned, is larger than github's recommended upload size.

Run k-rust on the test data, searching for kmers of length 5, like this:

$ cargo run 5 cerevisae.pan_S288C_chrI.fa

or, searching for kmers of length 21 across multiple records:

$ cargo run 21 cerevisae.pan.fa
106 changes: 106 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use std::{env, error::Error, fs::{self, File}, io::Write, path::Path, str, thread};

extern crate bio;
use bio::{alignment::sparse::hash_kmers, alphabets::dna::revcomp, io::fasta};

pub struct Config {

pub kmer_len: String,
pub filepath: String,
}

impl Config {
pub fn new(mut args: env::Args) -> Result<Config, &'static str> {

args.next();

let kmer_len = match args.next() {

Some(arg) => arg,
None => return Err("Didn't get a k-mer length"),
};
let filepath = match args.next() {

Some(arg) => arg,
None => return Err("Didn't get a file name"),
};
Ok(Config {

kmer_len,
filepath,
})
}
}

pub fn run(config: Config) -> Result<(), Box<dyn Error>> {

fs::create_dir("output")?;

let filepath: String = config.filepath;

let kmer_len = config.kmer_len.parse::<usize>().unwrap();

let reader = fasta::Reader::from_file(&filepath).unwrap();

let mut threads = Vec::new();

for result in reader.records() {

let t = thread::spawn( move || {

let result_data = &result.unwrap();

let pathname = format!("output/{}.tsv", result_data.id());

let path = Path::new(&pathname);

let display = path.display();

let mut file = match File::create(&path) {

Err(why) => panic!("couldn't create {}: {}", display, why),
Ok(file) => file,
};
for (kmer, kmer_positions) in hash_kmers(result_data.seq(), kmer_len) {

// trying to think about a better way to do
// this than the nested match patterns below.
// Here's immeidately below commented out is
// one attempt that compiles but this is from
// before I added print to file capability:
/*
let kmer_s = str::from_utf8(&kmer).unwrap();
let rvc = revcomp(kmer);
let rvc_s = str::from_utf8(&rvc).unwrap();
println!("{}\t{}\t{}\t{}", result_data.id(), kmer_s, rvc_s, kmer_positions.len());
}
});
*/
let rvc = revcomp(kmer);

match str::from_utf8(kmer) {

Err(e) => println!("Problem: {}", e),
Ok(kmer_s) => {

match str::from_utf8(&rvc) {

Ok(rvc) => {

let data = format!("{}\t{}\t{}\n", kmer_s, rvc, kmer_positions.len());
write!(file, "{}", data).expect("Unable to write file");
}
Err(why) => panic!("couldn't write to {}: {}", display, why),
}
}
}
}
});
threads.push(t);
}
for t in threads {

t.join().expect("thread failed");
}
Ok(println!("{}", filepath))
}
20 changes: 20 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::env;
use std::process;

use krust::Config;

fn main() {

let config = Config::new(env::args()).unwrap_or_else(|err| {
println!("Problem parsing arguments: {}", err);
process::exit(1);
});

println!("\nSearching for kmers of length {}", config.kmer_len);
println!("... in file {}\n", config.filepath);

if let Err(e) = krust::run(config) {
println!("Application error: {}", e);
process::exit(1);
}
}
3 changes: 3 additions & 0 deletions src/main.rs~
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

0 comments on commit 51ed321

Please sign in to comment.