in progress..
[jalview.git] / wiki / PhyloBioRuby.wiki
1 #summary Tutorial for multiple sequence alignments and phylogenetic methods in BioRuby -- under development!
2
3
4
5 = Introduction =
6
7 Under development!
8
9 Tutorial for multiple sequence alignments and phylogenetic methods in [http://bioruby.open-bio.org/ BioRuby].
10
11 Eventually, this is expected to be placed on the official !BioRuby page.
12
13 Author: [https://sites.google.com/site/cmzmasek/ Christian Zmasek], Sanford-Burnham Medical Research Institute
14
15  
16 Copyright (C) 2011 Christian M Zmasek. All rights reserved.
17
18
19 = Multiple Sequence Alignment =
20
21
22 == Multiple Sequence Alignment Input and Output ==
23
24 === Reading in a Multiple Sequence Alignment from a File ===
25
26 This automatically determines the format
27 {{{
28 #!/usr/bin/env ruby
29 require 'bio'
30
31 seq_ary = Array.new
32 ff = Bio::FlatFile.auto('bcl2.fasta')
33 ff.each_entry do |entry|
34   seq_ary.push(entry)
35   puts entry.entry_id          # prints the identifier of the entry
36   puts entry.definition        # prints the definition of the entry
37   puts entry.seq               # prints the sequence data of the entry
38 end
39
40 # Creates a multiple sequence alignment (possibly unaligned) named
41 # 'seqs' from array 'seq_ary'.
42 seqs = Bio::Alignment.new(seq_ary)
43 seqs.each { |seq| puts seq.to_s }
44
45 # Writes multiple sequence alignment (possibly unaligned) 'seqs'
46 # to a file in PHYLIP format.
47 File.open('out0.phylip', 'w') do |f|
48   f.write(seqs.output(:phylip))
49 end
50
51 # Writes multiple sequence alignment (possibly unaligned) 'seqs'
52 # to a file in FASTA format.
53 File.open('out0.fasta', 'w') do |f|
54   f.write(seqs.output(:fasta))
55 end
56 }}}
57
58
59 ==== ClustalW Format ====
60
61 The following example shows how to read in a *ClustalW*-formatted multiple sequence alignment.
62
63 {{{
64 #!/usr/bin/env ruby
65 require 'bio'
66
67 # Reads in a ClustalW-formatted multiple sequence alignment
68 # from a file named "infile_clustalw.aln" and stores it in 'report'.
69 report = Bio::ClustalW::Report.new(File.read('infile_clustalw.aln'))
70
71 # Accesses the actual alignment.
72 msa = report.alignment
73
74 # Goes through all sequences in 'msa' and prints the
75 # actual molecular sequence.
76 msa.each do |entry|
77   puts entry.seq
78 end
79 }}}
80
81 ==== FASTA Format ====
82
83 The following example shows how to read in a *FASTA*-formatted multiple sequence file. (_This seems a little clumsy, I wonder if there is a more direct way, avoiding the creation of an array.)
84 {{{
85 #!/usr/bin/env ruby
86 require 'bio'
87
88 # Reads in a FASTA-formatted multiple sequence alignment (which does
89 # not have to be aligned, though) and stores its sequences in
90 # array 'seq_ary'.
91 seq_ary = Array.new
92 fasta_seqs = Bio::Alignment::MultiFastaFormat.new(File.open('infile.fasta').read)
93 fasta_seqs.entries.each do |seq|
94   seq_ary.push(seq)
95 end
96
97 # Creates a multiple sequence alignment (possibly unaligned) named
98 # 'seqs' from array 'seq_ary'.
99 seqs = Bio::Alignment.new(seq_ary)
100
101 # Prints each sequence to the console.
102 seqs.each { |seq| puts seq.to_s }
103
104 # Writes multiple sequence alignment (possibly unaligned) 'seqs'
105 # to a file in PHYLIP format.
106 File.open('outfile.phylip', 'w') do |f|
107   f.write(seqs.output(:phylip))
108 end
109 }}}
110
111 Relevant API documentation:
112
113  * [http://bioruby.open-bio.org/rdoc/classes/Bio/ClustalW/Report.html Bio::ClustalW::Report]
114  * [http://bioruby.open-bio.org/rdoc/classes/Bio/Alignment.html Bio::Alignment]
115  * [http://bioruby.open-bio.org/rdoc/classes/Bio/Sequence.html Bio::Sequence]
116
117 === Creating a Multiple Sequence Alignment ===
118
119
120 === Creating a Multiple Sequence Alignment from a Database ===
121
122 ?
123
124 === Writing a Multiple Sequence Alignment to a File ===
125
126
127 The following example shows how to write a multiple sequence alignment in *FASTA*-format. It first creates a file named "outfile.fasta" for writing ('w') and then writes the multiple sequence alignment referred to by variable 'msa' to it in FASTA-format (':fasta').
128
129 {{{
130 #!/usr/bin/env ruby
131 require 'bio'
132
133 # Creates a new file named "outfile.fasta" and writes
134 # multiple sequence alignment 'msa' to it in fasta format.
135 File.open('outfile.fasta', 'w') do |f|
136   f.write(msa.output(:fasta))
137 end
138 }}}
139
140 ==== Setting the Output Format ====
141
142 The following symbols determine the output format:
143
144   * `:clustal` for ClustalW
145   * `:fasta` for FASTA
146   * `:phylip` for PHYLIP interleaved (will truncate sequence names to no more than 10 characters)
147   * `:phylipnon` for PHYLIP non-interleaved (will truncate sequence names to no more than 10 characters)
148   * `:msf` for MSF
149   * `:molphy` for Molphy
150
151
152 For example, the following writes in PHYLIP's non-interleaved format:
153
154 {{{
155 f.write(align.output(:phylipnon))
156 }}}
157
158
159 === Formatting of Individual Sequences ===
160
161 !BioRuby can format molecular sequences in a variety of formats.
162 Individual sequences can be formatted to (e.g.) Genbank format as shown in the following examples.
163
164 For Sequence objects:
165 {{{
166 seq.to_seq.output(:genbank)
167 }}}
168
169 For Bio::!FlatFile entries:
170 {{{
171 entry.to_biosequence.output(:genbank)
172 }}}
173
174 The following symbols determine the output format:
175   * `:genbank` for Genbank
176   * `:embl` for EMBL
177   * `:fasta` for FASTA
178   * `:fasta_ncbi` for NCBI-type FASTA
179   * `:raw` for raw sequence
180   * `:fastq` for FASTQ (includes quality scores)
181   * `:fastq_sanger` for Sanger-type FASTQ 
182   * `:fastq_solexa` for Solexa-type FASTQ 
183   * `:fastq_illumina` for Illumina-type FASTQ 
184
185 == Calculating Multiple Sequence Alignments ==
186
187 !BioRuby can be used to execute a variety of multiple sequence alignment
188 programs (such as [http://mafft.cbrc.jp/alignment/software/ MAFFT], [http://probcons.stanford.edu/ Probcons], [http://www.clustal.org/ ClustalW], [http://www.drive5.com/muscle/ Muscle], and [http://www.tcoffee.org/Projects_home_page/t_coffee_home_page.html T-Coffee]). 
189 In the following, examples for using the MAFFT and Muscle are shown.
190
191
192 === MAFFT ===
193
194 The following example uses the MAFFT program to align four sequences
195 and then prints the result to the screen.
196 Please note that if the path to the MAFFT executable is properly set `mafft=Bio::MAFFT.new(options)` can be used instead of explicitly indicating the path as in the example. 
197
198 {{{
199 #!/usr/bin/env ruby
200 require 'bio'
201
202 # 'seqs' is either an array of sequences or a multiple sequence 
203 # alignment. In general this is read in from a file as described in ?.
204 # For the purpose of this tutorial, it is generated in code.
205 seqs = ['MFQIPEFEPSEQEDSSSAER',
206         'MGTPKQPSLAPAHALGLRKS',
207         'PKQPSLAPAHALGLRKS',
208         'MCSTSGCDLE'] 
209
210
211 # Calculates the alignment using the MAFFT program on the local
212 # machine with options '--maxiterate 1000 --localpair'
213 # and stores the result in 'report'.
214 options = ['--maxiterate', '1000', '--localpair']
215 mafft = Bio::MAFFT.new('path/to/mafft', options)
216 report = mafft.query_align(seqs)
217
218 # Accesses the actual alignment.
219 align = report.alignment
220
221 # Prints each sequence to the console.
222 align.each { |s| puts s.to_s }
223
224 }}}
225
226 References:
227
228  * Katoh, Toh (2008) "Recent developments in the MAFFT multiple sequence alignment program" Briefings in Bioinformatics 9:286-298 
229
230  * Katoh, Toh 2010 (2010) "Parallelization of the MAFFT multiple sequence alignment program" Bioinformatics 26:1899-1900 
231
232
233
234 === Muscle ===
235
236 {{{
237 #!/usr/bin/env ruby
238 require 'bio'
239
240 # 'seqs' is either an array of sequences or a multiple sequence 
241 # alignment. In general this is read in from a file as described in ?.
242 # For the purpose of this tutorial, it is generated in code.
243 seqs = ['MFQIPEFEPSEQEDSSSAER',
244         'MGTPKQPSLAPAHALGLRKS',
245         'PKQPSLAPAHALGLRKS',
246         'MCSTSGCDLE']  
247
248 # Calculates the alignment using the Muscle program on the local
249 # machine with options '-quiet -maxiters 64'
250 # and stores the result in 'report'.
251 options = ['-quiet', '-maxiters', '64']
252 muscle = Bio::Muscle.new('path/to/muscle', options)
253 report = muscle.query_align(seqs)
254
255 # Accesses the actual alignment.
256 align = report.alignment
257
258 # Prints each sequence to the console.
259 align.each { |s| puts s.to_s }
260
261 }}}
262
263 References:
264
265  * Edgar, R.C. (2004) "MUSCLE: multiple sequence alignment with high accuracy and high throughput" Nucleic Acids Res 32(5):1792-1797
266
267 === Other Programs ===
268
269 _need more detail here..._
270
271 [http://probcons.stanford.edu/ Probcons], [http://www.clustal.org/ ClustalW], and [http://www.tcoffee.org/Projects_home_page/t_coffee_home_page.html T-Coffee] can be used in the same manner as the programs above. 
272
273
274
275 == Manipulating Multiple Sequence Alignments ==
276
277 Oftentimes, multiple sequence to be used for phylogenetic inference are 'cleaned up' in some manner. For instance, some researchers prefer to delete columns with more than 50% gaps. The following code is an example of how to do that in !BioRuby.
278
279
280 _... to be done_
281
282 {{{
283 #!/usr/bin/env ruby
284 require 'bio'
285
286 }}}
287
288
289 ----
290
291 = Phylogenetic Trees =
292
293
294 == Phylogenetic Tree Input and Output ==
295
296
297 === Reading in of Phylogenetic Trees ===
298
299
300
301 ====Newick or New Hampshire Format====
302
303 _... to be done_
304
305 {{{
306 #!/usr/bin/env ruby
307 require 'bio'
308
309 }}}
310
311 ====phyloXML Format====
312
313 Partially copied from [https://www.nescent.org/wg_phyloinformatics/BioRuby_PhyloXML_HowTo_documentation Diana Jaunzeikare's documentation].
314
315 In addition to !BioRuby, a libxml Ruby binding is also required. This can be installed with the following command: 
316
317 {{{
318 % gem install -r libxml-ruby
319 }}}
320
321 This example reads file "example.xml" and stores its [http://www.phyloxml.org/ phyloXML]-formatted trees in variable 'trees'.
322
323 {{{
324 #!/usr/bin/env ruby
325 require 'bio'
326
327 # This creates new phyloXML parser.
328 trees = Bio::PhyloXML::Parser.new('example.xml')
329
330 # This prints the names of all trees in the file.
331 trees.each do |tree|
332   puts tree.name
333 end
334
335 # If there are several trees in the file, you can access the one you wish via index.
336 tree = trees[3]
337
338 }}}
339
340
341 ====Nexus  Format====
342
343 _... to be done_
344
345 {{{
346 #!/usr/bin/env ruby
347 require 'bio'
348
349 }}}
350
351 === Writing of Phylogenetic Trees ===
352
353 ====Newick or New Hampshire Format====
354
355 _... to be done_
356
357 {{{
358 #!/usr/bin/env ruby
359 require 'bio'
360
361 }}}
362
363 ====phyloXML Format====
364
365 Partially copied from [https://www.nescent.org/wg_phyloinformatics/BioRuby_PhyloXML_HowTo_documentation Diana Jaunzeikare's documentation].
366
367 In addition to !BioRuby, a libxml Ruby binding is also required. This can be installed with the following command: 
368
369 {{{
370 % gem install -r libxml-ruby
371 }}}
372
373 This example writes trees 'tree1' and 'tree2' to file "tree.xml" in [http://www.phyloxml.org/ phyloXML] format.
374
375 {{{
376 #!/usr/bin/env ruby
377 require 'bio'
378
379 # this creates new phyloXML writer.
380 writer = Bio::PhyloXML::Writer.new('tree.xml')
381
382 # Writes tree to the file "tree.xml".
383 writer.write(tree1)
384
385 # Adds another tree to the file.
386 writer.write(tree2)
387
388
389 }}}
390
391
392 ====Nexus  Format====
393
394 _... to be done_
395
396 {{{
397 #!/usr/bin/env ruby
398 require 'bio'
399
400 }}}
401
402
403 = Phylogenetic Inference =
404
405 _Currently !BioRuby does not contain wrappers for phylogenetic inference programs, thus I am progress of writing a RAxML wrapper followed by a wrapper for FastME..._
406
407 == Optimality Criteria Based on Character Data ==
408
409 Character data based methods work directly on molecular sequences and thus do not require the calculation of pairwise distances but tend to be time consuming and sensitive to errors in the multiple sequence alignment.
410
411 === Maximum Likelihood ===
412
413 ==== RAxML ====
414
415 _... to be done_
416
417 {{{
418 #!/usr/bin/env ruby
419 require 'bio'
420
421 }}}
422
423
424 ==== PhyML ====
425
426 _... to be done_
427
428 {{{
429 #!/usr/bin/env ruby
430 require 'bio'
431
432 }}}
433
434 === Maximum Parsimony ===
435
436 Currently no direct support in !BioRuby.
437
438
439 === Bayesian Inference ===
440
441 Currently no direct support in !BioRuby.
442
443
444 == Pairwise Distance Based Methods ==
445
446 === Pairwise Sequence Distance Estimation ===
447
448 _... to be done_
449
450 {{{
451 #!/usr/bin/env ruby
452 require 'bio'
453
454 }}}
455
456
457 === Optimality Criteria Based on Pairwise Distances ===
458
459
460 ==== Minimal Evolution: FastME ====
461
462 _... to be done_
463
464 {{{
465 #!/usr/bin/env ruby
466 require 'bio'
467
468 }}}
469
470 === Algorithmic Methods Based on Pairwise Distances ===
471
472 ==== Neighbor Joining and Related Methods ====
473
474 _... to be done_
475
476 {{{
477 #!/usr/bin/env ruby
478 require 'bio'
479
480 }}}
481
482
483
484
485
486
487
488 == Support Calculation? ==
489
490 === Bootstrap Resampling? ===
491
492
493 ----
494
495 = Analyzing Phylogenetic Trees =
496
497 == PAML ==
498
499
500 == Gene Duplication Inference ==
501
502 _need to further test and then import GSoC 'SDI' work..._
503
504
505 == Others? ==
506
507
508 ----
509
510 = Putting It All Together =
511
512 Example of a small "pipeline"-type program running a mininal phyogenetic analysis: starting with a set of sequences and ending with a phylogenetic tree.
513