github.com/biogo/biogo@v1.0.4/seq/multi/multi_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 multi
     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  )
    15  
    16  var m, n *Multi
    17  
    18  func init() {
    19  	var err error
    20  	m, err = NewMulti("example multi",
    21  		[]seq.Sequence{
    22  			linear.NewSeq("example DNA 1", []alphabet.Letter("ACGCTGACTTGGTGCACGT"), alphabet.DNA),
    23  			linear.NewSeq("example DNA 2", []alphabet.Letter("ACGGTGACCTGGCGCGCAT"), alphabet.DNA),
    24  			linear.NewSeq("example DNA 3", []alphabet.Letter("ACGATGACGTGGCGCTCAT"), alphabet.DNA),
    25  		},
    26  		seq.DefaultConsensus)
    27  
    28  	if err != nil {
    29  		panic(err)
    30  	}
    31  }
    32  
    33  func ExampleNewMulti() {
    34  	m, err := NewMulti("example multi",
    35  		[]seq.Sequence{
    36  			linear.NewSeq("example DNA 1", []alphabet.Letter("ACGCTGACTTGGTGCACGT"), alphabet.DNA),
    37  			linear.NewSeq("example DNA 2", []alphabet.Letter("ACGGTGACCTGGCGCGCAT"), alphabet.DNA),
    38  			linear.NewSeq("example DNA 3", []alphabet.Letter("ACGATGACGTGGCGCTCAT"), alphabet.DNA),
    39  		},
    40  		seq.DefaultConsensus)
    41  
    42  	if err != nil {
    43  		return
    44  	}
    45  
    46  	fmt.Printf("%- s\n\n%-s\n", m, m.Consensus(false))
    47  	// Output:
    48  	// ACGCTGACTTGGTGCACGT
    49  	// ACGGTGACCTGGCGCGCAT
    50  	// ACGATGACGTGGCGCTCAT
    51  	//
    52  	// acgntgacntggcgcncat
    53  }
    54  
    55  func ExampleMulti_Add() {
    56  	var err error
    57  	fmt.Printf("%v %-s\n", m.Rows(), m.Consensus(false))
    58  	err = m.Add(linear.NewQSeq("example DNA",
    59  		[]alphabet.QLetter{{'a', 40}, {'c', 39}, {'g', 40}, {'C', 38}, {'t', 35}, {'g', 20}},
    60  		alphabet.DNA, alphabet.Sanger))
    61  	if err != nil {
    62  		fmt.Println(err)
    63  		return
    64  	}
    65  	fmt.Printf("%v %-s\n", m.Rows(), m.Consensus(false))
    66  	err = m.Add(linear.NewQSeq("example RNA",
    67  		[]alphabet.QLetter{{'a', 40}, {'c', 39}, {'g', 40}, {'C', 38}, {'t', 35}, {'g', 20}},
    68  		alphabet.RNA, alphabet.Sanger))
    69  	if err != nil {
    70  		fmt.Println(err)
    71  		return
    72  	}
    73  	// Output:
    74  	// 3 acgntgacntggcgcncat
    75  	// 4 acgctgacntggcgcncat
    76  	// multi: inconsistent alphabets
    77  }
    78  
    79  func ExampleMulti_Clone() {
    80  	n = m.Clone().(*Multi)
    81  	n.Row(2).Set(3, alphabet.QLetter{L: 't'})
    82  	fmt.Printf("%- s\n\n%-s\n\n%- s\n\n%-s\n",
    83  		m, m.Consensus(false),
    84  		n, n.Consensus(false),
    85  	)
    86  	// Output:
    87  	// ACGCTGACTTGGTGCACGT
    88  	// ACGGTGACCTGGCGCGCAT
    89  	// ACGATGACGTGGCGCTCAT
    90  	// acgCtg
    91  	//
    92  	// acgctgacntggcgcncat
    93  	//
    94  	// ACGCTGACTTGGTGCACGT
    95  	// ACGGTGACCTGGCGCGCAT
    96  	// ACGtTGACGTGGCGCTCAT
    97  	// acgCtg
    98  	//
    99  	// acgctgacntggcgcncat
   100  }
   101  
   102  func ExampleMulti_Rows() {
   103  	fmt.Println(m.Rows())
   104  	// Output:
   105  	// 4
   106  }
   107  
   108  func ExampleMulti_IsFlush() {
   109  	m.Row(3).SetOffset(13)
   110  	fmt.Printf("%- s\n\n%-s\n", m, m.Consensus(false))
   111  	fmt.Printf("\nFlush at left: %v\nFlush at right: %v\n", m.IsFlush(seq.Start), m.IsFlush(seq.End))
   112  	m.Flush(seq.Start, '-')
   113  	fmt.Printf("\n%- s\n\n%-s\n", m, m.Consensus(false))
   114  	fmt.Printf("\nFlush at left: %v\nFlush at right: %v\n", m.IsFlush(seq.Start), m.IsFlush(seq.End))
   115  	// Output:
   116  	// ACGCTGACTTGGTGCACGT
   117  	// ACGGTGACCTGGCGCGCAT
   118  	// ACGATGACGTGGCGCTCAT
   119  	//              acgCtg
   120  	//
   121  	// acgntgacntggcgcgcat
   122  	//
   123  	// Flush at left: false
   124  	// Flush at right: true
   125  	//
   126  	// ACGCTGACTTGGTGCACGT
   127  	// ACGGTGACCTGGCGCGCAT
   128  	// ACGATGACGTGGCGCTCAT
   129  	// -------------acgCtg
   130  	//
   131  	// acgntgacntggcgcgcat
   132  	//
   133  	// Flush at left: true
   134  	// Flush at right: true
   135  }
   136  
   137  func ExampleMulti_Join() {
   138  	fmt.Printf("%- s\n\n%-s\n", n, n.Consensus(false))
   139  	n.Join(m, seq.End)
   140  	fmt.Printf("\n%- s\n\n%-s\n", n, n.Consensus(false))
   141  	// Output:
   142  	// ACGCTGACTTGGTGCACGT
   143  	// ACGGTGACCTGGCGCGCAT
   144  	// ACGtTGACGTGGCGCTCAT
   145  	// acgCtg
   146  	//
   147  	// acgctgacntggcgcncat
   148  	//
   149  	// ACGCTGACTTGGTGCACGTACGCTGACTTGGTGCACGT
   150  	// ACGGTGACCTGGCGCGCATACGGTGACCTGGCGCGCAT
   151  	// ACGtTGACGTGGCGCTCATACGATGACGTGGCGCTCAT
   152  	// acgCtg--------------------------acgCtg
   153  	//
   154  	// acgctgacntggcgcncatacgntgacntggcgcgcat
   155  }
   156  
   157  func ExampleMulti_Len() {
   158  	fmt.Println(m.Len())
   159  	// Output:
   160  	// 19
   161  }
   162  
   163  func ExampleMulti_RevComp() {
   164  	fmt.Printf("%- s\n\n%-s\n\n", m, m.Consensus(false))
   165  	m.RevComp()
   166  	fmt.Printf("%- s\n\n%-s\n", m, m.Consensus(false))
   167  	// Output:
   168  	// ACGCTGACTTGGTGCACGT
   169  	// ACGGTGACCTGGCGCGCAT
   170  	// ACGATGACGTGGCGCTCAT
   171  	// -------------acgCtg
   172  	//
   173  	// acgntgacntggcgcgcat
   174  	//
   175  	// ACGTGCACCAAGTCAGCGT
   176  	// ATGCGCGCCAGGTCACCGT
   177  	// ATGAGCGCCACGTCATCGT
   178  	// caGcgt-------------
   179  	//
   180  	// atgcgcgccangtcancgt
   181  }
   182  
   183  type fe struct {
   184  	s, e int
   185  	st   seq.Strand
   186  	feat.Feature
   187  }
   188  
   189  func (f fe) Start() int                    { return f.s }
   190  func (f fe) End() int                      { return f.e }
   191  func (f fe) Len() int                      { return f.e - f.s }
   192  func (f fe) Orientation() feat.Orientation { return feat.Orientation(f.st) }
   193  
   194  type fs []feat.Feature
   195  
   196  func (f fs) Features() []feat.Feature { return []feat.Feature(f) }
   197  
   198  func ExampleMulti_Stitch() {
   199  	f := fs{
   200  		&fe{s: -1, e: 4},
   201  		&fe{s: 30, e: 38},
   202  	}
   203  	fmt.Printf("%- s\n\n%-s\n\n", n, n.Consensus(false))
   204  	if err := n.Stitch(f); err == nil {
   205  		fmt.Printf("%- s\n\n%-s\n", n, n.Consensus(false))
   206  	} else {
   207  		fmt.Println(err)
   208  	}
   209  	// Output:
   210  	// ACGCTGACTTGGTGCACGTACGCTGACTTGGTGCACGT
   211  	// ACGGTGACCTGGCGCGCATACGGTGACCTGGCGCGCAT
   212  	// ACGtTGACGTGGCGCTCATACGATGACGTGGCGCTCAT
   213  	// acgCtg--------------------------acgCtg
   214  	//
   215  	// acgctgacntggcgcncatacgntgacntggcgcgcat
   216  	//
   217  	// ACGCGTGCACGT
   218  	// ACGGGCGCGCAT
   219  	// ACGtGCGCTCAT
   220  	// acgC--acgCtg
   221  	//
   222  	// acgcgcgcgcat
   223  }
   224  
   225  func ExampleMulti_Truncate() {
   226  	fmt.Printf("%- s\n\n%-s\n\n", m, m.Consensus(false))
   227  	m.Truncate(4, 12)
   228  	fmt.Printf("%- s\n\n%-s\n", m, m.Consensus(false))
   229  	// Output:
   230  	// ACGTGCACCAAGTCAGCGT
   231  	// ATGCGCGCCAGGTCACCGT
   232  	// ATGAGCGCCACGTCATCGT
   233  	// caGcgt-------------
   234  	//
   235  	// atgcgcgccangtcancgt
   236  	//
   237  	// GCACCAAG
   238  	// GCGCCAGG
   239  	// GCGCCACG
   240  	// gt------
   241  	//
   242  	// gcgccang
   243  }