# Path: pydigree/io/base.py
# def read_ped(filename, population=None, delimiter=None, affected_labels=None,
#              population_handler=None, data_handler=None, connect_inds=True,
#              onlyinds=None):
#     """
#     Reads a plink format pedigree file, ie:
#     
#     ::    
#         familyid indid father mother sex whatever whatever whatever
#     
#     into a pydigree pedigree object, with optional population to
#     assign to pedigree members. If you don't provide a population
#     you can't simulate genotypes!
# 
# 
#     :param filename: The file to be read
#     :param population: The population to assign individuals to
#     :param delimiter: a string defining the field separator, 
#         default: any whitespace
#     :param affected_labels: The labels that determine affection status.
#     :param population_handler: a function to set up the population 
#     :param data_handler: a function to turn the 
#         data into useful individual information
#     :param connect_inds: build references between individuals. Requires all
#         individuals be present in the file
#     :param onlyinds: only include data for specified individuals 
# 
#     :type filename: string
#     :type population: Population
#     :type delimiter: string
#     :type affected_labels: dict (str -> value)
#     :type data_handler: callable
#     :type connect_inds: bool
#     :type onlyinds: iterable 
# 
# 
#     :returns: individuals contained in the pedigree file 
#     :rtype: PedigreeCollection
#     """
# 
#     if not affected_labels:
#         affected_labels = {'1': 0, '2': 1, 'A': 1, 'U': 0,
#                            'X': None, '-9': None}
# 
#     if not isinstance(data_handler, Callable):
#         data_handler = lambda *x: None
#     
#     if not isinstance(population_handler, Callable):
#         population_handler = lambda *x: None
# 
#     population = Population() if population is None else population
#     p = Pedigree()
# 
#     population_handler(p)
# 
#     # Step 1: Read the data and create the individuals
#     with smartopen(filename) as f:
#         # Parse the lines in the file
#         for line in f:
#             rec = PEDRecord(line, delimiter)
#             
#             if onlyinds and (rec.ind_id not in onlyinds):
#                 continue
# 
#             ind = rec.create_individual(population)
#             ind.pedigree = p
#             ind.phenotypes['affected'] = affected_labels.get(rec.aff, None)
#             p[ind.label] = ind
# 
#             if rec.data:
#                 data_handler(p[ind.label], rec.data)
# 
#     # Step 2: Create the between-individual relationships
# 
#     # Fix the individual-level data: individuals currently only have parent-ids
#     # in their parent fields and not references to actual individuals
#     if connect_inds:
#         connect_individuals(p)
# 
#     # Step 3: Separate the individuals into pedigrees
#     pc = sort_pedigrees(p.individuals, population_handler)
# 
# 
#     return pc


import os
import glob
from pydigree.io import read_ped



PEDDIR = os.path.abspath(os.path.join(os.path.abspath(__file__), '..', '..', 'sample_pedigrees'))
peds = {}
for filename in glob.glob(PEDDIR + '/*ped'):
    ped = list(read_ped(filename).pedigrees)[0]
    pedname = os.path.basename(filename)[:-4]
    peds[pedname] = ped

def test_parents():
	ped = peds['fullsib']
	ped['1'].parents() == (None, None)
	ped['3'].parents() == (ped['1'], ped['2'])

def test_isfounder():
	ped = peds['first_cousins']
	assert ped['1'].is_founder()
	assert not ped['3'].is_founder()
	assert ped['4'].is_founder()


# Path: pydigree/io/base.py
# def read_ped(filename, population=None, delimiter=None, affected_labels=None,
#              population_handler=None, data_handler=None, connect_inds=True,
#              onlyinds=None):
#     """
#     Reads a plink format pedigree file, ie:
#     
#     ::    
#         familyid indid father mother sex whatever whatever whatever
#     
#     into a pydigree pedigree object, with optional population to
#     assign to pedigree members. If you don't provide a population
#     you can't simulate genotypes!
# 
# 
#     :param filename: The file to be read
#     :param population: The population to assign individuals to
#     :param delimiter: a string defining the field separator, 
#         default: any whitespace
#     :param affected_labels: The labels that determine affection status.
#     :param population_handler: a function to set up the population 
#     :param data_handler: a function to turn the 
#         data into useful individual information
#     :param connect_inds: build references between individuals. Requires all
#         individuals be present in the file
#     :param onlyinds: only include data for specified individuals 
# 
#     :type filename: string
#     :type population: Population
#     :type delimiter: string
#     :type affected_labels: dict (str -> value)
#     :type data_handler: callable
#     :type connect_inds: bool
#     :type onlyinds: iterable 
# 
# 
#     :returns: individuals contained in the pedigree file 
#     :rtype: PedigreeCollection
#     """
# 
#     if not affected_labels:
#         affected_labels = {'1': 0, '2': 1, 'A': 1, 'U': 0,
#                            'X': None, '-9': None}
# 
#     if not isinstance(data_handler, Callable):
#         data_handler = lambda *x: None
#     
#     if not isinstance(population_handler, Callable):
#         population_handler = lambda *x: None
# 
#     population = Population() if population is None else population
#     p = Pedigree()
# 
#     population_handler(p)
# 
#     # Step 1: Read the data and create the individuals
#     with smartopen(filename) as f:
#         # Parse the lines in the file
#         for line in f:
#             rec = PEDRecord(line, delimiter)
#             
#             if onlyinds and (rec.ind_id not in onlyinds):
#                 continue
# 
#             ind = rec.create_individual(population)
#             ind.pedigree = p
#             ind.phenotypes['affected'] = affected_labels.get(rec.aff, None)
#             p[ind.label] = ind
# 
#             if rec.data:
#                 data_handler(p[ind.label], rec.data)
# 
#     # Step 2: Create the between-individual relationships
# 
#     # Fix the individual-level data: individuals currently only have parent-ids
#     # in their parent fields and not references to actual individuals
#     if connect_inds:
#         connect_individuals(p)
# 
#     # Step 3: Separate the individuals into pedigrees
#     pc = sort_pedigrees(p.individuals, population_handler)
# 
# 
#     return pc


# Path: tests/test_individual.py
import os
import glob
from pydigree.io import read_ped




PEDDIR = os.path.abspath(os.path.join(os.path.abspath(__file__), '..', '..', 'sample_pedigrees'))
peds = {}
for filename in glob.glob(PEDDIR + '/*ped'):
    ped = list(read_ped(filename).pedigrees)[0]
    pedname = os.path.basename(filename)[:-4]
    peds[pedname] = ped

def test_parents():
	ped = peds['fullsib']
	ped['1'].parents() == (None, None)
	ped['3'].parents() == (ped['1'], ped['2'])

def test_isfounder():
	ped = peds['first_cousins']
	assert ped['1'].is_founder()
	assert not ped['3'].is_founder()
	assert ped['4'].is_founder()


def test_depth():