/
program.go
178 lines (164 loc) · 3.63 KB
/
program.go
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
// Copyright ©2012 The bíogo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package sam
import (
"bytes"
"fmt"
)
// Program represents a SAM program.
type Program struct {
id int32
uid string
previous string
name string
command string
version string
otherTags []tagPair
}
// NewProgram returns a Program with the given unique ID, name, command,
// previous program ID in the pipeline and version.
func NewProgram(uid, name, command, prev, v string) *Program {
return &Program{
id: -1,
uid: uid,
previous: prev,
name: name,
command: command,
version: v,
}
}
// ID returns the header ID for the Program.
func (p *Program) ID() int {
if p == nil {
return -1
}
return int(p.id)
}
// UID returns the unique program ID for the program.
func (p *Program) UID() string {
if p == nil {
return ""
}
return p.uid
}
// Name returns the program's name.
func (p *Program) Name() string {
if p == nil {
return ""
}
return p.name
}
// Command returns the program's command line.
func (p *Program) Command() string {
if p == nil {
return ""
}
return p.command
}
// Previous returns the unique ID for the previous program in the pipeline.
func (p *Program) Previous() string {
if p == nil {
return ""
}
return p.previous
}
// Version returns the version of the program.
func (p *Program) Version() string {
if p == nil {
return ""
}
return p.version
}
// Clone returns a deep copy of the Program.
func (p *Program) Clone() *Program {
if p == nil {
return nil
}
cp := *p
cp.otherTags = make([]tagPair, len(cp.otherTags))
copy(cp.otherTags, p.otherTags)
cp.id = -1
return &cp
}
// Get returns the string representation of the value associated with the
// given program line tag. If the tag is not present the empty string is returned.
func (p *Program) Get(t Tag) string {
switch t {
case idTag:
return p.UID()
case programNameTag:
return p.Name()
case commandLineTag:
return p.Command()
case previousProgTag:
return p.Previous()
case versionTag:
return p.Version()
}
for _, tp := range p.otherTags {
if t == tp.tag {
return tp.value
}
}
return ""
}
// Set sets the value associated with the given program line tag to the specified
// value. If value is the empty string and the tag may be absent, it is deleted.
func (p *Program) Set(t Tag, value string) error {
switch t {
case idTag:
if value == "" {
return errDupProgram
}
case programNameTag:
p.name = value
case commandLineTag:
p.command = value
case previousProgTag:
p.previous = value
case versionTag:
p.version = value
default:
if value == "" {
for i, tp := range p.otherTags {
if t == tp.tag {
copy(p.otherTags[i:], p.otherTags[i+1:])
p.otherTags = p.otherTags[:len(p.otherTags)-1]
return nil
}
}
} else {
for i, tp := range p.otherTags {
if t == tp.tag {
p.otherTags[i].value = value
return nil
}
}
p.otherTags = append(p.otherTags, tagPair{tag: t, value: value})
}
}
return nil
}
// String returns a string representation of the program according to the
// SAM specification section 1.3,
func (p *Program) String() string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "@PG\tID:%s", p.uid)
if p.name != "" {
fmt.Fprintf(&buf, "\tPN:%s", p.name)
}
if p.command != "" {
fmt.Fprintf(&buf, "\tCL:%s", p.command)
}
if p.previous != "" {
fmt.Fprintf(&buf, "\tPP:%s", p.previous)
}
if p.version != "" {
fmt.Fprintf(&buf, "\tVN:%s", p.version)
}
for _, tp := range p.otherTags {
fmt.Fprintf(&buf, "\t%s:%s", tp.tag, tp.value)
}
return buf.String()
}