Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hi

I' m trying to convert a column to a csv file.

My data looks like:

##INFO=<ID=AA,

##INFO=<ID=AB,

##INFO=<ID=AC,

Num Data

1 AA=1;AB=2;AC=3

2 AA=2;AB=2

3 AA=5;AB=1;AC=1

And I want a csv like this:

AC AB AC

1 2 3

2 2 NA

5 1 1

First of all I do a hash obtaining the keys from the metadata (##)

open(I1,$ARGV[0]); my %info; while (my $line = <I1>) { if ($line =~ /##INFO=<ID=/) { my ($first,$second) = (split(/\,/, $line)); my ($firstsecond,$secondsecond) = (split(/ID=/, $first)); $info{$secondsecond}=(); } }

Now I have my hash “info” with my keys (AA, AB, AC)

Then I want to introduce the values.

I start with:

while (my $line = <I1>) { if ($line !~ /#/) { my ($numbers,$data) = (split(/\t/, $line)); foreach my $dat ($data){ my ($string, $int) = (split(/\;/, $dat));

That is because I want to eliminate “\t” and “;”

But I don't know how to introduce the missing values (NA)

I want something like this:

AA => 1,2,5

AB => 2,2,1

AC => 3,NA,1

Anyone knows how to introduce the “NA” string in its correct position?? (my real file is much bigger with a lot of “NA”)

Thank you very much.


In reply to Adding missing values into a hash by Biopolete

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-24 11:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found