<s> from __future__ import division <EOL> from collections import namedtuple <EOL> import logging <EOL> import subprocess <EOL> logger = logging . getLogger ( __name__ ) <EOL> SexGuess = namedtuple ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def predict_sex ( x_coverage , y_coverage ) : <EOL> """<STR_LIT>""" <EOL> if x_coverage == <NUM_LIT:0> : <EOL> return '<STR_LIT>' <EOL> elif ( y_coverage > <NUM_LIT:0> ) and ( x_coverage / y_coverage < <NUM_LIT:10> ) : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return '<STR_LIT>' <EOL> def sex_from_bam ( bam_path , prefix = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> regions = [ "<STR_LIT>" . format ( prefix ) , <EOL> "<STR_LIT>" . format ( prefix ) ] <EOL> averages = [ ] <EOL> for region in regions : <EOL> command = [ "<STR_LIT>" . format ( region , bam_path ) ] <EOL> logger . debug ( "<STR_LIT>" , command ) <EOL> bed_out = subprocess . check_output ( command , shell = True ) . decode ( '<STR_LIT:utf-8>' ) <EOL> bed_rows = [ line . split ( ) for line in bed_out . splitlines ( ) <EOL> if not line . startswith ( '<STR_LIT:#>' ) ] <EOL> if len ( bed_rows ) == <NUM_LIT:1> : <EOL> averages . append ( float ( bed_rows [ <NUM_LIT:0> ] [ <NUM_LIT:4> ] ) ) <EOL> else : <EOL> chromosome = region . split ( '<STR_LIT::>' ) [ <NUM_LIT:0> ] <EOL> logger . warn ( "<STR_LIT>" , chromosome ) <EOL> averages . append ( <NUM_LIT:0.> ) <EOL> x_coverage , y_coverage = list ( averages ) <EOL> sex = predict_sex ( x_coverage , y_coverage ) <EOL> return SexGuess ( x_coverage , y_coverage , sex ) </s>
