Module: Qu::Cmdwrapper

Defined in:
lib/qu/cmdwrapper.rb,
lib/qu/cmdwrapper/cmd.rb,
lib/qu/cmdwrapper/version.rb

Defined Under Namespace

Classes: ShellError

Constant Summary collapse

BIN_ROOT =
File.join(__dir__, 'ext_bin')
BIN_PATH =
File.join(BIN_ROOT, Utils::platform_os, Utils::platform_bit.to_s)
THERMO_PATH =
File.join(__dir__, 'primer3_config') + '/'
VERSION =
"1.0.5"

Class Method Summary collapse

Class Method Details

.faToTwoBit(fasta, twobit) ⇒ Object



101
102
103
104
# File 'lib/qu/cmdwrapper/cmd.rb', line 101

def faToTwoBit(fasta, twobit)
  cmd = File.join(BIN_PATH, 'faToTwoBit')
  `#{cmd} #{fasta} #{twobit}`
end

.get_binding_seq_list(binding_range_list, twobit_file) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/qu/cmdwrapper/cmd.rb', line 126

def get_binding_seq_list(binding_range_list, twobit_file) 
  amp_seq_list = []
  
  return amp_seq_list if binding_range_list.empty?

  begin
    fh = Tempfile.new('binding_range_list')
    fh.write(binding_range_list.join("\n"))
    fh.close
    amp_seq_list = twoBitToFa(fh.path, twobit_file)
  ensure
    fh.unlink
  end
  return amp_seq_list   
end

.ntthal(s1:, s2: nil, mv: 50, dv: 1.5, d: 50, n: 0.25, mode: 'ANY', tm_only: false) ⇒ Object



31
32
33
34
35
36
37
38
39
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/qu/cmdwrapper/cmd.rb', line 31

def ntthal(s1:, s2: nil, mv: 50, dv: 1.5, d: 50, n: 0.25, mode: 'ANY', tm_only: false)
  cmd = File.join(BIN_PATH, 'ntthal')

  if s2
    # tm = `#{cmd} -mv #{mv} -dv #{dv} -d #{d} -n #{n} -s1 #{s1} -s2 #{s2} -r -path #{THERMO_PATH} -a #{mode}`
    # out = `#{cmd} -mv #{mv} -dv #{dv} -d #{d} -n #{n} -s1 #{s1} -s2 #{s2} -path #{THERMO_PATH} -a #{mode}`
    out = system_quietly("#{cmd} -mv #{mv} -dv #{dv} -d #{d} -n #{n} -s1 #{s1} -s2 #{s2} -path #{THERMO_PATH} -a #{mode}")
  else
    # out = `#{cmd} -mv #{mv} -dv #{dv} -d #{d} -n #{n} -s1 #{s1} -path #{THERMO_PATH} -a HAIRPIN`
    out = system_quietly("#{cmd} -mv #{mv} -dv #{dv} -d #{d} -n #{n} -s1 #{s1} -path #{THERMO_PATH} -a HAIRPIN")
  end

  begin
    lines = out.lines

    tm = nil
    dg = nil
    if lines.shift =~ /.*dG\s+=\s+(-?\d+\.?\d+)\s+t\s+=\s+(\d+\.?\d+)/
      dg = $1.to_f / 1000
      tm = $2.to_f
    end

    if tm_only
      return tm
    end

    lines = lines.map {|line| line.split("\t")[1].chomp}

    if s2
      seq_1 = ''
      align = ''
      seq_2 = ''
      # puts lines
      (0...lines[0].size).each do |index|
        if lines[1][index] != ' '
          seq_1 << lines[1][index]
          seq_2 << lines[2][index]
          align << '|'
        else
          align << ' '
          seq_1 << lines[0][index]
          seq_2 << lines[3][index]
        end
      end

      seq_1.sub!(/\-+$/, '')
      return tm, dg, seq_1, align, seq_2

    else
      align = lines[0]
      seq = lines[1]

      return tm, dg, seq, align
    end

  rescue Exception => e

    if tm_only
      return 0
    else
      if s2
        return nil, nil, '', '', ''
      else
        return nil, nil, '', ''
      end
    end
  end

end

.primer3_core(p3_input_file) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/qu/cmdwrapper/cmd.rb', line 17

def primer3_core(p3_input_file)
  begin
    cmd = File.join(BIN_PATH, 'primer3_core')
    begin
      return system_quietly("#{cmd} #{File.realpath(p3_input_file)}")
    rescue ShellError
      return ''
    end
  rescue IOError
    $stderr.puts "Primer3 input file not exists: #{p3_input_file}"
    exit
  end
end

.system_quietly(*cmd) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/qu/cmdwrapper/cmd.rb', line 144

def system_quietly(*cmd)
  exit_status=nil
  err=nil
  out=nil
  Open3.popen3(*cmd) do |stdin, stdout, stderr, wait_thread|
    err = stderr.gets(nil)
    out = stdout.gets(nil)
    [stdin, stdout, stderr].each{|stream| stream.send('close')}
    exit_status = wait_thread.value
  end
  if exit_status.to_i > 0
    err = err.chomp if err
    raise ShellError, err
  elsif out
    return out.chomp
  else
    return true
  end
end

.twoBitToFa(seq_list_file, twobit_file) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/qu/cmdwrapper/cmd.rb', line 106

def twoBitToFa(seq_list_file, twobit_file)
  cmd = File.join(BIN_PATH, 'twoBitToFa')

  records = []
  begin
    out_file = Tempfile.new('twobit')
    `#{cmd} -seqList=#{seq_list_file} #{twobit_file} #{out_file.path}`
    out_file.rewind

    Bio::FlatFile.new(Bio::FastaFormat, out_file).each do |record|
      records << record.naseq.seq
    end
  ensure
    out_file.close
    out_file.unlink
  end

  return records
end