github.com/biogo/biogo@v1.0.4/seq/quality/phred.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 quality
     6  
     7  import (
     8  	"github.com/biogo/biogo/alphabet"
     9  	"github.com/biogo/biogo/seq"
    10  )
    11  
    12  // A slice of quality scores that satisfies the alphabet.Slice interface.
    13  type Qphreds []alphabet.Qphred
    14  
    15  func (q Qphreds) Make(len, cap int) alphabet.Slice    { return make(Qphreds, len, cap) }
    16  func (q Qphreds) Len() int                            { return len(q) }
    17  func (q Qphreds) Cap() int                            { return cap(q) }
    18  func (q Qphreds) Slice(start, end int) alphabet.Slice { return q[start:end] }
    19  func (q Qphreds) Append(a alphabet.Slice) alphabet.Slice {
    20  	return append(q, a.(Qphreds)...)
    21  }
    22  func (q Qphreds) Copy(a alphabet.Slice) int { return copy(q, a.(Qphreds)) }
    23  
    24  type Phred struct {
    25  	seq.Annotation
    26  	Qual   Qphreds
    27  	Encode alphabet.Encoding
    28  }
    29  
    30  // Create a new scoring type.
    31  func NewPhred(id string, q []alphabet.Qphred, encode alphabet.Encoding) *Phred {
    32  	return &Phred{
    33  		Annotation: seq.Annotation{ID: id},
    34  		Qual:       append([]alphabet.Qphred(nil), q...),
    35  		Encode:     encode,
    36  	}
    37  }
    38  
    39  // Returns the underlying quality score slice.
    40  func (q *Phred) Slice() alphabet.Slice { return q.Qual }
    41  
    42  // Set the underlying quality score slice.
    43  func (q *Phred) SetSlice(sl alphabet.Slice) { q.Qual = sl.(Qphreds) }
    44  
    45  // Append to the scores.
    46  func (q *Phred) Append(a ...alphabet.Qphred) { q.Qual = append(q.Qual, a...) }
    47  
    48  // Return the raw score at position pos.
    49  func (q *Phred) At(i int) alphabet.Qphred { return q.Qual[i-q.Offset] }
    50  
    51  // Return the error probability at position pos.
    52  func (q *Phred) EAt(i int) float64 { return q.Qual[i-q.Offset].ProbE() }
    53  
    54  // Set the raw score at position pos to qual.
    55  func (q *Phred) Set(i int, qual alphabet.Qphred) error { q.Qual[i-q.Offset] = qual; return nil }
    56  
    57  // Set the error probability to e at position pos.
    58  func (q *Phred) SetE(i int, e float64) error {
    59  	q.Qual[i-q.Offset] = alphabet.Ephred(e)
    60  	return nil
    61  }
    62  
    63  // Encode the quality at position pos to a letter based on the sequence Encode setting.
    64  func (q *Phred) QEncode(i int) byte {
    65  	return q.Qual[i-q.Offset].Encode(q.Encode)
    66  }
    67  
    68  // Decode a quality letter to a phred score based on the sequence Encode setting.
    69  func (q *Phred) QDecode(l byte) alphabet.Qphred { return q.Encode.DecodeToQphred(l) }
    70  
    71  // Return the quality Encode type.
    72  func (q *Phred) Encoding() alphabet.Encoding { return q.Encode }
    73  
    74  // Set the quality Encode type to e.
    75  func (q *Phred) SetEncoding(e alphabet.Encoding) error { q.Encode = e; return nil }
    76  
    77  // Return the length of the score sequence.
    78  func (q *Phred) Len() int { return len(q.Qual) }
    79  
    80  // Return the start position of the score sequence.
    81  func (q *Phred) Start() int { return q.Offset }
    82  
    83  // Return the end position of the score sequence.
    84  func (q *Phred) End() int { return q.Offset + q.Len() }
    85  
    86  // Return a copy of the quality sequence.
    87  func (q *Phred) Copy() seq.Quality {
    88  	c := *q
    89  	c.Qual = append([]alphabet.Qphred(nil), q.Qual...)
    90  
    91  	return &c
    92  }
    93  
    94  // Reverse the order of elements in the sequence.
    95  func (q *Phred) Reverse() {
    96  	l := q.Qual
    97  	for i, j := 0, len(l)-1; i < j; i, j = i+1, j-1 {
    98  		l[i], l[j] = l[j], l[i]
    99  	}
   100  }
   101  
   102  func (q *Phred) String() string {
   103  	qs := make([]byte, 0, len(q.Qual))
   104  	for _, s := range q.Qual {
   105  		qs = append(qs, s.Encode(q.Encode))
   106  	}
   107  	return string(qs)
   108  }