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
|
Author: Nilesh Patra <npatra974@gmil.com>
Description: Two of the files use "gonum.org/v1/gonum/stat" packaging this would mean packaging entire "https://github.com/gonum" which is a relatively complex package with many functionalities embedded inside it
This effort is not justified for just two minor changes. Hence, patched the files in a way that it uses the source code of the functions from missing package directly.
Last-Changed: September 16, 2020
Forwarded: Not-Needed
--- a/cluster_gff/consensus.go
+++ b/cluster_gff/consensus.go
@@ -3,10 +3,67 @@
import (
"fmt"
"github.com/biogo/biogo/feat/gene"
- "gonum.org/v1/gonum/stat"
"sort"
+ "math"
)
+func Sumfloats(s []float64) float64 {
+ var sum float64
+ for _, val := range s {
+ sum += val
+ }
+ return sum
+}
+
+func floatsHasNaN(s []float64) bool {
+ for _, v := range s {
+ if math.IsNaN(v) {
+ return true
+ }
+ }
+ return false
+}
+
+func empiricalQuantile(p float64, x, weights []float64, sumWeights float64) float64 {
+ var cumsum float64
+ fidx := p * sumWeights
+ for i := range x {
+ if weights == nil {
+ cumsum++
+ } else {
+ cumsum += weights[i]
+ }
+ if cumsum >= fidx {
+ return x[i]
+ }
+ }
+ panic("impossible")
+}
+
+func Quantile(p float64, x, weights []float64) float64 {
+ if !(p >= 0 && p <= 1) {
+ panic("stat: percentile out of bounds")
+ }
+
+ if weights != nil && len(x) != len(weights) {
+ panic("stat: slice length mismatch")
+ }
+ if floatsHasNaN(x) {
+ return math.NaN() // This is needed because the algorithm breaks otherwise.
+ }
+ if !sort.Float64sAreSorted(x) {
+ panic("x data are not sorted")
+ }
+
+ var sumWeights float64
+ if weights == nil {
+ sumWeights = float64(len(x))
+ } else {
+ sumWeights = Sumfloats(weights)
+ }
+ return empiricalQuantile(p, x, weights, sumWeights)
+}
+
// Generate consensus of transcript cluster by taking medians of exon boundaries.
func MedianClusterConsensus(cluster *TranscriptCluster) *gene.CodingTranscript {
@@ -37,8 +94,8 @@
sort.Float64s(exonEnds)
// Calculate median boundaries:
- medianStart := stat.Quantile(0.5, stat.Empirical, exonStarts, nil)
- medianEnd := stat.Quantile(0.5, stat.Empirical, exonEnds, nil)
+ medianStart := Quantile(0.5, exonStarts, nil)
+ medianEnd := Quantile(0.5, exonEnds, nil)
consExonStarts[i] = int(medianStart)
consExonEnds[i] = int(medianEnd)
--- a/polish_clusters/polish.go
+++ b/polish_clusters/polish.go
@@ -2,7 +2,6 @@
import (
"fmt"
- "gonum.org/v1/gonum/stat"
"io/ioutil"
"math"
"os"
@@ -10,6 +9,64 @@
"sort"
)
+func Sumfloats(s []float64) float64 {
+ var sum float64
+ for _, val := range s {
+ sum += val
+ }
+ return sum
+}
+
+func floatsHasNaN(s []float64) bool {
+ for _, v := range s {
+ if math.IsNaN(v) {
+ return true
+ }
+ }
+ return false
+}
+
+func empiricalQuantile(p float64, x, weights []float64, sumWeights float64) float64 {
+ var cumsum float64
+ fidx := p * sumWeights
+ for i := range x {
+ if weights == nil {
+ cumsum++
+ } else {
+ cumsum += weights[i]
+ }
+ if cumsum >= fidx {
+ return x[i]
+ }
+ }
+ panic("impossible")
+}
+
+func Quantile(p float64, x, weights []float64) float64 {
+ if !(p >= 0 && p <= 1) {
+ panic("stat: percentile out of bounds")
+ }
+
+ if weights != nil && len(x) != len(weights) {
+ panic("stat: slice length mismatch")
+ }
+ if floatsHasNaN(x) {
+ return math.NaN() // This is needed because the algorithm breaks otherwise.
+ }
+ if !sort.Float64sAreSorted(x) {
+ panic("x data are not sorted")
+ }
+
+ var sumWeights float64
+ if weights == nil {
+ sumWeights = float64(len(x))
+ } else {
+ sumWeights = Sumfloats(weights)
+ }
+ return empiricalQuantile(p, x, weights, sumWeights)
+}
+
+
// Polish cluster using minimap2 and racon.
func PolishCluster(clusterId string, reads []*Seq, outChan chan *Seq, tempRoot string, threads int, minimapParams, raconParams string) {
// Set up working space:
@@ -119,7 +176,7 @@
lengths[i] = float64(len(seq.Seq))
}
sort.Float64s(lengths)
- medianLength := stat.Quantile(0.5, stat.Empirical, lengths, nil)
+ medianLength := Quantile(0.5, lengths, nil)
i := sort.SearchFloat64s(lengths, medianLength)
//fmt.Println(lengths[i], len(segments))
|