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