# -*- coding: utf-8 -*-
from __future__ import division
from collections import namedtuple
import logging
import subprocess

logger = logging.getLogger(__name__)

SexGuess = namedtuple('SexGuess', ['x_coverage', 'y_coverage', 'sex'])


def predict_sex(x_coverage, y_coverage):
    """Make a simple yet accurate prediction of a samples sex.

    The calculation is based on the average coverage across the X and Y
    chromosomes. Note that an extrapolation from a subsection of bases is
    usually quite sufficient.

    Args:
        x_coverage (float): estimated average coverage on X chromosome
        y_coverage (float): estimated average coverage on Y chromosome

    Returns:
        str: prediction, ['male', 'female', 'unknown']
    """
    # algorithm doesn't work if coverage is missing for X chromosome
    if x_coverage == 0:
        return 'unknown'
    elif (y_coverage > 0) and (x_coverage / y_coverage < 10):
        # this is the entire prediction, it's usually very obvious
        return 'male'
    else:
        # the few reads mapping to the Y chromosomes are artifacts
        return 'female'


def sex_from_bam(bam_path, prefix=''):
    """Predict the sex from a BAM alignment file.

    Args:
        bam_path (path): path to a BAM alignment file
        prefix (str, optional): string to prefix to 'X', 'Y'

    Returns:
        SexGuess: tuple of X coverage, Y coverage, and sex prediction

    Examples:
        >>> sex_from_bam('alignment.bam', prefix='chr')
        SexGuess(x_coverage=123.31, y_coverage=0.13, sex='female')
    """
    # make up some sex chromosome regions
    regions = ["{}X:1-59373566".format(prefix), "{}Y:69362-11375310".format(prefix)]
    averages = []
    for region in regions:
        command = ["sambamba depth region -L {} {}".format(region, bam_path)]
        logger.debug("calling: %s", command)
        bed_out = subprocess.check_output(command, shell=True).decode('utf-8')
        bed_rows = [line.split() for line in bed_out.splitlines()
                    if not line.startswith('#')]
        if len(bed_rows) == 1:
            averages.append(float(bed_rows[0][4]))
        else:
            chromosome = region.split(':')[0]
            logger.warning("couldn't find any reads on %s-chromosome", chromosome)
            averages.append(0.)

    # make the guess
    x_coverage, y_coverage = list(averages)
    sex = predict_sex(x_coverage, y_coverage)
    return SexGuess(x_coverage, y_coverage, sex)

@@ -816,17 +816,17 @@
 %0A    if 
-x
+y
 _coverag
@@ -853,23 +853,20 @@
 rn '
-unknown
+male
 '%0A    el
 if (
@@ -865,64 +865,171 @@
   el
-if (y_coverage %3E 0) and (x_coverage / y_coverage
+se:%0A        ratio = x_coverage / y_coverage%0A        if x_coverage == 0 or (ratio %3E 12 and ratio
  %3C 10
+0
 ):%0A
+            return 'unknown'%0A        elif ratio %3C= 12:%0A    
     
@@ -1091,32 +1091,36 @@
 obvious%0A        
+    
 return 'male'%0A  
@@ -1113,38 +1113,46 @@
 turn 'male'%0A    
+  
+  
 else:%0A
+    
         # the fe
@@ -1202,16 +1202,20 @@
 tifacts%0A
+    
         

update sex prediction with "unknown" span