axis reverse

N

Nina

First format:
A| 4 10 5 7 4 6
C| 2 0 0 0 0 1
T| 3 0 3 1 3 1
G| 2 0 2 2 3 2

Second format:
#pos A C T G
-3 4 2 3 2
-2 10 0 0 0
-1 5 0 3 2
1 7 0 1 2
2 4 0 3 3
3 6 1 1 2

Can I ask, how can I make the script to change the first format file to
second one? It's like
axis reverse.
Thanks a lot!
 
X

xhoster

Nina said:
First format:
A| 4 10 5 7 4 6
C| 2 0 0 0 0 1
T| 3 0 3 1 3 1
G| 2 0 2 2 3 2

Second format:
#pos A C T G
-3 4 2 3 2
-2 10 0 0 0
-1 5 0 3 2
1 7 0 1 2
2 4 0 3 3
3 6 1 1 2

Can I ask, how can I make the script to change the first format file to
second one? It's like
axis reverse.

I can't do that exactly, because it is not clear where the first column
in the result comes from. But maybe this will help you get started.

transpose.pl:

my @data;
while (<STDIN>) {
chomp;
my @x=split;
push @{$data[$_]}, $x[$_] foreach (0..$#x) ;
};
print join ("\t", @$_), "\n" foreach @data ;


Notice that everything is stored in memory at once. There is no easy
alternative to that.

Xho
 
M

Matija Papec

X-Ftn-To: Nina

Nina said:
First format:
A| 4 10 5 7 4 6
C| 2 0 0 0 0 1
T| 3 0 3 1 3 1
G| 2 0 2 2 3 2

Second format:
#pos A C T G
-3 4 2 3 2
-2 10 0 0 0
-1 5 0 3 2
1 7 0 1 2
2 4 0 3 3
3 6 1 1 2

Can I ask, how can I make the script to change the first format file to
second one? It's like
axis reverse.

Assuming that all lines in first format have equal number of elements,

my @arr1 = (
[qw/A 4 10 5 7 4 6/],
[qw/C 2 0 0 0 0 1/],
[qw/T 3 0 3 1 3 1/],
[qw/G 2 0 2 2 3 2/],
);

my @arr2 =
map {
my $i = $_;
[ map $arr1[$_][$i], 0 .. $#arr1 ];
}
0 .. $#{ $arr1[0] };

use Data::Dumper;
print Dumper \@arr2;


Perhaps I'm wrong, but I have strong filing that this could be your homework
so you're on your own for the #pos column. :)
 
W

William James

Nina said:
First format:
A| 4 10 5 7 4 6
C| 2 0 0 0 0 1
T| 3 0 3 1 3 1
G| 2 0 2 2 3 2

Second format:
#pos A C T G
-3 4 2 3 2
-2 10 0 0 0
-1 5 0 3 2
1 7 0 1 2
2 4 0 3 3
3 6 1 1 2

Can I ask, how can I make the script to change the first format file to
second one? It's like
axis reverse.
Thanks a lot!

I really think that you would find it more enjoyable to use Ruby.
Try this:

# Read file and create transposed array.
a = ARGF.read.split("\n").map{|line| line.split(/\|?\s+/)}.transpose
half = a.size / 2
# Create "#pos" column.
pos = ['#pos'] + (-half .. half).reject{|n| n==0}
# Combine and print.
puts pos.zip(a).map{|x| x.flatten.join("\t")}


Output:

#pos A C T G
-3 4 2 3 2
-2 10 0 0 0
-1 5 0 3 2
1 7 0 1 2
2 4 0 3 3
3 6 1 1 2
 
D

Damian James

Assuming that all lines in first format have equal number of elements,

I'd actually suggest looking at Math::Matrix, since chances are
the OP will eventually want more of its features than just
transposition.

--Damian
 
A

Anno Siegel

Nina said:
First format:
A| 4 10 5 7 4 6
C| 2 0 0 0 0 1
T| 3 0 3 1 3 1
G| 2 0 2 2 3 2

Second format:
#pos A C T G
-3 4 2 3 2
-2 10 0 0 0
-1 5 0 3 2
1 7 0 1 2
2 4 0 3 3
3 6 1 1 2

Can I ask, how can I make the script to change the first format file to
second one? It's like
axis reverse.

Ah, transposition. Last time it came up (it's been a while, more than a
year I think) someone suggested Text::Table, but at the time it couldn't
do it. In the meantime Text::Table has learned a new trick: it accepts
multiline strings as data elements where each partial line goes into the
same table column. Feeding a table an array of multiline strings, each
of which contains the data for a whole would-be column does the trick:

use Text::Table;
print Text::Table->new->add( map join( "\n", split), <DATA>);

__DATA__
A| 4 10 5 7 4 6
C| 2 0 0 0 0 1
T| 3 0 3 1 3 1
G| 2 0 2 2 3 2

Anno
 
M

Matija Papec

X-Ftn-To: Damian James

Damian James said:
I'd actually suggest looking at Math::Matrix, since chances are
the OP will eventually want more of its features than just
transposition.

Tnx, the module looks very nice.
 
A

A. Sinan Unur

(e-mail address removed)-berlin.de (Anno Siegel) wrote in
Ah, transposition. Last time it came up (it's been a while, more than
a year I think) someone suggested Text::Table, but at the time it
couldn't do it. In the meantime Text::Table has learned a new trick:
it accepts multiline strings as data elements where each partial line
goes into the same table column. Feeding a table an array of
multiline strings, each of which contains the data for a whole
would-be column does the trick:

use Text::Table;
print Text::Table->new->add( map join( "\n", split), <DATA>);

Nice! Thank you for pointing this out.

Sinan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top