Store Data for a family tree?

Hi,

I want to to store some data for a family tree, but I cannot figure out
which way i should take. So after a few ides (that have not worked very
well) I came up with this:

[[Genotyp],[parents],[gender],[affected]],and so on]

I do not need names, the index of the person is enough.
But I guess this is bad.
So could anyone give me an advice how to store these data. I need the
index of the person (1 or 2 and so), the Genotyp (e.g. “AA” or so), the
index of her parents (e.g. [1,2]), the gender and if this person is
affected.

I am looking forward to hear from you.

Yours sincerely
Greenality

On Wed, Aug 28, 2013 at 12:20 PM, Green E. [email protected]
wrote:

I want to to store some data for a family tree, but I cannot figure out
which way i should take. So after a few ides (that have not worked very
well) I came up with this:

[[Genotyp],[parents],[gender],[affected]],and so on]

I do not need names, the index of the person is enough.
But I guess this is bad.

Not sure what you are describing above. Since a family tree is
essentially a graph that data structure is probably the best to store
it. Just create a class with all the properties you need for an
individual and reference parents and probably children as well. Then
you can move around the graph at will.

So could anyone give me an advice how to store these data. I need the
index of the person (1 or 2 and so), the Genotyp (e.g. “AA” or so), the
index of her parents (e.g. [1,2]), the gender and if this person is
affected.

Affected by what? And what is the index of a person?

Kind regards

robert

I agree with Robert, this kind of tree structure is easily solved by an
Object Oriented view. A Person class could hold references to other
people, and then it’s just a matter of fleshing-out the attributes.

I’m guessing you’ll want first name, last name, etc… but here’s a
simple example for you:

class Person
attr_accessor :gender, :genotype
attr_reader :parents, :children

def initialize( gender, genotype )
@parents = []
@children = []
@gender = gender
@genotype = genotype
end

def add_parent( parent )
@parents << parent unless parents.include?( parent )
parent.add_child( self ) unless parent.children.include?( self )
end

def add_child( child )
@children << child unless children.include?( child )
child.add_parent( self ) unless child.parents.include?( self )
end

def remove_parent( parent )
@parents.delete parent if parents.include?( parent )
parent.remove_child( self ) if parent.children.include( self )
end

def remove_child( child )
@children.delete child if children.include?( child )
child.remove_parent( self ) if child.parents.include( self )
end

end


granny = Person.new( ‘f’, ‘AA’ )
=> #<Person:0x474ab0 @parents=[], @children=[], @gender=“f”,
@genotype=“AA”>

dad = Person.new( ‘m’, ‘Aa’ )
=> #<Person:0x2e1ec08 @parents=[], @children=[], @gender=“m”,
@genotype=“Aa”>

me = Person.new( ‘m’, ‘aa’ )
=> #<Person:0x31b6f18 @parents=[], @children=[], @gender=“m”,
@genotype=“aa”>

granny.add_child dad
=> nil

dad.add_child me
=> nil

require ‘pp’
=> true

pp granny
#<Person:0x0474ab0
@children=
[#<Person:0x2e1ec08
@children=
[#<Person:0x31b6f18
@children=[],
@gender=“m”,
@genotype=“aa”,
@parents=[#<Person:0x2e1ec08 …>]>],
@gender=“m”,
@genotype=“Aa”,
@parents=[#<Person:0x0474ab0 …>]>],
@gender=“f”,
@genotype=“AA”,
@parents=[]>