# Path: xenoGI/analysis.py
# def printTable(L,indent=0,fileF=sys.stdout):
#     '''Given tabular data in a list of lists (where sublists are rows)
# print nicely so columns line up. Indent is an optional number of blank spaces to put in front of each row.'''
#     # get max width for each column
#     colMax=[]
#     for col in range(len(L[0])):
#         mx=0
#         for row in L:
#             if len(row[col]) > mx:
#                 mx = len(row[col])
#         colMax.append(mx)
#     
#     # print
#     for row in L:
#         for col in range(len(row)):
#             row[col]=row[col]+' ' * (colMax[col]-len(row[col]))
#             
#     for row in L:
#         printStr = " "*indent + " | ".join(row)
#         print(printStr.rstrip(),file=fileF)


import sys,numpy,os,random,glob,copy,shutil
from scipy.signal import find_peaks
from Bio import Phylo
from multiprocessing import Pool
from . import blast,genomes,trees,scores,DTLOR_DP,new_DTLOR_DP,islands
from .Family import *
from .Island import *
from .Tree import *
from .analysis import printTable
    summaryL.append(["Total number of Families",str(len(familiesO.familiesD))])
    summaryL.append(["Total number of LocusFamilies",str(len(familiesO.locusFamiliesD))])
    summaryL.append(["Total number of genes in families",str(len(familiesO.getAllGenes()))])

    singleLfFams = 0
    multipleLfFams = 0
    singleStrainFams = 0
    multipleStrainFams = 0
    famLenL = []
    for fam in familiesO.iterFamilies():
        famLenL.append(fam.geneCount())
        # locus families
        if len(fam.getLocusFamilies()) == 1:
            singleLfFams += 1
        else:
            multipleLfFams += 1
        # strains
        if len(set(fam.iterStrains())) == 1:
            singleStrainFams += 1
        else:
            multipleStrainFams += 1

    sizeDistSummaryL = [numpy.quantile(famLenL,0),numpy.quantile(famLenL,.25),numpy.quantile(famLenL,.5),numpy.quantile(famLenL,.75),numpy.quantile(famLenL,1)]
    summaryL.append(["Family size dist [min,q1,q2,q3,max]",str(sizeDistSummaryL)])
    
    summaryL.append(["Number of families with one LocusFamily",str(singleLfFams)])
    summaryL.append(["Number of families with multiple LocusFamilies",str(multipleLfFams)])
    summaryL.append(["Number of families with gene(s) in only one strain",str(singleStrainFams)])
    summaryL.append(["Number of families with genes in multiple strains",str(multipleStrainFams)])
    

# Path: xenoGI/analysis.py
# def printTable(L,indent=0,fileF=sys.stdout):
#     '''Given tabular data in a list of lists (where sublists are rows)
# print nicely so columns line up. Indent is an optional number of blank spaces to put in front of each row.'''
#     # get max width for each column
#     colMax=[]
#     for col in range(len(L[0])):
#         mx=0
#         for row in L:
#             if len(row[col]) > mx:
#                 mx = len(row[col])
#         colMax.append(mx)
#     
#     # print
#     for row in L:
#         for col in range(len(row)):
#             row[col]=row[col]+' ' * (colMax[col]-len(row[col]))
#             
#     for row in L:
#         printStr = " "*indent + " | ".join(row)
#         print(printStr.rstrip(),file=fileF)


# Path: xenoGI/families.py
import sys,numpy,os,random,glob,copy,shutil
from scipy.signal import find_peaks
from Bio import Phylo
from multiprocessing import Pool
from . import blast,genomes,trees,scores,DTLOR_DP,new_DTLOR_DP,islands
from .Family import *
from .Island import *
from .Tree import *
from .analysis import printTable

    summaryL.append(["Total number of Families",str(len(familiesO.familiesD))])
    summaryL.append(["Total number of LocusFamilies",str(len(familiesO.locusFamiliesD))])
    summaryL.append(["Total number of genes in families",str(len(familiesO.getAllGenes()))])

    singleLfFams = 0
    multipleLfFams = 0
    singleStrainFams = 0
    multipleStrainFams = 0
    famLenL = []
    for fam in familiesO.iterFamilies():
        famLenL.append(fam.geneCount())
        # locus families
        if len(fam.getLocusFamilies()) == 1:
            singleLfFams += 1
        else:
            multipleLfFams += 1
        # strains
        if len(set(fam.iterStrains())) == 1:
            singleStrainFams += 1
        else:
            multipleStrainFams += 1

    sizeDistSummaryL = [numpy.quantile(famLenL,0),numpy.quantile(famLenL,.25),numpy.quantile(famLenL,.5),numpy.quantile(famLenL,.75),numpy.quantile(famLenL,1)]
    summaryL.append(["Family size dist [min,q1,q2,q3,max]",str(sizeDistSummaryL)])
    
    summaryL.append(["Number of families with one LocusFamily",str(singleLfFams)])
    summaryL.append(["Number of families with multiple LocusFamilies",str(multipleLfFams)])
    summaryL.append(["Number of families with gene(s) in only one strain",str(singleStrainFams)])
    summaryL.append(["Number of families with genes in multiple strains",str(multipleStrainFams)])
    

    printTable(summaryL,indent=2,fileF=outputSummaryF)