The pyp_utils Module

pyp_utils contains a set of procedures for creating and operating on PyPedal pedigrees. This includes routines for reordering and renumbering pedigrees, as well as for modifying pedigrees.

Module Contents

assign_offspring(pedobj) ⇒ integer [#]

assign_offspring() assigns offspring to their parent(s)'s unknown sex offspring list (well, dictionary).

myped
An instance of a NewPedigree object.
Returns:
0 for failure and 1 for success.

assign_sexes(pedobj) ⇒ integer [#]

assign_sexes() assigns a sex to every animal in the pedigree using sire and daughter lists for improved accuracy.

pedobj
A renumbered and reordered PyPedal pedigree object.
Returns:
0 for failure and 1 for success.

delete_id_map(filetag='_renumbered_') ⇒ integer [#]

delete_id_map() checks to see if an ID map for the given filetag exists. If the file exists, it is deleted.

filetag
A descriptor prepended to output file names that is used to determine name of the file to delete.
Returns:
A flag indicating whether or not the file was successfully deleted (0|1)

fast_reorder(myped, filetag='_new_reordered_', io='no', debug=0) ⇒ list [#]

fast_reorder() renumbers a pedigree such that parents precede their offspring in the pedigree. In order to minimize overhead as much as is reasonably possible, a list of animal IDs that have already been seen is kept. Whenever a parent that is not in the seen list is encountered, the offspring of that parent is moved to the end of the pedigree. This should ensure that the pedigree is properly sorted such that all parents precede their offspring. myped is reordered in place. fast_reorder() uses dictionaries to renumber the pedigree based on paddedIDs.

myped
A PyPedal pedigree object.
filetag
A descriptor prepended to output file names.
io
Indicates whether or not to write the reordered pedigree to a file (yes|no).
debug
Flag to indicate whether or not debugging messages are written to STDOUT.
Returns:
A reordered PyPedal pedigree.

load_id_map(filetag='_renumbered_') ⇒ dictionary [#]

load_id_map() reads an ID map from the file generated by pyp_utils/renumber() into a dictionary. There is a VERY similar function, pyp_io/id_map_from_file(), that is deprecated because it is much more fragile that this procedure.

filetag
A descriptor prepended to output file names that is used to determine the input file name.
Returns:
A dictionary whose keys are renumbered IDs and whose values are original IDs or an empty dictionary (on failure).

pedigree_range(pedobj, n) ⇒ list [#]

pedigree_range() takes a renumbered pedigree and removes all individuals with a renumbered ID > n. The reduced pedigree is returned. Assumes that the input pedigree is sorted on animal key in ascending order.

myped
A PyPedal pedigree object.
n
A renumbered animalID.
Returns:
A pedigree containing only animals born in the given birthyear of an ampty list (on failure).

pyp_datestamp() ⇒ string [#]

pyp_datestamp() returns a datestamp, as a string, of the format YYYYMMDDHHMMSS.

None
Returns:
A 14-character string containing the datestamp.

pyp_nice_time() ⇒ string [#]

pyp_nice_time() returns the current date and time formatted as, e.g., Wed Mar 30 10:26:31 2005.

None
Returns:
A string containing the formatted date and time.

renumber(myped, filetag='_renumbered_', io='no', outformat='0', debug=0, returnmap=0, missingparent=0, animaltype='new') ⇒ list [#]

renumber() takes a pedigree as input and renumbers it such that the oldest animal in the pedigree has an ID of '1' and the n-th animal has an ID of 'n'. If the pedigree is not ordered from oldest to youngest such that all offspring precede their offspring, the pedigree will be reordered. The renumbered pedigree is written to disc in 'asd' format and a map file that associates sequential IDs with original IDs is also written.

myped
A PyPedal pedigree object.
filetag
A descriptor prepended to output file names.
io
Indicates whether or not to write the renumbered pedigree to a file (yes|no).
outformat
Flag to indicate whether or not to write an asd pedigree (0) or a full pedigree (1).
returnmap
Indicates whether or not an ID map, as well as the renumbered pedigree, should be returned. If nonzero, suppresses writing of ID maps to disc.
debug
Flag to indicate whether or not progress messages are written to stdout.
missingparent
Indicates the value used to indicate a missing parent.
animaltype
Type of animal object used to populate pedigree
Returns:
A reordered PyPedal pedigree.

reorder(myped, filetag='_reordered_', io='no', missingparent=0, debug=0, max_rounds=100) ⇒ list [#]

reorder() renumbers a pedigree such that parents precede their offspring in the pedigree. In order to minimize overhead as much as is reasonably possible, a list of animal IDs that have already been seen is kept. Whenever a parent that is not in the seen list is encountered, the offspring of that parent is moved to the end of the pedigree. This should ensure that the pedigree is properly sorted such that all parents precede their offspring. myped is reordered in place. reorder() is VERY slow, but I am pretty sure that it works correctly.

myped
A PyPedal pedigree object.
filetag
A descriptor prepended to output file names.
io
Indicates whether or not to write the reordered pedigree to a file (yes|no).
missingparent
Indicates the value used to indicate a missing parent.
debug
Toggles debugging messages on and off.
max_rounds
Maximum times to iterate through a pedigree trying to reorder it.
Returns:
A reordered PyPedal pedigree.

reverse_string(mystring) ⇒ string [#]

reverse_string() reverses the input string and returns the reversed version.

mystring
A non-empty Python string.
Returns:
The input string with the order of its characters reversed.

set_age(pedobj) ⇒ integer [#]

set_age() Computes ages for all animals in a pedigree based on the global BASE_DEMOGRAPHIC_YEAR defined in pyp_demog.py. If the by is unknown, the inferred generation is used. If the inferred generation is unknown, the age is set to -999.

pedobj
A PyPedal pedigree object.
Returns:
0 for failure and 1 for success.

set_ancestor_flag(pedobj) ⇒ integer [#]

set_ancestor_flag() loops through a pedigree to build a dictionary of all of the parents in the pedigree. It then sets the ancestor flags for the parents. set_ancestor_flag() expects a reordered and renumbered pedigree as input!

pedobj
A PyPedal NewPedigree object.
Returns:
0 for failure and 1 for success.

set_generation(pedobj) ⇒ integer [#]

set_generation() Works through a pedigree to infer the generation to which an animal belongs based on founders belonging to generation 1. The igen assigned to an animal as the larger of sire.igen+1 and dam.igen+1. This routine assumes that myped is reordered and renumbered.

pedobj
A PyPedal NewPedigree object.
Returns:
0 for failure and 1 for success.

set_species(pedobj, species='u') ⇒ integer [#]

set_species() assigns a specie to every animal in the pedigree.

pedobj
A PyPedal pedigree object.
species
A PyPedal string.
Returns:
0 for failure and 1 for success.

simple_histogram_dictionary(mydict, histchar='*', histstep=5) ⇒ dictionary [#]

simple_histogram_dictionary() returns a dictionary containing a simple, text histogram. The input dictionary is assumed to contain keys which are distinct levels and values that are counts.

mydict
A non-empty Python dictionary.
histchar
The character used to draw the histogram (default is '*').
histstep
Used to determine the number of bins (stars) in the diagram.
Returns:
A dictionary containing the histogram by level or an empty dictionary (on failure).

sort_dict_by_keys(mydict) ⇒ dictionary [#]

sort_dict_by_keys() returns a dictionary where the values in the dictionary in the order obtained by sorting the keys. Taken from the routine sortedDictValues3 in the "Python Cookbook", p. 39.

mydict
A non-empty Python dictionary.
Returns:
The input dictionary with keys sorted in ascending order or an empty dictionary (on failure).

sort_dict_by_values(first, second) ⇒ list [#]

sort_dict_by_values() returns a dictionary where the keys in the dictionary are sorted ascending value, first on value and then on key within value. The implementation was taken from John Hunter's contribution to a newsgroup thread: http://groups-beta.google.com/group/comp.lang.python/browse_thread/thread/bbc259f8454e4d3f/cc686f4cd795feb4?q=python+%22sorted+dictionary%22=1=en#cc686f4cd795feb4

mydict
A non-empty Python dictionary.
Returns:
A list of tuples sorted in ascending order.

string_to_table_name(instring) ⇒ string [#]

string_to_table_name() takes an arbitrary string and returns a string that is safe to use as an SQLite table name.

instring
A string that will be converted to an SQLite-safe table name.
Returns:
A string that is safe to use as an SQLite table name.

subpedigree(pedobj, anlist) ⇒ object [#]

subpedigree() takes a NewPedigree object and list of animal IDs and returns a NewPedigree object containing only the animals in the animals list.

pedobj
PyPedal pedigree object.
animals
A list of animal IDs.
Returns:
An instance of a NewPedigree object.

trim_pedigree_to_year(pedobj, year) ⇒ list [#]

trim_pedigree_to_year() takes pedigrees and removes all individuals who were not born in birthyear 'year'.

myped
A PyPedal pedigree object.
year
A birthyear.
Returns:
A pedigree containing only animals born in the given birthyear or an ampty list (on failure).