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