| 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 pals |
| 6 | |
| 7 | import ( |
| 8 | "github.com/biogo/biogo/feat" |
| 9 | "github.com/biogo/biogo/io/featio/gff" |
| 10 | |
| 11 | "fmt" |
| 12 | "io" |
| 13 | ) |
| 14 | |
| 15 | // Writer is a type that writes PALS pair feature in GFFv2 format. |
| 16 | type Writer struct { |
| 17 | w *gff.Writer |
| 18 | t *gff.Feature |
| 19 | } |
| 20 | |
| 21 | // NewWriter returns a new PALS writer that write PALS alignment features to the io.Writer w. |
| 22 | func NewWriter(w io.Writer, prec, width int, header bool) *Writer { |
| 23 | gw := gff.NewWriter(w, width, header) |
| 24 | gw.Precision = prec |
| 25 | return &Writer{ |
| 26 | w: gw, |
| 27 | t: &gff.Feature{Source: "pals", Feature: "hit"}, |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | // Write writes a single feature and return the number of bytes written and any error. |
| 32 | func (w *Writer) Write(pair *Pair) (n int, err error) { |
| 33 | t := w.t |
| 34 | t.SeqName = pair.B.Location().Name() |
| 35 | t.FeatStart = pair.B.Start() |
| 36 | t.FeatEnd = pair.B.End() |
| 37 | t.FeatScore = floatPtr(float64(pair.Score)) |
| 38 | t.FeatStrand = pair.Strand |
| 39 | t.FeatFrame = gff.NoFrame |
| 40 | t.FeatAttributes = append(t.FeatAttributes[:0], |
| 41 | gff.Attribute{ |
| 42 | Tag: "Target", |
| 43 | Value: fmt.Sprintf("%s %d %d", pair.A.Location().Name(), feat.ZeroToOne(pair.A.Start()), pair.A.End()), |
| 44 | }, |
| 45 | gff.Attribute{ |
| 46 | Tag: "maxe", |
| 47 | Value: fmt.Sprintf("%.2g", pair.Error), |
| 48 | }, |
| 49 | ) |
| 50 | |
| 51 | return w.w.Write(t) |
| 52 | } |
| 53 | |
| 54 | func floatPtr(f float64) *float64 { return &f } |