Perl 如何从文件中隔离数据块
我有一个这样的文件Perl 如何从文件中隔离数据块,perl,Perl,我有一个这样的文件 a score=-120.0 s Chicken.chr22 947 4 + 4081097 tgag s Turkey.chrZ 31560312 4 - 81011772 ttct s Mallard.apl2 2559751 4 - 153042893 TTCG a score=61344.0 s Chicken.chr22 951 15 + 4081097 c------tgggtga
a score=-120.0
s Chicken.chr22 947 4 + 4081097 tgag
s Turkey.chrZ 31560312 4 - 81011772 ttct
s Mallard.apl2 2559751 4 - 153042893 TTCG
a score=61344.0
s Chicken.chr22 951 15 + 4081097 c------tgggtgaagcactg
s Turkey.chrZ 31560316 15 - 81011772 t------tgggtaaggaactg
s Mallard.apl2 2559755 15 - 153042893 T------TGGGTTAGAAACTG
s Rock_pigeon.scaffold637 370291 15 + 418352 G------AGGGTCAGTTTCTG
s Common_cuckoo.scaffold569 739303 15 + 1009149 C------TGGGTTGAAAACTG
s Anna_s_hummingbird.scaffold44 3039342 15 - 10500161 C------TGGGTTAAACACTG
s Hoatzin.scaffold186 66281 15 + 155126 C------TGGATAAAGAACTG
s Emperor_penguin.Scaffold155 7152296 15 - 9595628 C------TGGGTAAAAAATTG
s Adelie_penguin.scaffold207 570235 15 - 3061884 C------TGGGTCAAAAACTG
s Crested_ibis.scaffold108 24271571 15 - 27015053 C------TGAGTAAAAACCTG
s Little_egret.scaffold238 365328 14 + 1015180 -------TGGGTTAAAAACTG
s Peregrine_falcon.scaffold41_1 3239034 14 - 3351735 -------TGGGTTAAAAGCTG
s Budgerigar.megascaffold18 4987476 14 + 17573940 -------TGGATAAAGAACTG
s Golden_collared_manakin.scaffold312 1652783 16 + 1993610 A-----CAGGGTTAGGAACTG
s Downy_woodpecker.scaffold1064 9341 21 - 117330 AGTGAGGTGGATTGTGAACTG
每个数据块的第一行以a
开头,其他行以s
开头。之后,一个空行分隔块
不幸的是,每个块包含不同数量的s
行
我想收集块(在具有相同格式的不同文件中),它将有第一行(从a
开始),并且s
行数将等于我将作为参数传递的数字
我写了下面的脚本,但不起作用。有人能帮我吗
#!/usr/bin/perl
use strict;
#use warnings;
use POSIX;
my $maf = $ARGV[0];
my $species = $ARGV[1];
#It filters the maf file. takes the blocks with all the species
open my $maf_file, $maf or die "Could not open $maf: $!";
my $count = 0;
my @array;
while (my $mline = <$maf_file>) {
next if /^\s*#/; #to avoid some lines with comments
if ($mline =~ /^a/) {
push(@array, $mline);
}
if ($mline =~ /^s/) {
until ($mline != ~/\s/) {
push(@array, $mline);
$count += 1;
}
foreach (@array) {
if ($count == $species) {
print "$_\n";
}
}
undef(@array);
}
#/usr/bin/perl
严格使用;
#使用警告;
使用POSIX;
my$maf=$ARGV[0];
my$species=$ARGV[1];
#它过滤maf文件。带着所有的物种去街区
打开我的$maf_文件,$maf或die“无法打开$maf:$!”;
我的$count=0;
我的@数组;
而(我的$mline=){
下一步if/^\s*#/##以避免出现带有注释的行
如果($mline=~/^a/){
推送(@array,$mline);
}
如果($mline=~/^s/){
直到($mline!=~/\s/){
推送(@array,$mline);
$count+=1;
}
foreach(@array){
如果($count==$species){
打印“$\u\n”;
}
}
未定义(@array);
}
如果文件是按块组织的,通常可以更改Perl的输入记录分隔符,使您能够逐块处理文件
# You should enable these.
use strict;
use warnings;
# Change the input record separator.
# You typically want to make this change within a subroutine or other narrowly
# scoped location within your program.
local $/ = "\n\n";
while (my $block = <>){
my @lines = split /\n/, $block;
# Do stuff with the lines in a block.
}
#您应该启用这些。
严格使用;
使用警告;
#更改输入记录分隔符。
#通常,您希望在子例程或其他程序中进行此更改
#程序中的作用域位置。
本地$/=“\n\n”;
while(my$block=){
my@lines=split/\n/,$block;
#用一个块中的线做一些东西。
}
您还没有真正提出问题,因此很难提供太多帮助。但是,如果您只想将每个块放入数组的单独元素中,这非常简单。只需将$/
设置为空字符串,即可将Perl设置为“段落模式”
open my $maf_file, $maf or die "Could not open $maf: $!";
my @blocks;
{
local $/ = ''; # always localise changes to Perl's special variables
@blocks = <$maf_file>;
}
打开我的$maf_文件,$maf或die“无法打开$maf:$!”;
我的@blocks;
{
local$/='';始终本地化对Perl特殊变量的更改
@区块=;
}
我相信我在FMc的帮助下解决了这个问题。
多谢各位
#!/usr/bin/perl
use strict;
use POSIX;
my $maf = $ARGV[0];
my $species = $ARGV[1];
my $nline = 0;
if ($species == "" || $species == "0") {
$species = 1;
#print "Forching number of species to 1\n";
}
open (FILE, $maf) or die("foo");
local $/ = "\n\n";
while (<FILE>){
my @lines = split /\n/, <>;
my $arraySize = @lines;
foreach (@lines) {
if ($arraySize == $species +1 ) {
print "$_\n";
$nline = 1;
}
}
if ($nline == 1) {
print"\n";
$nline = 0;
}
!/usr/bin/perl
严格使用;
使用POSIX;
my$maf=$ARGV[0];
my$species=$ARGV[1];
我的$nline=0;
如果($species==“”| |$species==“0”){
$species=1;
#打印“Forcing number of species to 1\n”;
}
打开(文件$maf)或死亡(“foo”);
本地$/=“\n\n”;
而(){
my@lines=split/\n/;
我的$arraySize=@行;
foreach(@行){
如果($arraySize==$species+1){
打印“$\u\n”;
$nline=1;
}
}
如果($nline==1){
打印“\n”;
$nline=0;
}
}这是圣诞晚餐精选吗?:)你说的“它不起作用”是什么意思?是否有错误消息?它是否有任何作用?您正在谈论另一个文件…该文件是否与您提供的示例相同?另外,请检查。它可能是您正在寻找的。它没有任何作用。是的,该文件与您的程序无法编译的示例一样:它具有不平衡的大括号
{…}
。此外,您决不能注释掉使用警告;您应该改为修复错误。不清楚您想要什么。在示例数据文件中,您有一个包含三条s
记录的块和一个包含十五条s
记录的块。假设我输入10
作为程序的第二个参数。您希望使用se吗在第一个区块中还要再添加几行?你认为第二个区块会删除五行吗?OP写道:“我写了下面的脚本,但它不起作用。有人能帮我吗?”这是我书中的一个问题。这不是一个很好的问题,但仍然是一个问题。他还在评论中说“它没有任何作用。是的,文件就像样本一样。”我想这就足够了。