2023-03-07 11:12:13 +07:00
|
|
|
package obiformats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2025-01-24 18:09:59 +01:00
|
|
|
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obidefault"
|
2023-11-29 12:14:37 +01:00
|
|
|
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obiseq"
|
|
|
|
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obiutils"
|
2023-03-07 11:12:13 +07:00
|
|
|
)
|
|
|
|
|
2024-11-24 19:33:24 +01:00
|
|
|
func CSVSequenceRecord(sequence *obiseq.BioSequence, opt Options) []string {
|
2023-03-07 11:12:13 +07:00
|
|
|
keys := opt.CSVKeys()
|
|
|
|
record := make([]string, 0, len(keys)+4)
|
|
|
|
|
|
|
|
if opt.CSVId() {
|
|
|
|
record = append(record, sequence.Id())
|
|
|
|
}
|
|
|
|
|
|
|
|
if opt.CSVCount() {
|
|
|
|
record = append(record, fmt.Sprint(sequence.Count()))
|
|
|
|
}
|
|
|
|
|
|
|
|
if opt.CSVTaxon() {
|
|
|
|
taxid := sequence.Taxid()
|
2024-11-14 19:10:23 +01:00
|
|
|
sn, ok := sequence.GetStringAttribute("scientific_name")
|
2023-03-07 11:12:13 +07:00
|
|
|
|
|
|
|
if !ok {
|
2024-11-14 19:10:23 +01:00
|
|
|
sn = opt.CSVNAValue()
|
2023-03-07 11:12:13 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
record = append(record, fmt.Sprint(taxid), fmt.Sprint(sn))
|
|
|
|
}
|
|
|
|
|
|
|
|
if opt.CSVDefinition() {
|
|
|
|
record = append(record, sequence.Definition())
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, key := range opt.CSVKeys() {
|
|
|
|
value, ok := sequence.GetAttribute(key)
|
|
|
|
if !ok {
|
|
|
|
value = opt.CSVNAValue()
|
|
|
|
}
|
|
|
|
|
2023-03-24 10:25:12 +07:00
|
|
|
svalue, _ := obiutils.InterfaceToString(value)
|
2023-03-07 11:12:13 +07:00
|
|
|
record = append(record, svalue)
|
|
|
|
}
|
|
|
|
|
|
|
|
if opt.CSVSequence() {
|
|
|
|
record = append(record, string(sequence.Sequence()))
|
|
|
|
}
|
|
|
|
|
|
|
|
if opt.CSVQuality() {
|
|
|
|
if sequence.HasQualities() {
|
|
|
|
l := sequence.Len()
|
|
|
|
q := sequence.Qualities()
|
|
|
|
ascii := make([]byte, l)
|
2025-01-24 18:09:59 +01:00
|
|
|
quality_shift := obidefault.WriteQualitiesShift()
|
2023-03-07 11:12:13 +07:00
|
|
|
for j := 0; j < l; j++ {
|
|
|
|
ascii[j] = uint8(q[j]) + uint8(quality_shift)
|
|
|
|
}
|
|
|
|
record = append(record, string(ascii))
|
|
|
|
} else {
|
|
|
|
record = append(record, opt.CSVNAValue())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return record
|
|
|
|
}
|