Module: ViralSeq::Muscle

Defined in:
lib/viral_seq/muscle.rb

Overview

alignment using MUSCLE alignment program

Class Method Summary collapse

Class Method Details

.align(ref_seq = "", test_seq = "", path_to_muscle = false) ⇒ Array

align a sequence with reference sequence Strings

Examples:

seq1 = 'AAGGCGTAGGAC'
seq2 = 'AAGCTTAGGACG'
aligned_seqs = ViralSeq::Muscle.align(seq1,seq2)
=> ["AAGGCGTAGGAC-", "-AAGCTTAGGACG"]

Parameters:

  • ref_seq (String) (defaults to: "")

    reference sequence

  • test_seq (String) (defaults to: "")

    test sequence

  • path_to_muscle (String) (defaults to: false)

    , path to MUSCLE excutable. if not provided (as default), it will use RubyGem::MuscleBio

Returns:

  • (Array)

    a pair of [:ref_seq_aligned, :test_seq_aligned] or nil if the cannot find MUSCLE excutable



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/viral_seq/muscle.rb', line 40

def self.align(ref_seq = "", test_seq = "", path_to_muscle = false)
  temp_dir = Dir.home
  temp_name = "_"  + SecureRandom.alphanumeric
  temp_file = File.join(temp_dir, temp_name)
  temp_aln = File.join(temp_dir, (temp_name + "_aln"))
  name = ">test"
  temp_in = File.open(temp_file,"w")
  temp_in.puts ">ref"
  temp_in.puts ref_seq
  temp_in.puts name
  temp_in.puts test_seq
  temp_in.close
  if path_to_muscle
    unless ViralSeq::Muscle.check_muscle?(path_to_muscle)
      File.unlink(temp_file)
      return nil;
    end
    print `#{path_to_muscle} -in #{temp_file} -out #{temp_aln} -quiet`
  else
    MuscleBio.run("muscle -in #{temp_file} -out #{temp_aln} -quiet")
  end
  aln_seq_hash = ViralSeq::SeqHash.fa(temp_aln).dna_hash
  File.unlink(temp_file)
  File.unlink(temp_aln)
  return [aln_seq_hash[">ref"], aln_seq_hash[">test"]]
end

.check_muscle?(path_to_muscle) ⇒ boolean

check if path_to_muscle is correct, prompt error messages if MUSCLE is not found.

Parameters:

  • path_to_muscle (String)

    path to muscle excutable

Returns:

  • (boolean)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/viral_seq/muscle.rb', line 11

def self.check_muscle?(path_to_muscle)
  begin
    `#{path_to_muscle} -version`
    return true
  rescue Errno::ENOENT
    puts "
          Error: MUSCLE is not found for at the provided {path_to_muscle}!!
          MUSLCE can be download at http://www.drive5.com/muscle
          Add MUSCLE excutable path to $PATH using
          $  export PATH=$PATH:/path/to/muscle
          or
          provide path_to_MUSCLE in the function arguments\n
          "
    return false
  end
end