rphylip 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "rdoc", "~> 3.12"
11
+ gem "bundler", "~> 1.0.0"
12
+ gem "jeweler", "~> 1.8.3"
13
+ gem "rcov", ">= 0"
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,29 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.8.3)
6
+ bundler (~> 1.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rdoc
10
+ json (1.6.6)
11
+ rake (0.9.2.2)
12
+ rcov (1.0.0)
13
+ rdoc (3.12)
14
+ json (~> 1.4)
15
+ shoulda (3.0.1)
16
+ shoulda-context (~> 1.0.0)
17
+ shoulda-matchers (~> 1.0.0)
18
+ shoulda-context (1.0.0)
19
+ shoulda-matchers (1.0.0)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ bundler (~> 1.0.0)
26
+ jeweler (~> 1.8.3)
27
+ rcov
28
+ rdoc (~> 3.12)
29
+ shoulda
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Fernando Izquierdo-Carrasco
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = rphylip
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to rphylip
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2012 Fernando Izquierdo-Carrasco. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "rphylip"
18
+ gem.homepage = "http://github.com/fizquierdo/rphylip"
19
+ gem.license = "MIT"
20
+ gem.summary = "Helper functions to handle phylogenetic phylip formatted alignments"
21
+ gem.description = "Build fake phylip alignments, extract names form a given alignment"
22
+ gem.email = "fer.izquierdo@gmail.com"
23
+ gem.authors = ["Fernando Izquierdo-Carrasco"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ require 'rcov/rcovtask'
36
+ Rcov::RcovTask.new do |test|
37
+ test.libs << 'test'
38
+ test.pattern = 'test/**/test_*.rb'
39
+ test.verbose = true
40
+ test.rcov_opts << '--exclude "gems/*"'
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rdoc/task'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "rphylip #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/rphylip.rb ADDED
@@ -0,0 +1,130 @@
1
+ #!/usr/bin/env ruby
2
+ def fisher_yates_shuffle(a)
3
+ (a.size-1).downto(1) do |i|
4
+ j = rand(i+1)
5
+ a[i], a[j] = a[j], a[i] if i != j
6
+ end
7
+ end
8
+ def pseudonormal(x,y)
9
+ num = 0
10
+ reps = 4
11
+ reps.times do
12
+ num += rand(y - x + 1) + x
13
+ end
14
+ num / reps
15
+ end
16
+
17
+ class Phylip
18
+ attr_reader :numtaxa, :seqlen, :seqs
19
+ def initialize(phylipfile)
20
+ raise "File #{phylipfile} does not exist" unless File.exists?(phylipfile)
21
+ @filename = phylipfile
22
+ @seqs = File.open(phylipfile).readlines
23
+ @numtaxa, @seqlen = @seqs[0].split.map{|w| w.to_i}
24
+ @seqs.delete_at(0)
25
+ @seqs.delete_if{|l| l=~ /^\s+$/}
26
+ raise "wrong number of seqs,parsed #{@seqs.size} expected ntaxa #{@numtaxa}" unless @seqs.size == @numtaxa
27
+ end
28
+ def names
29
+ ali_names = []
30
+ @seqs.each do |seq|
31
+ ali_names << seq.split.first.strip
32
+ end
33
+ raise "unexpected #names" unless ali_names.size == @numtaxa
34
+ ali_names
35
+ end
36
+ def generate_base_alignment(numseqs_base)
37
+ seqs = @seqs[0...numseqs_base].select{|s| s.split.last.split(//).uniq.size > 1}
38
+ basename = @filename + "_initial"
39
+ puts "Saving base alignment as #{basename} with #{seqs.size} seqs"
40
+ raise "too few seqs" unless seqs.size > 4
41
+ self.save_seqs_as(seqs, basename)
42
+ end
43
+ def generate_update(seqs, update_filename)
44
+ # remove from seqs single char stuff
45
+ seqs = seqs.select{|s| s.split.last.split(//).uniq.size > 1}
46
+ puts "Saving update alignment as #{update_filename} with #{seqs.size} seqs"
47
+ raise "too few seqs" unless seqs.size > 4
48
+ self.save_seqs_as(seqs, update_filename)
49
+ end
50
+ def subdivide(numseqs_base, num_subalignments)
51
+ # Subdivides the present alignemnt in a file base.phy and num_subalignments (i) subi.phy
52
+ # iterarions generated are of equal size
53
+ if numseqs_base.to_i + num_subalignments.to_i > @numtaxa then
54
+ raise "Wrong input to subdivide phylip file"
55
+ else
56
+ fisher_yates_shuffle(@seqs)
57
+ generate_base_alignment(numseqs_base)
58
+ # Generaute pseudo-new subsets of sequences
59
+ rest = @seqs[numseqs_base...@seqs.size]
60
+ subset_size = rest.size / num_subalignments
61
+ update_sequences = []
62
+ rest.each_slice(subset_size) do |subset|
63
+ # Note the last slice may be shorter unless rest.size % num_subalignments == 0
64
+ if update_sequences.size < num_subalignments
65
+ update_sequences << subset # a new subset
66
+ else
67
+ update_sequences[num_subalignments - 1] += subset # appends to the last subset
68
+ end
69
+ end
70
+ update_sequences.each_with_index do |seqs, i|
71
+ generate_update(seqs, @filename + "_sequpdate_#{i}.phy")
72
+ end
73
+ end
74
+ return update_sequences.size
75
+ end
76
+ def subdivide_random(conf)
77
+ numseqs_base = conf[:initial_seqs]
78
+ raise "too many seqs" if numseqs_base > @numtaxa
79
+ fisher_yates_shuffle(@seqs)
80
+ generate_base_alignment(numseqs_base)
81
+ n = numseqs_base
82
+ i = 0 #iteration id
83
+ srand(12345) # be deterministic
84
+ while n < @seqs.size
85
+ num_newseqs = pseudonormal(conf[:min_size_update], conf[:max_size_update])
86
+ num_newseqs = @seqs.size - n if (@seqs.size - n - num_newseqs < conf[:min_size_update])
87
+ if conf[:updates_as_full_alignments].nil? or not conf[:updates_as_full_alignments]
88
+ start = n
89
+ else
90
+ start = 0
91
+ end
92
+ generate_update(@seqs[start...n+num_newseqs], @filename + "_sequpdate_#{i}.phy")
93
+ n += num_newseqs
94
+ i += 1
95
+ end
96
+ i
97
+ end
98
+ def save_as(newfile)
99
+ self.save_seqs_as(@seqs, newfile)
100
+ end
101
+ def save_seqs_as(seqs, newfile)
102
+ File.open(newfile, "w") do |f|
103
+ f.puts "#{seqs.size} #{@seqlen}"
104
+ seqs.each{|seq| f.puts seq}
105
+ end
106
+ end
107
+ def expand_with(phylipfile)
108
+ additional_phylip = Phylip.new(phylipfile)
109
+ if additional_phylip.seqlen == self.seqlen then
110
+ additional_phylip.seqs.each do |newseq|
111
+ @seqs << newseq
112
+ @numtaxa += 1
113
+ end
114
+ else
115
+ raise "different sequence lengths for new #{phylipfile}, cannot be expanded"
116
+ end
117
+ end
118
+ def extract_partition(from_pos, to_pos)
119
+ @seqlen = to_pos - from_pos + 1
120
+ newseqs = []
121
+ from = from_pos - 1
122
+ to = to_pos - 1
123
+ @seqs.each do |seq|
124
+ name, info = seq.split
125
+ newseqs << name + " " + info.slice!(from..to)
126
+ end
127
+ @seqs = newseqs
128
+ self.save_as(@filename + "_from#{from_pos}_to#{to_pos}")
129
+ end
130
+ end
data/rphylip.gemspec ADDED
@@ -0,0 +1,62 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{rphylip}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = [%q{Fernando Izquierdo-Carrasco}]
12
+ s.date = %q{2012-03-27}
13
+ s.description = %q{Build fake phylip alignments, extract names form a given alignment}
14
+ s.email = %q{fer.izquierdo@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "lib/rphylip.rb",
28
+ "rphylip.gemspec",
29
+ "test/helper.rb",
30
+ "test/test_rphylip.rb"
31
+ ]
32
+ s.homepage = %q{http://github.com/fizquierdo/rphylip}
33
+ s.licenses = [%q{MIT}]
34
+ s.require_paths = [%q{lib}]
35
+ s.rubygems_version = %q{1.8.6}
36
+ s.summary = %q{Helper functions to handle phylogenetic phylip formatted alignments}
37
+
38
+ if s.respond_to? :specification_version then
39
+ s.specification_version = 3
40
+
41
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
42
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
43
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
44
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
45
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
46
+ s.add_development_dependency(%q<rcov>, [">= 0"])
47
+ else
48
+ s.add_dependency(%q<shoulda>, [">= 0"])
49
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
50
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
51
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
52
+ s.add_dependency(%q<rcov>, [">= 0"])
53
+ end
54
+ else
55
+ s.add_dependency(%q<shoulda>, [">= 0"])
56
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
57
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
58
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
59
+ s.add_dependency(%q<rcov>, [">= 0"])
60
+ end
61
+ end
62
+
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'rphylip'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestRphylip < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rphylip
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Fernando Izquierdo-Carrasco
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-03-27 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ type: :development
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ hash: 3
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ version_requirements: *id001
32
+ name: shoulda
33
+ prerelease: false
34
+ - !ruby/object:Gem::Dependency
35
+ type: :development
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ hash: 31
42
+ segments:
43
+ - 3
44
+ - 12
45
+ version: "3.12"
46
+ version_requirements: *id002
47
+ name: rdoc
48
+ prerelease: false
49
+ - !ruby/object:Gem::Dependency
50
+ type: :development
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ hash: 23
57
+ segments:
58
+ - 1
59
+ - 0
60
+ - 0
61
+ version: 1.0.0
62
+ version_requirements: *id003
63
+ name: bundler
64
+ prerelease: false
65
+ - !ruby/object:Gem::Dependency
66
+ type: :development
67
+ requirement: &id004 !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ~>
71
+ - !ruby/object:Gem::Version
72
+ hash: 49
73
+ segments:
74
+ - 1
75
+ - 8
76
+ - 3
77
+ version: 1.8.3
78
+ version_requirements: *id004
79
+ name: jeweler
80
+ prerelease: false
81
+ - !ruby/object:Gem::Dependency
82
+ type: :development
83
+ requirement: &id005 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ hash: 3
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ version_requirements: *id005
93
+ name: rcov
94
+ prerelease: false
95
+ description: Build fake phylip alignments, extract names form a given alignment
96
+ email: fer.izquierdo@gmail.com
97
+ executables: []
98
+
99
+ extensions: []
100
+
101
+ extra_rdoc_files:
102
+ - LICENSE.txt
103
+ - README.rdoc
104
+ files:
105
+ - .document
106
+ - Gemfile
107
+ - Gemfile.lock
108
+ - LICENSE.txt
109
+ - README.rdoc
110
+ - Rakefile
111
+ - VERSION
112
+ - lib/rphylip.rb
113
+ - rphylip.gemspec
114
+ - test/helper.rb
115
+ - test/test_rphylip.rb
116
+ homepage: http://github.com/fizquierdo/rphylip
117
+ licenses:
118
+ - MIT
119
+ post_install_message:
120
+ rdoc_options: []
121
+
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ none: false
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ hash: 3
130
+ segments:
131
+ - 0
132
+ version: "0"
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ none: false
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ hash: 3
139
+ segments:
140
+ - 0
141
+ version: "0"
142
+ requirements: []
143
+
144
+ rubyforge_project:
145
+ rubygems_version: 1.8.6
146
+ signing_key:
147
+ specification_version: 3
148
+ summary: Helper functions to handle phylogenetic phylip formatted alignments
149
+ test_files: []
150
+