pdist 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/pdist.rb +102 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3522e00bb37fc53412c5d1d68482b7dbac91e827
4
+ data.tar.gz: 98d2442ab90f5ccbaa273b8c02a8be4a455447b3
5
+ SHA512:
6
+ metadata.gz: b8f31aa6d19f74fa075a1002ba1fb76bf6420a0075885d77e0334f984a219cbd5b432d16be6e330a7be02cb5b8c15a614e1fc0df8d6e711ac628abac860f78af
7
+ data.tar.gz: 19dade1cf28fa0d889e603995248539e41356bd2b6be53ad3ff201ea4f2fc469660e81e5d482398a82e65aef44b5e7cb44fd7707467d8b05ec37337ce5b26ba2
data/lib/pdist.rb ADDED
@@ -0,0 +1,102 @@
1
+ #encoding: utf-8
2
+ class PDist
3
+
4
+ require 'diff/lcs'
5
+
6
+ def self.distances(original, permutation)
7
+ both, difference_abs = [], []
8
+ both << original.map{|x| permutation.index(x)} # works out the index of original values in permutation
9
+ both << Array(0..(original.length - 1)) # index values that original originally at
10
+ difference = both.transpose.map {|x| x.reduce(:-)} # taking away old position from new position, to find the distance that the frag has moved when re-ordered
11
+ difference.each {|i| difference_abs << i.abs }
12
+ return difference_abs
13
+ end
14
+
15
+ def self.dev_dist(original, permutation)
16
+ s = distances(original, permutation).inject(:+)
17
+ n = permutation.length
18
+ if n % 2 == 0
19
+ score = (2.0 / (n ** 2).to_f) * s
20
+ else
21
+ score = (2.0 / ((n ** 2) - 1).to_f) * s
22
+ end
23
+ return score
24
+ end
25
+
26
+ def self.sq_dev_dist(original, permutation)
27
+ sq_dists = []
28
+ distances(original, permutation).each{|d| sq_dists << d**2}
29
+ s = sq_dists.inject(:+)
30
+ n = permutation.length
31
+ return (3.0 / (n**3 - n).to_f) * s
32
+ end
33
+
34
+ def self.ham_dist(original, permutation)
35
+ x = 0
36
+ hds = [] # hamming distances
37
+ permutation.each do |frag_id|
38
+ if frag_id == original[x]
39
+ hds << 0
40
+ else
41
+ hds << 1
42
+ end
43
+ x+=1
44
+ end
45
+ return hds.inject(:+)
46
+ end
47
+
48
+ def self.gen_ham_dist(original, permutation)
49
+ ham_dist(original, permutation).to_f / permutation.length.to_f # normalize by dividing by the max score, which == number of objects
50
+ end
51
+
52
+ def self.mod_ham_dist(original, permutation)
53
+ pos_1 = permutation.index(original[0]) # position of the first object of original order in permutation
54
+ if original[0] != permutation[0]
55
+ new_a = [permutation[pos_1..-1], permutation[0..pos_1-1]].flatten # re-order the permutation to get first object at front
56
+ else
57
+ new_a = permutation # running the if above for a permutation with 1 at front results in duplicates
58
+ end
59
+ return (ham_dist(original, new_a) + pos_1).to_f / (2 * (permutation.length - 1)).to_f # no need to take away 1 from pos_1 as the index in ruby is position - 1
60
+ end
61
+
62
+ def self.r_dist(original, permutation) # reverse R distance (since higher scores = bad)
63
+ x = 0
64
+ r = []
65
+ n = permutation.length
66
+ (n - 1).times do
67
+ y = permutation.index(original[x])
68
+ if original[x+1] == permutation[y+1]
69
+ r << 0
70
+ else
71
+ r << 1
72
+ end
73
+ x+=1
74
+ end
75
+ return r.inject(:+).to_f / (n - 1).to_f
76
+ end
77
+
78
+ def self.lcs(original, permutation)
79
+ lcs = Diff::LCS.LCS(original, permutation)
80
+ return (permutation.length - lcs.length).to_f / (permutation.length - 1).to_f
81
+ end
82
+
83
+ def self.kendalls_tau(original, permutation)
84
+ n = permutation.length
85
+ x = 0
86
+ kt = []
87
+ n.times do
88
+ y = 0
89
+ n.times do
90
+ if original[x] < original[y] && permutation[x] > permutation[y]
91
+ kt << 1
92
+ else
93
+ kt << 0
94
+ end
95
+ y+=1
96
+ end
97
+ x+=1
98
+ end
99
+ s = kt.inject(:+)
100
+ return 2 * (s.to_f / (n**2 - n).to_f)
101
+ end
102
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pdist
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Edward Chalstrey
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Ruby gem to compare permutations using distance measures/metrics
14
+ email: edwardchalstrey@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/pdist.rb
20
+ homepage: http://rubygems.org/gems/pdist
21
+ licenses:
22
+ - none
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.0.3
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Permutation distance metrics
44
+ test_files: []