调用数组中的特定元素

Call upon specific elements from array

好的,所以我有一堆具有以下两种格式之一的文件名:

Sample-ID_Adapter-Sequence_L001_R1_001.fastq(向前)

Sample-ID_Adapter-Sequence_L001_R2_001.fastq(反转)

正向和反向格式之间的唯一区别是文件名中的 R1 和 R2 元素。现在,我已经设法使用户能够使用以下脚本提供包含这些文件的目录:

#!/usr/bin/perl
use strict;
use warnings;

#Print Directory

print "Please provide the directory containing the FASTQ files from your Illumina MiSeq run \n";
my $FASTQ = <STDIN>;
chomp ($FASTQ);

#Open Directory

my $dir = $FASTQ;
opendir(DIR, $dir) or die "Cannot open $dir: $!";
my @forwardreads = grep { /R1_001.fastq/ } readdir DIR;
closedir DIR;

my $direct = $FASTQ;
opendir(DIR, $direct) or die "Cannot open $dir: $!";
my @reversereads = grep { /R2_001.fastq/ } readdir DIR;
closedir DIR;

foreach my $ffile (@forwardreads) {
    my $forward = $ffile;
    print $forward;
    }

foreach my $rfile (@reversereads) {
    my $reverse = $rfile;
    print $reverse;
    }

问题

我想用上面的脚本做的是找到一种方法来配对从相同样本 ID 派生的两个数组的元素。就像我说的,正向和反向文件(来自相同的样本 ID)之间的唯一区别是文件名的 R1 和 R2 部分。

我试过寻找从数组中提取元素的方法,但我想让程序代替我进行匹配。

感谢阅读,希望对大家有所帮助!

您必须解析文件名。幸运的是,这非常简单。剥离扩展后,您可以 split _.

上的片段
# Strip the file extension.
my($suffix) = $filename =~ s{\.(.*?)$}{};

# Parse Sample-ID_Adapter-Sequence_L001_R1_001
my($sample_id, $adapter_sequence, $uhh, $format, $yeah) = split /_/, $filename;

现在您可以随心所欲地使用它们了。

我会提出一些改进代码的建议。首先,将该文件名解析放入一个函数中,以便它可以重用并使主要代码更简单。其次,将文件名解析为散列而不是一堆标量,它会更容易使用和传递。最后,将文件名本身包含在该散列中,然后散列包含完整的数据。顺便说一句,这是面向对象编程的入门药物。

sub parse_fastq_filename {
    # Read the next (in this case first and only) argument.
    my $filename = shift;

    # Strip the suffix
    my($suffix) = $filename =~ s{\.(.*?)$}{};

    # Parse Sample-ID_Adapter-Sequence_L001_R1_001
    my($sample_id, $adapter_sequence, $uhh, $format, $yeah) = split /_/, $filename;

    return {
        filename            => $filename,
        sample_id           => $sample_id,
        adapter_sequence    => $adapter_sequence,
        uhh                 => $uhh,
        format              => $format,
        yeah                => $yeah
    };
}

然后不要分别查找左右格式的文件,而是在一个循环中处理所有内容。将匹配的左右对放在散列中。使用 glob 仅选取 .fastq 个文件。

# This is where the pairs of files will be stored.
my %pairs;

# List just the *.fastq files
while( my $filename = glob("$FASTQ_DIR/*.fastq")) {
    # Parse the filename into a hash reference
    my $fastq = parse_fastq_filename($filename);

    # Put each parsed fastq filename into its pair
    $pairs{ $fastq->{sample_id} }{ $fastq->{format} } = $fastq;
}

然后你就可以用%pairs做你想做的事了。下面是打印每个样本 ID 及其格式的示例。

# Iterate through each sample and pair.
# $sample is a hash ref of format pairs
for my $sample (values %pairs) {
    # Now iterate through each pair in the sample
    for my $fastq (values %$sample) {
        say "$fastq->{sample_id} has format $fastq->{format}";
    }
}