Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Seekers of Perl Wisdom

( #479=superdoc: print w/ replies, xml ) Need Help??

If you have a question on how to do something in Perl, or you need a Perl solution to an actual real-life problem, or you're unsure why something you've tried just isn't working... then this section is the place to ask. Post a new question!

However, you might consider asking in the chatterbox first (if you're a registered user). The response time tends to be quicker, and if it turns out that the problem/solutions are too much for the cb to handle, the kind monks will be sure to direct you here.

User Questions
comparing sequence fasta headers
No replies — Read more | Post response
by Pasan
on Sep 16, 2014 at 13:56

    hi guys! I am a newbie to perl and I need help with finding list of fasta seuences which has specefic 3 letter code in the header. The question is explained below Scenario: I have a fasta file which contains list of sequences as follows (test.fa)

    >cel-let-7-5p MIMAT0000001 Caenorhabditis elegans let-7-5p


    >cfa-miR-761 MIMAT0009936 Canis familiaris miR-761


    >cfa-miR-764 MIMAT0009937 Canis familiaris miR-764


    >lus-miR167c MIMAT0027158 Linum usitatissimum miR167c


    I have set of organism codes in a separate file (codes.txt)




    what I want to do is search through test.fa for the codes and only print out the sequences which has that particular code in the header The example above contains only few sequences (not the entire file) So far I managed to create a hash and store the headers into keys and sequences into values. And I read through codes.txt and stored the codes in an array

    The problem is when I use 'if exists' function to find whether each code in array exists in the hash here is my code,

    #!/usr/bin/perl use warnings; use Bio::Perl; use Bio::Seq; use Bio::SeqIO; # Reading the first file and store it into a hash #the sequence header is stored in the hash key and sequence is stored +in the value my $FastaFile1 = Bio::SeqIO->new(-file => "test.fa", -format => 'fasta +', -alphabet => 'dna') or die "Failed to create SeqIO object from \n" +; my %fastaH1 =(); while( my $seqFile1 = $FastaFile1->next_seq() ) { unless (exists $fastaH1{$seqFile1->display_id."\t".$seqFile1->desc +}) { $fastaH1{$seqFile1->display_id."\t".$seqFile1->desc} = $seqFil +e1->seq; #key of the hash is fasta header (all line) and value is seq +uence. } } # printing the fasta headers print "stored fasta headers:\n"; foreach my $key (keys %fastaH1){ print "$key\n"; } # reading the codes.txt file and creating the array open my $file, '<', "codes.txt"; chomp(my @lines = <$file>); close $file; print "stored organism codes\n"; foreach (@lines) { print " $_\n"; } # if each code is found on the hash print match is found foreach my $line (@lines) { chomp $line; if( exists $fastaH1{$line} ){ print "match found\n"; } }

    The code is only printing "match found" if a match is found. But I need to write the sequences that has the 3 letter codes in a separate file. However, it doesn't seem to compare the codes and the fasta headers using exists function. Any help will be appreciated

Moscow time zone change in October 2014
1 direct reply — Read more / Contribute
by yanshuguang
on Sep 16, 2014 at 13:55

    Hi, Monks:

    According to, Moscow Time has been UTC+4 year-round since 27 March 2011, but will change to UTC+3 permanently on 25 October 2014.

    I am not an expert on Time::Zone module, could anyone have ideas what changes are needed in perl side to support this Moscow time zone change?

HTML::Template data loading
No replies — Read more | Post response
by Anonymous Monk
on Sep 16, 2014 at 13:41
    Hi Monks!
    I am trying to use HTML::Template to process the data results returned from my SQL query instead of returning json to the jQuery to process and display it. I can only get it to work if I return json, it ignores my HTML::Template tags in the .tmpl file, is there a way not to send back json and let the template handling it?
    I have sample code to show what I am trying to explain: search.tmpl
    This is the search form using jQuery autocomplete:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ +/"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Search</title> <script type="text/javascript" src="jquery-latest.js"></script> <script type="text/javascript"> var debug = false; $(document).ready(function() { $( "#search" ).autocomplete({ source: function(request, response) { $.ajax({ url: "", dataType: "json", data: { search: request.term, }, success: function(data) { // return data stuff here }, error: function(response){ if (debug == true) {console.log(response)}; alert('Somethings broken in the AJAX return'); } }); }, minLength: 5, delay: 500 }); }); </script> </head> <body> <div id="container"> <h2>TEST</h2> <div id="namesearch"> <form id="form_search" method="get" name="form_search"/> <label>Search: </label> <input type="text" name="search" id="search" class="search_box"/> </form> </div> <div> <p id="submitted"></p> <!-- display resutls from search here --> <div id='RESULTS'></div> <!-- end displaying results --> <!-- All Data - Here is where I want the data to display--> <TMPL_LOOP NAME=ALL_DATA> <TMPL_VAR NAME=NAME> <TMPL_VAR NAME=CITY> <TMPL_VAR NAME=STATE> </TMPL_LOOP> <TMPL_VAR NAME=COUNT> <!-- Data Ends --> </div> </body> </html>

    This is the Perl code that will process the request from the jQuery on the .tmpl

    ... my $main_tmpl = HTML::Template->new(filename => 'search.tmpl', die_on_ +bad_params => 1); ... sub data { my $id = shift || return; my $sql = qq{SELECT distinct name, city, state FROM my_table WHERE name like ? order by name asc}; my $sth = $dbh->prepare($sql); $sth->execute(${id}.'%') or die "SQL Error: $DBI::errstr\n"; my $all_data = $sth->fetchall_arrayref({}); my @loop_data = (); # initialize an array to hold loop # Count things my $c = 0; my @count; foreach my $row (@{$all_data}) { $c++; my %row_data; # get a fresh hash for the row data # fill in these rows $row_data{NAME} = $row->{NAME} || ''; $row_data{CITY} = $row->{CITY} || ''; $row_data{STATE} = $row->{STATE} || ''; push @count, $c; # the crucial step - push a reference to this row into the loop push(@loop_data, \%row_data); } # Count things my $counter = @count; # I need to load the data here but it doesn't. # load values into the template $main_tmpl->param( ALL_DATA => \@loop_data, COUNT => $counter, ); # If sending data through json use this # This works if I use jQuery to process the returned json from here # but I am trying to load the data using HTML::Template =code print $q->header(-type => "application/json"); if(@loop_data){ print to_json(\@loop_data); exit; }else{ my $json_string = [{ result => "No data"}]; print to_json($json_string); exit; } =cut } # end sub

    Thanks for looking!
DBI bind_columns
1 direct reply — Read more / Contribute
by fionbarr
on Sep 16, 2014 at 13:22
    my DBI book has both these formats:
    $sth->bind->columns(\$server); while ( $sth->fetch ) { $sth->bind->columns(undef, \$server); while ( $sth->fetch ) {
    Which is preferred and why, please?
whats wrong? it gives me many error
4 direct replies — Read more / Contribute
by yenne
on Sep 16, 2014 at 10:40
    use strict; use IO::Socket; #initial variables to work with my server my $host, $port, $request, $proto = 'tcp'; my $connectresponses = 2; #my ftp server responds with 2 lines when + you connect. print "What hostname do you want to connect to? "; chomp( $host = <STDIN> ); print "What port do you want to use? "; chomp( $port = <STDIN> ); my $sock = IO::Socket::INET->new( PeerAddr => $host, PeerPort => $port, Proto => $proto ) || die "Failure: $!"; print "Connection to $host on port $port successful!\n"; unless ( $port == 80 ) { for ( $i = 0; $i < $connectresponses; $i++ ) { $_ = <$sock>; print; } } print "Type commands (solely press enter to exit)\n"; &Terminal; close($sock); print "\nConnection to $host on port $port was closed successfully!\n" +; exit; #sub to emulate a terminal sub Terminal { while (1) { $request = ""; chomp( $request = <STDIN> ); print $sock "$request\n"; if ( $port == 80 ) { while (<$sock>) { print; } last; } else { unless ($request) { last; } $_ = <$sock>; print; } } }
    parenthesis missing arounf "my" list at line 7 print (..) interpreted as function at line 37 Global symbol "$port" requires explicit package name at line 7 Global symbol "$request" requires explicit package name at lin +e 7 Global symbol "$proto" requires explicit package name at line +7 Global symbol "$port" requires explicit package name at line 1 +3 Global symbol "$port" requires explicit package name at line 1 +5 Global symbol "$proto" requires explicit package name at line +7 has too many errors.
Problem creating animated GIF using GD and GD::Graph
1 direct reply — Read more / Contribute
by realityczar
on Sep 16, 2014 at 10:29
    Hi, Monks-

    I am trying to create an animated GIF of graphs using GD and GD::Graph. The following code creates individual graph GIF files, but the image.gif that results and is supposed to be the animated version won't open. Where have I gone wrong?

    Update: I searched the archives. I read them. And I didn't apply the wisdom. It's a lack of binmode. Sorry to have bothered you.

    use strict; use warnings; use GD; use GD::Graph::bars; my $img_width = 288; my $img_height = 216; my $number_of_hours = 24; my $img = GD::Image->new($img_width, $img_height); my $gifdata = $img->gifanimbegin(1,0); foreach my $hour (0..$number_of_hours-1) { my @h = CreateHistogram($hour); my @labels = 0..79; my @data = (\@labels, \@h); my $graph = GD::Graph::bars->new($img_width,$img_height); $graph->set( title => "HISTOGRAM HOUR $hour" ); my $gd = $graph->plot(\@data); $gifdata .= $gd->gifanimadd; open(FIL, ">image_$hour.gif"); binmode FIL; print FIL $gd->gif; close FIL; } $gifdata .= $img->gifanimend; open(GIF,">image.gif"); print GIF $gifdata; close GIF; exit; sub CreateHistogram { # stuff to create a histogram }
how to speed up pattern match between two files
3 direct replies — Read more / Contribute
by Anonymous Monk
on Sep 16, 2014 at 10:03
    Input file1: col1 col2 col3 col4 ZGLP1 ICAM4 13.27 0.2425 ICAM4 ZGLP1 13.27 0.2425 RRP1B CDH24 20.8 1 ZGLP1 OOEP 18.79 0.3060 ZGLP1 RRP1B 39.62 0.2972 ZGLP1 CDH24 51.21 0.2560 BBCDI DND1 19.44 0.2833 BBCDI SOHLH2 36.61 0.2909 DND1 SOHLH2 18 0.8
    Input file2: chr8 18640000 18960000 ZGLP1 RRP1B CDH24 #gene number he +re is not fixed can be #4 #5 or more chr8 19000000 19080000 BBCDI DND1 SOHLH2 #gene number he +re is not fixed can be #4 #5 or more

    I have written a code which compares col1 and col2 of file1 with each line of file2 such that, if any of the pair falls anywhere in a line of file2 then programme should print "chromosome pos1 pos2 and the matching content of the file1 with values

    output file: chr8 18640000 18960000 ZGLP1 RRP1B 39.62 0.2972 chr8 18640000 18960000 ZGLP1 CDH24 51.21 0.2560 chr8 18640000 18960000 RRP1B CDH24 20.8 1 chr8 19000000 19080000 BBCDI DND1 19.44 0.2833 chr8 19000000 19080000 BBCDI SOHLH2 36.61 0.2909 chr8 19000000 19080000 DND1 SOHLH2 18 0.8

    so far I have tried this but it is taking so much time as my input files are huge (2gb).

    my perl code open( AB, "file1" ) || die("cannot open"); open( BC, "file2" ) || die("cannot open"); open( OUT, ">output.txt" ); @file = <AB>; chomp(@file); @data = <BC>; chomp(@data); foreach $fl (@file) { if ( $fl =~ /(.*?)\s+(.*?)\s+(.*?)\s+(.*)/ ) { $one = $1; $two = $2; $thr = $3; $for = $4; } foreach $line (@data) { if ( $line =~ /(.*?)\s+(.*?)\s+(.*?)\s+(.*)+/ ) { $chr = $1; $pos1 = $2; $pos2 = $3; } if ( $line =~ /$one/ ) { if ( $line =~ /$two/ ) { print OUT $chr, "\t", $pos1, "\t", $pos2, "\t", $fl, " +\n"; } } } }
what is wrong with this perl script
1 direct reply — Read more / Contribute
by chai6125
on Sep 16, 2014 at 09:34
    Hi Monks, I'm totally confused about what is going wrong here. Here is my Perl code.
    my $COUNT_1; my $parameter1 = 'PU_CLERK'; $COUNT_1 = `sqlplus -s hr/password\@dbname\@sql_script.sql $parameter1 +`; $COUNT_2 = `sqlplus -s hr/password\@dbname\@sql_script1.sql $parameter +2`; if ($COUNT_1 == $COUNT_2) { then do something }

    I'm basically trying to pass a parameter to my sql script from perl namely $parameter1 which is how we do in a typical unix script. Here is a snippet of my oracle script. '&1' is my parameter.

    select count(*) from employees where job_id <> '&1' and salary > 9000 and commission_pct is not null order by first_name desc / exit;

    But my oracle instead of returning a number is returning a error string.

    But when i don't use the parameter and hardcode everything works fine.

    my $COUNT_1; my $parameter1 = 'PU_CLERK'; $COUNT_1 = `sqlplus -s hr/password\@dbname\@sql_script.sql`; $COUNT_2 = `sqlplus -s hr/password\@dbname\@sql_script1.sql`; if ($COUNT_1 == $COUNT_2) { then do something }
    select count(*) from employees where job_id <> 'PU_CLERK' and salary > 9000 and commission_pct is not null order by first_name desc / exit;
    So my question is how to pass arguments properly to oracle. Where am i going wrong :( Please ignore the + sign I don't know how to remove that
count number of overlapping words in a document
1 direct reply — Read more / Contribute
by dmarcel
on Sep 16, 2014 at 09:26

    I am beginning perl user, so I'm sorry in advance if I miss some easy things, but I cannot get it to work and I am not sure whether every step is necessary(I have problems in particular with the while loops and 'saving' its outcome). I would like to do the following:

    I have two text files. File 1 with a word list (one word per line) and File 2 that is a regular text document with text and numbers. I would like to count the number of words in file 2 that I can also find in file 1 and the total number of words in the document (so that I can calculate the percentage of corresponding words)

    this is how far I have come:

    use strict; use warnings; #Part one of the code, the wordlist is a file with one word per line a +nd I transform this into a hash my $filename = "wordlist.txt"; open(INPUT, $filename) or die "Cannot open $filename"; my $line = <INPUT>; while($line = <INPUT>){ chomp($line); my @words = split(/\s+/, $line); my %unique = map {$_ => 1 } @words; my @unique = %unique; #part two of code, open the text file and extract words only (because +the file also includes many numbers), and count the number of occuren +ce stored in a hash open (DATA, "4.txt") or die; my @UnNum; my $x; my %dict; while (<DATA>) { chomp; $_ = lc; s/ -- / /g; s/ - / /g; s/ +/ /g; s/[.,:;?"!_()\[\]]//g; my @UnNum = split(/\s+/); foreach $x(@UnNum){ if ($x =~ /(([a-zA-Z']+-)*[a-zA-Z']+)/ ){ ++$dict{$x}; }}} #part 3 of the code, I try to compare the two different hashes and add + the total number of occurrences while ((my $words,my $number) = each (%dict)) {my $total+= $number; if (exists($unique{$words})){ my $corresponding +=$number; print "There are $corresponding corresponding words of in total $total + words";}} }

    thank you in advance for the help!

AI Neural Networks based Prediciton
1 direct reply — Read more / Contribute
by kulls
on Sep 16, 2014 at 09:04
    I am new to Artificial Intelligence and studying Neural Networks. Can anyone suggest me how to do a prediction program through machine learning, especially stock market data.
    Please suggest me some example.
    Raja K

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (14)
    As of 2014-09-16 18:34 GMT
    Find Nodes?
      Voting Booth?

      How do you remember the number of days in each month?

      Results (42 votes), past polls