-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfragmap.go
183 lines (159 loc) · 3.73 KB
/
fragmap.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
package hhfrag
import (
"fmt"
"os"
"runtime"
"sort"
"sync"
"github.com/TuftsBCB/apps/hhsuite"
"github.com/TuftsBCB/fragbag"
"github.com/TuftsBCB/fragbag/bow"
"github.com/TuftsBCB/io/fasta"
"github.com/TuftsBCB/io/hmm"
"github.com/TuftsBCB/seq"
)
type MapConfig struct {
WindowMin int
WindowMax int
WindowIncrement int
Blits bool
}
var DefaultConfig = MapConfig{
WindowMin: 30,
WindowMax: 35,
WindowIncrement: 5,
Blits: true,
}
func getOneFastaSequence(queryFasta string) (s seq.Sequence, err error) {
fquery, err := os.Open(queryFasta)
if err != nil {
return
}
defer fquery.Close()
seqs, err := fasta.NewReader(fquery).ReadAll()
if err != nil {
return
} else if len(seqs) == 0 {
err = fmt.Errorf("No sequences found in '%s'.", queryFasta)
return
} else if len(seqs) > 1 {
err = fmt.Errorf("%d sequences found in '%s'. Expected only 1.",
len(seqs), queryFasta)
return
}
s = seqs[0]
return
}
func (m MapConfig) MapFromFasta(pdbDb PDBDatabase, seqDb hhsuite.Database,
queryFasta string) (*FragmentMap, error) {
qseq, err := getOneFastaSequence(queryFasta)
if err != nil {
return nil, err
}
queryHHM, err := hhsuite.BuildHHM(
hhsuite.HHBlitsDefault, hhsuite.HHMakePseudo, seqDb, queryFasta)
if err != nil {
return nil, err
}
return m.computeMap(pdbDb, qseq, queryHHM)
}
func (m MapConfig) MapFromHHM(pdbDb PDBDatabase, seqDb hhsuite.Database,
queryFasta string, queryHHM string) (*FragmentMap, error) {
qseq, err := getOneFastaSequence(queryFasta)
if err != nil {
return nil, err
}
fquery, err := os.Open(queryHHM)
if err != nil {
return nil, err
}
defer fquery.Close()
qhhm, err := hmm.ReadHHM(fquery)
if err != nil {
return nil, err
}
return m.computeMap(pdbDb, qseq, qhhm)
}
func (m MapConfig) computeMap(
pdbDb PDBDatabase, qseq seq.Sequence, qhhm *hmm.HHM) (*FragmentMap, error) {
type maybeFrag struct {
frags Fragments
err error
}
wg := new(sync.WaitGroup)
jobs := make(chan int, 10)
fragsChan := make(chan maybeFrag, 10)
workers := runtime.GOMAXPROCS(0)
if workers < 1 {
workers = 1
}
for i := 0; i < workers; i++ {
go func() {
wg.Add(1)
defer wg.Done()
min, max := m.WindowMin, m.WindowMax
CHANNEL:
for start := range jobs {
var best *Fragments
for end := min; end <= max && (start+end) <= qseq.Len(); end++ {
frags, err := FindFragments(
pdbDb, m.Blits, qhhm, qseq, start, start+end)
if err != nil {
fragsChan <- maybeFrag{
err: err,
}
continue CHANNEL
}
if best == nil || frags.better(*best) {
best = frags
}
}
fragsChan <- maybeFrag{
frags: *best,
}
}
}()
}
go func() {
for s := 0; s <= qseq.Len()-m.WindowMin; s += m.WindowIncrement {
jobs <- s
}
close(jobs)
wg.Wait()
close(fragsChan)
}()
fmap := &FragmentMap{
Name: qseq.Name,
Segments: make([]Fragments, 0, 50),
}
for maybeFrag := range fragsChan {
if maybeFrag.err != nil {
return nil, maybeFrag.err
}
fmap.Segments = append(fmap.Segments, maybeFrag.frags)
}
sort.Sort(fmap)
return fmap, nil
}
type FragmentMap struct {
Name string
Segments []Fragments
}
func (fmap *FragmentMap) Len() int {
return len(fmap.Segments)
}
func (fmap *FragmentMap) Less(i, j int) bool {
return fmap.Segments[i].Start < fmap.Segments[j].Start
}
func (fmap *FragmentMap) Swap(i, j int) {
fmap.Segments[i], fmap.Segments[j] = fmap.Segments[j], fmap.Segments[i]
}
func (fmap *FragmentMap) StructureBow(lib fragbag.StructureLibrary) bow.Bowed {
bag := bow.NewBow(lib.Size())
for _, fragGroup := range fmap.Segments {
for _, frag := range fragGroup.Frags {
bag = bag.Add(bow.StructureBow(lib, frag.CaAtoms))
}
}
return bow.Bowed{Id: fmap.Name, Bow: bag}
}