Module: Qu::Cmdwrapper

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

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.7"

Class Method Summary collapse

Class Method Details

.exec_cmd(cmd) ⇒ Object



148
149
150
151
152
153
154
155
156
# File 'lib/qu/cmdwrapper/cmd.rb', line 148

def exec_cmd(cmd)
  stdout, stderr, status = Open3.capture3(cmd)
  if status.success?
    return stdout
  else
    puts stderr
    exit
  end
end

.faToTwoBit(fasta, twobit) ⇒ Object



107
108
109
110
# File 'lib/qu/cmdwrapper/cmd.rb', line 107

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

.get_binding_seq_list(binding_range_list, twobit_file) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/qu/cmdwrapper/cmd.rb', line 132

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



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
100
101
102
103
104
105
# File 'lib/qu/cmdwrapper/cmd.rb', line 37

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 = exec_cmd("#{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 = exec_cmd("#{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(paras) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/qu/cmdwrapper/cmd.rb', line 18

def primer3_core(paras)
  cmd = File.join(BIN_PATH, 'primer3_core')

  lines = []
  paras.each_pair do |key, value|
    next if value.to_s.empty?
    lines << "#{key}=#{value}"
  end
  lines << "="

  stdout, stderr, status = Open3.capture3(cmd, stdin_data: lines.join("\n"))
  if status.success?
    return stdout
  else
    $stderr.puts stderr
    exit
  end
end

.system_quietly_old(*cmd) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/qu/cmdwrapper/cmd.rb', line 158

def system_quietly_old(*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



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/qu/cmdwrapper/cmd.rb', line 112

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