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