【问题标题】:Perl iterating through each line in a file and and appending to the end of each line in another file - follow upPerl 遍历文件中的每一行并附加到另一个文件中每一行的末尾 - 跟进
【发布时间】:2015-04-07 10:12:51
【问题描述】:

我有一个关于之前帖子的后续问题。 有问题的帖子是: Perl iterating through each line in a file and appending to the end of each line in another file

我用过:

use warnings;
use strict;

open my $animals, '<', 'File1.txt' or die "Can't open animals: $!";
open my $payloads, '<', 'File2.txt' or die "Can't open payloads: $!";

my @payloads = <$payloads>;   #each line of the file into an array
close $payloads or die "Can't close payloads: $!";

while (my $line = <$animals>) {
    chomp $line;
    print $line.$_ foreach (@payloads);
}
close $animals or die "Can't close animals: $!";

这适用于如下所示的文件:

file 1:     file 2:
line1       lineA
line2       lineB
line3       lineC

但不适用于如下所示的文件:

<01 line1
<02 line2

所以我想做的是:

file 1:              file 2:
<01 line1            <AA lineAA
<02 line2            <AB lineAB

应该变成:

file 3:
<01_AA line1lineAA
<01_AB line1lineAB
<02_AA line2lineAA
<02_AB line2lineAB

我试图通过在 while 循环中使用 while 循环拆分选项卡上的字符串来解决它(见下文),但我无法让它工作。

我的脚本:

#!C:/perl64/bin/perl.exe

use warnings;
use strict;


open my $file1, '<', 'file1.fasta' or die "Can't open file1: $!";
open my $file2, '<', 'file2.fasta' or die "Can't open file2:     $!";
open(OUT, '>', 'file3.fasta') or die "Cannot write $!";



while (<$file2>)
{
    chomp;
    my ($F2_Id, @SF2_seq) = split (/\t/, $_);

     while (<$file1>)
        {
            chomp;
            my ($F1_Id, @F1_seq) = split (/\t/, $_);
            foreach my $seq (@F1_seq)
                {
                    print OUT $F1_Id,"_",$F2_Id,"\t",$seq.$_ foreach (@F2_seq),"\n";
                }
            close;
        }
}

我最近才开始使用 perl,所以我可以想象脚本中有很多错误。

很抱歉这篇文章很长,但我希望能提供任何帮助。

【问题讨论】:

  • 所有行看起来都像&lt;01 line1 还是只是其中的一部分?
  • 是的,所有行看起来都像

标签: perl filehandle


【解决方案1】:

您可以将第一个文件的 id 和 seq 存储在数组数组中。

您还必须将第二个文件中的&lt; 替换为_

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

open my $LEFT,  '<', 'file1.fasta' or die "Can't open file1: $!";
open my $RIGHT, '<', 'file2.fasta' or die "Can't open file2: $!";
open my $OUT,   '>', 'file3.fasta' or die "Cannot write: $!";

my @left;
while (<$LEFT>) {
    chomp;
    push @left, [ split /\t/ ];
}

while (<$RIGHT>) {
    chomp;
    my ($id, $seq) = split /\t/;
    $id =~ s/</_/;
    print {$OUT} "$_->[0]$id\t$_->[1]$seq\n" for @left;
}
close $OUT or die "Cannot close: $!";

【讨论】:

  • 非常感谢!还有1个小故障。尽管有 s/>/_/,但第二个 '>' 不会被 '_' 替换;可能是什么原因?
  • 找到了,有一个'',我看了看。再次感谢!
猜你喜欢
  • 2013-01-21
  • 2018-06-15
  • 2016-04-04
  • 2010-09-23
  • 2014-11-13
  • 1970-01-01
  • 2020-02-02
  • 2013-06-24
  • 2012-12-08
相关资源
最近更新 更多