/
bigWigEditChromNames.go
103 lines (89 loc) · 3.07 KB
/
bigWigEditChromNames.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
/* Copyright (C) 2017 Philipp Benner
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
/* -------------------------------------------------------------------------- */
import "fmt"
import "encoding/binary"
import "log"
import "os"
import "regexp"
import "strings"
import "github.com/pborman/getopt"
import . "github.com/pbenner/gonetics"
/* -------------------------------------------------------------------------- */
func editChromNames(filename, regex, repl string, dryRun, verbose bool) {
bwf := new(BbiFile)
{
f, err := os.Open(filename); if err != nil {
log.Fatal(err)
}
// read chromosome list
if err := bwf.Open(f); err != nil {
log.Fatal(err)
}
f.Close()
}
// file pointer for writing
fptr, err := os.OpenFile(filename, os.O_RDWR, 0); if err != nil {
log.Fatal(err)
}
r, err := regexp.Compile(regex); if err != nil {
log.Fatal("invalid regular expression:", err)
}
for i := 0; i < len(bwf.ChromData.Keys); i++ {
if len(bwf.ChromData.Values[i]) != 8 {
log.Fatalf("reading `%s' failed: invalid chromosome list", filename)
}
// read seqname
seqname_old := strings.TrimRight(string(bwf.ChromData.Keys[i]), "\x00")
// apply regular expression
seqname_new := r.ReplaceAllString(seqname_old, repl)
if dryRun {
fmt.Printf("`%s' -> `%s'\n", seqname_old, seqname_new)
} else {
// update ChromData
copy(bwf.ChromData.Keys[i], seqname_new)
// write new seqname
if _, err := fptr.Seek(bwf.ChromData.PtrKeys[i], 0); err != nil {
log.Fatal(err)
}
if err := binary.Write(fptr, bwf.Order, bwf.ChromData.Keys[i]); err != nil {
log.Fatal("writing new seqname failed:", err)
}
}
}
}
func main() {
options := getopt.New()
options.SetProgram(fmt.Sprintf("%s", os.Args[0]))
optDryRun := options. BoolLong("dry-run", 0 , "just print changes and do not edit the file")
optHelp := options. BoolLong("help", 'h', "print help")
optVerbose := options. BoolLong("verbose", 'v', "be verbose")
options.SetParameters("<input.bw> <regex> <replacement>")
options.Parse(os.Args)
if *optHelp {
options.PrintUsage(os.Stdout)
os.Exit(0)
}
if len(options.Args()) != 3 {
options.PrintUsage(os.Stderr)
os.Exit(1)
}
filename := options.Args()[0]
regex := options.Args()[1]
repl := options.Args()[2]
editChromNames(filename, regex, repl, *optDryRun, *optVerbose)
}