axis reverse

Discussion in 'Perl Misc' started by Nina, Sep 20, 2005.

  1. Nina

    Nina Guest

    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!
    Nina, Sep 20, 2005
    #1
    1. Advertising

  2. Nina

    Guest

    "Nina" <> wrote:
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Sep 20, 2005
    #2
    1. Advertising

  3. Nina

    Matija Papec Guest

    X-Ftn-To: Nina

    "Nina" <> wrote:
    >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. :)


    --
    Matija
    Matija Papec, Sep 20, 2005
    #3
  4. Nina wrote:
    > 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
    William James, Sep 21, 2005
    #4
  5. Nina

    Damian James Guest

    On Tue, 20 Sep 2005 18:10:23 +0200, Matija Papec said:
    > "Nina" <> wrote:
    >>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,


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

    --Damian
    Damian James, Sep 21, 2005
    #5
  6. Nina

    Anno Siegel Guest

    Nina <> wrote in comp.lang.perl.misc:
    > 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
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Sep 21, 2005
    #6
  7. Nina

    Matija Papec Guest

    X-Ftn-To: Damian James

    Damian James <> wrote:
    >>>second one? It's like
    >>>axis reverse.

    >>
    >> 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.


    Tnx, the module looks very nice.


    --
    Matija
    Matija Papec, Sep 21, 2005
    #7
  8. -berlin.de (Anno Siegel) wrote in
    news:dgrobi$t1c$-Berlin.DE:

    > 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

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Sep 21, 2005
    #8
  9. Nina

    Nina Guest

    Thanks Anno,

    PS: your README file
    http://search.cpan.org/src/ANNO/Text-Table-1.107/README
    is repeatly presented.

    :)

    Anno Siegel wrote:
    > Nina <> wrote in comp.lang.perl.misc:
    > > 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
    > --
    > If you want to post a followup via groups.google.com, don't use
    > the broken "Reply" link at the bottom of the article. Click on
    > "show options" at the top of the article, then click on the
    > "Reply" at the bottom of the article headers.
    Nina, Sep 23, 2005
    #9
  10. Nina

    Anno Siegel Guest

    Nina <> wrote in comp.lang.perl.misc:
    > Thanks Anno,
    >
    > PS: your README file
    > http://search.cpan.org/src/ANNO/Text-Table-1.107/README
    > is repeatly presented.


    Thanks for noticing. It's going in the Todo list.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Sep 23, 2005
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joey Vendetta
    Replies:
    0
    Views:
    3,433
    Joey Vendetta
    Apr 1, 2004
  2. Stefan
    Replies:
    0
    Views:
    720
    Stefan
    May 6, 2004
  3. Replies:
    0
    Views:
    900
  4. hocho888
    Replies:
    1
    Views:
    611
  5. dogbite
    Replies:
    4
    Views:
    681
    osmium
    Oct 10, 2003
Loading...

Share This Page