[BioRuby-cvs] bioruby/lib/bio map.rb,1.5,1.6
Jan Aerts
aerts at dev.open-bio.org
Wed Jun 7 12:55:52 UTC 2006
Update of /home/repository/bioruby/bioruby/lib/bio
In directory dev.open-bio.org:/tmp/cvs-serv3575
Modified Files:
map.rb
Log Message:
* Added Bio::Locations#equal? method. (Should be moved to location.rb, if approved.)
* Methods add_mapping_to_marker and add_mapping_to_map renamed to
add_mapping_as_map and add_mapping_as_marker, respectively, to lessen the
confusion of what is mapped to what.
* When adding a new mapping to a map or marker, a check is done first to see
if that mapping has been added before or not.
* New method: Bio::Map::ActsLikeMarker#positions_on(map)
Index: map.rb
===================================================================
RCS file: /home/repository/bioruby/bioruby/lib/bio/map.rb,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** map.rb 4 May 2006 18:41:08 -0000 1.5
--- map.rb 7 Jun 2006 12:55:50 -0000 1.6
***************
*** 6,12 ****
--- 6,28 ----
# Licence:: Ruby's
#
+ # $Id$
require 'bio/location'
module Bio
+
+ # Add a method to Bio::Locations class
+ class Locations
+ def equals?(other)
+ if ! other.kind_of?(Bio::Locations)
+ return nil
+ end
+ if self.sort == other.sort
+ return true
+ else
+ return false
+ end
+ end
+ end
+
# = DESCRIPTION
# The Bio::Module contains classes that describe mapping information and can
***************
*** 42,48 ****
# my_map2 = Bio::Map::SimpleMap.new('consensus', 'linkage', 'cM')
#
! # my_map1.add_mapping_to_marker(my_marker1, '17')
! # my_map1.add_mapping_to_marker(Bio::Map::Marker.new('marker2'), '5')
! # my_marker3.add_mapping_to_marker(my_map1, '9')
#
# puts "Does my_map1 contain marker3? => " + my_map1.contains_marker?(my_marker3).to_s
--- 58,64 ----
# my_map2 = Bio::Map::SimpleMap.new('consensus', 'linkage', 'cM')
#
! # my_map1.add_mapping_as_map(my_marker1, '17')
! # my_map1.add_mapping_as_map(Bio::Map::Marker.new('marker2'), '5')
! # my_marker3.add_mapping_as_marker(my_map1, '9')
#
# puts "Does my_map1 contain marker3? => " + my_map1.contains_marker?(my_marker3).to_s
***************
*** 50,63 ****
#
# my_map1.sort.each do |mapping|
! # puts mapping.map.name + "\t" + mapping.marker.name + "\t" + mapping.location.from.to_s + ".." + mapping.location.to.to_s
# end
# puts my_map1.min.marker.name
# my_map2.each do |mapping|
! # puts mapping.map.name + "\t" + mapping.marker.name + "\t" + mapping.location.from.to_s + ".." + mapping.location.to.to_s
# end
- #
- # = TODO
- # Check if initialization of @mappings can be done in ActsLikeMap and
- # ActsLikeMarker, instead of in the classes that include these modules.
module Map
# = DESCRIPTION
--- 66,75 ----
#
# my_map1.sort.each do |mapping|
! # puts mapping.map.name + "\t" + mapping.marker.name + "\t" + mapping.location[0].from.to_s + ".." + mapping.location[-1].to.to_s
# end
# puts my_map1.min.marker.name
# my_map2.each do |mapping|
! # puts mapping.map.name + "\t" + mapping.marker.name + "\t" + mapping.location[0].from.to_s + ".." + mapping.location[-1].to.to_s
# end
module Map
# = DESCRIPTION
***************
*** 67,72 ****
--- 79,87 ----
# * check if a given marker is mapped to it
# , and can be mixed into other classes (e.g. Bio::Map::SimpleMap)
+ #
+ # Classes that include this mixin should provide an array property called mappings_as_map.
module ActsLikeMap
include Enumerable
+
# = DESCRIPTION
# Adds a Bio::Map::Mappings object to its array of mappings.
***************
*** 74,78 ****
# = USAGE
# # suppose we have a Bio::Map::SimpleMap object called my_map
! # my_map.add_mapping_to_marker(Bio::Map::Marker.new('marker_a'), '5')
# ---
# *Arguments*:
--- 89,93 ----
# = USAGE
# # suppose we have a Bio::Map::SimpleMap object called my_map
! # my_map.add_mapping_as_map(Bio::Map::Marker.new('marker_a'), '5')
# ---
# *Arguments*:
***************
*** 80,91 ****
# * _location_: location of mapping. Should be a _string_, not a _number_.
# *Returns*:: itself
! def add_mapping_to_marker(marker, location = nil)
unless marker.class.include?(Bio::Map::ActsLikeMarker)
raise "[Error] marker is not object that implements Bio::Map::ActsLikeMarker"
end
! my_mapping = Bio::Map::Mapping.new(self, marker, Bio::Location.new(location))
! @mappings.push(my_mapping)
! unless marker.mapped_to?(self)
! marker.mappings.push(my_mapping)
end
--- 95,117 ----
# * _location_: location of mapping. Should be a _string_, not a _number_.
# *Returns*:: itself
! def add_mapping_as_map(marker, location = nil)
unless marker.class.include?(Bio::Map::ActsLikeMarker)
raise "[Error] marker is not object that implements Bio::Map::ActsLikeMarker"
end
! my_mapping = ( location.nil? ) ? Bio::Map::Mapping.new(self, marker, nil) : Bio::Map::Mapping.new(self, marker, Bio::Locations.new(location))
! if ! marker.mapped_to?(self)
! self.mappings_as_map.push(my_mapping)
! marker.mappings_as_marker.push(my_mapping)
! else
! already_mapped = false
! marker.positions_on(self).each do |loc|
! if loc.equals?(Bio::Locations.new(location))
! already_mapped = true
! end
! end
! if ! already_mapped
! self.mappings_as_map.push(my_mapping)
! marker.mappings_as_marker.push(my_mapping)
! end
end
***************
*** 103,107 ****
end
contains = false
! @mappings.each do |mapping|
if mapping.marker == marker
contains = true
--- 129,133 ----
end
contains = false
! self.mappings_as_map.each do |mapping|
if mapping.marker == marker
contains = true
***************
*** 114,118 ****
# Go through all Bio::Map::Mapping objects linked to this Bio::Map::SimpleMap.
def each
! @mappings.each do |mapping|
yield mapping
end
--- 140,144 ----
# Go through all Bio::Map::Mapping objects linked to this Bio::Map::SimpleMap.
def each
! self.mappings_as_map.each do |mapping|
yield mapping
end
***************
*** 126,129 ****
--- 152,157 ----
# * check if it's mapped to a given map
# , and can be mixed into other classes (e.g. Bio::Map::Marker)
+ #
+ # Classes that include this mixin should provide an array property called mappings_as_marker.
module ActsLikeMarker
include Enumerable
***************
*** 134,138 ****
# = USAGE
# # suppose we have a Bio::Map::Marker object called marker_a
! # marker_a.add_mapping_to_map(Bio::Map::SimpleMap.new('my_map'), '5')
# ---
# *Arguments*:
--- 162,166 ----
# = USAGE
# # suppose we have a Bio::Map::Marker object called marker_a
! # marker_a.add_mapping_as_marker(Bio::Map::SimpleMap.new('my_map'), '5')
# ---
# *Arguments*:
***************
*** 140,151 ****
# * _location_: location of mapping. Should be a _string_, not a _number_.
# *Returns*:: itself
! def add_mapping_to_map(map, location = nil)
unless map.class.include?(Bio::Map::ActsLikeMap)
raise "[Error] map is not object that implements Bio::Map::ActsLikeMap"
end
! my_mapping = Bio::Map::Mapping.new(map, self, Bio::Location.new(location))
! @mappings.push(my_mapping)
! unless map.contains_marker?(self)
! map.mappings.push(my_mapping)
end
end
--- 168,197 ----
# * _location_: location of mapping. Should be a _string_, not a _number_.
# *Returns*:: itself
! def add_mapping_as_marker(map, location = nil)
unless map.class.include?(Bio::Map::ActsLikeMap)
raise "[Error] map is not object that implements Bio::Map::ActsLikeMap"
end
! my_mapping = (location.nil?) ? Bio::Map::Mappings.new(map, self, nil) : Bio::Map::Mapping.new(map, self, Bio::Locations.new(location))
! if ! self.mapped_to?(map)
! self.mappings_as_marker.push(my_mapping)
! map.mappings_as_map.push(my_mapping)
! else
! already_mapped = false
! self.positions_on(map).each do |loc|
! if loc.equals?(Bio::Locations.new(location))
! already_mapped = true
! end
! end
! if ! already_mapped
! self.mappings_as_marker.push(my_mapping)
! map.mappings_as_map.push(my_mapping)
! end
! end
! end
!
! # Go through all Mapping objects linked to this marker.
! def each
! self.mappings_as_marker.each do |mapping|
! yield mapping
end
end
***************
*** 160,166 ****
raise "[Error] map is not object that implements Bio::Map::ActsLikeMap"
end
!
mapped = false
! @mappings.each do |mapping|
if mapping.map == map
mapped = true
--- 206,212 ----
raise "[Error] map is not object that implements Bio::Map::ActsLikeMap"
end
!
mapped = false
! self.mappings_as_marker.each do |mapping|
if mapping.map == map
mapped = true
***************
*** 168,180 ****
end
end
return mapped
end
!
! # Go through all Mapping objects linked to this marker.
! def each
! @mappings.each do |mapping|
! yield mapping
end
end
end #ActsLikeMarker
--- 214,241 ----
end
end
+
return mapped
end
!
! # Return all positions of this marker on a given map.
! # ---
! # *Arguments*:
! # * _map_: an object that mixes in Bio::Map::ActsLikeMap
! # *Returns*:: array of Bio::Location objects
! def positions_on(map)
! unless map.class.include?(Bio::Map::ActsLikeMap)
! raise "[Error] map is not object that implements Bio::Map::ActsLikeMap"
! end
!
! positions = Array.new
! self.mappings_as_marker.each do |mapping|
! if mapping.map == map
! positions.push(mapping.location)
! end
end
+
+ return positions
end
+
end #ActsLikeMarker
***************
*** 191,195 ****
# * _map_: a Bio::Map::SimpleMap object
# * _marker_: a Bio::Map::Marker object
! # * _location_: a Bio::Location object
def initialize (map, marker, location = nil)
@map, @marker, @location = map, marker, location
--- 252,257 ----
# * _map_: a Bio::Map::SimpleMap object
# * _marker_: a Bio::Map::Marker object
! # * _location_: a Bio::Locations object
!
def initialize (map, marker, location = nil)
@map, @marker, @location = map, marker, location
***************
*** 197,219 ****
attr_accessor :map, :marker, :location
! # Compares the location of this mapping to another mapping.
! # ---
! # *Arguments*:
! # * other_mapping: Bio::Map::Mapping object
! # *Returns*::
! # * 1 if self < other location
! # * -1 if self > other location
! # * 0 if both location are the same
! # * nil if the argument is not a Bio::Location object
! def <=>(other)
! unless other.kind_of?(Bio::Map::Mapping)
! raise "[Error] markers are not comparable"
! end
! unless @map.equal?(other.map)
! raise "[Error] maps have to be the same"
! end
!
! return self.location.<=>(other.location)
! end
end # Mapping
--- 259,280 ----
attr_accessor :map, :marker, :location
! # # Compares the location of this mapping to another mapping.
! # # ---
! # # *Arguments*:
! # # * other_mapping: Bio::Map::Mapping object
! # # *Returns*::
! # # * 1 if self < other location
! # # * -1 if self > other location
! # # * 0 if both location are the same
! # # * nil if the argument is not a Bio::Location object
! # def <=>(other)
! # unless other.kind_of?(Bio::Map::Mapping)
! # raise "[Error] markers are not comparable"
! # end
! # unless @map.equal?(other.map)
! # raise "[Error] maps have to be the same"
! # end
! # return self.location<=>(other.location) # FIXME: no <=>-method in Bio::Locations
! # end
end # Mapping
***************
*** 228,232 ****
# my_map1.add_marker(Bio::Map::Marker.new('marker_b', '5')
class SimpleMap
! include ActsLikeMap
# Builds a new Bio::Map::SimpleMap object
--- 289,293 ----
# my_map1.add_marker(Bio::Map::Marker.new('marker_b', '5')
class SimpleMap
! include Bio::Map::ActsLikeMap
# Builds a new Bio::Map::SimpleMap object
***************
*** 237,243 ****
# * units: unit of the map (e.g. cM, cR, ...)
# *Returns*:: new Bio::Map::SimpleMap object
! def initialize (name = nil, type = nil, units = nil)
! @name, @type, @units = name, type, units
! @mappings = Array.new
end
--- 298,304 ----
# * units: unit of the map (e.g. cM, cR, ...)
# *Returns*:: new Bio::Map::SimpleMap object
! def initialize (name = nil, type = nil, length = nil, units = nil)
! @name, @type, @length, @units = name, type, length, units
! @mappings_as_map = Array.new
end
***************
*** 247,256 ****
# Type of the map
attr_accessor :type
!
# Units of the map
attr_accessor :units
! # Array of mappings for the map
! attr_accessor :mappings
end # SimpleMap
--- 308,321 ----
# Type of the map
attr_accessor :type
!
! # Length of the map
! attr_accessor :length
!
# Units of the map
attr_accessor :units
! # Mappings
! attr_accessor :mappings_as_map
!
end # SimpleMap
***************
*** 264,268 ****
# marker_b = Bio::Map::Marker.new('marker_b')
class Marker
! include ActsLikeMarker
# Builds a new Bio::Map::Marker object
--- 329,333 ----
# marker_b = Bio::Map::Marker.new('marker_b')
class Marker
! include Bio::Map::ActsLikeMarker
# Builds a new Bio::Map::Marker object
***************
*** 273,315 ****
def initialize(name)
@name = name
! @mappings = Array.new
end
# Name of the marker
attr_accessor :name
!
! # Array of mappings for the marker
! attr_accessor :mappings
end # Marker
end # Map
end # Bio
-
- if __FILE__ == $0
- my_marker1 = Bio::Map::Marker.new('marker1')
- # my_marker2 = Bio::Map::Marker.new('marker2')
- my_marker3 = Bio::Map::Marker.new('marker3')
-
- my_map1 = Bio::Map::SimpleMap.new('RH_map_ABC (2006)', 'RH', 'cR')
- my_map2 = Bio::Map::SimpleMap.new('consensus', 'linkage', 'cM')
-
- my_map1.add_mapping_to_marker(my_marker1, '17')
- my_map1.add_mapping_to_marker(Bio::Map::Marker.new('marker2'), '5')
- my_marker3.add_mapping_to_map(my_map1, '9')
-
- my_map2.add_mapping_to_marker(my_marker1, '57')
-
- puts "Does my_map1 contain marker3? => " + my_map1.contains_marker?(my_marker3).to_s
- puts "Does my_map2 contain marker3? => " + my_map2.contains_marker?(my_marker3).to_s
-
- my_map1.sort.each do |mapping|
- puts mapping.map.name + "\t" + mapping.marker.name + "\t" + mapping.location.from.to_s + ".." + mapping.location.to.to_s
- end
- puts my_map1.min.marker.name
-
- my_map2.each do |mapping|
- puts mapping.map.name + "\t" + mapping.marker.name + "\t" + mapping.location.from.to_s + ".." + mapping.location.to.to_s
- end
-
- # p my_map1.between?(my_mappable2,my_mappable3)
- # p my_map1.between?(my_mappable,my_mappable2)
- end
--- 338,351 ----
def initialize(name)
@name = name
! @mappings_as_marker = Array.new
end
# Name of the marker
attr_accessor :name
!
! # Mappings
! attr_accessor :mappings_as_marker
!
end # Marker
end # Map
end # Bio
More information about the bioruby-cvs
mailing list