-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfragment.go
293 lines (257 loc) · 7.52 KB
/
fragment.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package hhfrag
import (
"fmt"
"io"
"io/ioutil"
"os"
"path"
"strings"
"text/tabwriter"
"github.com/TuftsBCB/apps/hhsuite"
"github.com/TuftsBCB/io/hhr"
"github.com/TuftsBCB/io/hmm"
"github.com/TuftsBCB/io/pdb"
"github.com/TuftsBCB/seq"
"github.com/TuftsBCB/structure"
)
type PDBDatabase hhsuite.Database
func (db PDBDatabase) HHsuite() hhsuite.Database {
resolved := hhsuite.Database(db).Resolve()
dbName := path.Base(resolved)
return hhsuite.Database(path.Join(resolved, dbName))
}
func (db PDBDatabase) PDB() string {
resolved := hhsuite.Database(db).Resolve()
return path.Join(resolved, "pdb")
}
type Fragments struct {
Frags []Fragment
Start, End int
}
// better returns true if f1 is 'better' than f2. Otherwise false.
func (f1 Fragments) better(f2 Fragments) bool {
return len(f1.Frags) >= len(f2.Frags)
}
func (frags Fragments) Write(w io.Writer) {
tabw := tabwriter.NewWriter(w, 0, 4, 4, ' ', 0)
fmt.Fprintln(tabw, "Hit\tQuery\tTemplate\tProb\tCorrupt")
for _, frag := range frags.Frags {
var corruptStr string
if frag.IsCorrupt() {
corruptStr = "\tcorrupt"
}
fmt.Fprintf(tabw, "%s\t(%d-%d)\t(%d-%d)\t%f%s\n",
frag.Template.Name,
frag.Hit.QueryStart, frag.Hit.QueryEnd,
frag.Hit.TemplateStart, frag.Hit.TemplateEnd,
frag.Hit.Prob,
corruptStr)
}
tabw.Flush()
}
func FindFragments(pdbDb PDBDatabase, blits bool,
queryHHM *hmm.HHM, qs seq.Sequence, start, end int) (*Fragments, error) {
pre := fmt.Sprintf("bcbgo-hhfrag-hhm-%d-%d_", start, end)
hhmFile, err := ioutil.TempFile("", pre)
if err != nil {
return nil, err
}
defer os.Remove(hhmFile.Name())
defer hhmFile.Close()
hhmName := hhmFile.Name()
if err := hmm.WriteHHM(hhmFile, queryHHM.Slice(start, end)); err != nil {
return nil, err
}
var results *hhr.HHR
if blits {
conf := hhsuite.HHBlitsDefault
conf.CPUs = 1
results, err = conf.Run(pdbDb.HHsuite(), hhmName)
} else {
conf := hhsuite.HHSearchDefault
conf.CPUs = 1
results, err = conf.Run(pdbDb.HHsuite(), hhmName)
}
if err != nil {
return nil, err
}
frags := make([]Fragment, 0, len(results.Hits))
for _, hit := range results.Hits {
hit.QueryStart += start
hit.QueryEnd += start
for _, splitted := range splitHit(hit) {
frag, err := NewFragment(pdbDb, qs, splitted)
if err != nil {
return nil, err
}
frags = append(frags, frag)
}
}
return &Fragments{
Frags: frags,
Start: start,
End: end,
}, nil
}
func splitHit(hit hhr.Hit) []hhr.Hit {
splitted := make([]hhr.Hit, 0)
chunks := 0
start := 0
alignLen := len(hit.Aligned.TSeq)
qgaps, tgaps := 0, 0
for i := 0; i < alignLen; i++ {
qr, tr := hit.Aligned.QSeq[i], hit.Aligned.TSeq[i]
if qr == '-' || tr == '-' {
// Skip if this is the first residue or last residue was '-',
// since we haven't accumulated anything.
if i == 0 ||
hit.Aligned.TSeq[i-1] == '-' || hit.Aligned.QSeq[i-1] == '-' {
start = i + 1
if qr == '-' {
qgaps++
}
if tr == '-' {
tgaps++
}
continue
}
piece := splitAt(hit, chunks, start, i, qgaps, tgaps)
splitted = append(splitted, piece)
chunks++
start = i + 1
if qr == '-' {
qgaps++
}
if tr == '-' {
tgaps++
}
}
}
if start < alignLen {
piece := splitAt(hit, chunks, start, alignLen, qgaps, tgaps)
splitted = append(splitted, piece)
}
return splitted
}
func splitAt(hit hhr.Hit, chunkNum, start, end, qgaps, tgaps int) hhr.Hit {
cpy := hit
cpy.Chunk = chunkNum
cpy.NumAlignedCols = end - start
cpy.QueryStart = cpy.QueryStart + start - qgaps
cpy.QueryEnd = cpy.QueryStart + cpy.NumAlignedCols - 1
cpy.TemplateStart = cpy.TemplateStart + start - tgaps
cpy.TemplateEnd = cpy.TemplateStart + cpy.NumAlignedCols - 1
cpy.Aligned.QSeq = cpy.Aligned.QSeq[start:end]
cpy.Aligned.QConsensus = cpy.Aligned.QConsensus[start:end]
if len(cpy.Aligned.QDssp) > 0 {
cpy.Aligned.QDssp = cpy.Aligned.QDssp[start:end]
}
if len(cpy.Aligned.QPred) > 0 {
cpy.Aligned.QPred = cpy.Aligned.QPred[start:end]
}
if len(cpy.Aligned.QConf) > 0 {
cpy.Aligned.QConf = cpy.Aligned.QConf[start:end]
}
cpy.Aligned.TSeq = cpy.Aligned.TSeq[start:end]
cpy.Aligned.TConsensus = cpy.Aligned.TConsensus[start:end]
if len(cpy.Aligned.TDssp) > 0 {
cpy.Aligned.TDssp = cpy.Aligned.TDssp[start:end]
}
if len(cpy.Aligned.TPred) > 0 {
cpy.Aligned.TPred = cpy.Aligned.TPred[start:end]
}
if len(cpy.Aligned.TConf) > 0 {
cpy.Aligned.TConf = cpy.Aligned.TConf[start:end]
}
return cpy
}
// An HHfrag Fragment corresponds to a match between a portion of a query
// HMM and a portion of a template HMM. The former is represented as a slice
// of a regular sequence, where the latter is represented as an hhsuite hit
// and a list of alpha-carbon atoms corresponding to the matched region.
type Fragment struct {
Query seq.Sequence
Template seq.Sequence
Hit hhr.Hit
CaAtoms []structure.Coords
}
// IsCorrupt returns true when a particular fragment could not be paired
// with alpha-carbon positions for every residue in the template strand.
// (This problem stems from the fact that we use SEQRES records for sequence
// information, but not all residues in SEQRES have alpha-carbon ATOM records
// associated with them.)
func (frag Fragment) IsCorrupt() bool {
return frag.CaAtoms == nil
}
// NewFragment constructs a new fragment from a full query sequence and the
// hit from the HHR file.
//
// Since NewFragment requires access to the raw PDB alpha-carbon atoms (and
// the sequence) of the template hit, you'll also need to pass a path to the
// PDB database. (Which is a directory containing a flat list of all
// PDB files used to construct the corresponding hhblits database.) This
// database is usually located inside the 'pdb' directory contained in the
// corresponding hhsuite database. i.e., $HHLIB/data/pdb-select25/pdb
func NewFragment(
pdbDb PDBDatabase, qs seq.Sequence, hit hhr.Hit) (Fragment, error) {
pdbName := getTemplatePdbName(hit.Name)
pdbEntry, err := pdb.ReadPDB(path.Join(
pdbDb.PDB(), fmt.Sprintf("%s.pdb", pdbName)))
if err != nil {
pdbEntry, err = pdb.ReadPDB(path.Join(
pdbDb.PDB(), fmt.Sprintf("%s.ent.gz", pdbName)))
if err != nil {
return Fragment{}, err
}
}
// Load in the sequence from the PDB file using the SEQRES residues.
ts, te := hit.TemplateStart, hit.TemplateEnd
chain := pdbEntry.Chain(pdbName[4])
if chain == nil {
return Fragment{}, fmt.Errorf("Could not find chain '%c' in PDB "+
"entry '%s'.", pdbName[4], pdbEntry.Path)
}
tseq := seq.Sequence{
Name: pdbName,
Residues: make([]seq.Residue, te-ts+1),
}
// We copy here to avoid pinning pdb.Entry objects.
copy(tseq.Residues, chain.Sequence[ts-1:te])
frag := Fragment{
Query: qs.Slice(hit.QueryStart-1, hit.QueryEnd),
Template: tseq,
Hit: hit,
CaAtoms: nil,
}
// We designate "corrupt" if the query/template hit regions are of
// different length. i.e., we don't allow gaps (yet).
// BUG(burntsushi): Fragments with gaps are marked as corrupt.
if hit.QueryEnd-hit.QueryStart != hit.TemplateEnd-hit.TemplateStart {
return frag, nil
}
// We also designate "corrupt" if there are any gaps in our alpha-carbon
// atom list.
atoms := chain.SequenceCaAtomSlice(ts-1, te)
if atoms == nil {
return frag, nil
}
// One again, we copy to avoid pinning memory.
frag.CaAtoms = make([]structure.Coords, len(atoms))
copy(frag.CaAtoms, atoms)
return frag, nil
}
func getTemplatePdbName(hitName string) string {
return strings.SplitN(strings.TrimSpace(hitName), " ", 2)[0]
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func max(a, b int) int {
if a > b {
return a
}
return b
}