lib/npsearch/signalp.rb in npsearch-2.1.0 vs lib/npsearch/signalp.rb in npsearch-2.1.1

- old
+ new

@@ -1,6 +1,8 @@ require 'forwardable' +require 'open3' +require 'timeout' # Top level module / namespace. module NpSearch # A class to hold sequence data class Signalp @@ -9,36 +11,37 @@ def_delegators NpSearch, :opt def analyse_sequence(seq) sp_headers = %w(name cmax cmax_pos ymax ymax_pos smax smax_pos smean d sp dmaxcut networks orf) - data = setup_analysis(seq) - orf_results = [] - s = `echo "#{data[:fasta]}\n" | #{opt[:signalp_path]} -t euk \ - -f short -U 0.34 -u 0.34` - sp_results = s.split("\n").delete_if { |l| l[0] == '#' } - sp_results.each_with_index do |line, idx| - line = line + ' ' + data[:seq][idx].to_s - orf_results << Hash[sp_headers.map(&:to_sym).zip(line.split)] + seqs = setup_analysis(seq) + sp_results = [] + seqs.each do |seq| + sp_results << run_signalp(seq, sp_headers) end - orf_results.sort_by { |h| h[:d] }.reverse[0] + sp_results.sort_by { |h| h[:d] }.reverse[0] end - def setup_analysis(seq) - if opt[:type] == :protein - data = { seq: [seq], fasta: ">seq\n#{seq}" } - else - orfs = seq.scan(/(?=(M\w+))./).flatten - orfs.unshift(seq) - data = { seq: orfs, fasta: create_orf_fasta(orfs) } + private + + def run_signalp(seq, sp_headers) + Timeout::timeout(300) do + cmd = "echo '>seq\n#{seq}\n' | #{opt[:signalp_path]} -t euk" \ + " -f short -U 0.34 -u 0.34" + stdin, stdout, stderr, wait_thr = Open3.popen3(cmd) + out = stdout.gets(nil).split("\n").delete_if { |l| l[0] == '#' } + stdin.close; stdout.close; stderr.close + result = out[0] + ' ' + seq + return Hash[sp_headers.map(&:to_sym).zip(result.split)] end - data + rescue Timeout::Error + no_results = [0,0,1,1,1,1,1,1,1,'N',1,1, seq] + return Hash[sp_headers.map(&:to_sym).zip(no_results)] end - def create_orf_fasta(m_orf) - fasta = '' - m_orf.each_with_index { |seq, idx| fasta << ">#{idx}\n#{seq}\n" } - fasta + def setup_analysis(seq) + orfs = seq.scan(/(?=(M\w{#{opt[:min_orf_length]},}))./).flatten + (opt[:type] == :protein || orfs.empty? || orfs.nil?) ? [seq] : orfs end end end end