matr 0.1.0 → 0.2.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 (5) hide show
  1. checksums.yaml +4 -4
  2. data/exe/matr +70 -15
  3. data/lib/matr.rb +20 -18
  4. data/lib/matr/version.rb +1 -1
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 27143ae159138fad54cd2d4c85138c47dbacf90a
4
- data.tar.gz: d35cebbbe078e2a3689c9f32ba1c093c1befa2ca
3
+ metadata.gz: 47333ccd6a3ba0eb865f6840aec2068973e5eef8
4
+ data.tar.gz: f3d9ac5d78ddb25604fb701dab4b8c0b0aca650b
5
5
  SHA512:
6
- metadata.gz: '08c61835a0633930babf25435a2ee4d96a5f2444003ff1ce1ced4ce426d98eb18459738619007d9d2c5b84cdb0a2586d35ed297061aeb6be8cff6978df57526e'
7
- data.tar.gz: fbee20edd8a637b072b3f6e5a34964f11131744722e316ad9e59567c276bcd7143a960a50ad6953e123aea855ae763854370f663368ee7f5e3a0afc1a8bd0c32
6
+ metadata.gz: 68b87abc4da6215e12acb57406ce9097fa31b6c96563f289ef939760e5d6fb0eb32d3fd2fed353365f3b963e4823c7c42dcc534fe31b62cddd02c52ae950adf3
7
+ data.tar.gz: 3a896981433e70be9c4d559c64cea412f224cb582eb573a8a5391b785c1371801b81e6e2d5da87594aa2b3d5f645149c837ba9a14d6fb2c353b4dfdb523dfe8d
data/exe/matr CHANGED
@@ -7,38 +7,93 @@ require "matr"
7
7
  require "set"
8
8
  require "trollop"
9
9
 
10
+ mode_info = <<-EOS
11
+ Modes
12
+ -----
13
+
14
+ - All vs. all non-symmetric (ava): I will make a square matrix
15
+ that contains all versus all comparisons for each item
16
+ specified, regardless of the column that the item was in. Any
17
+ comparisons not specified in the input file will get an 'na'
18
+ value (or whatever you tell us to replace it with).
19
+
20
+ - All vs. all symmetric (ava_symmetric): I will also ensure
21
+ symmetry by adding a reverse entry for each row (i.e., source -
22
+ target - score AND target - source - score), even if the reverse
23
+ entry isn't specified explicitly. If this isn't possible, then
24
+ I will raise an error.
25
+ EOS
26
+
10
27
  opts = Trollop.options do
11
28
  banner <<-EOS
12
29
 
13
- Take a three-column matrix file and ensure that it is symmetric for input into R.
30
+ Take a three-column matrix file and do stuff with it.
31
+
14
32
 
15
- *Note* -- I ALWAYS enforce symmetry of the output.
33
+ #{mode_info}
16
34
 
17
35
  Option details
18
36
  --------------
19
37
 
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.
38
+ --self-scores: I will add a self score of this value if one is not
39
+ already specified.
22
40
 
23
- Things I do
24
- ------------------
41
+ --ensure-self-scores: I will ensure all self scores equal the
42
+ value set in --self-scores even if a self-score is present but
43
+ set to something else.
44
+
45
+ --na-replace <value>: If you pass this flag, I will replace any
46
+ 'na' values with this value.
25
47
 
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.
28
48
 
29
49
  Options:
30
50
  EOS
31
51
 
32
- opt(:infile, "Input file", type: :string)
52
+ opt(:infile,
53
+ "Input file",
54
+ type: :string)
55
+
56
+ opt(:mode,
57
+ "Which mode should I run in?",
58
+ default: "ava")
59
+ opt(:print_modes,
60
+ "Print available modes then exit")
61
+
62
+ opt(:na_replace,
63
+ "Replace any 'na' values with this value",
64
+ default: 0)
65
+
66
+ opt(:self_score,
67
+ "Score for self connections",
68
+ default: 100)
69
+ opt(:ensure_self_scores,
70
+ "This flag will ensure that self hit scores all match the " \
71
+ "value of --self-score option")
72
+
73
+ opt(:output_style,
74
+ "Output style [wide,long]",
75
+ default: "wide")
76
+ end
77
+
78
+ if opts[:print_modes]
79
+ help = <<-EOS
33
80
 
34
- opt(:missing_score, "Score for missing connections (unless they are already put in because of symmetry)", default: 0)
81
+ Available modes: ava, ava_symmetric
35
82
 
36
- # opt(:ensure_symmetry, "This flag will ensure that your output will be a symmetric matrix.", default: true)
83
+ #{mode_info}
84
+ EOS
37
85
 
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)
86
+ STDERR.puts help
87
+ exit
88
+ end
40
89
 
41
- opt(:output_style, "Output style [wide,long]", default: "wide")
90
+ unless opts[:infile_given]
91
+ abort "FATAL: --infile is a required argument"
42
92
  end
43
93
 
44
- Matr.main opts
94
+ begin
95
+ Matr.main opts
96
+ rescue StandardError => err
97
+ # We only are meant to throw StandardError
98
+ abort err
99
+ end
@@ -8,12 +8,18 @@ module Matr
8
8
  def self.main opts
9
9
 
10
10
  infile = opts[:infile]
11
+ mode = self.default_opt opts, :mode, "ava"
11
12
  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
13
  ensure_self_scores = self.default_opt opts, :ensure_self_scores, true
16
14
 
15
+ if opts[:na_replace_given]
16
+ missing_score = opts[:na_replace]
17
+ else
18
+ missing_score = "na"
19
+ end
20
+
21
+ output_style = self.default_opt opts, :output_style, "wide"
22
+
17
23
  unless File.exist? infile
18
24
  raise StandardError, "FATAL -- infile '#{infile}' does not exist"
19
25
  end
@@ -36,20 +42,15 @@ module Matr
36
42
 
37
43
  all_keys << source << target
38
44
 
39
- # Fix self score if the option is there.
40
- if source == target && ensure_self_scores
41
- score = self_score
42
- end
43
-
44
45
  # If you have duplicates with different scores, that's an error: a
45
46
  # => b = 10 and a => b = 20
46
47
  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
+ raise StandardError, "FATAL -- source--target (#{source}--#{target}) was repeated with a different score. Old score was #{graph[source][target]}, and the new score was #{score}."
48
49
  end
49
50
 
50
51
  graph[source][target] = score
51
52
 
52
- if ensure_symmetry
53
+ if mode == "ava_symmetric"
53
54
  # Since you might have a files that specifies a => b = 10, and b
54
55
  # => a = 10, that would be okay. But if you have a => b = 10, and
55
56
  # b => a = 5, then that would be an error.
@@ -59,14 +60,17 @@ module Matr
59
60
 
60
61
  graph[target][source] = score
61
62
  end
63
+ end
64
+ end
62
65
 
63
- # Makes sure these weren't already specified earlier.
64
- unless graph[target].has_key? target
65
- graph[target][target] = self_score
66
- end
66
+ all_keys = all_keys.to_a.sort
67
67
 
68
- unless graph[source].has_key? source
69
- graph[source][source] = self_score
68
+ # Zip through and add any self scores that weren't added.
69
+ all_keys.each do |source|
70
+ all_keys.each do |target|
71
+ # With ensure_self_scores, we overwrite the self score even if it was already given. Otherwise, only if it was NOT given.
72
+ if source == target && (ensure_self_scores || !graph[source][target])
73
+ graph[source][target] = self_score
70
74
  end
71
75
  end
72
76
  end
@@ -80,8 +84,6 @@ module Matr
80
84
  end
81
85
  end
82
86
  else
83
- all_keys = all_keys.sort
84
-
85
87
  puts ["", all_keys].join "\t"
86
88
 
87
89
  all_keys.each do |source|
@@ -1,3 +1,3 @@
1
1
  module Matr
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.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.1.0
4
+ version: 0.2.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-24 00:00:00.000000000 Z
11
+ date: 2018-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: trollop