Arrays Perl:组合两个数组,删除相同的头,保留格式
下面您将看到我生成的两个数组的内容。我如何组合这两个数组,删除重复的相同标题,但保持相同的格式—就像构建矩阵一样?我目前正在使用mesh将阵列合并为一个阵列,但它不太管用。我也没有遇到任何其他可能有帮助的东西,比如split、push等。我在下面展示了我的代码 输入文件“phred.txt” 输入文件“bases.txt” 打印阵列1的输出Arrays Perl:组合两个数组,删除相同的头,保留格式,arrays,perl,split,mesh,Arrays,Perl,Split,Mesh,下面您将看到我生成的两个数组的内容。我如何组合这两个数组,删除重复的相同标题,但保持相同的格式—就像构建矩阵一样?我目前正在使用mesh将阵列合并为一个阵列,但它不太管用。我也没有遇到任何其他可能有帮助的东西,比如split、push等。我在下面展示了我的代码 输入文件“phred.txt” 输入文件“bases.txt” 打印阵列1的输出 Sequence_1 1 2 3 4 5 打印阵列2的输出 Sequence_1 A B C D
Sequence_1
1 2 3 4 5
打印阵列2的输出
Sequence_1
A B C D E
Sequence_1
Sequence_1
1A 2B 3C 4D 5E
组合两个阵列所需的输出
Sequence_1
1 2 3 4 5
A B C D E
来自当前使用网格的策略
Sequence_1
A B C D E
Sequence_1
Sequence_1
1A 2B 3C 4D 5E
当前代码
use warnings;
use strict;
use List::MoreUtils qw(mesh);
open( PHRED, '<', '/path/to/phred.txt' ) or die $!;
open( BASES, '<', '/path/to/bases.txt' ) or die $!;
open( OUT, '>', '/path/to/out.txt' ) or die $!;
my @symbols;
my @bases;
my $count = 0;
my @finalphred;
my @finalbases;
my %hash = (
'"' => "1",
'#' => "2",
'$' => "3",
'%' => "4",
'&' => "5",
q(') => "6",
'(' => "7",
')' => "8"
);
while ( my $fastq = <PHRED> ) {
my $substring = substr( $fastq, 0, 5 );
push( @symbols, $substring );
}
foreach ( @symbols ) {
my @eachsymbol = split //, $_;
$count++;
push( @finalphred, "\n", "Sequence_$count\n" );
foreach my $symbol ( @eachsymbol ) {
if ( exists( $hash{$symbol} ) ) {
push( @finalphred, $hash{$symbol}, "\t" );
}
}
}
my $count_again = 0;
while ( my $fastq_again = <BASES> ) {
my $substring_again = substr( $fastq_again, 0, 5 );
push( @bases, $substring_again );
}
foreach ( @bases ) {
my @eachsymbol_again = split //, $_;
$count_again++;
push( @finalbases, "\n", "Sequence_$count_again\n" );
foreach my $symbol_again (@eachsymbol_again){
push (@finalbases, $symbol_again, "\t");
}
}
foreach (@finalphred){ #diagnostic to test array contents
print "$_";
}
foreach (@finalbases){ #diagnostic to test array contents
print "$_";
}
my @last = mesh @finalphred, @finalbases;
print OUT @last;
使用警告;
严格使用;
使用列表::MoreUtils qw(网格);
开放式(短语,
其中一个主要问题是,您再也不会打印出@eachsymbol\u的任何内容
。您将每个四个字符的字符串拆分为四个字符,并将其放入该数组中,然后将其忽略。它肯定不会产生您所说的输出
另外,mesh
这样组合阵列是一个奇怪的选择
作为参考,您的数组如下所示
@末日
[
“\n”,
“序列号\u 1\n”,
1.
“\t”,
2.
“\t”,
3.
“\t”,
4.
“\t”,
“\n”,
“序列2\n”,
5.
“\t”,
6.
“\t”,
7.
“\t”,
8.
“\t”,
)
@最终案例
(
“\n”,
“序列号\u 1\n”,
“\n”,
“序列号\u 2\n”
)
这两个数组中的元素数量甚至不相同,因此对它们调用mesh
没有多大意义
更新
这是一个工作程序
我使用了以下数据
phred.txt
“#$%
&'()
bases.txt
ABCD
EFGH
Perl代码
使用严格;
使用“全部”警告;
使用自动模具;
我的%xlate=map{chr($\u33)=>$\u1..8;
打开我的$phred_fh,“我认为这项工作根本不需要使用mesh
。将文件读入数组并处理它们,然后将它们写入格式文件更为简单。同时,如果文件大小较大,无法放入主内存,则还可以对其进行修改,以便逐行处理
#!/usr/bin/perl
use warnings;
use strict;
open( PHRED, '<', 'phred.txt' ) or die $!;
open( BASES, '<', 'bases.txt' ) or die $!;
open( OUT, '>', 'out.txt' ) or die $!;
my @finalphred;
my @finalbases;
my %hash = (
'"' => "1",
'#' => "2",
'$' => "3",
'%' => "4",
'&' => "5",
q(') => "6",
'(' => "7",
')' => "8"
);
while ( my $fastq = <PHRED> ) {
chomp $fastq;
my @items = split //, $fastq;
my @phreds = map {$hash{$_}} grep {exists $hash{$_}} @items;
push (@finalphred, \@phreds);
}
while ( my $fastq_again = <BASES> ) {
chomp $fastq_again;
my @items = split //, $fastq_again;
push(@finalbases, \@items);
}
for my $i (0 .. $#finalbases) {
if(@{$finalbases[$i]} && @{$finalphred[$i]}) {
print OUT "Sequence_" . ($i + 1),"\n";
printf OUT "%-6s" x scalar @{$finalphred[$i]},@{$finalphred[$i]};
print OUT "\n";
printf OUT "%-6s" x scalar @{$finalbases[$i]},@{$finalbases[$i]};
print OUT "\n";
}
else {
print "Both arrays doesn't contain equal no of elements\n";
}
}
!/usr/bin/perl
使用警告;
严格使用;
open(PHRED,这是Perl 6中的一个解决方案:
#!/usr/bin/env perl6
subset File of Str where *.IO.f;
sub MAIN (File :$phred='phred.txt', File :$bases='bases.txt') {
my $phred-fh = open $phred;
my $bases-fh = open $bases;
my %xlate = map { chr($_ + 33) => $_ }, 1..8;
for 1..* Z $phred-fh.IO.lines Z $bases-fh.IO.lines -> ($i, $score, $seq) {
put join "\n",·
"Sequence_$i",·
(map { %xlate{$_} }, $score.comb).join("\t"),·
$seq.comb.join("\t");
}
}
问题是什么?嗨,Matt,我刚刚编辑了我的问题,包括了我需要帮助的内容。基本上,我希望将我的两个数组打印为一个数组,这样它看起来就像是我的“合并两个数组的期望输出”“一些示例输入如何?这个问题不容易像编写的那样重现;请参阅:。您的代码肯定不会产生您所说的输出。@eachsymbol\u中的值也不会输出。
如果您真的希望我们帮助您,那么您需要说出真相。问题寻求调试帮助。”(“为什么这个代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现这些问题所需的最短代码。没有明确问题说明的问题对其他读者没有用处。请参阅:我相信我现在已经解决了您的批评。请让我知道我是否可以做些其他事情来帮助获得答案。比谢谢。