Esempio n. 1
0
// Write a single sequence and return the number of bytes written and any error.
func (w *Writer) Write(s seq.Sequence) (n int, err error) {
	var (
		_n  int
		enc alphabet.Encoding
	)
	if e, ok := s.(Encoder); ok {
		enc = e.Encoding()
	} else {
		enc = alphabet.Sanger
	}

	n, err = w.writeHeader('@', s)
	if err != nil {
		return
	}
	for i := 0; i < s.Len(); i++ {
		_n, err = w.w.Write([]byte{byte(s.At(i).L)})
		if n += _n; err != nil {
			return
		}
	}
	_n, err = w.w.Write([]byte{'\n'})
	if n += _n; err != nil {
		return
	}
	if w.QID {
		_n, err = w.writeHeader('+', s)
		if n += _n; err != nil {
			return
		}
	} else {
		_n, err = w.w.Write([]byte("+\n"))
		if n += _n; err != nil {
			return
		}
	}
	for i := 0; i < s.Len(); i++ {
		_n, err = w.w.Write([]byte{s.At(i).Q.Encode(enc)})
		if n += _n; err != nil {
			return
		}
	}
	_n, err = w.w.Write([]byte{'\n'})
	if n += _n; err != nil {
		return
	}

	return
}
Esempio n. 2
0
// Write a single sequence and return the number of bytes written and any error.
func (w *Writer) Write(s seq.Sequence) (n int, err error) {
	var (
		_n     int
		prefix = append([]byte{'\n'}, w.SeqPrefix...)
	)
	id, desc := s.Name(), s.Description()
	header := make([]byte, 0, len(w.IDPrefix)+len(id)+len(desc)+1)
	header = append(header, w.IDPrefix...)
	header = append(header, id...)
	if len(desc) > 0 {
		header = append(header, ' ')
		header = append(header, desc...)
	}

	n, err = w.w.Write(header)
	if err != nil {
		return n, err
	}
	for i := 0; i < s.Len(); i++ {
		if i%w.Width == 0 {
			_n, err = w.w.Write(prefix)
			if n += _n; err != nil {
				return n, err
			}
		}
		_n, err = w.w.Write([]byte{byte(s.At(i).L)})
		if n += _n; err != nil {
			return n, err
		}
	}
	_n, err = w.w.Write([]byte{'\n'})
	if n += _n; err != nil {
		return n, err
	}

	return n, nil
}