github.com/biogo/biogo@v1.0.4/seq/alignment/alignment_example_test.go (about)

     1  // Copyright ©2011-2012 The bíogo Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package alignment
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/biogo/biogo/alphabet"
    11  	"github.com/biogo/biogo/feat"
    12  	"github.com/biogo/biogo/seq"
    13  	"github.com/biogo/biogo/seq/linear"
    14  	"github.com/biogo/biogo/seq/sequtils"
    15  )
    16  
    17  var m, n *Seq
    18  
    19  func init() {
    20  	var err error
    21  	m, err = NewSeq("example alignment",
    22  		[]string{"seq 1", "seq 2", "seq 3"},
    23  		[][]alphabet.Letter{
    24  			[]alphabet.Letter("AAA"),
    25  			[]alphabet.Letter("CCC"),
    26  			[]alphabet.Letter("GGG"),
    27  			[]alphabet.Letter("CGA"),
    28  			[]alphabet.Letter("TTT"),
    29  			[]alphabet.Letter("GGG"),
    30  			[]alphabet.Letter("AAA"),
    31  			[]alphabet.Letter("CCC"),
    32  			[]alphabet.Letter("TCG"),
    33  			[]alphabet.Letter("TTT"),
    34  			[]alphabet.Letter("GGG"),
    35  			[]alphabet.Letter("GGG"),
    36  			[]alphabet.Letter("TCC"),
    37  			[]alphabet.Letter("GGG"),
    38  			[]alphabet.Letter("CCC"),
    39  			[]alphabet.Letter("AGT"),
    40  			[]alphabet.Letter("CCC"),
    41  			[]alphabet.Letter("GAA"),
    42  			[]alphabet.Letter("TTT"),
    43  		},
    44  		alphabet.DNA,
    45  		seq.DefaultConsensus)
    46  
    47  	if err != nil {
    48  		panic(err)
    49  	}
    50  }
    51  
    52  func ExampleNewSeq() {
    53  	m, err := NewSeq("example alignment",
    54  		[]string{"seq 1", "seq 2", "seq 3"},
    55  		[][]alphabet.Letter{
    56  			[]alphabet.Letter("AAA"),
    57  			[]alphabet.Letter("CCC"),
    58  			[]alphabet.Letter("GGG"),
    59  			[]alphabet.Letter("CGA"),
    60  			[]alphabet.Letter("TTT"),
    61  			[]alphabet.Letter("GGG"),
    62  			[]alphabet.Letter("AAA"),
    63  			[]alphabet.Letter("CCC"),
    64  			[]alphabet.Letter("TCG"),
    65  			[]alphabet.Letter("TTT"),
    66  			[]alphabet.Letter("GGG"),
    67  			[]alphabet.Letter("GGG"),
    68  			[]alphabet.Letter("TCC"),
    69  			[]alphabet.Letter("GGG"),
    70  			[]alphabet.Letter("CCC"),
    71  			[]alphabet.Letter("AGT"),
    72  			[]alphabet.Letter("CCC"),
    73  			[]alphabet.Letter("GAA"),
    74  			[]alphabet.Letter("TTT"),
    75  		},
    76  		alphabet.DNA,
    77  		seq.DefaultConsensus)
    78  	if err == nil {
    79  		fmt.Printf("%-s\n\n%-s\n", m, m.Consensus(false))
    80  	}
    81  
    82  	// Output:
    83  	// ACGCTGACTTGGTGCACGT
    84  	// ACGGTGACCTGGCGCGCAT
    85  	// ACGATGACGTGGCGCTCAT
    86  	//
    87  	// acgntgacntggcgcncat
    88  }
    89  
    90  func ExampleSeq_Add() {
    91  	fmt.Printf("%v %-s\n", m.Rows(), m.Consensus(false))
    92  	m.Add(linear.NewQSeq("example DNA",
    93  		[]alphabet.QLetter{{'a', 40}, {'c', 39}, {'g', 40}, {'C', 38}, {'t', 35}, {'g', 20}},
    94  		alphabet.DNA, alphabet.Sanger))
    95  	fmt.Printf("%v %-s\n", m.Rows(), m.Consensus(false))
    96  	// Output:
    97  	// 3 acgntgacntggcgcncat
    98  	// 4 acgctgacntggcgcncat
    99  }
   100  
   101  func ExampleSeq_Clone() {
   102  	n = m.Clone().(*Seq)
   103  	n.Row(2).Set(3, alphabet.QLetter{L: 't'})
   104  	fmt.Printf("%-s\n\n%-s\n\n", m, m.Consensus(false))
   105  	fmt.Printf("%-s\n\n%-s\n", n, n.Consensus(false))
   106  	// Output:
   107  	// ACGCTGACTTGGTGCACGT
   108  	// ACGGTGACCTGGCGCGCAT
   109  	// ACGATGACGTGGCGCTCAT
   110  	// acgCtg-------------
   111  	//
   112  	// acgctgacntggcgcncat
   113  	//
   114  	// ACGCTGACTTGGTGCACGT
   115  	// ACGGTGACCTGGCGCGCAT
   116  	// ACGtTGACGTGGCGCTCAT
   117  	// acgCtg-------------
   118  	//
   119  	// acgctgacntggcgcncat
   120  }
   121  
   122  func ExampleSeq_Rows() {
   123  	fmt.Println(m.Rows())
   124  	// Output:
   125  	// 4
   126  }
   127  
   128  func ExampleSeq_join() {
   129  	fmt.Printf("%-s\n\n%-s\n", n, n.Consensus(false))
   130  	err := sequtils.Join(n, m, seq.End)
   131  	if err == nil {
   132  		fmt.Printf("\n%-s\n\n%-s\n", n, n.Consensus(false))
   133  	}
   134  	// Output:
   135  	// ACGCTGACTTGGTGCACGT
   136  	// ACGGTGACCTGGCGCGCAT
   137  	// ACGtTGACGTGGCGCTCAT
   138  	// acgCtg-------------
   139  	//
   140  	// acgctgacntggcgcncat
   141  	//
   142  	// ACGCTGACTTGGTGCACGTACGCTGACTTGGTGCACGT
   143  	// ACGGTGACCTGGCGCGCATACGGTGACCTGGCGCGCAT
   144  	// ACGtTGACGTGGCGCTCATACGATGACGTGGCGCTCAT
   145  	// acgCtg-------------acgCtg-------------
   146  	//
   147  	// acgctgacntggcgcncatacgctgacntggcgcncat
   148  }
   149  
   150  func ExampleSeq_Len() {
   151  	fmt.Println(m.Len())
   152  	// Output:
   153  	// 19
   154  }
   155  
   156  func ExampleSeq_RevComp() {
   157  	fmt.Printf("%-s\n\n%-s\n", m, m.Consensus(false))
   158  	fmt.Println()
   159  	m.RevComp()
   160  	fmt.Printf("%-s\n\n%-s\n", m, m.Consensus(false))
   161  	// Output:
   162  	// ACGCTGACTTGGTGCACGT
   163  	// ACGGTGACCTGGCGCGCAT
   164  	// ACGATGACGTGGCGCTCAT
   165  	// acgCtg-------------
   166  	//
   167  	// acgctgacntggcgcncat
   168  	//
   169  	// ACGTGCACCAAGTCAGCGT
   170  	// ATGCGCGCCAGGTCACCGT
   171  	// ATGAGCGCCACGTCATCGT
   172  	// -------------caGcgt
   173  	//
   174  	// atgngcgccangtcagcgt
   175  }
   176  
   177  type fe struct {
   178  	s, e int
   179  	st   seq.Strand
   180  	feat.Feature
   181  }
   182  
   183  func (f fe) Start() int                    { return f.s }
   184  func (f fe) End() int                      { return f.e }
   185  func (f fe) Len() int                      { return f.e - f.s }
   186  func (f fe) Orientation() feat.Orientation { return feat.Orientation(f.st) }
   187  
   188  type fs []feat.Feature
   189  
   190  func (f fs) Features() []feat.Feature { return []feat.Feature(f) }
   191  
   192  func ExampleSeq_stitch() {
   193  	f := fs{
   194  		&fe{s: -1, e: 4},
   195  		&fe{s: 30, e: 38},
   196  	}
   197  	fmt.Printf("%-s\n\n%-s\n", n, n.Consensus(false))
   198  	if err := sequtils.Stitch(n, n, f); err == nil {
   199  		fmt.Printf("\n%-s\n\n%-s\n", n, n.Consensus(false))
   200  	} else {
   201  		fmt.Println(err)
   202  	}
   203  	// Output:
   204  	// ACGCTGACTTGGTGCACGTACGTGCACCAAGTCAGCGT
   205  	// ACGGTGACCTGGCGCGCATATGCGCGCCAGGTCACCGT
   206  	// ACGtTGACGTGGCGCTCATATGAGCGCCACGTCATCGT
   207  	// acgCtg--------------------------caGcgt
   208  	//
   209  	// acgctgacntggcgcncatatgngcgccangtcagcgt
   210  	//
   211  	// ACGCGTCAGCGT
   212  	// ACGGGTCACCGT
   213  	// ACGtGTCATCGT
   214  	// acgC--caGcgt
   215  	//
   216  	// acgcgtcagcgt
   217  }
   218  
   219  func ExampleSeq_truncate() {
   220  	fmt.Printf("%-s\n\n%-s\n", m, m.Consensus(false))
   221  	err := sequtils.Truncate(m, m, 4, 12)
   222  	if err == nil {
   223  		fmt.Printf("\n%-s\n\n%-s\n", m, m.Consensus(false))
   224  	}
   225  	// Output:
   226  	// ACGTGCACCAAGTCAGCGT
   227  	// ATGCGCGCCAGGTCACCGT
   228  	// ATGAGCGCCACGTCATCGT
   229  	// -------------caGcgt
   230  	//
   231  	// atgngcgccangtcagcgt
   232  	//
   233  	// GCACCAAG
   234  	// GCGCCAGG
   235  	// GCGCCACG
   236  	// --------
   237  	//
   238  	// gcgccang
   239  }