File: records.go

package info (click to toggle)
golang-github-shenwei356-bio 0.13.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 792 kB
  • sloc: perl: 114; sh: 58; makefile: 21
file content (198 lines) | stat: -rwxr-xr-x 4,629 bytes parent folder | download | duplicates (2)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package fastx

import (
	"bytes"
	"fmt"
	"sync"

	"github.com/shenwei356/bio/seq"
	"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 {
	var buf bytes.Buffer

	if len(record.Seq.Qual) > 0 || ForcelyOutputFastq {
		buf.Write(_mark_fastq)
		buf.Write(record.Name)
		buf.Write(_mark_newline)

		buf.Write(record.Seq.Seq)
		buf.Write(_mark_newline_plus_newline)

		buf.Write(record.Seq.Qual)
		buf.Write(_mark_newline)

		return buf.Bytes()
	}

	buf.Write(_mark_fasta)
	buf.Write(record.Name)
	buf.Write(_mark_newline)

	if width < 1 {
		buf.Write(record.Seq.Seq)
	} else {
		var text []byte
		buffer := poolBuffer.Get().(*bytes.Buffer)
		text, buffer = wrapByteSlice(record.Seq.Seq, width, buffer)
		buf.Write(text)
		poolBuffer.Put(buffer)
	}

	buf.Write(_mark_newline)

	return buf.Bytes()
}

// 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(_mark_fastq)
		outfh.Write(record.Name)
		outfh.Write(_mark_newline)

		outfh.Write(record.Seq.Seq)
		outfh.Write(_mark_newline_plus_newline)

		outfh.Write(record.Seq.Qual)
		outfh.Write(_mark_newline)

		return
	}

	outfh.Write(_mark_fasta)
	outfh.Write(record.Name)
	outfh.Write(_mark_newline)

	if width < 1 {
		outfh.Write(record.Seq.Seq)
	} else {
		var text []byte
		buffer := poolBuffer.Get().(*bytes.Buffer)
		text, buffer = wrapByteSlice(record.Seq.Seq, width, buffer)
		outfh.Write(text)
		poolBuffer.Put(buffer)
	}

	outfh.Write(_mark_newline)
}

// It's unsafe for concurrency
// var buffer *bytes.Buffer

var poolBuffer = &sync.Pool{New: func() interface{} {
	return bytes.NewBuffer(make([]byte, 0, 1024))
}}

var _mark_fasta = []byte{'>'}
var _mark_fastq = []byte{'@'}
var _mark_newline_plus_newline = []byte{'\n', '+', '\n'}
var _mark_newline = []byte{'\n'}

func wrapByteSlice(s []byte, width int, buffer *bytes.Buffer) ([]byte, *bytes.Buffer) {
	if width < 1 {
		return s, buffer
	}
	l := len(s)
	if l == 0 {
		return s, buffer
	}

	var lines int
	if l%width == 0 {
		lines = l/width - 1
	} else {
		lines = int(l / width)
	}

	if buffer == nil {
		buffer = bytes.NewBuffer(make([]byte, 0, l+lines))
	} else {
		buffer.Reset()
	}

	var start, end int
	for i := 0; i <= lines; i++ {
		start = i * width
		end = (i + 1) * width
		if end > l {
			end = l
		}

		buffer.Write(s[start:end])
		if i < lines {
			buffer.Write(_mark_newline)
		}
	}
	return buffer.Bytes(), buffer
}