Perl在String中插入特定字符

时间:2022-09-13 08:35:54

I have two Characters first Sequences and Second Quality. Second Quality show first sequence`s quality like below.

我有两个字符第一个序列和第二个质量。第二质量显示第一序列的质量如下。

1. ACTGACTGACTG
2. KKKKKKKKKKKK

After processing(Aligned) first sequence it will change as below

在处理(对齐)第一序列之后,它将如下改变

1. ACT-GACTG-ACTG
2. KKKKKKKKKKKK

So I have to extend Second information also as below (Using space)

所以我必须扩展第二个信息如下(使用空格)

1. ACT-GACTG-ACTG
2. KKK KKKKK KKKK

I already did using for loop and check each character for first one and make a space for second one.

我已经使用for循环并检查每个字符的第一个字符并为第二个字符创建一个空格。

is there any easy and simple way to make it?

有没有简单易行的方法呢?

Thank you!

谢谢!

2 个解决方案

#1


3  

Use Positional Information and substr:

使用位置信息和子目录:

use strict;
use warnings;

my $str1 = 'ACT-GACTG-ACTG';
my $str2 = 'KKKKKKKKKKKK';

while ($str1 =~ /\W/g) {
    substr $str2, $-[0], 0, ' ';
}

print "$str1\n";
print "$str2\n";

Outputs:

输出:

ACT-GACTG-ACTG
KKK KKKKK KKKK

#2


1  

One way would be to add a space to Quality each time you add a dash to Sequence.

一种方法是每次向Sequen添加短划线时为Quality添加一个空格。

Or, you can loop over Sequence and check the positions of dashes, and insert spaces to Quality based on that:

或者,您可以遍历Sequence并检查破折号的位置,并根据以下内容向Quality插入空格:

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

my $sequence = 'ACT-GACTG-ACTG';
my $quality  = 'KKKKKKKKKKKK';

my $pos = 0;
while (0 <= ($pos = index $sequence, '-', $pos)) {
    substr $quality, $pos++, 0, ' ';
}

print "$quality\n";

#1


3  

Use Positional Information and substr:

使用位置信息和子目录:

use strict;
use warnings;

my $str1 = 'ACT-GACTG-ACTG';
my $str2 = 'KKKKKKKKKKKK';

while ($str1 =~ /\W/g) {
    substr $str2, $-[0], 0, ' ';
}

print "$str1\n";
print "$str2\n";

Outputs:

输出:

ACT-GACTG-ACTG
KKK KKKKK KKKK

#2


1  

One way would be to add a space to Quality each time you add a dash to Sequence.

一种方法是每次向Sequen添加短划线时为Quality添加一个空格。

Or, you can loop over Sequence and check the positions of dashes, and insert spaces to Quality based on that:

或者,您可以遍历Sequence并检查破折号的位置,并根据以下内容向Quality插入空格:

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

my $sequence = 'ACT-GACTG-ACTG';
my $quality  = 'KKKKKKKKKKKK';

my $pos = 0;
while (0 <= ($pos = index $sequence, '-', $pos)) {
    substr $quality, $pos++, 0, ' ';
}

print "$quality\n";