2013-12-19 39 views
0

我有这样我怎样才能隔离数据块从一个文件

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); 

    } 
+0

这是圣诞大餐的选择? :) – simbabque

+3

你是什么意思的“它不工作”?是否有错误讯息?它是否做任何事情?你在谈论另一个文件...这个文件看起来和你给的样本一样吗? – simbabque

+2

此外,检查出https://metacpan.org/pod/Bio::AlignIO::maf。它可能是你正在寻找的东西。 – simbabque

回答

0

我相信我根据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; 
} 

}

1

如果你有块组织了一个文件,你可以经常改变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. 
} 
0

你还没有真正问过问题,所以很难得到很多帮助。但是,如果你只是想把每个块都放到一个数组的单独元素中,那真的很简单。您只需将$/设置为空字符串,即可将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>; 
} 
+0

的OP写*“我写了下面的脚本,但它不工作。可能有人帮助我吗?” *。这是我书中的一个问题。这不是一个很好的问题,但仍然是一个问题。他还表示,在评论*“它不会做任何事情。是的文件就像样本” *。这足以让我想起来。 – Borodin