This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Bioruby Read Fasta Multithreaded

Hi,

While writing a multithreaded application using bioruby I ran into a problem namely, I was not able to read and parse FASTA files. The error message I get is "uninitialized constant Bio::FastaFormat". The error reproduction code is shown below (both fasta files are approx 60 MB with ~150000 sequences each).

I am using bioruby version 1.4.1 with ruby 1.8.7.

I will appreciate any help in this regard.

cheers

require 'bio'
require "thread"

def fasta_summary(f)
    begin
      puts f
      fasta = Bio::FastaFormat.open(f)
      n_seq = 0
      n_bp = 0
      fasta.each do |seq|
             n_seq += 1
             n_bp += seq.length
      end
      puts n_seq.to_s + " " + n_bp.to_s
    rescue Exception=>e
      puts e.message
      puts e.backtrace.inspect
    end
end

files = ["1.fna","2.fna"]
files.each do |f|
    Thread.new do
            #works fine
            #Thread.exclusive {fasta_summary(f)}
            #produces error
            fasta_summary(f)
    end
end

The error:

uninitialized constant Bio::FastaFormat
["./fasta_read_thread.rb:9:in `fasta_summary'", "./fasta_read_thread.rb:27", "./fasta_read_thread.rb:25:in `initialize'",  "./fasta_read_thread.rb:25:in `new'", "./fasta_read_thread.rb:25", "./fasta_read_thread.rb:24:in `each'", "./fasta_read_thread.rb:24"]
fasta read

1 answer

This seems to be an issue with loading modules within threads.

http://stackoverflow.com/questions/2837912/is-autoload-thread-safe-in-ruby-1-9

I think if you just loaded up the module first outside of the threading loop to have it initialize properly it might just work.

Log in to answer this question.