-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbactlist.rb
executable file
·50 lines (44 loc) · 1.61 KB
/
bactlist.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/local/bin/ruby
# The Blaster project is copyright 2008, 2009, Stanford University, released as open source under the MIT license.
# Ed Allen, Stanford Genome Technology Center
# Started May 5, 2008
# Finished May 6, 2008
# Builds bacteria.yaml file from data directory of *.fna files
# Assumes typical fasta headers with space before and after genus and species names and single fasta record per file.
$: << File.expand_path(File.dirname(__FILE__) + "/./lib")
# Ruby library & gem requires
require 'rubygems'
require 'yaml'
require 'pathname'
class BactList
def initialize
config_path = File.expand_path(File.dirname(__FILE__) + "/config/config.yaml")
@config = YAML.load_file(config_path)
@data_dir_path = Pathname.new(@config[:blast_path] + @config[:data_dir])
@bacteria_file_path = Pathname.new(@config[:blast_path] + @config[:data_dir] + @config[:bacteria_file])
end
def fill_bacteria_array
@a_bacts = []
@r_sub = Regexp.new("[\s[:punct:]]")
Dir.chdir(@data_dir_path)
Dir.glob("*.fna") do |fn|
ncid = File.basename(fn,".fna")
File.open(fn,"r") do |f|
line = f.gets.chomp!
puts line
a_words = line.split(" ")
id_block,genus,species,*a_strain = a_words
strain = (a_strain.join(" ").split(','))[0].gsub(@r_sub,"_")
puts "strain: " + strain
@a_bacts<< {:nc_id=>ncid,:genus=>genus,:species=>species,:strain=>strain}
puts @a_bacts.to_s
end
end
end
def bact_hash_to_yaml
File.open(@bacteria_file_path,"w"){|f| YAML.dump(@a_bacts, f)}
end
end
bl = BactList.new()
bl.fill_bacteria_array
bl.bact_hash_to_yaml