CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Modifying a perl script

 



SMcG
New User

Sep 4, 2013, 3:23 AM

Post #1 of 7 (829 views)
Modifying a perl script Can't Post

Hi there,

I'm kinda brand new to perl (well programming in general), and have been presented with a perl script (Id_script3.pl).
The script requires a modification, in which 'SpeciesId3.txt' will be used instead of the 'SpeciesId.txt' which is currently used by the script.

There is a slight difference between the two files, so a slight modification would need to be made to the script for it to function; the difference being that SpeciesId3.txt contains no letters (A =, B =, C =) and simply a (much) longer list of values as compared to the original 'SpeciesId.txt'.

I've tried a couple of attempted "work-arounds" but am yet to discover one that works.

Many Thanks,

Stephen.


Code
# main sub 
{ # closure
# keep %species local to sub-routine but only init it once
my %species;
sub _init {
open my $in, '<', 'SpeciesId.txt' or die "could not open SpeciesId.txt: $!";
my $spec;
while (<$in>) {
chomp;
next if /^\s*$/; # skip blank lines
if (m{^([A-Z])\s*=\s*(\d+(?:\.\d)?)(?:\s+AND\s+(\d+(?:\.\d)?))?$}) {
# handle letter = lines
$species{$spec}{$1} = [$2];
push @{$species{$spec}{$1}}, $3 if $3;
} else {
# handle species name lines
$spec = $_;
$len = length($spec) if (length($spec) > $len);
}
}
close $in;
}
sub analyze {
my ($masses) = @_;
_init() unless %species;
my %data;
# loop over species entries
SPEC:
foreach my $spec (keys %species) {
# loop over each letter of a species
LTR:
foreach my $ltr (keys %{$species{$spec}}) {
# loop over each mass for a letter
foreach my $mass (@{$species{$spec}{$ltr}}) {
# skip to next letter if it is not found
next LTR unless exists($masses->{$mass});
}
# if we get here, all mass values were found for the species/letter
$data{$spec}{cnt}++;
}
}

Attachments: Id_script3.pl (4.60 KB)
  SpeciesId.txt (3.85 KB)
  SpeciesId3.txt (27.8 KB)


BillKSmith
Veteran

Sep 4, 2013, 6:08 AM

Post #2 of 7 (823 views)
Re: [SMcG] Modifying a perl script [In reply to] Can't Post

Your existing script very much depends on the 'letters'. You need a scheme to infer them from the new data. If this is not possible, I doubt that the existing script is going to be much help at all in processing the new data. Please try to describe both the old and new data in terms that programmers (not biologists) can understand.
Good Luck,
Bill


SMcG
New User

Sep 4, 2013, 6:47 AM

Post #3 of 7 (816 views)
Re: [BillKSmith] Modifying a perl script [In reply to] Can't Post

Hi Bill,

I've attached an example workthrough (example_workthrough.txt) along with an example set of data to try to clarify the scripts function along with its usage of data.

Thanks again,

Stephen.
Attachments: example_workthrough.txt (4.80 KB)
  20130730_p12_A7.csv (7.11 KB)
  SpeciesId3.txt (27.8 KB)
  Id_script3.pl (4.61 KB)
  CSVfile_structure.jpg (168 KB)


SMcG
New User

Sep 4, 2013, 7:13 AM

Post #4 of 7 (813 views)
Re: [SMcG] Modifying a perl script [In reply to] Can't Post

Basically, for each CSV results file, I want to take the mass lists and see which animal (from SpeciesId3.txt) each CSV file best aligns (most matches) to, with match scores and percentage matches.


Laurent_R
Veteran / Moderator

Sep 4, 2013, 10:34 AM

Post #5 of 7 (807 views)
Re: [SMcG] Modifying a perl script [In reply to] Can't Post

I do not think that using a closure is the easiest way to start learning Perl for a beginner.


SMcG
New User

Sep 5, 2013, 7:47 AM

Post #6 of 7 (795 views)
Re: [Laurent_R] Modifying a perl script [In reply to] Can't Post

Thanks Laurent R,

So how would you suggest I go about this then?

Thanks,

Stephen.


Laurent_R
Veteran / Moderator

Sep 5, 2013, 12:01 PM

Post #7 of 7 (791 views)
Re: [SMcG] Modifying a perl script [In reply to] Can't Post

Well, I don't really have the time now to look into it and look at your various file to really understand the details of what you want to do.

Two points.

1. As mentioned by others, your current script relies heavily on the current structure of your file, most of it will be unusable with the new data.

2. Closures are a nice features of Perl, pretty useful, but they are a somewhat advanced topic. And as far as I can say from reading quickly your program, it is not extremely useful in the case in point and it might give headaches to a beginner. I would simply make %species a global variable in this case.

Removing the closure feature is very easy, but changing the program to adapt to the new data format is slightly more complicated, because you need first to understand the format and figure out what to extract. I don't have time for that right now (probably not before the weekend).

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives