2

我有这个输出数据:

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

是不是这个主意。

4

5 回答 5

1

看起来您只是在打印语句中缺少回车符。例如,

print $1."\t".$4."\n";
于 2012-11-01T04:44:53.047 回答
1

像这样的东西:

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

每当你得到模式“snRNA”时就断线;

于 2012-11-01T05:03:06.500 回答
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
于 2012-11-01T05:08:12.600 回答
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
于 2012-11-01T05:35:03.740 回答
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
于 2012-11-01T06:12:27.227 回答