Need help with perl script to read in values from a files
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
Need help with perl script to read in values from a files
Hi all,
I'm trying to write a script to query a database with the scientific names of organisms for their taxonomic IDs to automatically bin DNA sequences in a dataset as being from an animal, bacteria, etc. I have a text file containing many scientific names that I would like to run through this script. I'm having a hard time editing the script to iterate through the text file pulling in variables one after the other and none of the examples I've been able to find on the web match the situation I'm in. I realize that I'll need to change the lines "my @scientific_name = ("Neosartorya fischeri NRRL 181");" and "foreach my $scientific_name (@scientific_name) {" but otherwise I'm really stuck.
Here's the script I'm trying to edit:
Code:
use strict;
use warnings;
use Bio::DB::Taxonomy;
use Bio::Tree::Tree;
my @scientific_name = ("Neosartorya fischeri NRRL 181");
my @lineages = ();
my $db = Bio::DB::Taxonomy->new(-source => 'entrez');
foreach my $scientific_name (@scientific_name) {
my $taxon = $db->get_taxon(-name => @scientific_name);
my $tree = Bio::Tree::Tree->new(-node => $taxon);
my @taxa = $tree->get_nodes;
my @tids = ();
foreach my $t (@taxa) {
unshift(@tids, $t->id());
}
push(@lineages, @scientific_name . "\t|\t" . $taxon->ancestor() . "\t|\t" . "@tids")
}
foreach my $lineage (@lineages) {
print "$lineage\n";
}
Here's the first few lines from species_names.txt, which contains the terms I'd like to feed into my @scientific_name:
I'm not entirely clear, but if you are asking about how to read file line by line
Code:
open(KFILE, "<", "kfile.txt" ) or die "Can't open kfile: $!\n";
while ( defined($krec = <KFILE>) )
{
chomp($krec);
# Here you do stuff with the rec;
# Note that your recs seem to sometimes have space separated fields, sometimes ';' separators
}
close(KFILE) or die "Can't close kfile: $!\n";
Obviously you rename the vars etc, but you get the idea.
Also, I'd avoid having scalars and arrays having effectively the same name, even if they are in separate name-spaces; it becomes prone to difficult to find typos as the code gets longer.
Please use the proper Perl way of reading files as per my example.
I'd also point out that it splits on new lines by default (you can change that for funky files).
I also re-iterate my advice about not using the 'same' names for scalars/arrays (& indeed hashes).
You'll thank me later...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.