Module: Qu::Utils

Defined in:
lib/qu/utils.rb,
lib/qu/utils/version.rb

Constant Summary collapse

IUPAC =
{
  A: ['A'],
  T: ['T'],
  C: ['C'],
  G: ['G'],
  R: ['G', 'A'],
  Y: ['T', 'C'],
  S: ['G', 'C'],
  W: ['T', 'A'],
  K: ['G', 'T'],
  M: ['A', 'C'],
  D: ['G', 'T', 'A'],
  H: ['T', 'A', 'C'],
  B: ['G', 'T', 'C'],
  V: ['G', 'A', 'C'],
  N: ['G', 'A', 'T', 'C'],
  I: ['G', 'A', 'T', 'C'],
}
VERSION =
"1.0.1"

Class Method Summary collapse

Class Method Details

.convert_degenerate_primer(primer_file) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/qu/utils.rb', line 44

def convert_degenerate_primer(primer_file)
  primer_records = Bio::FlatFile.new(Bio::FastaFormat, File.open(primer_file))

  primer_list = []
  primer_records.each do |primer|
    if primer.naseq.to_s =~ /[^atcgATCG]+/
      normal_seq_list = iupac2normal(primer.naseq.upcase)
      fasta_io = StringIO.new
      normal_seq_list.each_with_index do |normal_seq, index|
        fasta_io << ">#{primer.entry_name}_#{index+1} #{primer.description}\n#{normal_seq}\n"
      end
      fasta_io.rewind
      primer_list += Bio::FlatFile.new(Bio::FastaFormat, fasta_io).to_a
      fasta_io.close
    else
      primer_list << primer
    end
  end

  return primer_list
end

.iupac2normal(seq, prefixes = ['']) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/qu/utils.rb', line 27

def iupac2normal(seq, prefixes = [''])
  return prefixes if seq.size == 0

  first = seq[0].to_sym
  last_seq = seq[1..-1]
  new_prefixes = []
  prefixes.each do |prefix|
    if IUPAC.include?(first)
      IUPAC[first].each {|base| new_prefixes << "#{prefix}#{base}"}
    else
      $stderr.puts "Error: unrecognized base: #{first}"
      exit
    end
  end
  return iupac2normal(last_seq, prefixes = new_prefixes)
end

.long_seq_wrap(word, length = 80, separator = "\n") ⇒ Object



73
74
75
76
77
78
# File 'lib/qu/utils.rb', line 73

def long_seq_wrap(word, length=80, separator="\n")
  # Wrap a long sentence with may words into multiple lines
  (word.length < length) ?
    word :
    word.scan(/.{1,#{length}}/).join(separator)
end

.platform_bitObject



114
115
116
117
118
119
120
121
122
123
# File 'lib/qu/utils.rb', line 114

def platform_bit
  case RUBY_PLATFORM
  when /64/
    return 64
  when /32/
    return 32
  else
    raise Error::WebDriverError, "Unknown os bit: #{RUBY_PLATFORM.inspect}"
  end
end

.platform_osObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/qu/utils.rb', line 99

def platform_os
  case RUBY_PLATFORM
  when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
    return 'windows'
  when /darwin|mac/
    return 'mac'
  when /linux/
    return 'linux'
  when /solaris|bsd/
    return 'unix'
  else
    raise Error::WebDriverError, "Unknown os: #{RUBY_PLATFORM.inspect}"
  end
end

.plural_word(word, count) ⇒ Object



95
96
97
# File 'lib/qu/utils.rb', line 95

def plural_word(word, count)
  count > 1 ? word + 's' : word
end

.seconds_to_units(seconds) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/qu/utils.rb', line 80

def seconds_to_units(seconds)
  return "#{seconds.round(2)} second" if seconds < 1
  return "#{seconds.round(2)} seconds" if seconds < 60
  '%d minutes, %d seconds' %
  #'%d days, %d hours, %d minutes, %d seconds' %
  # the .reverse lets us put the larger units first for readability

  #[24,60,60].reverse.inject([seconds]) {|result, unitsize|

  [60].reverse.inject([seconds]) {|result, unitsize|
    result[0,0] = result.shift.divmod(unitsize)
    result
  }
end

.word_wrap(text, line_width = 80) ⇒ Object

File actionpack/lib/action_view/helpers/text_helper.rb, line 215



67
68
69
70
71
# File 'lib/qu/utils.rb', line 67

def word_wrap(text, line_width = 80)
  text.split("\n").collect do |line|
    line.length > line_width ? line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip : line
  end * "\n"
end