use anyhow::{Context, Result};
use clap::Args;
use crate::core::{SeqReader, SeqRecord, SeqWriter};
#[derive(Args, Debug)]
pub struct MergepeArgs {
#[arg(value_name = "in1.fq")]
pub input1: String,
#[arg(value_name = "in2.fq")]
pub input2: String,
}
pub fn run(args: &MergepeArgs) -> Result<()> {
let mut reader1 = if args.input1 == "-" {
SeqReader::from_stdin()
} else {
SeqReader::from_path(&args.input1)
.with_context(|| format!("无法打开输入文件: {}", args.input1))?
};
let mut reader2 = if args.input2 == "-" {
SeqReader::from_stdin()
} else {
SeqReader::from_path(&args.input2)
.with_context(|| format!("无法打开输入文件: {}", args.input2))?
};
let mut writer = SeqWriter::to_stdout();
let mut record1 = SeqRecord::new(Vec::new(), Vec::new());
let mut record2 = SeqRecord::new(Vec::new(), Vec::new());
loop {
let has_r1 = reader1.read_next(&mut record1)?;
if !has_r1 {
if reader2.read_next(&mut record2)? {
eprintln!("[W::mergepe] 第1个文件的记录数较少。");
}
break;
}
let has_r2 = reader2.read_next(&mut record2)?;
if !has_r2 {
eprintln!("[W::mergepe] 第2个文件的记录数较少。");
break;
}
writer.write_record(&record1)?;
writer.write_record(&record2)?;
}
writer.flush()?;
Ok(())
}