-
Notifications
You must be signed in to change notification settings - Fork 1
/
genes.go
132 lines (110 loc) · 3.66 KB
/
genes.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
/* Copyright (C) 2016 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 gonetics
/* -------------------------------------------------------------------------- */
//import "fmt"
/* -------------------------------------------------------------------------- */
// Container for genes. Tx contains the transcription start and end
// positions. Cds specifies the coding regions.
type Genes struct {
GRanges
// pointer to some meta columns
Names []string
Cds []Range
index map[string]int
}
/* constructors
* -------------------------------------------------------------------------- */
func newGenes(granges GRanges) Genes {
names := granges.GetMetaStr("names")
if len(names) == 0 {
panic("NewGenes(): names column is missing")
}
cds := granges.GetMetaRange("cds")
if len(cds) == 0 {
panic("NewGenes(): cds column is missing")
}
index := map[string]int{}
for i := 0; i < granges.Length(); i++ {
// check if strand is valid
if granges.Strand[i] != '+' && granges.Strand[i] != '-' {
panic("NewGenes(): Invalid strand!")
}
index[names[i]] = i
}
return Genes{granges, names, cds, index}
}
func NewGenes(names, seqnames []string, txFrom, txTo, cdsFrom, cdsTo []int, strand []byte) Genes {
granges := NewGRanges(seqnames, txFrom, txTo, strand)
// construct cds ranges
cds := make([]Range, granges.Length())
for i := 0; i < granges.Length(); i++ {
cds[i] = NewRange(cdsFrom[i], cdsTo[i])
}
granges.AddMeta("names", names)
granges.AddMeta("cds", cds)
return newGenes(granges)
}
func (g *Genes) Clone() Genes {
return newGenes(g.GRanges.Clone())
}
/* -------------------------------------------------------------------------- */
func (obj Genes) Remove(indices []int) Genes {
r := obj.GRanges.Remove(indices)
return newGenes(r)
}
func (obj Genes) RemoveOverlapsWith(subject Genes) Genes {
r := obj.GRanges.RemoveOverlapsWith(subject.GRanges)
return newGenes(r)
}
func (obj Genes) KeepOverlapsWith(subject Genes) Genes {
r := obj.GRanges.KeepOverlapsWith(subject.GRanges)
return newGenes(r)
}
func (obj Genes) Subset(indices []int) Genes {
r := obj.GRanges.Subset(indices)
return newGenes(r)
}
func (obj Genes) Slice(ifrom, ito int) Genes {
r := obj.GRanges.Slice(ifrom, ito)
return newGenes(r)
}
func (obj Genes) Intersection(subject Genes) Genes {
r := obj.GRanges.Intersection(subject.GRanges)
return newGenes(r)
}
func (obj Genes) Sort(name string, reverse bool) (Genes, error) {
if r, err := obj.GRanges.Sort(name, reverse); err != nil {
return Genes{}, err
} else {
return newGenes(r), nil
}
}
func (obj Genes) FilterGenome(genome Genome) Genes {
r := obj.GRanges.FilterGenome(genome)
return newGenes(r)
}
/* -------------------------------------------------------------------------- */
// Returns the index of a gene.
func (g Genes) FindGene(name string) (int, bool) {
i, ok := g.index[name]
return i, ok
}
/* convert to string
* -------------------------------------------------------------------------- */
func (genes Genes) String() string {
return genes.PrintPretty(10)
}