/
mk_paired.go
90 lines (80 loc) · 2.54 KB
/
mk_paired.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
package main
import (
"flag"
"fmt"
"strings"
"github.com/TuftsBCB/fragbag"
"github.com/TuftsBCB/seq"
"github.com/TuftsBCB/structure"
"github.com/TuftsBCB/tools/util"
)
var cmdMkPaired = &command{
name: "mk-paired",
positionalUsage: "in-frag-lib out-frag-lib",
shortHelp: "concatenates all fragment pairs",
help: `
The mk-paired command generates a new fragment library from the one given
by concatenating all pairs of fragments and using each concatenation as
a fragment in the new library.
This command will produce a fragment library with N * (N-1) fragments each of
M*2 size, where N is the number of fragments in the input library and M is
the fragment size of the input library.
The 'in-frag-lib' is the source library with which to generate fragment
pairs. The file given is not modified. It must NOT be a weighted fragment
library. (Weights may be added to the paired fragment library with the
mk-weighted command.)
The 'out-frag-lib' is the path to write the new library with fragment pairs.
`,
flags: flag.NewFlagSet("mk-paired", flag.ExitOnError),
run: mkPaired,
addFlags: func(c *command) {
c.setOverwriteFlag()
},
}
func mkPaired(c *command) {
c.assertNArg(2)
in := util.Library(c.flags.Arg(0))
outPath := c.flags.Arg(1)
util.AssertOverwritable(outPath, flagOverwrite)
if _, ok := in.(fragbag.WeightedLibrary); ok {
util.Fatalf("%s is a weighted library (not allowed)", in.Name())
}
name := fmt.Sprintf("paired-%s", in.Name())
if fragbag.IsStructure(in) {
var pairs [][]structure.Coords
lib := in.(fragbag.StructureLibrary)
nfrags := lib.Size()
for i := 0; i < nfrags; i++ {
for j := 0; j < nfrags; j++ {
if i == j {
continue
}
f1, f2 := lib.Atoms(i), lib.Atoms(j)
pairs = append(pairs, append(f1, f2...))
}
}
pairLib, err := fragbag.NewStructureAtoms(name, pairs)
util.Assert(err)
fragbag.Save(util.CreateFile(outPath), pairLib)
} else if strings.Contains(in.Tag(), "hmm") {
var pairs []*seq.HMM
lib := in.(fragbag.SequenceLibrary)
nfrags := lib.Size()
for i := 0; i < nfrags; i++ {
for j := 0; j < nfrags; j++ {
if i == j {
continue
}
f1, f2 := lib.Fragment(i).(*seq.HMM), lib.Fragment(j).(*seq.HMM)
pairs = append(pairs, seq.HMMCat(f1, f2))
}
}
pairLib, err := fragbag.NewSequenceHMM(name, pairs)
util.Assert(err)
fragbag.Save(util.CreateFile(outPath), pairLib)
} else if strings.Contains(in.Tag(), "profile") {
util.Fatalf("Sequence profiles not implemented.")
} else {
util.Fatalf("Unrecognized fragment library: %s", in.Tag())
}
}