Class: FastaUtility

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
bin/fasta_util

Instance Method Summary collapse

Instance Method Details

#clean(filename) ⇒ Object



82
83
84
85
86
87
# File 'bin/fasta_util', line 82

def clean(filename)
  invoke :filecheck
  Bio::FlatFile.open(filename).each do |entry|
    puts entry.to_biosequence.output(:fasta, :header => entry.definition, :width => options.wrap_width)
  end
end

#composition(filename) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'bin/fasta_util', line 99

def composition(filename)
  invoke :filecheck
  composition_hash = Hash[Bio::FlatFile.open(filename).map{|entry| [entry.definition, entry.seq.composition]}]
  
  all_chars = composition_hash.values.map{|h| h.keys}.inject(:+).uniq.sort
  
  puts "definition\t#{all_chars.join("\t")}"
  composition_hash.each do |definition, composition|
    composition.default = 0
    print definition, "\t"
    puts all_chars.map{|char| composition[char]}.join("\t")
  end
end

#filecheck(filename) ⇒ Object



39
40
41
# File 'bin/fasta_util', line 39

def filecheck(filename)
  say "The file '#{filename}' doesn't seem to exist!", :red unless File.exists?(filename)
end

#filter(filename) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'bin/fasta_util', line 64

def filter(filename)
  invoke :filecheck
  invoke :filecheck, [options.definitions_file]
  requested_definitions = File.open(options.definitions_file).map{|definition| definition.strip} unless options.definitions_file == ''
  entry_ids = File.open(options.entry_id_file).map{|id| id.strip} unless options.entry_id_file == ''
  Bio::FlatFile.open(filename).each do |entry|
    passed = true
    passed &&= (entry.length >= options.length_cutoff)
    passed &&= (entry.definition.match(Regexp.new(options.defline_grep)))
    passed &&= (requested_definitions.include? entry.definition) unless options.definitions_file == ''
    passed &&= (entry_ids.include? entry.entry_id) unless options.entry_id_file == ''
    passed = !passed if options.inverse_match
    puts entry if passed
  end
end

#lengths(filename) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'bin/fasta_util', line 45

def lengths(filename)
  invoke :filecheck
  lengths =  Bio::FlatFile.open(filename).map{|entry| (entry.seq[-1,1] == "*") ? entry.length - 1 : entry.length}
  
  say "All entries", :green
  puts format(stats(lengths))
  if options.cutoff > 0
    say "Entries with length >= #{options.cutoff}", :green
    puts format(stats(lengths.find_all{|l| l >= options.cutoff}))
  end
end

#sort(filename) ⇒ Object



91
92
93
94
95
96
# File 'bin/fasta_util', line 91

def sort(filename)
  invoke :filecheck
  Bio::FlatFile.open(filename).to_a.sort{|a,b| b.length <=> a.length}.each do |entry|
    puts entry
  end
end