matr 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0aefd54bc8a7c57da41e3843bbc8cd9e29885457
4
- data.tar.gz: 3f6a1f167731135a93423bcb076a99259a22a5de
3
+ metadata.gz: 27143ae159138fad54cd2d4c85138c47dbacf90a
4
+ data.tar.gz: d35cebbbe078e2a3689c9f32ba1c093c1befa2ca
5
5
  SHA512:
6
- metadata.gz: 47c0ca490761b79d628b72ba7ae805450844da7ec550284207fe4cea78e497cf239f71faf9ee5de070b319e9137bd869997c3776bd3d764db0ecf5a01f722326
7
- data.tar.gz: 0baeb31d028459fba8ab48a409e3f5666c1c3d4449504b92e32d233b5291acf3288801404d628d36acedccc4657cdebb1a3cbf4000a608bec0d74d7bcfa074ea
6
+ metadata.gz: '08c61835a0633930babf25435a2ee4d96a5f2444003ff1ce1ced4ce426d98eb18459738619007d9d2c5b84cdb0a2586d35ed297061aeb6be8cff6978df57526e'
7
+ data.tar.gz: fbee20edd8a637b072b3f6e5a34964f11131744722e316ad9e59567c276bcd7143a960a50ad6953e123aea855ae763854370f663368ee7f5e3a0afc1a8bd0c32
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- matr (0.0.1)
4
+ matr (0.1.0)
5
5
  trollop (~> 2.1, >= 2.1.3)
6
6
 
7
7
  GEM
data/exe/matr CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  Signal.trap("PIPE", "EXIT")
4
4
 
5
+ require "matr"
6
+
5
7
  require "set"
6
8
  require "trollop"
7
9
 
@@ -10,89 +12,33 @@ opts = Trollop.options do
10
12
 
11
13
  Take a three-column matrix file and ensure that it is symmetric for input into R.
12
14
 
13
- Options:
14
- EOS
15
+ *Note* -- I ALWAYS enforce symmetry of the output.
15
16
 
16
- opt(:infile, "Input file", type: :string)
17
-
18
- opt(:self_score, "Score for self connections", default: 100)
19
- opt(:missing_score, "Score for missing connections", default: 0)
20
- opt(:output_style, "Output style [wide,long]", default: "wide")
21
- end
22
-
23
- infile = opts[:infile]
24
- self_score = opts[:self_score]
25
- missing_score = opts[:missing_score]
26
- output_style = opts[:output_style]
27
-
28
- unless File.exist? infile
29
- abort "FATAL -- infile '#{infile}' does not exist"
30
- end
31
-
32
- unless output_style == "wide" || output_style == "long"
33
- abort "FATAL -- output style must be either wide or long"
34
- end
17
+ Option details
18
+ --------------
35
19
 
36
- graph = Hash.new { |ht, k| ht[k] = Hash.new }
37
- all_keys = Set.new
38
- header = nil
20
+ --self-scores: I will add a self score of this value if one is not already specified.
21
+ --ensure-self-scores: I will ensure all self scores equal the value set in --self-scores even if a self-score is present but set to something else.
39
22
 
40
- File.open(infile, "rt").each_line.with_index do |line, idx|
41
- line.chomp!
23
+ Things I do
24
+ ------------------
42
25
 
43
- if idx.zero?
44
- header = line
45
- else
46
- source, target, score = line.split "\t"
26
+ - Raise error if a source-target pair is specified twice with a different score
27
+ - Raise error if source-target and target-source are both specified and they don't match.
47
28
 
48
- all_keys << source << target
49
-
50
- # If you have duplicates with different scores, that's an error: a
51
- # => b = 10 and a => b = 20
52
- if graph[source].has_key?(target) && graph[source][target] != score
53
- abort "source--target (#{source}--#{target}) was repeated with a different score"
54
- end
55
-
56
- graph[source][target] = score
29
+ Options:
30
+ EOS
57
31
 
58
- # Since you might have a files that specifies a => b = 10, and b
59
- # => a = 10, that would be okay. But if you have a => b = 10, and
60
- # b => a = 5, then that would be an error.
61
- if graph[target].has_key?(source) && graph[target][source] != score
62
- abort "target--source (#{target}--#{source}) is specified seperately from source--target (#{source}--#{target}), BUT there scores do not match. This means your matrix is NOT symmetric."
32
+ opt(:infile, "Input file", type: :string)
63
33
 
64
- end
34
+ opt(:missing_score, "Score for missing connections (unless they are already put in because of symmetry)", default: 0)
65
35
 
66
- graph[target][source] = score
36
+ # opt(:ensure_symmetry, "This flag will ensure that your output will be a symmetric matrix.", default: true)
67
37
 
68
- unless graph[target].has_key? target
69
- graph[target][target] = self_score
70
- end
38
+ opt(:self_score, "Score for self connections", default: 100)
39
+ opt(:ensure_self_scores, "This flag will ensure that self hit scores all match the value of --self-score option", default: true)
71
40
 
72
- unless graph[source].has_key? source
73
- graph[source][source] = self_score
74
- end
75
- end
41
+ opt(:output_style, "Output style [wide,long]", default: "wide")
76
42
  end
77
43
 
78
- if output_style == "long"
79
- puts header
80
-
81
- all_keys.each do |source|
82
- all_keys.each do |target|
83
- puts [source, target, graph[source][target] || missing_score].join "\t"
84
- end
85
- end
86
- else
87
- all_keys = all_keys.sort
88
-
89
- puts ["", all_keys].join "\t"
90
-
91
- all_keys.each do |source|
92
- row = all_keys.map do |target|
93
- graph[source][target] || missing_score
94
- end
95
-
96
- puts [source, row].join "\t"
97
- end
98
- end
44
+ Matr.main opts
@@ -1,5 +1,96 @@
1
1
  require "matr/version"
2
2
 
3
3
  module Matr
4
- # Your code goes here...
4
+ def self.default_opt opts, key, default
5
+ opts.has_key?(key) ? opts[key] : default
6
+ end
7
+
8
+ def self.main opts
9
+
10
+ infile = opts[:infile]
11
+ self_score = self.default_opt opts, :self_score, 100
12
+ missing_score = self.default_opt opts, :missing_score, 0
13
+ output_style = self.default_opt opts, :output_style, "wide"
14
+ ensure_symmetry = true # self.default_opt opts, :ensure_symmetry, true
15
+ ensure_self_scores = self.default_opt opts, :ensure_self_scores, true
16
+
17
+ unless File.exist? infile
18
+ raise StandardError, "FATAL -- infile '#{infile}' does not exist"
19
+ end
20
+
21
+ unless output_style == "wide" || output_style == "long"
22
+ raise StandardError, "FATAL -- output style must be either wide or long"
23
+ end
24
+
25
+ graph = Hash.new { |ht, k| ht[k] = Hash.new }
26
+ all_keys = Set.new
27
+ header = nil
28
+
29
+ File.open(infile, "rt").each_line.with_index do |line, idx|
30
+ line.chomp!
31
+
32
+ if idx.zero?
33
+ header = line
34
+ else
35
+ source, target, score = line.split "\t"
36
+
37
+ all_keys << source << target
38
+
39
+ # Fix self score if the option is there.
40
+ if source == target && ensure_self_scores
41
+ score = self_score
42
+ end
43
+
44
+ # If you have duplicates with different scores, that's an error: a
45
+ # => b = 10 and a => b = 20
46
+ if graph[source].has_key?(target) && graph[source][target] != score
47
+ raise StandardError, "FATAL -- source--target (#{source}--#{target}) was repeated with a different score"
48
+ end
49
+
50
+ graph[source][target] = score
51
+
52
+ if ensure_symmetry
53
+ # Since you might have a files that specifies a => b = 10, and b
54
+ # => a = 10, that would be okay. But if you have a => b = 10, and
55
+ # b => a = 5, then that would be an error.
56
+ if graph[target].has_key?(source) && graph[target][source] != score
57
+ raise StandardError, "FATAL -- target--source (#{target}--#{source}) is specified seperately from source--target (#{source}--#{target}), BUT their scores do not match. This means your matrix is NOT symmetric. Did you really mean to pass the --ensure-symmetry flag?"
58
+ end
59
+
60
+ graph[target][source] = score
61
+ end
62
+
63
+ # Makes sure these weren't already specified earlier.
64
+ unless graph[target].has_key? target
65
+ graph[target][target] = self_score
66
+ end
67
+
68
+ unless graph[source].has_key? source
69
+ graph[source][source] = self_score
70
+ end
71
+ end
72
+ end
73
+
74
+ if output_style == "long"
75
+ puts header
76
+
77
+ all_keys.each do |source|
78
+ all_keys.each do |target|
79
+ puts [source, target, graph[source][target] || missing_score].join "\t"
80
+ end
81
+ end
82
+ else
83
+ all_keys = all_keys.sort
84
+
85
+ puts ["", all_keys].join "\t"
86
+
87
+ all_keys.each do |source|
88
+ row = all_keys.map do |target|
89
+ graph[source][target] || missing_score
90
+ end
91
+
92
+ puts [source, row].join "\t"
93
+ end
94
+ end
95
+ end
5
96
  end
@@ -1,3 +1,3 @@
1
1
  module Matr
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: matr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Moore
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-08-23 00:00:00.000000000 Z
11
+ date: 2018-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: trollop