-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DRAFT: SortFastq #38
Open
kockan
wants to merge
1
commit into
fulcrumgenomics:main
Choose a base branch
from
kockan:can_sort_fastq
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
DRAFT: SortFastq #38
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod command; | ||
pub mod demux; | ||
pub mod sort_fastq; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use crate::commands::command::Command; | ||
use anyhow::Result; | ||
use clap::Parser; | ||
use ext_sort::{ExternalSorter, ExternalSorterBuilder, LimitedBufferBuilder}; | ||
use seq_io::fastq::Reader as FastqReader; | ||
use seq_io::fastq::{OwnedRecord, Record}; | ||
use serde::{Deserialize, Serialize}; | ||
use std::cmp::Ordering; | ||
use std::fs::File; | ||
use std::io::BufWriter; | ||
use std::path::{Path, PathBuf}; | ||
|
||
#[derive(Deserialize, Serialize)] | ||
struct SortableFastqRecord(OwnedRecord); | ||
|
||
impl PartialEq for SortableFastqRecord { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.0.id().unwrap() == other.0.id().unwrap() | ||
} | ||
} | ||
|
||
impl Eq for SortableFastqRecord {} | ||
|
||
impl PartialOrd for SortableFastqRecord { | ||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | ||
Some(self.cmp(&other)) | ||
} | ||
} | ||
|
||
impl Ord for SortableFastqRecord { | ||
fn cmp(&self, other: &Self) -> Ordering { | ||
self.0.id().unwrap().cmp(&other.0.id().unwrap()) | ||
} | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
// SortFastq (main class) and it's impls | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
/// Sorts the input FASTQ file by lexicographic ordering of its read names. | ||
/// | ||
/// ## Example Command Line | ||
/// | ||
/// ``` | ||
/// fqtk sort-fastq \ | ||
/// --input test.fq \ | ||
/// --output test.sorted.fq \ | ||
/// --max-records 1000000 | ||
/// ``` | ||
/// | ||
#[derive(Parser, Debug)] | ||
#[command(version)] | ||
pub(crate) struct SortFastq { | ||
/// Input fastq file | ||
#[clap(long, short = 'i', required = true)] | ||
input: PathBuf, | ||
|
||
/// Output fastq path | ||
#[clap(long, short = 'o', required = true)] | ||
output: PathBuf, | ||
|
||
/// Maximum number of records to be kept in buffer | ||
#[clap(long, short = 'm', default_value = "1000000")] | ||
max_records: usize, | ||
} | ||
|
||
impl Command for SortFastq { | ||
/// Executes the sort_fastq command | ||
fn execute(&self) -> Result<()> { | ||
let mut fq_reader = FastqReader::from_path(&self.input)?; | ||
let mut fq_writer = BufWriter::new(File::create(&self.output)?); | ||
|
||
let sorter: ExternalSorter< | ||
SortableFastqRecord, | ||
seq_io::fastq::Error, | ||
LimitedBufferBuilder, | ||
> = ExternalSorterBuilder::new() | ||
.with_tmp_dir(Path::new("./")) | ||
.with_buffer(LimitedBufferBuilder::new(self.max_records, true)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
.build() | ||
.unwrap(); | ||
|
||
let sorted = sorter | ||
.sort( | ||
fq_reader.records().map(|record| record.map(|record| SortableFastqRecord(record))), | ||
) | ||
.unwrap(); | ||
|
||
for item in sorted.map(Result::unwrap) { | ||
let _ = | ||
seq_io::fastq::write_to(&mut fq_writer, &item.0.head, &item.0.seq, &item.0.qual); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You probably want to use
FastqReader::with_capacity(1024 * 1024)
or something large like we do in the demux tool