Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

perl - loop

by annymous (Initiate)
on Jul 17, 2009 at 11:51 UTC ( [id://781033]=perlquestion: print w/replies, xml ) Need Help??

annymous has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I am writing a perl script that uses eLink to eFetch (NCBI Eutilities), from eLink to eFetch the script works if I use a list of id’s at $params{id} ‘ ‘;

But I want the script to read each file from my home directory and outputs the result of each file with an extension separately into my home directory.

And also in some cases there is a whole genome sequence, from which I need to retrieve the coordinates of starting point and the ending point of the sequences into a separate file.

Can anyone help me, how can I proceed further with this script?

#!/usr/bin/perl # ELink (batch) - EFetch use strict; use NCBI_PowerScripting; my (%params, %links); my @db = qw(protein nucleotide, gene); my $link; my $dir ='/home/'; opendir (DIR,$dir)or die $!; while (my $file = readdir (DIR)) { next unless (-f "$dir/$file"); next unless ($file =~m/\. gi$/); my @data; open (MYFILE,$file) or die "cannot open $file:$!"; open (OUT,">$file.data") or die "cannot open $file:$!"; #ELink $params{db} = $db[0]; $params{id} = ''; %links = elink_batch_to($db[1], %params); #EFetch foreach $link (keys %links) { %params = extract_links($link, %links); $params{outfile} = $link . '.dat'; $params{retmode} = 'text'; if ($link =~ /nuccore/) { $params{rettype} = 'fasta'; } efetch_batch(%params); print nuccore; }

20090719 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: perl - loop
by davorg (Chancellor) on Jul 17, 2009 at 12:21 UTC

    As ELISHEVA said, welcome to the monastery.

    It's probably worth pointing out how this site works. Everyone here is a volunteer. We're taking time out of our busy day to come here and offer help to people. We're not getting paid for the work we do here and we don't have to help anyone if we don't want to.

    Bearing that in mind, you're far more likely to get help if you make it as easy as possible for us to help you. The harder you make it for us to help, the less likely it is that we'll bother.

    Before you posted your node, you had to preview it. And underneath the preview was this text:

    If something looked unlike you expected it to you might need to check out Writeup Formatting Tips

    I assume that you read this message, but that you thought that your post looked fine. I can't understand that as your code is an unreadable mess. Had you bothered to click on the Writeup Formatting Tips, you would have learned that putting <code> tags around your source code would have formatted it more readably.

    The fact that you didn't bother to do that will almost certainly mean that many of the most knowledgeable helpers on this site won't bother trying to understand your code and won't offer you any help.

    We get a lot of people whose first post is messed up like yours. If you have any suggestions on how we can make the formatting process easier to follow, then we'd love to hear them.

    --

    See the Copyright notice on my home node.

    Perl training courses

Re: perl - loop
by ELISHEVA (Prior) on Jul 17, 2009 at 12:01 UTC
Re: perl - loop
by derby (Abbot) on Jul 17, 2009 at 12:39 UTC

    As others have said, please format your code. Also, when I have situations like this (looping and processing), I always find it easier to phase my coding:

    • First write code to fetch one url
    • Add code to save fetched data
    • Add code to loop *mocked* ids
    • Add code to get real data from the filesystem
    If you do that, you may find the road less bumpy and not littered with X-Y problems.

    -derby
      Your suggestions have the added benefits that you get to unit-test basic functionality as well as structuring that lower-level functionality as a set of subs.
Re: perl - loop
by Utilitarian (Vicar) on Jul 17, 2009 at 13:42 UTC
    As has been said extensively already it is difficult to answer on the basis of the supplied code sample. use <c> tags to allow us read your code

    I have made an attempt at parsing code out of it and added a few comments could you let us know what happens when you try to run the script?

    ... my $dir ='/home/'; # perhaps $ENV{HOME} ? ... next unless ($file =~m/\. gi$/); # really you're looking for a file e +nding ". gi" perhaps $file =~m/\.gi$/ ... %links = elink_batch_to($db1, %params); # where does $db1 come from .. } # missing closing brace
Re: perl - loop
by ssandv (Hermit) on Jul 17, 2009 at 17:13 UTC
    Thankfully, view source restores the newlines that html ignores when displaying.

    Here's the OP's code, I think: (to the OP: always always *always* use preview first.)
    #!/usr/bin/perl # ELink (batch) - EFetch use strict; use NCBI_PowerScripting; my (%params, %links); my @db = qw(protein nucleotide, gene); my $link; my $dir ='/home/'; opendir (DIR,$dir)or die $!; while (my $file = readdir (DIR)) { next unless (-f "$dir/$file"); next unless ($file =~m/\. gi$/); my @data; open (MYFILE,$file) or die "cannot open $file:$!"; open (OUT,">$file.data") or die "cannot open $file:$!"; #ELink $params{db} = $db&#91;0&#93;; $params{id} = ''; %links = elink_batch_to($db<a href="?node=1">1</a>, %params); #EFetch foreach $link (keys %links) { %params = extract_links($link, %links); $params{outfile} = $link . '.dat'; $params{retmode} = 'text'; if ($link =~ /nuccore/) { $params{rettype} = 'fasta'; } efetch_batch(%params); print nuccore; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-04-19 10:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found