The Computer Language
Benchmarks Game

k-nucleotide Perl #3 program

source code

# The Computer Language Benchmarks Game
# http://benchmarksgame.alioth.debian.org/
#
#  contributed by Mykola Zubach

use strict;
use threads;

my($sequence, $seq_len, $thr, @threads, %h);

sub read_sequence($) {
  my $id = shift;
  my $out;

  while(<STDIN>) {
    last if /^>$id /;
  }

  while(<STDIN>) {
    last if /^>/;
    chomp;
    $out .= uc($_);
  }

  return $out;
}

sub count_freq($) {
  my $len = shift;
  my $end = $seq_len - $len;

  %h = ();
  for(my $i = 0; $i <= $end; $i++) {
    $h{substr($sequence, $i, $len)} ++;
  }
}

sub count_len($) {
  my $len = shift;
  my $sum = $seq_len - $len + 1;
  my $out = '';

  count_freq($len);

  foreach my $s  (sort { $h{$b} <=> $h{$a} || $a cmp $b } keys %h) {
    $out .= sprintf "$s %.3f\n", $h{$s} * 100 / $sum;
  }
  $out .= "\n";

  return $out;
}

sub count_sequence($) {
  my $s = shift;

# count ALL sequences of specified length!
  count_freq(length($s));

  return sprintf("%d\t$s\n", $h{$s});
}

## MAIN()

$sequence = read_sequence('THREE');
$seq_len = length($sequence);

foreach my $len (1,2) {
  push @threads, threads->create(\&count_len, $len);
}

foreach my $s (qw(GGT GGTA GGTATT GGTATTTTAATT GGTATTTTAATTTATAGT)) {
  push @threads, threads->create(\&count_sequence, $s);
}

foreach $thr (@threads) {
  print $thr->join();
}
    

notes, command-line, and program output

NOTES:
32-bit Ubuntu one core
This is perl 5, version 24, subversion 0 (v5.24.0) built for i686-linux


Tue, 10 May 2016 19:04:37 GMT

COMMAND LINE:
/usr/local/src/perl-5.24.0_no_ithreads_no_multi/bin/perl knucleotide.perl-3.perl 0 < knucleotide-input250000.txt

PROGRAM FAILED 


PROGRAM OUTPUT:

This Perl not built to support threads
Compilation failed in require at knucleotide.perl-3.perl line 7.
BEGIN failed--compilation aborted at knucleotide.perl-3.perl line 7.