【问题标题】:Calling upon keys within a hash in perl在 perl 中调用哈希中的键
【发布时间】:2016-01-08 01:13:08
【问题描述】:

这是本网站上my very first question 的扩展。

现在我有以下代码:

#!/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);

print "Please provide the minimum overlap between the two reads in bp";
my $min = <STDIN>;
chomp ($min);

print "Please provide the maximum overlap between the two reads in bp";
my $max = <STDIN>;
chomp ($max);

print "Now provide the output directory for your merged fastq reads";
my $output = <STDIN>;
chomp ($output);

#Open Directory

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

sub parse_fastq_filename {
    # Strip the suffix
    my $filename = shift;

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

    return {
        filename            => $filename,
        sample_id           => $sample_id,
        adapter_sequence    => $adapter_sequence,
        $L001               => $L001,
        format              => $format,
        001                 => $001
    };
}


# The pairs of files will be stored within the following hash.
my %pairs;

# List just the *.fastq files
for my $filename (@reads) {
    # 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;
}

for my $sample (values %pairs) {
    # Go through each pair in the sample
    for my $fastq (values %$sample) {
        print "$fastq->{filename} has format $fastq->{format}\n";
    }
}

for my $forward (values %pairs) {
    for my $fastq (values %$forward) {

    }
}

#print the keys within the hash
foreach (keys %pairs){
    print "$_ => $pairs{$_}\n";
}

#place the hash into an array
my @unique = keys %pairs;
print @unique;

#change directory to the user-inputted directory and merge reads 
chdir $dir;
`/usr/local/flash/flash @array[0] @array[1] -m $min -M $max -d $output`;

我成功地将正向和反向 fastq 文件按照 Unix 命令末尾的要求相互配对。现在我被困在如何相互调用对的各个组件。

我考虑过通过键入其键作为用户输入来访问哈希,但这些数字是在脚本中随机生成的,我不想强​​迫用户在运行 Unix 脚本之前输入这些值。

另外,我检查过的所有示例都已经在哈希中提供了固定数量的键。根据用户想要合并的文件数量,散列中的键数量也会有所不同。

我知道我想创建一个循环,以便命令可以多次运行;程序FLASH一次只能接受一个正向和反向fastq文件。因此,正向和反向 fastq 读取必须循环,以便处理每个读取。

在将它们配对成哈希后,如何取出要使用的特定文件?

【问题讨论】:

  • 每个问题都应该独立存在——您真的不应该依赖前一个问题的内容来为您填写详细信息。这个问题真的很难理解,但是我认为你想提取每个样本id对应两种格式的两个文件的名称。那正确吗?格式是否总是R1 和R2?
  • “现在我被困在如何相互调用配对的各个组件上。”您能否通过一个示例来说明此要求,以显示您到底想做什么? “我考虑过通过键入其键作为用户输入来访问散列”。我认为这也需要一些澄清。听起来您可以为此使用“keys %pairs”。
  • @Borodin 每对都有一个正向和反向读取,分别由 R1 和 R2 组件区分。所以每个文件都被格式化为 sample-id_adapter-sequence_L001_R1/R2_001.fastq/ Ramana 现在,我已经有了脚本,它可以相互解析正向和反向读取。但是它们被困在散列中的单个键中。因此,如果我在哈希中有一个样本 ID 为“11215”的键,那么该键将同时包含 R1 和 R2 文件。

标签: perl hash bioinformatics


【解决方案1】:

我对您需要的最佳猜测是将给定目录中的所有 .fastq 文件配对,并为每一对调用一次您的 flash 实用程序

您自己的代码存在许多问题,而且您似乎在%pairs 哈希中保留了更多信息(我认为您需要的只是文件名、示例 ID 和格式)所以我写了这个

我还使用了一个简单的 glob 而不是 opendir、readdir、closedir,这似乎是一个更好的选择

我已经尽可能地对此进行了测试,看起来还不错

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

use File::Basename qw/ basename /;

my $flash = '/usr/local/flash/flash';

print "Please provide the directory containing\n";
print "the fastq files from your Illumina MiSeq run: ";
my $fastq_file_dir = <STDIN>;
chomp $fastq_file_dir;

print "Please provide the minimum overlap between the two reads in bp: ";
my $min_overlap = <STDIN>;
chomp $min_overlap;

print "Please provide the maximum overlap between the two reads in bp: ";
my $max_overlap = <STDIN>;
chomp $max_overlap;

print "Now provide the output directory for your merged fastq reads: ";
my $out_dir = <STDIN>;
chomp $out_dir;

my @files = glob "$fastq_file_dir/*.fastq";

my %pairs;

for my $fastq_file ( @files ) {

    my $file = basename $fastq_file;
    my ($sample_id, $format) = (split /_/, $file)[0,3];

    $pairs{ $sample_id }{ $format } = $file;
}

printf "Processing %d pairs of FASTQ files\n\n", scalar keys %pairs;
chdir $fastq_file_dir;

for my $sample ( sort keys %pairs ) {

    my $pair = $pairs{$sample};
    my ($forward, $reverse) = @{$pair}{qw/ R1 R2 /};

    print "Forward: $forward\n";
    print "Reverse: $reverse\n";
    print "\n";

    my $cmd = qq{$flash $forward $reverse -m $min_overlap -M $max_overlap -d $out_dir};
    system $cmd;
}

【讨论】:

    猜你喜欢
    • 2017-09-04
    • 2016-01-28
    • 1970-01-01
    • 1970-01-01
    • 2011-06-14
    • 2021-12-30
    • 2023-04-08
    • 2018-04-02
    • 1970-01-01
    相关资源
    最近更新 更多