我是一名生物学家。我需要计算机专家的帮助来帮助编辑文本文件。
输入文件如下所示:
##dsfsd2
##sdf-sdf sasg 5.6.3
gi34_ex Gen CDS 161 317 . + . Name=Xm ZAK;created by=User
gi56_ex Gen CDS 2194 2280 . + . Name=Xm ZAK;created by=User
gi37_ex Gen CDS 2848 2951 . + . Name=Xm ZAK;created by=User
gi37_ex Gen CDS 4554 4619 . + . Name=Xm ZAK;created by=User
gi37_ex Gen CDS 4729 4756 . + . Name=Xm ZAK;created by=User
gi37_ex Gen extracted region 1 11677 . + . Name=Extracted region from gi|371443185|gb|JH556675.1|;Extracted interval="1960862 -> 1972538"
请注意,从第三行开始,输入有 12 列($1 到 $12),制表符分隔。文件的最后一行有 16 列($1 到 $16),制表符分隔。 ## 行应该被忽略。
我想将最后一行的 14 美元 (interval="1960862) 仅将数字 (1960862) 添加到 $4 列 (161,2194,2848..4729) 即 (161+1960862=1961023,2194+1960862=1963056.. ) 和 $5 列 (317,2280,2951..4756) 即 (317+1960862=1961179,2280+1960862=1963142),忽略最后一行。
输出应如下所示:
##dsfsd2
##sdf-sdf sasg 5.6.3
gi34_ex Gen CDS 1961023 1961179 . + . Name=Xm ZAK;created by=User
gi56_ex Gen CDS 1963056 1963142 . + . Name=Xm ZAK;created by=User
gi37_ex Gen CDS 1963710 1963813 . + . Name=Xm ZAK;created by=User
gi37_ex Gen CDS 1965416 1965481 . + . Name=Xm ZAK;created by=User
gi37_ex Gen CDS 1965591 1965618 . + . Name=Xm ZAK;created by=User
gi37_ex Gen extracted region 1 11677 . + . Name=Extracted region from gi|371443185|gb|JH556675.1|;Extracted interval="1960862 -> 1972538"
答案1
这是一个可行的解决方案。你可以在评论中找到解释。
#!/usr/bin/perl
use warnings;
use strict;
open my $IN, '<', '1.in' or die $!;
my $line;
$line = $_ while <$IN>; # Remember the last line.
my $last = $.; # Remember the number of the last line.
my $interval = (split /\t/, $line)[13]; # Extract the 14th column.
$interval =~ s/[^0-9]+//; # Keep only the number.
seek $IN, 0, 0; # Rewind to the beginning of the input.
$. = 0; # Restart the line counter.
my $start = 1; # Flag to skip first lines.
while (<$IN>) {
my @columns = split /\t/;
/^##/ or undef $start; # Unset start if the header is over.
if (not ($start or $. == $last)) { # Not header or last line?
$_ += $interval for @columns[3, 4];
}
print join "\t", @columns;
}