-
Notifications
You must be signed in to change notification settings - Fork 5
/
classify16S
executable file
·161 lines (139 loc) · 4.05 KB
/
classify16S
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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env ruby
require 'optimist'
require 'bio'
require 'Newick'
require 'fpdf'
ARGV.push("--help") if ARGV.empty?
opts = Optimist::options do
banner File.basename($0)
opt :input, "input file", :required=>true, :type=>:string
opt :db, "16S blast database", :required=>true, :type=>:string
opt :percent, "percent blast identity", :default=>95.0
opt :algorithm, "algorithm to use (ml or nj)", :default=>"ml"
opt :max_target, "maximum number of target sequences", :default=>10
opt :threads, "CPU threads to use", :default=>2
end
queries = []
Bio::FlatFile.new(Bio::FastaFormat, File.new(opts.input)).each do |seq|
queries.push(seq)
end
STDERR << "Found #{queries.length} query sequences...\n"
name = File.basename(opts.input).split(".").first
blast = name + ".blast"
list = name + ".list"
tmp = name + ".tmp"
tmp2 = name + ".tmp2"
homologs = name + ".hom"
aln = name + ".afa"
tree = name + ".tre"
pdf = tree + ".pdf"
cmd = "blastn -db #{opts.db} -query=#{opts.input} "
cmd += "-perc_identity #{opts.percent} -outfmt 6 -num_threads #{opts.threads} "
cmd += "-max_target_seqs #{opts.max_target} > #{blast}"
if File.exists?(blast)
STDERR << "Found #{blast} Skipping BLAST step...\n"
else
STDERR << "Running blast...\n"
system(cmd)
end
out = File.new(list, "w")
seen = Hash.new
File.new(blast).each do |line|
id = line.chomp.split("\t")[1]
out.print id+"\n" if !seen[id]
seen[id] = true
end
out.close
cmd = "blastdbcmd -db #{opts.db} -entry_batch #{list} > #{tmp}"
if File.exists?(homologs)
STDERR << "Found #{homologs} Skipping homolog step...\n"
else
STDERR << "Extracting homologs...\n"
system(cmd)
out = File.new(homologs,"w")
Bio::FlatFile.new(Bio::FastaFormat, File.new(tmp)).each do |seq|
sname = seq.entry_id
tax = seq.definition.split(" ",2).last.split(";")
sp = tax.pop
genus = tax.pop
sp = sp.gsub(genus,"")
sname += ("_"+genus+"_"+sp).tr(" :()","_").gsub("__","_")
out.print Bio::Sequence.new(seq.seq).to_fasta(sname, 60)
end
out.close
end
out = File.new(tmp, "w")
queries.each do |seq|
out.print seq
end
Bio::FlatFile.new(Bio::FastaFormat, File.new(homologs)).each do |seq|
out.print seq
end
out.close
cmd = "muscle -in #{tmp} -out #{aln}"
if File.exists?(aln)
STDERR << "Found #{aln} Skipping alignment step...\n"
else
system(cmd)
end
def percent_identity(x,y)
ident = 0
0.upto(x.length - 1) do |i|
a = x[i].upcase.tr("U","T")
b = y[i].upcase.tr("U","T")
ident += 1 if a==b || a=="-" || b == "-"
end
(10000*ident/x.length)/100.0
end
align = Hash.new
Bio::FlatFile.new(Bio::FastaFormat, File.new(aln)).each do |seq|
align[seq.entry_id] = seq.seq
end
if File.exists?(tree)
STDERR << "Found #{tree} Skipping phylogeny step...\n"
else
out = File.new(tmp, "w")
out << " #{align.keys.length} #{align.values.first.length}\n"
align.keys.each do |key|
out << key + " " + align[key].seq+"\n"
end
out.close
STDERR << "Building Phylogeny...\n"
if opts.algorithm == "ml"
cmd = "phyml --quiet -i #{tmp} -m GTR -c 4 >/dev/null"
system(cmd)
File.rename(tmp+"_phyml_tree.txt", tree)
File.unlink(tmp+"_phyml_stats.txt")
else
out = File.new(tmp2, "w")
out << "# STOCKHOLM 1.0\n"
align.keys.each do |key|
out << key + " " + align[key] << "\n"
end
out.close
cmd = "quicktree -boot 100 #{tmp2} > #{tree}"
system(cmd)
end
end
STDOUT << "\n\n"
ttree = NewickTree.fromFile(tree).midpointRoot
queries.each do |query|
qname = query.definition.split(" ").first
node = ttree.root.findNode(qname)
taxa = node.parent.taxa
STDOUT << "Neighbors of #{qname} are:"
taxa.each do |hname|
identity = percent_identity(align[qname],align[hname])
STDOUT << " " + hname + " (#{identity})" if hname != qname
end
STDOUT << "\n"
end
ttree.draw(pdf) if !File.exists?(pdf)
File.unlink(list) if File.exists?(list)
File.unlink(tmp) if File.exists?(tmp)
File.unlink(tmp2) if File.exists?(tmp2)
File.unlink(homologs) if File.exists?(homologs)
STDOUT << "\n\nBlast is #{blast}\n"
STDOUT << "Alignment is #{aln}\n"
STDOUT << "Tree is #{tree}\n"
STDOUT << "PDF is #{pdf}\n"