如何使用Perl以此表格格式转换此输出?

时间:2012-11-01 04:40:40

标签: perl file-io

我有这个输出数据:

10dvex1_miRNA_ce.out.data|3331
10dvex1_misc_RNA_ce.out.data|0
10dvex1_rRNA_ce.out.data|60
10dvex1_snoRNA_ce.out.data|895
10dvex1_snRNA_ce.out.data|2127
11dvex1_miRNA_ce.out.data|3367
11dvex1_misc_RNA_ce.out.data|0
11dvex1_rRNA_ce.out.data|54
11dvex1_snoRNA_ce.out.data|839
11dvex1_snRNA_ce.out.data|1770
12dvex1_miRNA_ce.out.data|3321
12dvex1_misc_RNA_ce.out.data|0
12dvex1_rRNA_ce.out.data|50
12dvex1_snoRNA_ce.out.data|854
12dvex1_snRNA_ce.out.data|1821

我想以这种格式转换此输出,如表:

`Fragment \t miRNA \t misc_RNA \t rRNA \t snRNA \t snoRNA`
 10 \t 3331 \t 0 \t 60 \t 2127 \ 895 \n
 11 \t 3367 \t 0 \t 54 \t 1770 \t 839 \n
 12 \t 3321 \t 0 \t 50 \t 1821 \t 854 \n

我需要将此表用作R的输入。一些想法?我用这个脚本尝试使用perl,但结果并不好:

#!/usr/bin/perl

use warnings;
use strict;

open(MYINPUTFILE, $ARGV[0]); # open for input
my @lines = <MYINPUTFILE>; # read file into list
print "Frag"."\t"."miRNA"."\t"."misc_RNA"."\t"."rRNA"."\t"."snRNA"."\t"."snoRNA"."\n";
foreach my $lines (@lines){
    my $pattern = $lines;
    $pattern =~ s/(.*)dvex\d_(.*)_(.*)\|(.*)/$1 $2  $4/g;
    print $1."\t".$4;
}
close(MYINPUTFILE);
exit;

结果:

Frag    miRNA   misc_RNA    rRNA    snRNA   snoRNA
10  333110  010 6010    89510   212711  336711  011 5411    83911   177012  332112  012 5012

不是这个想法。

5 个答案:

答案 0 :(得分:1)

您的打印声明中似乎缺少回车符。如,

print $1."\t".$4."\n";

答案 1 :(得分:1)

这样的事情:

print $1."\t".$4;
print "\n" if ($2 eq "snRNA");

每当你得到“snRNA”模式时就打破这条线;

答案 2 :(得分:1)

此代码有效。当片段号改变时它将换行。它假定数据的顺序始终与标题的顺序相对应。

open(MYINPUTFILE, $ARGV[0]); # open for input
my @lines = <MYINPUTFILE>; # read file into list
print "Frag"."\t"."miRNA"."\t"."misc_RNA"."\t"."rRNA"."\t"."snRNA"."\t"."snoRNA";
my $frag = '';
foreach my $line (@lines){
    if ( $line =~ /^(\d+)dvex.*\|(\d+)/ ) {
        my $fr = $1;
        if ( $fr ne $frag ) {
            print "\n$fr";
            $frag = $fr;
        }
        print "\t".$2;
    }
}
print "\n";
close(MYINPUTFILE);
exit;

输出如下:

Frag    miRNA   misc_RNA        rRNA    snRNA   snoRNA
10      3331    0       60      895     2127
11      3367    0       54      839     1770
12      3321    0       50      854     1821

答案 3 :(得分:0)

这个不关心文件的顺序,并从数据中提取标题。策略是将数据累积到结构中,然后在检查完所有数据后立即输出所有数据。如果你真的(真正的)大文件,你可能会吃掉内存。

open(MYINPUTFILE, $ARGV[0]); # open for input
my @lines = <MYINPUTFILE>; # read file into list
close(MYINPUTFILE);

## parse the data
my $types_found = {};
my $data = {};
foreach my $line (@lines){
    if ( $line =~ /^(\d+)dvex\d+_(.+)_ce\.out\.data\|(\d+)/ ) {
        $types_found->{$2} = '';
        $data->{$1}{$2} = $3;
    }
}

## print the header
my @types = sort keys %$types_found;
print "Frag";
foreach my $type ( @types ) {
    print "\t" . $type;
}
print "\n";

## print the rows
foreach my $frag ( sort keys %$data ) {
    print $frag;
    foreach my $type ( @types ) {
        print "\t" . $data->{$frag}{$type};
    }
    print "\n";
}

输出:

Frag    miRNA   misc_RNA        rRNA    snRNA   snoRNA
10      3331    0       60      2127    895
11      3367    0       54      1770    839
12      3321    0       50      1821    854

答案 4 :(得分:0)

这是另一个按OP的要求对列进行排序的选项:

use strict;
use warnings;

my %hash;
my @header = qw (Frag miRNA misc_RNA rRNA snRNA snoRNA);

/(\d+).+?_(.+)_ce.+\|(.+)/ and $hash{$1}{$2} = $3 for <>;

print +( join "\t", @header ) . "\n";

for my $key ( sort { $a <=> $b } keys %hash ) {
    my @line;
    push @line, $hash{$key}{ $header[$_] } for 1 .. $#header;
    print +( join "\t", $key, @line ) . "\n";
}

输出:

Frag    miRNA   misc_RNA    rRNA    snRNA   snoRNA
10  3331    0   60  2127    895
11  3367    0   54  1770    839
12  3321    0   50  1821    854