JAL-2805 JAL-2847 JAL-281 getTreeFile made public
[jalview.git] / wiki / PhyloBioRuby.wiki
index 847b552..4b6b45f 100644 (file)
@@ -10,20 +10,55 @@ Tutorial for multiple sequence alignments and phylogenetic methods in [http://bi
 
 Eventually, this is expected to be placed on the official !BioRuby page.
 
-Author: [http://www.cmzmasek.net/ Christian M Zmasek], Sanford-Burnham Medical Research Institute
+Author: [https://sites.google.com/site/cmzmasek/ Christian Zmasek], Sanford-Burnham Medical Research Institute
 
  
-Copyright (C) 2011 Christian M Zmasek
+Copyright (C) 2011 Christian M Zmasek. All rights reserved.
 
 
-= Multiple Sequence Alignments =
+= Multiple Sequence Alignment =
 
 
 == Multiple Sequence Alignment Input and Output ==
 
 === Reading in a Multiple Sequence Alignment from a File ===
 
-The follow example shows how to read in a *ClustalW*-formatted multiple sequence alignment.
+This automatically determines the format
+{{{
+#!/usr/bin/env ruby
+require 'bio'
+
+seq_ary = Array.new
+ff = Bio::FlatFile.auto('bcl2.fasta')
+ff.each_entry do |entry|
+  seq_ary.push(entry)
+  puts entry.entry_id          # prints the identifier of the entry
+  puts entry.definition        # prints the definition of the entry
+  puts entry.seq               # prints the sequence data of the entry
+end
+
+# Creates a multiple sequence alignment (possibly unaligned) named
+# 'seqs' from array 'seq_ary'.
+seqs = Bio::Alignment.new(seq_ary)
+seqs.each { |seq| puts seq.to_s }
+
+# Writes multiple sequence alignment (possibly unaligned) 'seqs'
+# to a file in PHYLIP format.
+File.open('out0.phylip', 'w') do |f|
+  f.write(seqs.output(:phylip))
+end
+
+# Writes multiple sequence alignment (possibly unaligned) 'seqs'
+# to a file in FASTA format.
+File.open('out0.fasta', 'w') do |f|
+  f.write(seqs.output(:fasta))
+end
+}}}
+
+
+==== ClustalW Format ====
+
+The following example shows how to read in a *ClustalW*-formatted multiple sequence alignment.
 
 {{{
 #!/usr/bin/env ruby
@@ -34,50 +69,119 @@ require 'bio'
 report = Bio::ClustalW::Report.new(File.read('infile_clustalw.aln'))
 
 # Accesses the actual alignment.
-align = report.alignment
+msa = report.alignment
 
-# Goes through all sequences in 'align' and prints the
+# Goes through all sequences in 'msa' and prints the
 # actual molecular sequence.
-align.each do |entry|
+msa.each do |entry|
   puts entry.seq
 end
 }}}
 
+==== FASTA Format ====
+
+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.)
+{{{
+#!/usr/bin/env ruby
+require 'bio'
+
+# Reads in a FASTA-formatted multiple sequence alignment (which does
+# not have to be aligned, though) and stores its sequences in
+# array 'seq_ary'.
+seq_ary = Array.new
+fasta_seqs = Bio::Alignment::MultiFastaFormat.new(File.open('infile.fasta').read)
+fasta_seqs.entries.each do |seq|
+  seq_ary.push(seq)
+end
+
+# Creates a multiple sequence alignment (possibly unaligned) named
+# 'seqs' from array 'seq_ary'.
+seqs = Bio::Alignment.new(seq_ary)
+
+# Prints each sequence to the console.
+seqs.each { |seq| puts seq.to_s }
+
+# Writes multiple sequence alignment (possibly unaligned) 'seqs'
+# to a file in PHYLIP format.
+File.open('outfile.phylip', 'w') do |f|
+  f.write(seqs.output(:phylip))
+end
+}}}
+
+Relevant API documentation:
+
+ * [http://bioruby.open-bio.org/rdoc/classes/Bio/ClustalW/Report.html Bio::ClustalW::Report]
+ * [http://bioruby.open-bio.org/rdoc/classes/Bio/Alignment.html Bio::Alignment]
+ * [http://bioruby.open-bio.org/rdoc/classes/Bio/Sequence.html Bio::Sequence]
+
+=== Creating a Multiple Sequence Alignment ===
+
+
+=== Creating a Multiple Sequence Alignment from a Database ===
+
+?
 
 === Writing a Multiple Sequence Alignment to a File ===
 
 
-The follow example shows how to writing a multiple sequence alignment in *Fasta*-format:
+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').
 
 {{{
 #!/usr/bin/env ruby
 require 'bio'
 
 # Creates a new file named "outfile.fasta" and writes
-# multiple sequence alignment 'align' to it in fasta format.
+# multiple sequence alignment 'msa' to it in fasta format.
 File.open('outfile.fasta', 'w') do |f|
-  f.write(align.output(:fasta))
+  f.write(msa.output(:fasta))
 end
 }}}
 
-The following constants determine the output format
+==== Setting the Output Format ====
+
+The following symbols determine the output format:
 
-  * ClustalW: `:clustal`
-  * Fasta:    `:fasta`
-  * [http://evolution.genetics.washington.edu/phylip.html PHYLIP] interleaved: `:phylip`
-  * [http://evolution.genetics.washington.edu/phylip.html PHYLIP] non-interleaved: `:phylipnon`
-  * MSF: `:msf`
-  * Molphy: `:molphy`
+  * `:clustal` for ClustalW
+  * `:fasta` for FASTA
+  * `:phylip` for PHYLIP interleaved (will truncate sequence names to no more than 10 characters)
+  * `:phylipnon` for PHYLIP non-interleaved (will truncate sequence names to no more than 10 characters)
+  * `:msf` for MSF
+  * `:molphy` for Molphy
 
 
-For example, the following writes in [http://evolution.genetics.washington.edu/phylip.html PHYLIP]'s non-interleaved format:
+For example, the following writes in PHYLIP's non-interleaved format:
 
 {{{
 f.write(align.output(:phylipnon))
 }}}
 
 
+=== Formatting of Individual Sequences ===
+
+!BioRuby can format molecular sequences in a variety of formats.
+Individual sequences can be formatted to (e.g.) Genbank format as shown in the following examples.
+
+For Sequence objects:
+{{{
+seq.to_seq.output(:genbank)
+}}}
+
+For Bio::!FlatFile entries:
+{{{
+entry.to_biosequence.output(:genbank)
+}}}
+
+The following symbols determine the output format:
+  * `:genbank` for Genbank
+  * `:embl` for EMBL
+  * `:fasta` for FASTA
+  * `:fasta_ncbi` for NCBI-type FASTA
+  * `:raw` for raw sequence
+  * `:fastq` for FASTQ (includes quality scores)
+  * `:fastq_sanger` for Sanger-type FASTQ 
+  * `:fastq_solexa` for Solexa-type FASTQ 
+  * `:fastq_illumina` for Illumina-type FASTQ 
+
 == Calculating Multiple Sequence Alignments ==
 
 !BioRuby can be used to execute a variety of multiple sequence alignment
@@ -98,10 +202,10 @@ require 'bio'
 # 'seqs' is either an array of sequences or a multiple sequence 
 # alignment. In general this is read in from a file as described in ?.
 # For the purpose of this tutorial, it is generated in code.
-seqs = ["KMLFGVVFFFGG",
-        "LMGGHHF",
-        "GKKKKGHHHGHRRRGR",
-        "KKKKGHHHGHRRRGR"] 
+seqs = ['MFQIPEFEPSEQEDSSSAER',
+        'MGTPKQPSLAPAHALGLRKS',
+        'PKQPSLAPAHALGLRKS',
+        'MCSTSGCDLE'] 
 
 
 # Calculates the alignment using the MAFFT program on the local
@@ -136,10 +240,10 @@ require 'bio'
 # 'seqs' is either an array of sequences or a multiple sequence 
 # alignment. In general this is read in from a file as described in ?.
 # For the purpose of this tutorial, it is generated in code.
-seqs = ["KMLFGVVFFFGG",
-        "LMGGHHF",
-        "GKKKKGHHHGHRRRGR",
-        "KKKKGHHHGHRRRGR"] 
+seqs = ['MFQIPEFEPSEQEDSSSAER',
+        'MGTPKQPSLAPAHALGLRKS',
+        'PKQPSLAPAHALGLRKS',
+        'MCSTSGCDLE']  
 
 # Calculates the alignment using the Muscle program on the local
 # machine with options '-quiet -maxiters 64'
@@ -162,9 +266,12 @@ References:
 
 === Other Programs ===
 
+_need more detail here..._
+
 [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. 
 
 
+
 == Manipulating Multiple Sequence Alignments ==
 
 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.
@@ -183,10 +290,16 @@ require 'bio'
 
 = Phylogenetic Trees =
 
+
 == Phylogenetic Tree Input and Output ==
 
+
 === Reading in of Phylogenetic Trees ===
 
+
+
+====Newick or New Hampshire Format====
+
 _... to be done_
 
 {{{
@@ -195,12 +308,50 @@ require 'bio'
 
 }}}
 
-Also, see: https://www.nescent.org/wg_phyloinformatics/BioRuby_PhyloXML_HowTo_documentation
+====phyloXML Format====
+
+Partially copied from [https://www.nescent.org/wg_phyloinformatics/BioRuby_PhyloXML_HowTo_documentation Diana Jaunzeikare's documentation].
+
+In addition to !BioRuby, a libxml Ruby binding is also required. This can be installed with the following command: 
+
+{{{
+% gem install -r libxml-ruby
+}}}
+
+This example reads file "example.xml" and stores its [http://www.phyloxml.org/ phyloXML]-formatted trees in variable 'trees'.
+
+{{{
+#!/usr/bin/env ruby
+require 'bio'
+
+# This creates new phyloXML parser.
+trees = Bio::PhyloXML::Parser.new('example.xml')
 
+# This prints the names of all trees in the file.
+trees.each do |tree|
+  puts tree.name
+end
+
+# If there are several trees in the file, you can access the one you wish via index.
+tree = trees[3]
+
+}}}
 
 
+====Nexus  Format====
+
+_... to be done_
+
+{{{
+#!/usr/bin/env ruby
+require 'bio'
+
+}}}
+
 === Writing of Phylogenetic Trees ===
 
+====Newick or New Hampshire Format====
+
 _... to be done_
 
 {{{
@@ -209,21 +360,36 @@ require 'bio'
 
 }}}
 
-Also, see: https://www.nescent.org/wg_phyloinformatics/BioRuby_PhyloXML_HowTo_documentation
+====phyloXML Format====
 
+Partially copied from [https://www.nescent.org/wg_phyloinformatics/BioRuby_PhyloXML_HowTo_documentation Diana Jaunzeikare's documentation].
 
+In addition to !BioRuby, a libxml Ruby binding is also required. This can be installed with the following command: 
 
-== Phylogenetic Inference ==
+{{{
+% gem install -r libxml-ruby
+}}}
 
-_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..._
+This example writes trees 'tree1' and 'tree2' to file "tree.xml" in [http://www.phyloxml.org/ phyloXML] format.
 
-_What about pairwise distance calculation?_
+{{{
+#!/usr/bin/env ruby
+require 'bio'
 
+# this creates new phyloXML writer.
+writer = Bio::PhyloXML::Writer.new('tree.xml')
 
+# Writes tree to the file "tree.xml".
+writer.write(tree1)
 
-== Maximum Likelihood ==
+# Adds another tree to the file.
+writer.write(tree2)
 
-=== RAxML ===
+
+}}}
+
+
+====Nexus  Format====
 
 _... to be done_
 
@@ -234,7 +400,17 @@ require 'bio'
 }}}
 
 
-=== PhyML ===
+= Phylogenetic Inference =
+
+_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..._
+
+== Optimality Criteria Based on Character Data ==
+
+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.
+
+=== Maximum Likelihood ===
+
+==== RAxML ====
 
 _... to be done_
 
@@ -244,9 +420,56 @@ require 'bio'
 
 }}}
 
+
+==== PhyML ====
+
+_... to be done_
+
+{{{
+#!/usr/bin/env ruby
+require 'bio'
+
+}}}
+
+=== Maximum Parsimony ===
+
+Currently no direct support in !BioRuby.
+
+
+=== Bayesian Inference ===
+
+Currently no direct support in !BioRuby.
+
+
 == Pairwise Distance Based Methods ==
 
-=== FastME ===
+=== Pairwise Sequence Distance Estimation ===
+
+_... to be done_
+
+{{{
+#!/usr/bin/env ruby
+require 'bio'
+
+}}}
+
+
+=== Optimality Criteria Based on Pairwise Distances ===
+
+
+==== Minimal Evolution: FastME ====
+
+_... to be done_
+
+{{{
+#!/usr/bin/env ruby
+require 'bio'
+
+}}}
+
+=== Algorithmic Methods Based on Pairwise Distances ===
+
+==== Neighbor Joining and Related Methods ====
 
 _... to be done_
 
@@ -258,7 +481,7 @@ require 'bio'
 
 
 
-=== PHYLIP? ===
+