Class: BacterialComparator
- Inherits:
-
Object
- Object
- BacterialComparator
- Defined in:
- lib/bacterial-comparator.rb
Instance Attribute Summary collapse
-
#genomes_list ⇒ Object
readonly
Returns the value of attribute genomes_list.
-
#stats ⇒ Object
readonly
Returns the value of attribute stats.
Instance Method Summary collapse
- #build_multifasta(ref_prot, synteny) ⇒ Object
- #concat_alignments(outfile) ⇒ Object
- #extract_syntenic_fasta(min_cov, min_pid) ⇒ Object
- #get_ref_prot ⇒ Object
- #get_sequence_from_flatfile(flatfile, name) ⇒ Object
- #initialize(options, root) ⇒ BacterialComparator constructor
- #mafft_align(f) ⇒ Object
- #mafft_align_all_dna ⇒ Object
- #mafft_align_all_pep ⇒ Object
- #mafft_aln(aln_opt) ⇒ Object
- #raxml_tree(aln_opt, bt) ⇒ Object
- #raxml_tree_dna(bt) ⇒ Object
- #raxml_tree_pep(bt) ⇒ Object
- #read_prot_synteny ⇒ Object
Constructor Details
#initialize(options, root) ⇒ BacterialComparator
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/bacterial-comparator.rb', line 19 def initialize , root @root = root @outdir = [:outdir] Dir.mkdir(@outdir) if ! Dir.exists? @outdir @genomes_list = [:genomes_list] @proc = [:proc].to_i @phylo_nb_genes = [:phylo_nb_genes] min_cov = [:min_cov].to_f min_pid = [:pidentity].to_f if min_cov > 1 min_cov = min_cov/100 end if min_pid > 1 min_pid = min_pid/100 end @ref_prot = get_ref_prot @synteny = read_prot_synteny @stats = extract_syntenic_fasta min_cov, min_pid end |
Instance Attribute Details
#genomes_list ⇒ Object (readonly)
Returns the value of attribute genomes_list.
15 16 17 |
# File 'lib/bacterial-comparator.rb', line 15 def genomes_list @genomes_list end |
#stats ⇒ Object (readonly)
Returns the value of attribute stats.
15 16 17 |
# File 'lib/bacterial-comparator.rb', line 15 def stats @stats end |
Instance Method Details
#build_multifasta(ref_prot, synteny) ⇒ Object
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 |
# File 'lib/bacterial-comparator.rb', line 86 def build_multifasta ref_prot, synteny pep_out_dir = "./#{@outdir}/align-genes-pep" dna_out_dir = "./#{@outdir}/align-genes-dna" # create multifasta by syntenic proteins (pep) if ! File.exists? pep_out_dir+"/#{ref_prot}.pep" pep_out = File.open(pep_out_dir+"/#{ref_prot}.pep", "w") pep_file = Dir["#{@genomes_list[0]}/*.pep"] flatfile = Bio::FlatFile.auto("#{pep_file[0]}") pep_out.write(get_sequence_from_flatfile flatfile, ref_prot) flatfile.close @genomes_list.each_with_index do |g,i| flatfile = Bio::FlatFile.auto("#{g}/Proteins.fa") pep_out.write(get_sequence_from_flatfile flatfile, synteny[i][:query_prot]) flatfile.close end pep_out.close end # create multifasta by syntenic genes (dna) if ! File.exists? dna_out_dir+"/#{ref_prot}.dna" dna_out = File.open(dna_out_dir+"/#{ref_prot}.dna", "w") # create multifasta by syntenic proteins dna_file = Dir["#{@genomes_list[0]}/*.dna"] flatfile = Bio::FlatFile.auto("#{dna_file[0]}") dna_out.write(get_sequence_from_flatfile flatfile, ref_prot) flatfile.close @genomes_list.each_with_index do |g,i| flatfile = Bio::FlatFile.auto("#{g}/Genes.fa") dna_out.write(get_sequence_from_flatfile flatfile, synteny[i][:query_prot]) flatfile.close end dna_out.close end end |
#concat_alignments(outfile) ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/bacterial-comparator.rb', line 269 def concat_alignments outfile if File.exists?("../#{outfile}") and File.size("../#{outfile}") > 0 puts "..Alignment concatenated file already exists, skipping." return end fout = File.open("../#{outfile}", "w") ref_id = Dir["../../#{@genomes_list[0]}/*.pep"][0].gsub(/.*\//,"").gsub(".pep","") seq = "" Dir["*.aln"].each do |f| flat = Bio::FlatFile.auto(f) ref_seq = flat.entries[0] flat.close seq += ref_seq.seq end bioseq = Bio::Sequence.auto(seq) out = bioseq.output_fasta("#{ref_id}",60) fout.write(out) for i in 1..@genomes_list.length seq = "" Dir["*.aln"].each do |f| flat = Bio::FlatFile.auto(f) j=0 flat.each_entry do |entry| if j<i j+=1 next elsif i == j seq += entry.seq j+=1 else break end end flat.close end bioseq = Bio::Sequence.auto(seq) out = bioseq.output_fasta("#{@genomes_list[i-1]}",60) fout.write(out) end fout.close end |
#extract_syntenic_fasta(min_cov, min_pid) ⇒ Object
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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/bacterial-comparator.rb', line 125 def extract_syntenic_fasta min_cov, min_pid "# Extracting Proteins and Genes multifasta.." nb_of_syntenic = 0 stats = {} stats[:syntenic] = [] fout = File.open("#{@outdir}/cds-synteny.tsv", "w") fout.write("Gene\t"+@genomes_list.join("\t")+"\n") to_build_multifasta = [] @synteny.each do |k,v| is_syntenic = 1 v.each do |v_| if v_[:query_cov].nil? is_syntenic = 0 break elsif v_[:query_cov] > min_cov and v_[:ref_cov] > min_cov and v_[:pId] > min_pid # synteny -> great ! else is_syntenic = 0 break end end if is_syntenic == 1 nb_of_syntenic += 1 # build_multifasta k, v to_build_multifasta << [k,v] fout.write("#{k}") v.each do |x| fout.write("\t#{x[:query_prot]}|#{x[:query_cov]}|#{x[:ref_cov]}") stats[:syntenic] << k end fout.write("\n") end end fout.close pep_out_dir = "./#{@outdir}/align-genes-pep" dna_out_dir = "./#{@outdir}/align-genes-dna" Dir.mkdir(pep_out_dir) if ! Dir.exists? pep_out_dir Dir.mkdir(dna_out_dir) if ! Dir.exists? dna_out_dir Parallel.map(to_build_multifasta, in_processes: @proc) { |k,v| build_multifasta k, v } stats[:nb_of_syntenic] = nb_of_syntenic puts "Syntenic genes : " + nb_of_syntenic.to_s + " / " + @ref_prot.length.to_s end |
#get_ref_prot ⇒ Object
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/bacterial-comparator.rb', line 60 def get_ref_prot ref_prot = [] pep_file = Dir["#{@genomes_list[0]}/*.pep"] flatfile = Bio::FlatFile.auto("#{pep_file[0]}") flatfile.each_entry do |entry| ref_prot << entry.definition.split(" ")[0] end flatfile.close ref_prot end |
#get_sequence_from_flatfile(flatfile, name) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/bacterial-comparator.rb', line 72 def get_sequence_from_flatfile flatfile, name out = "" flatfile.each_entry do |entry| if entry.definition.split(" ")[0] == name bioseq = Bio::Sequence.auto(entry.seq) out = bioseq.output_fasta("#{name}",60) end end out end |
#mafft_align(f) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/bacterial-comparator.rb', line 183 def mafft_align f = 0 begin cmd = system("#{@root}/mafft.linux --quiet #{f} > #{f}.aln") if File.size("#{f}.aln") == 0 puts "File size of 0.. --#{f}--" puts "Command used : #{@root}/mafft.linux --quiet #{f} > #{f}.aln" fail else status = "OK" status = "FAILED" if cmd != true puts "Alignment #{f} : #{status}" end rescue if < 3 += 1 retry end status = "FAILED" puts "Alignment #{f} : #{status}" end end |
#mafft_align_all_dna ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/bacterial-comparator.rb', line 239 def mafft_align_all_dna puts "# MAFFT multialign all gene sequences.." ori_dir = Dir.pwd Dir.chdir("#{@outdir}/align-genes-dna/") is_done = 1 if Dir["*.dna"].length == Dir["*.aln"].length Dir["*.aln"].each do |a| if File.size(a) == 0 is_done = 0 end end else is_done = 0 end if is_done == 0 Parallel.map(Dir["*.dna"], in_processes: @proc) { |f| mafft_align f } else puts "..Gene alignment files already exists, skipping." end concat_alignments "align-genes-dna.all.fasta" Dir.chdir(ori_dir) end |
#mafft_align_all_pep ⇒ 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 |
# File 'lib/bacterial-comparator.rb', line 209 def mafft_align_all_pep puts "# MAFFT multialign all protein sequences.." ori_dir = Dir.pwd Dir.chdir("#{@outdir}/align-genes-pep/") is_done = 1 if Dir["*.pep"].length == Dir["*.aln"].length Dir["*.aln"].each do |a| if File.size(a) == 0 is_done = 0 end end else is_done = 0 end if is_done==0 Parallel.map(Dir["*.pep"], in_processes: @proc) { |f| mafft_align f } else puts "..Prot alignment files already exists, skipping." end concat_alignments "align-genes-pep.all.fasta" Dir.chdir(ori_dir) end |
#mafft_aln(aln_opt) ⇒ Object
319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/bacterial-comparator.rb', line 319 def mafft_aln aln_opt if aln_opt == "both" mafft_align_all_pep mafft_align_all_dna elsif aln_opt == "prot" mafft_align_all_pep elsif aln_opt == "dna" mafft_align_all_dna end end |
#raxml_tree(aln_opt, bt) ⇒ Object
366 367 368 369 370 371 372 373 374 375 376 377 |
# File 'lib/bacterial-comparator.rb', line 366 def raxml_tree aln_opt, bt if aln_opt == "both" raxml_tree_dna bt raxml_tree_pep bt elsif aln_opt == "prot" raxml_tree_pep bt elsif aln_opt == "dna" raxml_tree_dna bt end end |
#raxml_tree_dna(bt) ⇒ Object
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/bacterial-comparator.rb', line 333 def raxml_tree_dna bt # DNA tree puts "# RAXML DNA tree creation.. " ori_dir = Dir.pwd Dir.chdir(@outdir) Dir.mkdir("tree-genes-dna") if ! Dir.exists?("tree-genes-dna") current_dir = Dir.pwd tree_dir = "#{current_dir}/tree-genes-dna" cmd = system("#{@root}/raxml.linux -T #{@proc} -f d -N #{bt} -s align-genes-dna.all.fasta -m GTRGAMMA -p 123454321 -n DnaTree -w #{tree_dir}") cmd = system("cat #{tree_dir}/RAxML_result.DnaTree.RUN.* >> #{tree_dir}/RAxML_result.BS") cmd = system("#{@root}/raxml.linux -T 3 -f b -z #{tree_dir}/RAxML_result.BS -t #{tree_dir}/RAxML_bestTree.DnaTree -m GTRGAMMA -n DNA_BS_TREE -w #{tree_dir}") cmd = system("ln -s #{tree_dir}/RAxML_bipartitionsBranchLabels.DNA_BS_TREE #{tree_dir}/../") Dir.chdir(ori_dir) end |
#raxml_tree_pep(bt) ⇒ Object
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/bacterial-comparator.rb', line 349 def raxml_tree_pep bt # Prot tree puts "# RAXML Protein tree creation.. " ori_dir = Dir.pwd Dir.chdir(@outdir) Dir.mkdir("tree-genes-pep") if ! Dir.exists?("tree-genes-pep") current_dir = Dir.pwd tree_dir = "#{current_dir}/tree-genes-pep" cmd = system("#{@root}/raxml.linux -T #{@proc} -f d -N #{bt} -s align-genes-pep.all.fasta -m PROTGAMMAAUTO -p 123454321 -n PepTree -w #{tree_dir}") cmd = system("cat #{tree_dir}/RAxML_result.PepTree.RUN.* >> #{tree_dir}/RAxML_result.BS") cmd = system("#{@root}/raxml.linux -T 3 -f b -z #{tree_dir}/RAxML_result.BS -t #{tree_dir}/RAxML_bestTree.PepTree -m PROTGAMMAAUTO -n PEP_BS_TREE -w #{tree_dir}") cmd = system("ln -s #{tree_dir}/RAxML_bipartitionsBranchLabels.PEP_BS_TREE #{tree_dir}/../") Dir.chdir(ori_dir) end |
#read_prot_synteny ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/bacterial-comparator.rb', line 43 def read_prot_synteny synteny = {} @genomes_list.each do |g| puts "#{g}/Prot-Synteny.tsv" file = File.open("#{g}/Prot-Synteny.tsv", "r") l = file.gets # skip header while l = file.gets # AAK98805.1 spr0001 453 1.0 100.0 ABAC01000005_14 453 1.0 lA = l.chomp.split("\t") synteny[lA[0]] = [] if ! synteny.has_key? lA[0] synteny[lA[0]] << {ref_cov: lA[3].to_f, pId: lA[4].to_f, query_prot: lA[5], query_cov: lA[7].to_f} end file.close end synteny end |