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

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

	return
}
Beispiel #2
0
func (self *Writer) writeHeader(prefix byte, s seqio.Sequence) (n int, err error) {
	var ln int
	n, err = self.w.Write([]byte{prefix})
	if err != nil {
		return
	}
	ln, err = io.WriteString(self.w, *s.Name())
	if n += ln; err != nil {
		return
	}
	if desc := *s.Description(); len(desc) != 0 {
		ln, err = self.w.Write([]byte{' '})
		if n += ln; err != nil {
			return
		}
		ln, err = io.WriteString(self.w, desc)
		if n += ln; err != nil {
			return
		}
	}
	ln, err = self.w.Write([]byte("\n"))
	n += ln
	return
}
Beispiel #3
0
// Write a single sequence and return the number of bytes written and any error.
func (self *Writer) Write(s seqio.Sequence) (n int, err error) {
	var (
		ln, c  int
		prefix = append([]byte{'\n'}, self.SeqPrefix...)
	)
	id, desc := *s.Name(), *s.Description()
	header := make([]byte, 0, len(self.IDPrefix)+len(id)+len(desc)+1)
	header = append(header, self.IDPrefix...)
	header = append(header, id...)
	if len(desc) > 0 {
		header = append(header, ' ')
		header = append(header, desc...)
	}

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

	return
}