Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor refactoring #93

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,5 @@ matrix:
branches:
only:
- master
- develop

23 changes: 11 additions & 12 deletions lib/bio/alignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1475,22 +1475,21 @@ class SequenceHash < Hash
module OriginalPrivate

# Gets the sequence from given object.
# TODO Quite possibly could be shortened more,
# as the first condition appears to be a short circuit
# but since `seq` is really an identity, doesn't
# make sense.
def extract_seq(obj)
seq = nil
if obj.is_a?(Bio::Sequence::NA) or obj.is_a?(Bio::Sequence::AA) then
seq = obj
obj
else
for m in [ :seq, :naseq, :aaseq ]
begin
seq = obj.send(m)
rescue NameError, ArgumentError
seq = nil
end
break if seq
end
seq = obj unless seq
m = [ :seq, :naseq, :aaseq ].find {|m|
obj.respond_to? m
}
m ?
obj.send(m) :
obj
end
seq
end
module_function :extract_seq

Expand Down
72 changes: 21 additions & 51 deletions lib/bio/data/aa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,23 +108,21 @@ module Data
}

def weight(x = nil)
if x
if x.length > 1
total = 0.0
x.each_byte do |byte|
aa = byte.chr.upcase
if WEIGHT[aa]
total += WEIGHT[aa]
else
raise "Error: invalid amino acid '#{aa}'"
end
return WEIGHT if x.nil?

if x.length > 1
total = 0.0
x.each_byte do |byte|
aa = byte.chr.upcase
if WEIGHT[aa]
total += WEIGHT[aa]
else
raise "Error: invalid amino acid '#{aa}'"
end
total -= NucleicAcid.weight[:water] * (x.length - 1)
else
WEIGHT[x]
end
total -= NucleicAcid.weight[:water] * (x.length - 1)
else
WEIGHT
WEIGHT[x]
end
end

Expand Down Expand Up @@ -172,27 +170,18 @@ def to_3(x)
alias three to_3

def one2three(x)
if x and x.length != 1
raise ArgumentError
else
NAMES[x]
end
fail ArgumentError if x.nil? or x.length != 1
NAMES[x]
end

def three2one(x)
if x and x.length != 3
raise ArgumentError
else
reverse[x]
end
fail ArgumentError if x.nil? or x.length != 3
reverse[x]
end

def one2name(x)
if x and x.length != 1
raise ArgumentError
else
three2name(NAMES[x])
end
fail ArgumentError if x.nil? or x.length != 1
three2name(NAMES[x])
end

def name2one(x)
Expand All @@ -205,11 +194,8 @@ def name2one(x)
end

def three2name(x)
if x and x.length != 3
raise ArgumentError
else
NAMES[x]
end
fail ArgumentError if x.nil? or x.length != 3
NAMES[x]
end

def name2three(x)
Expand Down Expand Up @@ -237,11 +223,7 @@ def to_re(seq)


def reverse
hash = Hash.new
NAMES.each do |k, v|
hash[v] = k
end
hash
@reverse ||= NAMES.invert
end

end
Expand All @@ -254,18 +236,6 @@ def reverse
extend Data


private


# override when used as an instance method to improve performance
alias orig_reverse reverse
def reverse
unless @reverse
@reverse = orig_reverse
end
@reverse
end

end

end # module Bio
Expand Down
18 changes: 8 additions & 10 deletions lib/bio/io/flatfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,15 @@ def self.open(*arg, &block)
raise ArgumentError, 'wrong number of arguments (0 for 1)'
end
x = arg.shift
if x.is_a?(Module) then
# FlatFile.open(dbclass, filename_or_io, ...)
dbclass = x
elsif x.nil? then
# FlatFile.open(nil, filename_or_io, ...)
dbclass = nil
else
# FlatFile.open(filename, ...)
dbclass = nil
arg.unshift(x)

dbclass = case x
when Module then x # FlatFile.open(dbclass, filename_or_io, ...)
when nil then nil # FlatFile.open(nil, filename_or_io, ...)
else # FlatFile.open(filename, ...)
arg.unshift(x)
nil
end

if arg.size <= 0
raise ArgumentError, 'wrong number of arguments (1 for 2)'
end
Expand Down
25 changes: 11 additions & 14 deletions lib/bio/sequence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,17 @@ module Bio
# puts dna.gc_percent
class Sequence

autoload :Common, 'bio/sequence/common'
autoload :NA, 'bio/sequence/na'
autoload :AA, 'bio/sequence/aa'
autoload :Generic, 'bio/sequence/generic'
autoload :Format, 'bio/sequence/format'
autoload :Adapter, 'bio/sequence/adapter'
autoload :QualityScore, 'bio/sequence/quality_score'
autoload :SequenceMasker, 'bio/sequence/sequence_masker'

#--
# require "bio/sequence/compat.rb" here to avoid circular require and
# possible superclass mismatch of AA class
#++
require 'bio/sequence/compat'
require File.expand_path(File.join File.dirname(__FILE__),"sequence/common.rb")
require File.expand_path(File.join File.dirname(__FILE__),"sequence/na.rb")
require File.expand_path(File.join File.dirname(__FILE__),"sequence/aa.rb")
require File.expand_path(File.join File.dirname(__FILE__),"sequence/generic.rb")
require File.expand_path(File.join File.dirname(__FILE__),"sequence/format.rb")
require File.expand_path(File.join File.dirname(__FILE__),"sequence/adapter.rb")
require File.expand_path(File.join File.dirname(__FILE__),"sequence/quality_score.rb")
require File.expand_path(File.join File.dirname(__FILE__),"sequence/sequence_masker.rb")


require File.expand_path(File.join File.dirname(__FILE__),"sequence/compat.rb")

include Format
include SequenceMasker
Expand Down
8 changes: 5 additions & 3 deletions lib/bio/sequence/aa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

module Bio

autoload :AminoAcid, 'bio/data/aa' unless const_defined?(:AminoAcid)
require File.expand_path(File.join File.dirname(__FILE__), '../sequence.rb')
require File.expand_path(File.join File.dirname(__FILE__), '../sequence/compat.rb')

require 'bio/sequence' unless const_defined?(:Sequence)
require File.expand_path(File.join File.dirname(__FILE__), '../data/aa.rb')

class Sequence

Expand All @@ -30,9 +31,10 @@ class Sequence
#
# # What is the molecular weight of this peptide?
# puts aa.molecular_weight
class AA < String
class AA < ::String

include Bio::Sequence::Common
extend Bio::Sequence::Common::ClassMethods

# Generate an amino acid sequence object from a string.
#
Expand Down
Loading