# Path: genda/formats/genotype.py
# class Genotype(object):
#     __doc__ = """ 
#     Base class for genotypes. Not meant to be used directly.
# 
#     %{param)s
#     data : genotype dataframe
# 
#     %(extra_param)s
#     """
#     def __init__(self, data):
#         self.df = data
#         self.ix = self.df.ix
# 
# 
#     def dendrogram(self):
#         """ Plots dendrogram
#         """
#         from scipy.cluster.hierarchy import linkage, dendrogram
#         g = self.geno.copy()
#         X = g.as_matrix()
#         Z = linkage(X,'single')
#         dendrogram(Z)
# 
# 
#     def hardyweinberg(self, snp, excludeNan = True):
#         """ Calculate hardyweinberg
# 
#         Parameters
#         ----------
#         snp : identifier to run the test at 
#         excludeNan : exlude nas
# 
#         Returns
#         -------
#         boolean
# 
#         """
#         from scipy.stats import chisquare
#         if excludeNan:
#             n=sum([0 if np.isnan(x) else 1 for x in self.geno.ix[snp,:]])
#         else:
#             n=self.geno.ix[snp,:].size[1]
#         q = float(sum([0 if np.isnan(x) else x for x in self.geno.ix[snp,:]]))/(2*n)
#         p = 1-q
#         probs=[p**2, 2*p*q, q**2]
#         exp=np.array([probs[0]*n, probs[1]*n, probs[2]*n])
#         if excludeNan:
#             obs=np.array([sum([1 if x == 0 else 0 for x in self.geno.ix[snp,:]]), 
#                 sum([1 if x == 1 else 0 for x in self.geno.ix[snp,:]]),\
#                 sum([1 if x == 2 else 0 for x in self.geno.ix[snp,:]])])
#         else:
#             obs=np.array([sum([1 if x == 0 or np.isnan(x) else\
#                     0 for x in self.geno.ix[snp,:]]),\
#                     sum([1 if x == 1 else 0 for x in self.geno.ix[snp,:]]), 
#                     sum([1 if x == 2 else 0 for x in self.geno.ix[snp,:]])])
#         if chisquare(obs,exp)[1] > 0.05:
#             return True
#         else:
#             return False
# 
# 
#     def geno_save(self, f):
#         """ Save genotype object
# 
#         Paremeters
#         ----------
#         f : file location
#         """
#         # Hmm how to remove the raw df temporarily before saving?
#         del self.df
#         with open(f, 'w') as temp:
#             dump(self, temp)
#             temp.close()


from itertools import cycle
from .genotype import Genotype
        from copy import deepcopy
        from Bio import SeqIO
        from Bio import pairwise2
import functools
import numpy as np
import pandas as pd
def parse_chr(entry):
    try:
        chrom = int(entry.lstrip("chr"))
        return(chrom)
    except ValueError:
        print("Chromosomes must be numeric")



def parse_geno(entry, GT=0):
    """ 
    :TODO 
    Need to somehow keep phase, maybe use multi_index, but only
    works if haplotype is contigous across whole section.  Maybe
    groups to denote haplotype regions?
    """
    try:
        g = entry.split(":")[GT]
    except:
        g = entry.split(":")[0]
    if g == "0/0" or g == "0|0" or g == '0':
        return 0
    elif g == "1/1" or g == "1|1" or g=='1':
        return 2
    elif g == "0/1" or g == "0|1" or g == "1/0" or g == "1|0":
        return 1
    else:
        return np.NaN



# Path: genda/formats/genotype.py
# class Genotype(object):
#     __doc__ = """ 
#     Base class for genotypes. Not meant to be used directly.
# 
#     %{param)s
#     data : genotype dataframe
# 
#     %(extra_param)s
#     """
#     def __init__(self, data):
#         self.df = data
#         self.ix = self.df.ix
# 
# 
#     def dendrogram(self):
#         """ Plots dendrogram
#         """
#         from scipy.cluster.hierarchy import linkage, dendrogram
#         g = self.geno.copy()
#         X = g.as_matrix()
#         Z = linkage(X,'single')
#         dendrogram(Z)
# 
# 
#     def hardyweinberg(self, snp, excludeNan = True):
#         """ Calculate hardyweinberg
# 
#         Parameters
#         ----------
#         snp : identifier to run the test at 
#         excludeNan : exlude nas
# 
#         Returns
#         -------
#         boolean
# 
#         """
#         from scipy.stats import chisquare
#         if excludeNan:
#             n=sum([0 if np.isnan(x) else 1 for x in self.geno.ix[snp,:]])
#         else:
#             n=self.geno.ix[snp,:].size[1]
#         q = float(sum([0 if np.isnan(x) else x for x in self.geno.ix[snp,:]]))/(2*n)
#         p = 1-q
#         probs=[p**2, 2*p*q, q**2]
#         exp=np.array([probs[0]*n, probs[1]*n, probs[2]*n])
#         if excludeNan:
#             obs=np.array([sum([1 if x == 0 else 0 for x in self.geno.ix[snp,:]]), 
#                 sum([1 if x == 1 else 0 for x in self.geno.ix[snp,:]]),\
#                 sum([1 if x == 2 else 0 for x in self.geno.ix[snp,:]])])
#         else:
#             obs=np.array([sum([1 if x == 0 or np.isnan(x) else\
#                     0 for x in self.geno.ix[snp,:]]),\
#                     sum([1 if x == 1 else 0 for x in self.geno.ix[snp,:]]), 
#                     sum([1 if x == 2 else 0 for x in self.geno.ix[snp,:]])])
#         if chisquare(obs,exp)[1] > 0.05:
#             return True
#         else:
#             return False
# 
# 
#     def geno_save(self, f):
#         """ Save genotype object
# 
#         Paremeters
#         ----------
#         f : file location
#         """
#         # Hmm how to remove the raw df temporarily before saving?
#         del self.df
#         with open(f, 'w') as temp:
#             dump(self, temp)
#             temp.close()


# Path: genda/formats/panVCF.py
from itertools import cycle
from .genotype import Genotype
        from copy import deepcopy
        from Bio import SeqIO
        from Bio import pairwise2
import functools
import numpy as np
import pandas as pd

def parse_chr(entry):
    try:
        chrom = int(entry.lstrip("chr"))
        return(chrom)
    except ValueError:
        print("Chromosomes must be numeric")



def parse_geno(entry, GT=0):
    """ 
    :TODO 
    Need to somehow keep phase, maybe use multi_index, but only
    works if haplotype is contigous across whole section.  Maybe
    groups to denote haplotype regions?
    """
    try:
        g = entry.split(":")[GT]
    except:
        g = entry.split(":")[0]
    if g == "0/0" or g == "0|0" or g == '0':
        return 0
    elif g == "1/1" or g == "1|1" or g=='1':
        return 2
    elif g == "0/1" or g == "0|1" or g == "1/0" or g == "1|0":
        return 1
    else:
        return np.NaN



class VCF(Genotype):