aai 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.coveralls.yml +1 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +9 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +42 -0
- data/Rakefile +6 -0
- data/aai.gemspec +43 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/aai.rb +64 -0
- data/lib/aai.rb +309 -0
- data/lib/aai/core_extensions.rb +80 -0
- data/lib/aai/utils.rb +36 -0
- data/lib/aai/version.rb +13 -0
- metadata +200 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bd9af1e776474a382a6c83d27194d1001579e0e3
|
4
|
+
data.tar.gz: 3cfbfb4aa4a9371f470cd129dc32ab9dcffd1838
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bfdc658e2d8b5bb1b635428038a7fe18e13e8c1cfabe934cda192e1091a3ee87f55f0113494a61ad7a85c0bc231ef9854e98af69ab589ef44f3cdadbb4c2f65f
|
7
|
+
data.tar.gz: 37478156d0f3e00340ac6e0f4097e020f666c350e5f1e60e118f061bd996b2b4834036af579d2875e4a02fbbe20eb3e5f6f6a46ab2cdd1831df8b2fad8dbe8ff
|
data/.coveralls.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
service_name: travis-ci
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -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
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2017 Ryan Moore
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# AAI
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/mooreryan/aai.svg?branch=master)](https://travis-ci.org/mooreryan/aai)
|
4
|
+
[![Coverage Status](https://coveralls.io/repos/github/mooreryan/aai/badge.svg?branch=master)](https://coveralls.io/github/mooreryan/aai?branch=master)
|
5
|
+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
|
6
|
+
|
7
|
+
Calculate Seanie's multi-genome (or genome bin) amino acid similarity.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'aai'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install aai
|
24
|
+
|
25
|
+
## Requirements
|
26
|
+
|
27
|
+
- GNU Parallel
|
28
|
+
- NCBI Blast suite
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
TODO: Write usage instructions here
|
33
|
+
|
34
|
+
## Development
|
35
|
+
|
36
|
+
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.
|
37
|
+
|
38
|
+
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).
|
39
|
+
|
40
|
+
## Contributing
|
41
|
+
|
42
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/aai. 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.
|
data/Rakefile
ADDED
data/aai.gemspec
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'aai/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "aai"
|
8
|
+
spec.version = Aai::VERSION
|
9
|
+
spec.authors = ["Ryan Moore"]
|
10
|
+
spec.email = ["moorer@udel.edu"]
|
11
|
+
|
12
|
+
spec.summary = %q{Seanie's amino acid similarity.}
|
13
|
+
spec.description = %q{Calculate Seanie's amino acid similarity score between multiple genomes/bins.}
|
14
|
+
spec.homepage = "https://github.com/mooreryan/aai.git"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
# if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
# else
|
22
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
# "public gem pushes."
|
24
|
+
# end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
f.match(%r{^(test|spec|features)/})
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
34
|
+
spec.add_development_dependency "coveralls", "~> 0.8.21"
|
35
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
36
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
37
|
+
spec.add_development_dependency "yard", "~> 0.9.9"
|
38
|
+
|
39
|
+
spec.add_runtime_dependency "abort_if", "~> 0.2.0"
|
40
|
+
spec.add_runtime_dependency "parse_fasta", "~> 2.2"
|
41
|
+
spec.add_runtime_dependency "systemu", "~> 2.6", ">= 2.6.5"
|
42
|
+
spec.add_runtime_dependency "trollop", "~> 2.1", ">= 2.1.2"
|
43
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "aai"
|
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
data/exe/aai.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
Signal.trap("PIPE", "EXIT")
|
4
|
+
|
5
|
+
require "aai"
|
6
|
+
require "abort_if"
|
7
|
+
require "fileutils"
|
8
|
+
require "trollop"
|
9
|
+
|
10
|
+
include AbortIf
|
11
|
+
include AbortIf::Assert
|
12
|
+
|
13
|
+
Aai.extend Aai
|
14
|
+
Aai.extend Aai::Utils
|
15
|
+
|
16
|
+
version_banner =
|
17
|
+
|
18
|
+
opts = Trollop.options do
|
19
|
+
version Aai::VERSION_BANNER
|
20
|
+
|
21
|
+
banner <<-EOS
|
22
|
+
|
23
|
+
#{Aai::VERSION_BANNER}
|
24
|
+
|
25
|
+
Seanie's AAI calculator.
|
26
|
+
|
27
|
+
Each input file is treated as a file of ORFs for a single genome.
|
28
|
+
|
29
|
+
Options:
|
30
|
+
EOS
|
31
|
+
|
32
|
+
opt(:infiles, "Input files", type: :strings)
|
33
|
+
opt(:outdir, "Output directory", type: :string, default: ".")
|
34
|
+
opt(:basename, "Base name for output file", type: :string,
|
35
|
+
default: "aai_scores")
|
36
|
+
end
|
37
|
+
|
38
|
+
abort_if opts[:infiles].nil? || opts[:infiles].empty?,
|
39
|
+
"No infiles given"
|
40
|
+
|
41
|
+
Aai.check_files opts[:infiles]
|
42
|
+
|
43
|
+
FileUtils.mkdir_p opts[:outdir]
|
44
|
+
|
45
|
+
seq_lengths, clean_fnames = Aai.process_input_seqs! opts[:infiles]
|
46
|
+
|
47
|
+
blast_db_basenames = Aai.make_blastdbs! clean_fnames
|
48
|
+
|
49
|
+
btabs = Aai.blast_permutations! clean_fnames, blast_db_basenames
|
50
|
+
|
51
|
+
best_hits = Aai.get_best_hits btabs, seq_lengths
|
52
|
+
|
53
|
+
one_way = Aai.one_way_aai best_hits
|
54
|
+
|
55
|
+
two_way = Aai.two_way_aai best_hits
|
56
|
+
|
57
|
+
aai_strings = Aai.aai_strings one_way, two_way
|
58
|
+
|
59
|
+
outf = File.join opts[:outdir], "#{opts[:basename]}.aai.txt"
|
60
|
+
File.open(outf, "w") do |f|
|
61
|
+
Aai.aai_strings(one_way, two_way).each do |str|
|
62
|
+
f.puts str
|
63
|
+
end
|
64
|
+
end
|
data/lib/aai.rb
ADDED
@@ -0,0 +1,309 @@
|
|
1
|
+
require "abort_if"
|
2
|
+
require "systemu"
|
3
|
+
require "parse_fasta"
|
4
|
+
|
5
|
+
require "aai/core_extensions"
|
6
|
+
require "aai/utils"
|
7
|
+
require "aai/version"
|
8
|
+
|
9
|
+
AbortIf.extend AbortIf
|
10
|
+
Process.extend Aai::CoreExtensions::Process
|
11
|
+
|
12
|
+
module Aai
|
13
|
+
include Utils
|
14
|
+
|
15
|
+
BLAST_DB_SEPARATOR = "...."
|
16
|
+
BLAST_DB_SUFFIX = "#{BLAST_DB_SEPARATOR}aai"
|
17
|
+
|
18
|
+
PIDENT_CUTOFF = 30
|
19
|
+
EVALUE_CUTOFF = 1e-3
|
20
|
+
LENGTH_CUTOFF = 70 # actually is 70 percent
|
21
|
+
|
22
|
+
def blast_permutations! fastas, blast_dbs
|
23
|
+
file_permutations = one_way_combinations fastas, blast_dbs, true
|
24
|
+
file_permutations = file_permutations.select do |f1, f2|
|
25
|
+
genome_from_fname(f1) != genome_from_fname(f2)
|
26
|
+
end
|
27
|
+
|
28
|
+
first_files = file_permutations.map(&:first)
|
29
|
+
second_files = file_permutations.map(&:last)
|
30
|
+
|
31
|
+
first_genomes = first_files.map do |fname|
|
32
|
+
ary = fname.split(".")
|
33
|
+
ary.take(ary.length - 1).join
|
34
|
+
end
|
35
|
+
|
36
|
+
second_genomes = second_files.map do |fname|
|
37
|
+
ary = fname.split(BLAST_DB_SEPARATOR).take(1)
|
38
|
+
AbortIf.abort_unless ary.length == 1,
|
39
|
+
"Bad file name for #{fname}"
|
40
|
+
|
41
|
+
ary = ary.first.split(".")
|
42
|
+
|
43
|
+
File.basename ary.take(ary.length - 1).join
|
44
|
+
end
|
45
|
+
|
46
|
+
outf_names = first_genomes.zip(second_genomes).map do |f1, f2|
|
47
|
+
"#{f1}____#{f2}.aai_blastp"
|
48
|
+
end
|
49
|
+
|
50
|
+
title = "Running blast jobs"
|
51
|
+
cmd = "parallel --link blastp -outfmt 6 " +
|
52
|
+
"-query {1} -db {2} " +
|
53
|
+
"-out {3} -evalue 1e-3 " +
|
54
|
+
"::: #{first_files.join " "} ::: " +
|
55
|
+
"#{second_files.join " "} ::: " +
|
56
|
+
"#{outf_names.join " "}"
|
57
|
+
|
58
|
+
Process.run_and_time_it! title, cmd
|
59
|
+
|
60
|
+
outf_names
|
61
|
+
end
|
62
|
+
|
63
|
+
# Make blast dbs given an array of filenames.
|
64
|
+
#
|
65
|
+
# @param fnames [Array<String>] an array of filenames
|
66
|
+
#
|
67
|
+
# @return [Array<String>] blast db basenames
|
68
|
+
def make_blastdbs! fnames
|
69
|
+
suffix = BLAST_DB_SUFFIX
|
70
|
+
outfiles = fnames.map { |fname| fname + suffix }
|
71
|
+
|
72
|
+
title = "Making blast databases"
|
73
|
+
cmd = "parallel makeblastdb -in {} " +
|
74
|
+
"-out {}#{BLAST_DB_SUFFIX} -dbtype prot " +
|
75
|
+
"::: #{fnames.join " "}"
|
76
|
+
|
77
|
+
Process.run_and_time_it! title, cmd
|
78
|
+
|
79
|
+
outfiles
|
80
|
+
end
|
81
|
+
|
82
|
+
# Returns a hash table with sequence lengths and writes new fasta
|
83
|
+
# files with clean headers for blast.
|
84
|
+
#
|
85
|
+
# The sequences are annotated with the genome that they came from.
|
86
|
+
#
|
87
|
+
# @param fnames [Array<String>] an array of fasta file names
|
88
|
+
def process_input_seqs! fnames
|
89
|
+
seq_lengths = {}
|
90
|
+
clean_fnames = []
|
91
|
+
|
92
|
+
fnames.each do |fname|
|
93
|
+
clean_fname = fname + "_aai_clean"
|
94
|
+
clean_fnames << clean_fname
|
95
|
+
File.open(clean_fname, "w") do |f|
|
96
|
+
Object::ParseFasta::SeqFile.open(fname).each_record do |rec|
|
97
|
+
header =
|
98
|
+
annotate_header clean_header(rec.header),
|
99
|
+
File.basename(fname)
|
100
|
+
|
101
|
+
seq_lengths[header] = rec.seq.length
|
102
|
+
|
103
|
+
f.puts ">#{header}\n#{rec.seq}"
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
[seq_lengths, clean_fnames]
|
109
|
+
end
|
110
|
+
|
111
|
+
def get_best_hits fnames, seq_lengths
|
112
|
+
best_hits = {}
|
113
|
+
fnames.each do |fname| # blast files
|
114
|
+
File.open(fname, "rt").each_line do |line|
|
115
|
+
ary = line.chomp.split "\t"
|
116
|
+
|
117
|
+
query = ary[0]
|
118
|
+
target = ary[1]
|
119
|
+
pident = ary[2].to_f
|
120
|
+
length = ary[3].to_f
|
121
|
+
evalue = ary[10].to_f
|
122
|
+
|
123
|
+
query_genome = query.split("____").first
|
124
|
+
query_seq = query.split("____").last
|
125
|
+
|
126
|
+
target_genome = target.split("____").first
|
127
|
+
target_seq = target.split("____").last
|
128
|
+
|
129
|
+
seq_length_key = "#{query_genome}____#{query_seq}"
|
130
|
+
|
131
|
+
AbortIf.abort_unless seq_lengths.has_key?(seq_length_key),
|
132
|
+
"#{seq_length_key} is missing from " +
|
133
|
+
"seq_lengths"
|
134
|
+
|
135
|
+
query_length = seq_lengths[seq_length_key].to_f
|
136
|
+
length_percent = length / query_length * 100
|
137
|
+
|
138
|
+
hit_info = {
|
139
|
+
query_name: query_seq,
|
140
|
+
target_name: target_seq,
|
141
|
+
query_genome: query_genome,
|
142
|
+
target_genome: target_genome,
|
143
|
+
pident: pident,
|
144
|
+
length: length_percent,
|
145
|
+
evalue: evalue
|
146
|
+
}
|
147
|
+
|
148
|
+
# check if it is a best hit candidate
|
149
|
+
if pident >= PIDENT_CUTOFF &&
|
150
|
+
evalue <= EVALUE_CUTOFF &&
|
151
|
+
length_percent >= LENGTH_CUTOFF
|
152
|
+
|
153
|
+
if best_hits.has_key? query_genome
|
154
|
+
if best_hits[query_genome].has_key? query_seq
|
155
|
+
if best_hits[query_genome][query_seq].
|
156
|
+
has_key?(target_genome)
|
157
|
+
# check if we should replace the best hit?
|
158
|
+
current_best_hit =
|
159
|
+
best_hits[query_genome][query_seq][target_genome]
|
160
|
+
|
161
|
+
if pident >= current_best_hit[:pident]
|
162
|
+
best_hits[query_genome][query_seq][target_genome] =
|
163
|
+
hit_info
|
164
|
+
end
|
165
|
+
else
|
166
|
+
best_hits[query_genome][query_seq][target_genome] =
|
167
|
+
hit_info
|
168
|
+
end
|
169
|
+
else
|
170
|
+
best_hits[query_genome][query_seq] = {
|
171
|
+
target_genome => hit_info
|
172
|
+
}
|
173
|
+
end
|
174
|
+
else
|
175
|
+
best_hits[query_genome] = {
|
176
|
+
query_seq => {
|
177
|
+
target_genome => hit_info
|
178
|
+
}
|
179
|
+
}
|
180
|
+
end
|
181
|
+
else
|
182
|
+
# pass
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
best_hits
|
188
|
+
end
|
189
|
+
|
190
|
+
def one_way_aai best_hits
|
191
|
+
one_way_best_hits(best_hits).map do |genome_pair, best_hits|
|
192
|
+
[genome_pair, best_hits.map { |hit| hit[:pident] }.reduce(:+) /
|
193
|
+
best_hits.length.to_f]
|
194
|
+
end.to_h
|
195
|
+
end
|
196
|
+
|
197
|
+
def two_way_aai best_hits
|
198
|
+
# the pair key is the [g1, g2].sort
|
199
|
+
|
200
|
+
two_way_aai = {}
|
201
|
+
|
202
|
+
one_way_hits = one_way_best_hits best_hits
|
203
|
+
genome_pair_keys = one_way_hits.keys.map { |pair| pair.sort }.uniq
|
204
|
+
|
205
|
+
genome_pair_keys.each do |pair_key|
|
206
|
+
AbortIf.abort_unless one_way_hits.has_key?(pair_key) &&
|
207
|
+
one_way_hits.has_key?(pair_key.reverse),
|
208
|
+
"Missing keys for #{pair_key}"
|
209
|
+
|
210
|
+
forward_hits = one_way_hits[pair_key]
|
211
|
+
reverse_hits = one_way_hits[pair_key.reverse]
|
212
|
+
|
213
|
+
combinations = one_way_combinations forward_hits, reverse_hits
|
214
|
+
|
215
|
+
two_way_hits = combinations.select do |h1, h2|
|
216
|
+
two_way_hit? h1, h2
|
217
|
+
end
|
218
|
+
|
219
|
+
two_way_hit_info = two_way_hits.map do |h1, h2|
|
220
|
+
{ genome_pair: [h1[:query_genome],
|
221
|
+
h1[:target_genome]].sort,
|
222
|
+
pident: (h1[:pident] + h2[:pident]) / 2.0 }
|
223
|
+
end
|
224
|
+
|
225
|
+
two_way_hit_info.each do |hit|
|
226
|
+
if two_way_aai.has_key? hit[:genome_pair]
|
227
|
+
two_way_aai[hit[:genome_pair]] << hit[:pident]
|
228
|
+
else
|
229
|
+
two_way_aai[hit[:genome_pair]] = [hit[:pident]]
|
230
|
+
end
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
two_way_aai.map do |genome_pair, pidents|
|
235
|
+
[genome_pair, pidents.reduce(:+) / pidents.length.to_f]
|
236
|
+
end.to_h
|
237
|
+
end
|
238
|
+
|
239
|
+
# Returns an array (enumerable) of aai strings ready to print
|
240
|
+
def aai_strings one_way_aai, two_way_aai
|
241
|
+
aai_strings = {}
|
242
|
+
keys = (one_way_aai.keys + two_way_aai.keys).
|
243
|
+
map { |key| key.sort }.uniq
|
244
|
+
|
245
|
+
keys.each do |key|
|
246
|
+
a_to_b_aai = one_way_aai[key] || "NA"
|
247
|
+
b_to_a_aai = one_way_aai[key.reverse] || "NA"
|
248
|
+
two_way = two_way_aai[key] || "NA"
|
249
|
+
|
250
|
+
aai_strings[key] = [a_to_b_aai,
|
251
|
+
b_to_a_aai,
|
252
|
+
two_way]
|
253
|
+
end
|
254
|
+
|
255
|
+
aai_strings.map do |genome_pair, aais|
|
256
|
+
[genome_pair.join("----"),
|
257
|
+
aais.join("\t")].join "\t"
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
private
|
262
|
+
|
263
|
+
def two_way_hit? hit1, hit2
|
264
|
+
hit1[:query_name] == hit2[:target_name] &&
|
265
|
+
hit1[:query_genome] == hit2[:target_genome]
|
266
|
+
end
|
267
|
+
|
268
|
+
def one_way_best_hits best_hits
|
269
|
+
one_way = {}
|
270
|
+
|
271
|
+
best_hits.each do |query_genome, orfs|
|
272
|
+
orfs.each do |orf, target_genomes|
|
273
|
+
target_genomes.each do |target_genome, best_hit|
|
274
|
+
genome_pair = [query_genome, target_genome]
|
275
|
+
if one_way.has_key? genome_pair
|
276
|
+
one_way[genome_pair] << best_hit
|
277
|
+
else
|
278
|
+
one_way[genome_pair] = [best_hit]
|
279
|
+
end
|
280
|
+
end
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
one_way
|
285
|
+
end
|
286
|
+
|
287
|
+
def genome_from_fname fname
|
288
|
+
fname_no_aai = fname.split(BLAST_DB_SEPARATOR).first
|
289
|
+
ext = fname_no_aai.split(".").last
|
290
|
+
genome = File.basename fname_no_aai, ".#{ext}"
|
291
|
+
|
292
|
+
genome
|
293
|
+
end
|
294
|
+
|
295
|
+
def clean_header header
|
296
|
+
header.gsub(/ +/, "_")
|
297
|
+
end
|
298
|
+
|
299
|
+
# Adds the file name to the header minus the directory and the
|
300
|
+
# final extension.
|
301
|
+
#
|
302
|
+
# @note If the file is blah.fa.gz or something like that, then the
|
303
|
+
# genome name will be blah.fa, and not just blah.
|
304
|
+
def annotate_header header, fname
|
305
|
+
genome = genome_from_fname fname
|
306
|
+
|
307
|
+
"#{genome}____#{header}"
|
308
|
+
end
|
309
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
module Aai
|
2
|
+
module CoreExtensions
|
3
|
+
module Time
|
4
|
+
def date_and_time fmt="%F %T.%L"
|
5
|
+
Object::Time.now.strftime fmt
|
6
|
+
end
|
7
|
+
|
8
|
+
def time_it title="", logger=nil, run: true
|
9
|
+
if run
|
10
|
+
t = Object::Time.now
|
11
|
+
|
12
|
+
yield
|
13
|
+
|
14
|
+
time = Object::Time.now - t
|
15
|
+
|
16
|
+
if title == ""
|
17
|
+
msg = "Finished in #{time} seconds"
|
18
|
+
else
|
19
|
+
msg = "#{title} finished in #{time} seconds"
|
20
|
+
end
|
21
|
+
|
22
|
+
if logger
|
23
|
+
logger.info msg
|
24
|
+
else
|
25
|
+
$stderr.puts msg
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
module Process
|
32
|
+
include CoreExtensions::Time
|
33
|
+
|
34
|
+
def run_it *a, &b
|
35
|
+
exit_status, stdout, stderr = systemu *a, &b
|
36
|
+
|
37
|
+
puts stdout unless stdout.empty?
|
38
|
+
$stderr.puts stderr unless stderr.empty?
|
39
|
+
|
40
|
+
exit_status.exitstatus
|
41
|
+
end
|
42
|
+
|
43
|
+
def run_it! *a, &b
|
44
|
+
exit_status = self.run_it *a, &b
|
45
|
+
|
46
|
+
AbortIf.abort_unless exit_status.zero?,
|
47
|
+
"ERROR: non-zero exit status " +
|
48
|
+
"(#{exit_status}) " +
|
49
|
+
"when running '#{a.inspect}', " +
|
50
|
+
"'#{b.inspect}'"
|
51
|
+
|
52
|
+
exit_status
|
53
|
+
end
|
54
|
+
|
55
|
+
# Examples
|
56
|
+
#
|
57
|
+
# Process.extend CoreExtensions::Process
|
58
|
+
# Time.extend CoreExtensions::Time
|
59
|
+
#
|
60
|
+
# Process.run_and_time_it! "Saying hello",
|
61
|
+
# %Q{echo "hello world"}
|
62
|
+
#
|
63
|
+
# Process.run_and_time_it! "This will raise SystemExit",
|
64
|
+
# "ls arstoeiarntoairnt" do
|
65
|
+
# puts "i like pie"
|
66
|
+
# end
|
67
|
+
def run_and_time_it! title="",
|
68
|
+
cmd="",
|
69
|
+
logger=AbortIf::logger,
|
70
|
+
&b
|
71
|
+
|
72
|
+
AbortIf.logger.debug { "Running: #{cmd}" }
|
73
|
+
|
74
|
+
time_it title, logger do
|
75
|
+
run_it! cmd, &b
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
data/lib/aai/utils.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
module Aai
|
2
|
+
module Utils
|
3
|
+
# Raises SystemExit if one of the fnames does not exist.
|
4
|
+
def check_files fnames
|
5
|
+
fnames.each do |fname|
|
6
|
+
AbortIf.abort_unless_file_exists fname
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def two_ary_permutations a1, a2
|
11
|
+
permutations = []
|
12
|
+
|
13
|
+
a1.each do |elem1|
|
14
|
+
a2.each do |elem2|
|
15
|
+
permutations << [elem1, elem2] << [elem2, elem1]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
permutations
|
20
|
+
end
|
21
|
+
|
22
|
+
def one_way_combinations a1, a2, no_duplicates=true
|
23
|
+
permutations = []
|
24
|
+
|
25
|
+
a1.each do |elem1|
|
26
|
+
a2.each do |elem2|
|
27
|
+
if !no_duplicates || (no_duplicates && elem1 != elem2)
|
28
|
+
permutations << [elem1, elem2]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
permutations
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/aai/version.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
module Aai
|
2
|
+
VERSION = "0.1.0"
|
3
|
+
COPYRIGHT = "2017 Ryan Moore"
|
4
|
+
CONTACT = "moorer@udel.edu"
|
5
|
+
WEBSITE = "https://github.com/mooreryan/aai"
|
6
|
+
LICENSE = "MIT"
|
7
|
+
|
8
|
+
VERSION_BANNER = " # Version: #{VERSION}
|
9
|
+
# Copyright #{COPYRIGHT}
|
10
|
+
# Contact: #{CONTACT}
|
11
|
+
# Website: #{WEBSITE}
|
12
|
+
# License: #{LICENSE}"
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,200 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: aai
|
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-06-22 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.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
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: parse_fasta
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '2.2'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '2.2'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: systemu
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '2.6'
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: 2.6.5
|
121
|
+
type: :runtime
|
122
|
+
prerelease: false
|
123
|
+
version_requirements: !ruby/object:Gem::Requirement
|
124
|
+
requirements:
|
125
|
+
- - "~>"
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: '2.6'
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: 2.6.5
|
131
|
+
- !ruby/object:Gem::Dependency
|
132
|
+
name: trollop
|
133
|
+
requirement: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - "~>"
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: '2.1'
|
138
|
+
- - ">="
|
139
|
+
- !ruby/object:Gem::Version
|
140
|
+
version: 2.1.2
|
141
|
+
type: :runtime
|
142
|
+
prerelease: false
|
143
|
+
version_requirements: !ruby/object:Gem::Requirement
|
144
|
+
requirements:
|
145
|
+
- - "~>"
|
146
|
+
- !ruby/object:Gem::Version
|
147
|
+
version: '2.1'
|
148
|
+
- - ">="
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
version: 2.1.2
|
151
|
+
description: Calculate Seanie's amino acid similarity score between multiple genomes/bins.
|
152
|
+
email:
|
153
|
+
- moorer@udel.edu
|
154
|
+
executables:
|
155
|
+
- aai.rb
|
156
|
+
extensions: []
|
157
|
+
extra_rdoc_files: []
|
158
|
+
files:
|
159
|
+
- ".coveralls.yml"
|
160
|
+
- ".gitignore"
|
161
|
+
- ".rspec"
|
162
|
+
- ".travis.yml"
|
163
|
+
- CODE_OF_CONDUCT.md
|
164
|
+
- Gemfile
|
165
|
+
- LICENSE
|
166
|
+
- README.md
|
167
|
+
- Rakefile
|
168
|
+
- aai.gemspec
|
169
|
+
- bin/console
|
170
|
+
- bin/setup
|
171
|
+
- exe/aai.rb
|
172
|
+
- lib/aai.rb
|
173
|
+
- lib/aai/core_extensions.rb
|
174
|
+
- lib/aai/utils.rb
|
175
|
+
- lib/aai/version.rb
|
176
|
+
homepage: https://github.com/mooreryan/aai.git
|
177
|
+
licenses:
|
178
|
+
- MIT
|
179
|
+
metadata: {}
|
180
|
+
post_install_message:
|
181
|
+
rdoc_options: []
|
182
|
+
require_paths:
|
183
|
+
- lib
|
184
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
185
|
+
requirements:
|
186
|
+
- - ">="
|
187
|
+
- !ruby/object:Gem::Version
|
188
|
+
version: '0'
|
189
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
190
|
+
requirements:
|
191
|
+
- - ">="
|
192
|
+
- !ruby/object:Gem::Version
|
193
|
+
version: '0'
|
194
|
+
requirements: []
|
195
|
+
rubyforge_project:
|
196
|
+
rubygems_version: 2.6.12
|
197
|
+
signing_key:
|
198
|
+
specification_version: 4
|
199
|
+
summary: Seanie's amino acid similarity.
|
200
|
+
test_files: []
|