Skip to content

Commit

Permalink
feat: Add fa2fq and fq2fa conversion functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cauliyang committed Nov 6, 2023
1 parent 1a896de commit 355698d
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@ log = "0.4"
noodles-bam = "0.49.0"
noodles-bgzf = "0.25"
noodles-csi = "0.26"
noodles-fasta = "0.30"
noodles-fastq = "0.9"
noodles-sam = "0.46"
25 changes: 25 additions & 0 deletions src/fa2fq.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use anyhow::Result;
use noodles_fasta as fasta;
use noodles_fastq as fastq;
use std::path::Path;

pub fn fa2fq<P: AsRef<Path>>(input: P) -> Result<()> {
let mut reader = fasta::reader::Builder.build_from_path(input)?;

let mut writer = fastq::Writer::new(std::io::stdout());

for result in reader.records() {
let record = result?;
let name = record.name().to_string();
let sequence = record.sequence().as_ref().to_vec();
let qualities = vec![b'@'; sequence.len()];
let fastq_record = fastq::Record::new(
fastq::record::Definition::new(name, ""),
sequence,
qualities,
);
writer.write_record(&fastq_record)?;
}

Ok(())
}
26 changes: 26 additions & 0 deletions src/fq2fa.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use anyhow::Result;
use noodles_fasta as fasta;
use noodles_fastq as fastq;
use std::path::Path;
use std::{
fs::File,
io::{self, BufReader},
};

pub fn fq2fa<P: AsRef<Path>>(input: P) -> Result<()> {
let mut readerr = File::open(input)
.map(BufReader::new)
.map(fastq::Reader::new)?;

let mut writer = fasta::Writer::new(io::stdout());

for result in readerr.records() {
let record = result?;
let name = String::from_utf8(record.name().to_vec())?;
let sequence = fasta::record::Sequence::from(record.sequence().to_vec());
let fasta_record = fasta::Record::new(fasta::record::Definition::new(name, None), sequence);
writer.write_record(&fasta_record)?;
}

Ok(())
}
22 changes: 22 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use log::LevelFilter;
use std::path::PathBuf;

mod extract;
mod fa2fq;
mod fq2fa;
mod index;

#[derive(Parser)]
Expand Down Expand Up @@ -38,6 +40,18 @@ enum Commands {
/// Bam input file
input: PathBuf,
},

/// Convert a FASTA file to FASTQ
Fa2fq {
/// fasta input file
input: PathBuf,
},

/// Convert a FASTQ file to FASTA
Fq2fa {
/// fastq input file
input: PathBuf,
},
}

fn main() {
Expand Down Expand Up @@ -75,6 +89,14 @@ fn main() {
info!("'index' {input:?} ");
index::index_bam(input).unwrap();
}
Some(Commands::Fa2fq { input }) => {
info!("'fa2fq' {input:?} ");
fa2fq::fa2fq(input).unwrap();
}
Some(Commands::Fq2fa { input }) => {
info!("'fq2fa' {input:?} ");
fq2fa::fq2fa(input).unwrap();
}

// If no subcommand was used, it's a normal top level command
None => info!("No subcommand was used"),
Expand Down
4 changes: 4 additions & 0 deletions tests/data/test_case1.fa
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
>sequence1
ATGCTAGCTAGCTAGCTAGCTAGCTA
GCTAGCTAGCTAGCTAGCTAGCTAGC
TAGCTAGCTAGCTAGCTAGCTAGCTA
4 changes: 4 additions & 0 deletions tests/data/test_case1.fq
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@sequence1
ATGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTA
+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

0 comments on commit 355698d

Please sign in to comment.