将两个元素组合在一个数组perl中

combine two element in an array perl

我想使用 standalone blastn 比较两个序列。

但在我这样做之前,我必须将序列切割成每个片段的 1020nt。如果最后一个片段小于 1020nt,我必须将最后一个片段中的(序列)与前一个片段中的序列合并。例如我将一个序列切割成 1020nt 并得到 7 个片段。但是第 7 个片段小于 1020nt,所以我必须将它合并到第 6 个片段中。有谁知道如何使用 Perl language?

这是我的代码:

while(<QR>){

    chomp;
    s/>//g;

    my ($scaf,$seq)= split /\n/,$_,2;
    my $scaf_name = (split /\s+/,$scaf)[0];
    $seq =~ s/\s+//g;
    my @cut = ($seq =~ /(.{1,$chop_len})/g);

    if (length($#cut) < 1020) {
        $#cut="@cut[-2 .. -1]"; # i get error here
    }
    else {
    }

    print $#cut;
    for my $cut_num (0..$#cut){
        my $sgmID = "$scaf_name\_$cut_num";
        print CR ">$sgmID\n$cut[$cut_num]\n";
    }
}

close QR;
close CR;

实际上我在网上找到了这个Perl脚本,并对其进行了修改,以便我可以合并最后两个片段。

您需要删除最后一个剪辑并使用 .= 连接运算符将其附加到倒数第二个剪辑:

这是一个简化的例子:

#!/usr/bin/env perl

use warnings;
use strict;

my $total_length = 100;
my $chop_length  = 14;
my @letters = qw( A C G T );

my $long_string = join '', map { $letters[ int rand scalar @letters ] } ( 1 .. $total_length );
print "Long string ($total_length chars) :\n$long_string\n";
print '-'x$total_length . "\n";
my @cut = ($long_string =~ /(.{1,$chop_length})/g);

my $last_cut = pop @cut; # Take last one off
if (length $last_cut < $chop_length) {
    $cut[$#cut] .= $last_cut; # Concatenate it to the (now) last one
} else {
    push @cut, $last_cut; # Put it back, it has the right length
}

print "Parts cut into >= $chop_length char length pieces.\n";
for my $part (@cut) {
    print $part . "\n";
}

输出

Long string (100 chars) :
CCATCCTGCACATTCGGTGATTTATCAGAAGTAAGATCCTCGTCCCACTGACCGTGCGGGGATACGGAGCTCAAACAGAGAGAAACGGTTGGTCTGTAGA
----------------------------------------------------------------------------------------------------
Parts cut into >= 14 char length pieces.
CCATCCTGCACATT
CGGTGATTTATCAG
AAGTAAGATCCTCG
TCCCACTGACCGTG
CGGGGATACGGAGC
TCAAACAGAGAGAA
ACGGTTGGTCTGTAGA

的问题
if (length($#cut) < 1020) {
    $#cut="@cut[-2 .. -1]"; # i get error here
}

是它试图将字符串分配给$#cut$#cut@cut 的最后一个索引,因此它需要一个整数值。

像这样的东西应该可以工作:

if (length($#cut) < 1020) {
    $cut[$#cut-1] = join '', @cut[-2 .. -1];
    $#cut -= 1;  # Remove last element from @cut
}