Reputation: 651
I have this output data:
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
I want to convert this output in this format, like as table:
`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
I need to use this table as Input to R. Some ideas? I try with perl with this script, but the result aren´t good:
#!/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;
And the result:
Frag miRNA misc_RNA rRNA snRNA snoRNA
10 333110 010 6010 89510 212711 336711 011 5411 83911 177012 332112 012 5012
Isn't the idea.
Upvotes: 2
Views: 2002
Reputation: 6204
Here's another option that orders the columns as requested by the 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";
}
Output:
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
Upvotes: 0
Reputation: 22731
This one doesn't care what order the file is in, and extracts the headers from the data. The strategy is to accumulate the data into a structure, then output everything at once after all the data has been examined. If you have really (REALLY) big files, you could eat up memory.
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";
}
output:
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
Upvotes: 0
Reputation: 22731
This code works. It will wrap when the fragment number changes. It assumes that the order of the data always corresponds with the order of the headers.
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;
Output looks like:
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
Upvotes: 1
Reputation: 16984
Something like this:
print $1."\t".$4;
print "\n" if ($2 eq "snRNA");
Break the line whenever you get the pattern "snRNA";
Upvotes: 1
Reputation: 22731
It looks like you are just missing a carriage return in your print statement. E.g.,
print $1."\t".$4."\n";
Upvotes: 1