Class: Sequin::Seq

Inherits:
Thor
  • Object
show all
Defined in:
lib/sequin.rb

Instance Method Summary collapse

Instance Method Details

#check(*args) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/sequin.rb', line 148

def check(*args)
  signature = args.join(" ")

  chunks = signature.split(/\[|\]/)
  if chunks.count < 2
    display_str = (signature.length > 60) ? "#{signature[0, 60]}..." : signature
    msg = "Error: #{display_str} is not a valid sequence signature."
    puts msg.red
    help :check
    return
  end

  fn_template = chunks[0].strip  
  numbers = chunks[1].split(/([0-9]+)/) 

  if numbers.count < 4
    display_str = (signature.length > 60) ? "#{signature[0, 60]}..." : signature
    msg = "Error: #{display_str} is not a valid sequence signature. Should have [<start> to <end> [by <by_frame>]]. "
    puts msg.red
    help :check
    return
  end


  match = /#+/.match(fn_template)
  if match.nil?  
    msg = "Error: #{fn_template} has no # characters so does not represent a sequence."
    puts msg.red
    help :check
    return
  end

  range_spec=Hash.new
  range_spec[:padding] = match[0].length
  range_spec[:start_frame] = numbers[1].to_i
  range_spec[:end_frame] = numbers[3].to_i
  range_spec[:by_frame] = numbers[5].to_i || 1

  if range_spec[:start_frame].nil? || range_spec[:end_frame].nil? || ( range_spec[:start_frame] >= range_spec[:end_frame] )    
    msg = "Error: start_frame #{start_frame} must be less than end_frame #{end_frame}."
    puts msg.red
    help :check
    return
  end

  # range_spec.merge!(options)
  membership =Sequin::Membership.new(fn_template, range_spec) 
  
  membership.output(options)

end

#generate(name) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/sequin.rb', line 118

def generate(name)
  if name.downcase == "config"
    yml = File.join(ENV['HOME'],".seq.yml")
    FileUtils.rm(yml) if options[:force] && File.exists?(yml)
    if File.exists?(yml)
      msg = "Skipping write of #{yml} because it exists. Use --force to remove it."
      puts msg.red
      help :generate
      return
    else
      puts "Generating #{yml}"
      File.open(yml , 'w') do |f|
        f.write(regex_comments)
        YAML.dump(regex_hash, f)
      end
    end
  else
    msg = "Error: #{name} is not recognized."
    puts msg.red
    help :generate
    return
  end
end

#list(*files) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/sequin.rb', line 99

def list(*files)
  unless files.count == 0 
    l = Sequin::Listing.new(files)
    l.list(options[:verbose])
  else
    msg = "No files."
    puts msg.red
    help :list
    return
  end
end

#match(*files) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/sequin.rb', line 209

def match(*files)
  keys =  options[:regex].split(":")
  if keys.count != 2
    the_regex = options[:regex]
  else

    begin
      yml = File.join(ENV['HOME'],".seq.yml")
      regexp_hash = YAML::load_file(yml)
    rescue
      msg = "Error: opening file #{yml} - please check the file exists and is valid YAML.\nIf it doesn't exist, run: seq generate config."
      puts msg.red
      help :match
      return
    end

    unless regexp_hash.has_key?(keys[0]) 
      msg = "Error: #{keys[0]} not in #{yml} - please check the file is valid YAML and has a top level key: #{keys[0]}."
      puts msg.red
      help :match
      return
    end

    unless regexp_hash[keys[0]].has_key?(keys[1]) 
      msg = "Error: #{keys[1]} not in #{yml} - please check the file is valid YAML and has a second level key: #{keys[1]}."
      puts msg.red
      help :match
      return
    end

    the_regex = regexp_hash[keys[0]][keys[1]]

  end

  unless the_regex.nil?
    l = Sequin::Listing.new(files)
    l.match(the_regex, options) 
  else
    msg = "Error: No valid regular expression."
    puts msg.red
    help :match
    return
  end

end

#usageObject



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/sequin.rb', line 68

def usage
  help  # default Task descriptions

  puts <<-BANNER
  ------------------------------------------------------------------------------------
  Seq is a collection of commands to help deal with sequences on disk
  
  More help here
  
  BANNER

end