Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

De referencing

by rkk (Novice)
on Jun 22, 2013 at 04:34 UTC ( #1040247=perlquestion: print w/ replies, xml ) Need Help??
rkk has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am using BIO::DB::SAM perl module to parse binary file. Here is my code.

#!/usr/bin/perl use strict; use Bio::DB::Sam; open (OUTPUT1,">op_info.txt") || die "can't: $!"; my $bam = Bio::DB::Bam->open('input.bam'); my $header = $bam->header; my $target_count = $header->n_targets; my $target_names = $header->target_name; while (my $align = $bam->read1) { print OUTPUT1 "$align\n"; } output: Bio::DB::Bam::Alignment=SCALAR(0x7fe4d0b194d8) Bio::DB::Bam::Alignment=SCALAR(0x7fe4d0b194c0) Bio::DB::Bam::Alignment=SCALAR(0x7fe4d0b194d8) Bio::DB::Bam::Alignment=SCALAR(0x7fe4d0b194c0) Bio::DB::Bam::Alignment=SCALAR(0x7fe4d0b194d8) Bio::DB::Bam::Alignment=SCALAR(0x7fe4d0b194c0) Bio::DB::Bam::Alignment=SCALAR(0x7fe4d0b194d8) Bio::DB::Bam::Alignment=SCALAR(0x7fe4d0b194c0) Bio::DB::Bam::Alignment=SCALAR(0x7fe4d0b194d8)

How to dereference the variable and print the contents of the file.

Thanks in advance.

Comment on De referencing
Download Code
Re: De referencing
by davido (Archbishop) on Jun 22, 2013 at 06:00 UTC

    Assuming the scalar ref held in $align is simple, and not just the head of some more complex structure (or object)...

    print OUTPUT1 "$$align\n";

    If it's the head of something more complex, use Data::Dumper to get a look at the structure, or read the module POD so you can plan how to dereference it effectively.

    A nice introduction to references is found in perlreftut.


    Dave

Re: De referencing
by aitap (Deacon) on Jun 22, 2013 at 07:00 UTC
    Your $align variable is actually a Bio::DB::Bam::Alignment object, so instead of stringifying it you have to call its methods and do something with their return values. You'll probably want to print $align->query->dna.
Re: De referencing
by poj (Priest) on Jun 22, 2013 at 07:01 UTC
    From the docs;
    $alignment = $bam->read1() Read one alignment from the BAM file and return it as a Bio::DB::Bam:: +Alignment object.

    You need to use the methods in Bio::DB::Bam::Alignment to access the information. For example

    while (my $align = $bam->read1) { my $seqid = $align->seq_id; my $start = $align->start; my $end = $align->end; my $strand = $align->strand; my $ref_dna= $align->dna; }

    poj
Re: De referencing
by sundialsvc4 (Abbot) on Jun 22, 2013 at 22:25 UTC

    One good tool for sorting this sort of thing out, in a general sense, is Data::Dumper, which is good at printing-out any ol’ thing so that you can see what it is.   Once you can see it, you can probably see your way clear (along with taking a good re-look at the documentation) as to how to properly de-reference it and use it.

    use Data::Dumper;
    print STDERR Data::Dumper->Dump([ $align ], [ 'align' ]);

    The STDERR (“standard error-output”) stream is a traditional place to send error-messages and “chatty” in-progress status messages.   It normally maps to the terminal, or, in the case of Apache, to the error-log.

    In this case, though, the documentation example for Bio::DB::Sam ought to be sufficient.   In their example, read1 clearly returns an object ... and the documentation obliquely says (without actually saying), that this object is a Bio::DB::Bam::Alignment object, which is separately documented in CPAN.   When the documentation does not go that far, Data::Dumper will usually give you the important-clue by having identified the object class-name that a particular variable has been bless()ed into.   A search for that class-name on CPAN, more often than not, will be fruitful.

      How do you complete align record. For instance I would like to print all the align record as it is in sam file if it satisfies a particular condition.
        YEs I kind of have same qn....how to print complete record

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1040247]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2014-12-26 22:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls