-
Notifications
You must be signed in to change notification settings - Fork 12
/
build.go
187 lines (171 loc) · 4.48 KB
/
build.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
/**
* Filename: /Users/htang/code/allhic/build.go
* Path: /Users/htang/code/allhic
* Created Date: Saturday, January 27th 2018, 10:21:08 pm
* Author: htang
*
* Copyright (c) 2018 Haibao Tang
*/
package allhic
import (
"bufio"
"fmt"
"io"
"os"
"strings"
"github.com/shenwei356/bio/seq"
"github.com/shenwei356/bio/seqio/fastx"
)
// Builder reconstructs the genome release AGP and FASTA files
type Builder struct {
Tourfiles []string
Fastafile string
// Output file
OutAGPfile string
OutFastafile string
}
// OOLine describes a simple contig entry in a scaffolding experiment
type OOLine struct {
id string
componentID string
componentSize int
strand byte
}
// OO describes a scaffolding experiment and contains an array of OOLine
type OO struct {
seqs map[string]*seq.Seq
entries []OOLine
}
// getFastaSizes returns a dictionary of contig sizes
func (r *OO) getFastaSizes(fastafile string) {
log.Noticef("Parse FASTA file `%s`", fastafile)
reader, _ := fastx.NewDefaultReader(fastafile)
seq.ValidateSeq = false
r.seqs = map[string]*seq.Seq{}
for {
rec, err := reader.Read()
if err == io.EOF || rec == nil {
break
}
name := string(rec.Name)
r.seqs[name] = rec.Seq.Clone()
}
}
// Add instantiates a new OOLine object and add to the array in OO
func (r *OO) Add(scaffold, ctg string, ctgsize int, strand byte) {
o := OOLine{scaffold, ctg, ctgsize, strand}
r.entries = append(r.entries, o)
}
// writeAGP converts the simplistic OOLine into AGP format
func (r *Builder) writeAGP(oo *OO, gapSize int) {
r.OutAGPfile = RemoveExt(r.OutFastafile) + ".agp"
gapType := "scaffold"
linkage := "yes"
evidence := "map"
prevObject := ""
objectBeg := 1
objectEnd := 1
partNumber := 0
componentType := 'W'
f, _ := os.Create(r.OutAGPfile)
w := bufio.NewWriter(f)
components := 0
// Write AGP for each object group
for _, line := range oo.entries {
if line.id != prevObject {
prevObject = line.id
objectBeg = 1
partNumber = 0
}
if partNumber > 0 && gapSize > 0 {
if gapSize == 100 {
componentType = 'U'
} else {
componentType = 'N'
}
objectEnd = objectBeg + gapSize - 1
partNumber++
_, _ = fmt.Fprintf(w, "%s\t%d\t%d\t%d\t%c\t%d\t%s\t%s\t%s\n",
line.id, objectBeg, objectEnd, partNumber,
componentType, gapSize, gapType, linkage, evidence)
objectBeg += gapSize
}
objectEnd = objectBeg + line.componentSize - 1
partNumber++
_, _ = fmt.Fprintf(w, "%s\t%d\t%d\t%d\t%c\t%s\t%d\t%d\t%c\n",
line.id, objectBeg, objectEnd, partNumber,
'W', line.componentID, 1, line.componentSize, line.strand)
objectBeg += line.componentSize
components++
}
_ = w.Flush()
log.Noticef("A total of %d tigs written to `%s`", components, r.OutAGPfile)
_ = f.Close()
}
// Run kicks off the Build and constructs molecule using component FASTA sequence
func (r *Builder) Run() {
oo := new(OO)
oo.getFastaSizes(r.Fastafile)
// oo.parseLastTour(r.Tourfile)
oo.mergeTours(r.Tourfiles)
r.writeAGP(oo, 100)
buildFasta(r.OutAGPfile, oo.seqs)
log.Notice("Success")
}
// mergeTours merges a number of tours typically generated by partition and optimize
// In contrast to parseLastTour which only parse one tour
func (r *OO) mergeTours(tourfiles []string) {
for i, tourfile := range tourfiles {
seqid := fmt.Sprintf("g%d", i+1)
log.Noticef("Import `%s` => %s", tourfile, seqid)
r.parseLastTour(tourfile, seqid)
}
}
// parseLastTour reads tour from file
//
// A tour file has the following format:
// > name
// contig1+ contig2- contig3?
func (r *OO) parseLastTour(tourfile string, seqid string) {
words := parseTourFile(tourfile)
var strand byte
for _, tig := range words {
at, ao := tig[:len(tig)-1], tig[len(tig)-1]
if ao == '+' || ao == '-' || ao == '?' {
tig, strand = at, ao
} else {
strand = '?'
}
r.Add(seqid, tig, r.seqs[tig].Length(), strand)
}
}
// ParseAllTours reads tour from file
//
// A tour file has the following format:
// > name
// contig1+ contig2- contig3?
func (r *OO) ParseAllTours(tourfile string) {
log.Noticef("Parse tourfile `%s`", tourfile)
file := mustOpen(tourfile)
scanner := bufio.NewScanner(file)
var (
name string
strand byte
)
for scanner.Scan() {
words := strings.Fields(scanner.Text())
if words[0][0] == '>' {
name = words[0][1:]
continue
}
for _, tig := range words {
at, ao := tig[:len(tig)-1], tig[len(tig)-1]
if ao == '+' || ao == '-' || ao == '?' {
tig, strand = at, ao
} else {
strand = '?'
}
r.Add(name, tig, r.seqs[tig].Length(), strand)
}
}
}