tree_clusters 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 814af5b305dc911aa41bb70fc7afd7c5103ca0bc
4
+ data.tar.gz: cefdde1e0149b0ccd30b44ae0ae1e2c3646548c9
5
+ SHA512:
6
+ metadata.gz: 5a60b4ab7d3284f2ba2f81b122771868c6f33d4e5f3c2a8d7b36fab6bb234a123587bd7f6db81e7b5190e0c27c6287a58eb745403d9f5abdad5308e2549f59b4
7
+ data.tar.gz: 6e649c14f41a250e1150e798d9109f1b96f32445dfb33192c5b361f6ea2078b5e785319b18616cbe41c1c243e74b50aebc55b7e31c01e50faaa65be42d56c044
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.0
5
+ - 2.1
6
+ - 2.2
7
+ - 2.3
8
+ - 2.4
9
+ - 2.4.1
10
+ before_install: gem install bundler -v 1.15.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at moorer@udel.edu. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in tree_clusters.gemspec
6
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Ryan Moore
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # TreeClusters
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/tree_clusters.svg)](http://badge.fury.io/rb/tree_clusters) [![Build Status](https://travis-ci.org/mooreryan/tree_clusters.svg?branch=master)](https://travis-ci.org/mooreryan/tree_clusters) [![Coverage Status](https://coveralls.io/repos/mooreryan/tree_clusters/badge.svg)](https://coveralls.io/r/mooreryan/tree_clusters)
4
+
5
+ Wanna do something with every cluster in a Newick tree? So do we!
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'tree_clusters'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install tree_clusters
22
+
23
+ ## Documentation
24
+
25
+ Checkout
26
+ [TreeClusters docs](http://rubydoc.info/gems/tree_clusters)
27
+ for the full api documentation.
28
+
29
+ ## Usage
30
+
31
+ Here is a small example.
32
+
33
+ ```ruby
34
+ # Require the library
35
+ require "tree_clusters"
36
+
37
+ # Make the TreeClusters methods available under the namespace
38
+ # TreeClusters
39
+ TreeClusters.extend TreeClusters
40
+
41
+ # Read in the Newick formatted tree
42
+ tree = NewickTree.fromFile ARGV.first
43
+
44
+ # Iterate through all the clades
45
+ TreeClusters.all_clades(tree).each do |clade|
46
+ # Print out the clade name and the names of all leaves in that clade
47
+ printf "%s\t%s\n", clade.name, clade.all_leaves.join(", ")
48
+ end
49
+ ```
50
+
51
+ ## Development
52
+
53
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
54
+
55
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
56
+
57
+ ## Contributing
58
+
59
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/tree_clusters. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
60
+
61
+ ## Code of Conduct
62
+
63
+ Everyone interacting in the TreeClusters project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/tree_clusters/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "tree_clusters"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,168 @@
1
+ require "abort_if"
2
+ require "Newick"
3
+ require "set"
4
+ require "tree_clusters/version"
5
+
6
+ include AbortIf
7
+ include AbortIf::Assert
8
+
9
+ # Monkey patch of the NewickTree class
10
+ class NewickTree
11
+ # Like NewickTree.clades but returns NewickNodes instead of taxa
12
+ # names.
13
+ #
14
+ # @return [Array<NewickNode>] array of NewickNodes representing the
15
+ # tree clades
16
+ def clade_nodes
17
+ clades = []
18
+ @root.descendants.each do |clade|
19
+ clades.push(clade) if (!clade.children.empty?)
20
+ end
21
+ return clades
22
+ end
23
+ end
24
+
25
+ # Top level namespace of the Gem.
26
+ module TreeClusters
27
+
28
+ # Given a NewickTree, return an array of all Clades in that tree.
29
+ #
30
+ # @param tree [NewickTree] a NewickTree object
31
+ #
32
+ # @return [Array<Clade>] array of Clade objects
33
+ def all_clades tree
34
+ tree.clade_nodes.reverse.map do |node|
35
+ Clade.new node, tree
36
+ end
37
+ end
38
+
39
+ # A Hash table for genome/leaf/taxa attributes
40
+ class Attrs < Hash
41
+
42
+ # Returns the an AttrArray of Sets for the given genomes and
43
+ # attribute.
44
+ #
45
+ # @note If a genome is in the leaves array, but is not in the hash
46
+ # table, NO error will be raised. Rather that genome will be
47
+ # skipped. This is for cases in which not all genomes have
48
+ # attributes.
49
+ #
50
+ # @param leaves [Array<String>] names of the leaves for which you
51
+ # need attributes
52
+ # @param attr [Symbol] the attribute you are interested in eg,
53
+ # :genes
54
+ #
55
+ # @return [AttrArray<Set>] an AttrArray of Sets of
56
+ # attributes
57
+ #
58
+ # @raise [AbortIf::Exit] if they leaf is present but doesn't have
59
+ # the requested attr
60
+ def attrs leaves, attr
61
+ ary = leaves.map do |leaf|
62
+
63
+ if self.has_key? leaf
64
+ abort_unless self[leaf].has_key?(attr),
65
+ "Missing attr #{attr.inspect} for leaf '#{leaf}'"
66
+
67
+ self[leaf][attr]
68
+ else
69
+ nil
70
+ end
71
+ end.compact
72
+
73
+ TreeClusters::AttrArray.new ary
74
+ end
75
+ end
76
+
77
+ # Provides convenience methods for working with Arrays of Sets
78
+ class AttrArray < Object::Array
79
+ # Takes the union of all sets in the AttrArray
80
+ #
81
+ # @return [Set]
82
+ def union
83
+ self.reduce(&:union)
84
+ end
85
+
86
+ # Takes the intersection of all sets in the AttrArray
87
+ #
88
+ # @return [Set]
89
+ def intersection
90
+ self.reduce(&:intersection)
91
+ end
92
+ end
93
+
94
+ # Represents a clade in a NewickTree
95
+ class Clade
96
+ attr_accessor :name,
97
+ :all_leaves,
98
+ :left_leaves,
99
+ :right_leaves,
100
+ :sibling_leaves,
101
+ :parent_leaves,
102
+ :other_leaves
103
+
104
+ # @param node [NewickNode] a NewickNode from a NewickTree
105
+ # @param tree [NewickTree] a NewickTree
106
+ def initialize node, tree
107
+ @name = node.name
108
+ @all_leaves = descendant_leaves node
109
+
110
+ children = node.children
111
+ assert children.count == 2,
112
+ "Not a bifurcating tree (See: #{node.name})"
113
+ lchild, rchild = node.children
114
+
115
+ @left_leaves = descendant_leaves lchild
116
+
117
+ @right_leaves = descendant_leaves rchild
118
+
119
+ siblings = node.siblings
120
+ assert siblings.count == 1,
121
+ "Node #{node.name} has more than one sibling."
122
+ @sibling_leaves = descendant_leaves siblings.first
123
+
124
+ parent = node.parent
125
+ assert parent,
126
+ "Noge #{node.name} has no parent. Is it the root?"
127
+ @parent_leaves = descendant_leaves parent
128
+
129
+ @other_leaves =
130
+ Object::Set.new(tree.taxa) - Object::Set.new(all_leaves)
131
+ end
132
+
133
+ # Compares two Clades field by field.
134
+ #
135
+ # If all instance variables are == than the two clades are == as
136
+ # well.
137
+ def == clade
138
+ (
139
+ self.name == clade.name &&
140
+ self.all_leaves == clade.all_leaves &&
141
+ self.left_leaves == clade.left_leaves &&
142
+ self.right_leaves == clade.right_leaves &&
143
+ self.sibling_leaves == clade.sibling_leaves &&
144
+ self.parent_leaves == clade.parent_leaves &&
145
+ self.other_leaves == clade.other_leaves
146
+ )
147
+ end
148
+
149
+ # Alias for ==
150
+ def eql? clade
151
+ self == clade
152
+ end
153
+
154
+ private
155
+
156
+ def descendant_leaves node
157
+ if node.leaf?
158
+ [node.name]
159
+ else
160
+ node.
161
+ descendants.
162
+ flatten.
163
+ uniq.
164
+ select { |node| node.leaf? }.map(&:name)
165
+ end
166
+ end
167
+ end
168
+ end
@@ -0,0 +1,3 @@
1
+ module TreeClusters
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,15 @@
1
+ # Require the library
2
+ require "tree_clusters"
3
+
4
+ # Make the TreeClusters methods available under the namespace
5
+ # TreeClusters
6
+ TreeClusters.extend TreeClusters
7
+
8
+ # Read in the Newick formatted tree
9
+ tree = NewickTree.fromFile ARGV.first
10
+
11
+ # Iterate through all the clades
12
+ TreeClusters.all_clades(tree).each do |clade|
13
+ # Print out the clade name and the names of all leaves in that clade
14
+ printf "%s\t%s\n", clade.name, clade.all_leaves.join(", ")
15
+ end
@@ -0,0 +1 @@
1
+ ((((((g1:1, g2:1)cluster1:1, g3:2)cluster4:1, (g4a:1, g4b:1)cluster7:2)cluster6:1, ((g5:2, g6:2)cluster11:1, g7:3)cluster14:1)cluster10:1, g8:5)cluster16:1, ((g9:1, g10:1)cluster19:1, g11:2)cluster22:4)cluster18:1;
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "tree_clusters/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tree_clusters"
8
+ spec.version = TreeClusters::VERSION
9
+ spec.authors = ["Ryan Moore"]
10
+ spec.email = ["moorer@udel.edu"]
11
+
12
+ spec.summary = %q{Snazzy code for working with each cluster in a tree.}
13
+ spec.description = spec.summary
14
+ spec.homepage = "https://github.com/mooreryan/tree_clusters"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.required_ruby_version = ">= 2"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.15"
26
+ spec.add_development_dependency "coveralls", "~> 0.8.21"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ spec.add_development_dependency "yard", "~> 0.9.9"
30
+
31
+ spec.add_runtime_dependency "abort_if", "~> 0.2.0"
32
+ spec.add_runtime_dependency "newick-ruby", "~> 1.0", ">= 1.0.4"
33
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tree_clusters
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Moore
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-10-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: coveralls
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.8.21
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.8.21
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: yard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.9.9
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.9.9
83
+ - !ruby/object:Gem::Dependency
84
+ name: abort_if
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.2.0
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.2.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: newick-ruby
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.0'
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: 1.0.4
107
+ type: :runtime
108
+ prerelease: false
109
+ version_requirements: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - "~>"
112
+ - !ruby/object:Gem::Version
113
+ version: '1.0'
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: 1.0.4
117
+ description: Snazzy code for working with each cluster in a tree.
118
+ email:
119
+ - moorer@udel.edu
120
+ executables: []
121
+ extensions: []
122
+ extra_rdoc_files: []
123
+ files:
124
+ - ".gitignore"
125
+ - ".rspec"
126
+ - ".travis.yml"
127
+ - CODE_OF_CONDUCT.md
128
+ - Gemfile
129
+ - LICENSE
130
+ - README.md
131
+ - Rakefile
132
+ - bin/console
133
+ - bin/setup
134
+ - lib/tree_clusters.rb
135
+ - lib/tree_clusters/version.rb
136
+ - test_files/test.rb
137
+ - test_files/test.tre
138
+ - tree_clusters.gemspec
139
+ homepage: https://github.com/mooreryan/tree_clusters
140
+ licenses: []
141
+ metadata: {}
142
+ post_install_message:
143
+ rdoc_options: []
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '2'
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubyforge_project:
158
+ rubygems_version: 2.6.14
159
+ signing_key:
160
+ specification_version: 4
161
+ summary: Snazzy code for working with each cluster in a tree.
162
+ test_files: []