美文网首页
Combining the multi-fastq files

Combining the multi-fastq files

作者: 生信学习者2 | 来源:发表于2020-12-28 15:13 被阅读0次

按照文件要求合并同一样本的多个fastq文件至目标目录。更多知识分享请到 https://zouhua.top/

脚本

#!/usr/bin/perl

use warnings;
use strict;
use Getopt::Long;
use Cwd 'abs_path';

my ($file, $fq_dir, $out, $help, $version);
GetOptions(
    "f|file:s"  =>  \$file,
    "d|fq_dir:s"   =>  \$fq_dir,
    "o|out:s"   =>  \$out,
    "h|help:s"  =>  \$help,
    "v|version" =>  \$version
);
&usage if(!defined $out);

my $cwd = abs_path;
# output
system "mkdir -p $out" unless(-d $out);

my %sampleid;
open(IN, "$file") or die "can't open $file\n";
<IN>;
while(<IN>){
    chomp;
    my @array = split("\t", $_);
    $sampleid{$array[0]} = 1;
}
close(IN);

my (%read1, %read2);
opendir(DIR, $fq_dir) or die "can't open $fq_dir";
foreach (sort grep(/gz$/, readdir(DIR))){
    my $samplename = $1 if $_ =~ m/(\S+)\_(S\d+)\_L/;    # N47_S181_L001_R1_001.fastq.gz
    if(exists($sampleid{$samplename})){
        my $read_mode = $1 if $_ =~ m/\_(R\d)\_(\d+)\.fastq.gz/;  # N47_S181_L001_R1_001.fastq.gz
        if($read_mode eq "R1"){
            my $sample_key = join("_", $samplename, "R1");
            my $fq1 = join("/", $fq_dir, $_);
            push(@{$read1{$sample_key}}, $fq1);
        }elsif($read_mode eq "R2"){
            my $sample_key = join("_", $samplename, "R2");
            my $fq2 = join("/", $fq_dir, $_);
            push(@{$read2{$sample_key}}, $fq2);
        }
    }
}
closedir(DIR);


my $out_dir = join("/", $cwd, $out);

foreach my $key_r1 (keys %read1){
    my $fq_r1 = join(" ", @{$read1{$key_r1}});
    my $length = scalar(@{$read1{$key_r1}});
    my $target = "$out_dir/$key_r1\.fastq.gz";
    unless(-e $target){
        my $shell = "cat $fq_r1 > $target";
        system("$shell");
        print("$key_r1 ($length fastq) successfully combined\n");
    }
}

foreach my $key_r2 (keys %read2){
    my $fq_r2 = join(" ", @{$read2{$key_r2}});
    my $length2 = scalar(@{$read2{$key_r2}});
    my $target2 = "$out_dir/$key_r2\.fastq.gz";
    unless(-e $target2){
        my $shell2 = "cat $fq_r2 > $target2";
        system("$shell2");
        print("$key_r2 ($length2 fastq) successfully combined\n");
    }
}

sub usage{
        print <<USAGE;
usage:
        perl $0 -f <file> -d <fq_dir> -o <out>
options:
        -f|file     :[essential].
        -d|dir      :[essential].
        -o|out      :[essential].
USAGE
    exit;
};

sub version {
    print <<VERSION;
    version:    v1.0
    update:     20201228 - 20201228
    author:     zouhua1\@outlook.com
VERSION

运行

perl combine_fq.pl -f phenotype.tsv -d /data_backup/rawdata/-o RawData_rename > combine.log

相关文章

网友评论

      本文标题:Combining the multi-fastq files

      本文链接:https://www.haomeiwen.com/subject/zextoktx.html