1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
package fastx
import (
"fmt"
"github.com/shenwei356/bio/seq"
"github.com/shenwei356/util/byteutil"
"github.com/shenwei356/xopen"
)
// Record is a struct for FASTA/Q
type Record struct {
ID []byte // id
Name []byte // full name
Desc []byte // Description
Seq *seq.Seq // seq
}
// Clone of a Record
func (record *Record) Clone() *Record {
return &Record{
[]byte(string(record.ID)),
[]byte(string(record.Name)),
[]byte(string(record.Desc)),
record.Seq.Clone(),
}
}
func (record *Record) String() string {
return string(record.Format(60))
}
// NewRecord is constructor of type Record for FASTA
func NewRecord(t *seq.Alphabet, id, name, desc, s []byte) (*Record, error) {
seq, err := seq.NewSeq(t, s)
if err != nil {
return nil, fmt.Errorf("error when parsing seq: %s (%s)", id, err)
}
return &Record{id, name, desc, seq}, nil
}
// NewRecordWithoutValidation is constructor of type Record for FASTA
// without validation of the sequence
func NewRecordWithoutValidation(t *seq.Alphabet, id, name, desc, s []byte) (*Record, error) {
seq, err := seq.NewSeqWithoutValidation(t, s)
if err != nil {
return nil, err
}
return &Record{id, name, desc, seq}, nil
}
// NewRecordWithSeq is constructor of type Record
// for FASTA with a existed seq.Seq object
func NewRecordWithSeq(id, name, desc []byte, s *seq.Seq) (*Record, error) {
return &Record{id, name, desc, s}, nil
}
// NewRecordWithQual is constructor of type Record for FASTQ
func NewRecordWithQual(t *seq.Alphabet, id, name, desc, s, q []byte) (*Record, error) {
seq, err := seq.NewSeqWithQual(t, s, q)
if err != nil {
return nil, fmt.Errorf("error when parsing seq: %s (%s)", id, err)
}
return &Record{id, name, desc, seq}, nil
}
// NewRecordWithQualWithoutValidation is constructor of type Record for FASTQ
func NewRecordWithQualWithoutValidation(t *seq.Alphabet, id, name, desc, s, q []byte) (*Record, error) {
seq, err := seq.NewSeqWithQualWithoutValidation(t, s, q)
if err != nil {
return nil, err
}
return &Record{id, name, desc, seq}, nil
}
// ForcelyOutputFastq means outputing record as fastq even if it has no quality (zero-length fastq)
var ForcelyOutputFastq bool
// Format returns formated (wrapped with fixed length of) sequence record
func (record *Record) Format(width int) []byte {
if len(record.Seq.Qual) > 0 || ForcelyOutputFastq {
return append(append(append(append([]byte(fmt.Sprintf("@%s\n", record.Name)),
byteutil.WrapByteSlice(record.Seq.Seq, width)...), []byte("\n+\n")...),
byteutil.WrapByteSlice(record.Seq.Qual, width)...), []byte("\n")...)
}
return append(append([]byte(fmt.Sprintf(">%s\n", record.Name)),
byteutil.WrapByteSlice(record.Seq.Seq, width)...), []byte("\n")...)
}
var defaultBytesBufferSize = 1 << 20
// bufferedByteSliceWrapper is for function FormatToWriter
var bufferedByteSliceWrapper *byteutil.BufferedByteSliceWrapper
func init() {
bufferedByteSliceWrapper = byteutil.NewBufferedByteSliceWrapper(1, defaultBytesBufferSize)
}
// FormatToWriter formats and directly writes to writer
func (record *Record) FormatToWriter(outfh *xopen.Writer, width int) {
if len(record.Seq.Qual) > 0 || ForcelyOutputFastq {
outfh.Write([]byte(fmt.Sprintf("@%s\n", record.Name)))
if len(record.Seq.Seq) <= pageSize {
outfh.Write(byteutil.WrapByteSlice(record.Seq.Seq, width))
} else {
text, b := bufferedByteSliceWrapper.Wrap(record.Seq.Seq, width)
outfh.Write(text)
outfh.Flush()
bufferedByteSliceWrapper.Recycle(b)
}
outfh.Write([]byte("\n+\n"))
if len(record.Seq.Qual) <= pageSize {
outfh.Write(byteutil.WrapByteSlice(record.Seq.Qual, width))
} else {
text, b := bufferedByteSliceWrapper.Wrap(record.Seq.Qual, width)
outfh.Write(text)
outfh.Flush()
bufferedByteSliceWrapper.Recycle(b)
}
outfh.Write([]byte("\n"))
return
}
outfh.Write([]byte(fmt.Sprintf(">%s\n", record.Name)))
if len(record.Seq.Seq) <= pageSize {
outfh.Write(byteutil.WrapByteSlice(record.Seq.Seq, width))
} else {
text, b := bufferedByteSliceWrapper.Wrap(record.Seq.Seq, width)
outfh.Write(text)
outfh.Flush()
bufferedByteSliceWrapper.Recycle(b)
}
outfh.Write([]byte("\n"))
}
|