<s> """<STR_LIT>""" <EOL> import datetime <EOL> import ftplib <EOL> import gzip <EOL> import os <EOL> import subprocess <EOL> from argparse import ArgumentParser <EOL> import re <EOL> import shutil <EOL> FTP = "<STR_LIT>" <EOL> REMOTES = { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" } <EOL> def main ( org ) : <EOL> work_dir = "<STR_LIT>" % org <EOL> if not os . path . exists ( work_dir ) : <EOL> os . makedirs ( work_dir ) <EOL> conn = ftplib . FTP ( FTP , "<STR_LIT>" , "<STR_LIT>" ) <EOL> conn . cwd ( REMOTES [ org ] ) <EOL> os . chdir ( work_dir ) <EOL> files = [ ] <EOL> def add_files ( x ) : <EOL> if x . endswith ( "<STR_LIT>" ) : <EOL> files . append ( get_file ( x , REMOTES [ org ] , conn ) ) <EOL> conn . retrlines ( "<STR_LIT>" , add_files ) <EOL> out_file = "<STR_LIT>" % ( org , datetime . datetime . now ( ) . strftime ( "<STR_LIT>" ) ) <EOL> with open ( out_file , "<STR_LIT:w>" ) as out_handle : <EOL> for i , f in enumerate ( karyotype_sort ( files ) ) : <EOL> with gzip . open ( f ) as in_handle : <EOL> for line in in_handle : <EOL> if line . startswith ( "<STR_LIT:#>" ) : <EOL> if i == <NUM_LIT:0> : <EOL> out_handle . write ( line ) <EOL> else : <EOL> out_handle . write ( "<STR_LIT:\t>" . join ( fix_info ( fix_chrom ( line . rstrip ( ) . split ( "<STR_LIT:\t>" ) ) ) ) + "<STR_LIT:\n>" ) <EOL> subprocess . check_call ( [ "<STR_LIT>" , out_file ] ) <EOL> shutil . move ( out_file + "<STR_LIT>" , os . path . join ( os . pardir , out_file + "<STR_LIT>" ) ) <EOL> os . chdir ( os . pardir ) <EOL> subprocess . check_call ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , out_file + "<STR_LIT>" ] ) <EOL> shutil . rmtree ( work_dir ) <EOL> multi_whitespace = re . compile ( r"<STR_LIT>" ) <EOL> def fix_info ( parts ) : <EOL> """<STR_LIT>""" <EOL> parts [ <NUM_LIT:7> ] = multi_whitespace . sub ( "<STR_LIT:_>" , parts [ <NUM_LIT:7> ] ) <EOL> return parts <EOL> def fix_chrom ( parts ) : <EOL> MAX_CHROMOSOMES = <NUM_LIT:50> <EOL> if parts [ <NUM_LIT:0> ] in [ str ( x ) for x in range ( <NUM_LIT:1> , MAX_CHROMOSOMES ) ] + [ "<STR_LIT:X>" , "<STR_LIT:Y>" ] : <EOL> new_chrom = "<STR_LIT>" % parts [ <NUM_LIT:0> ] <EOL> elif parts [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> new_chrom = "<STR_LIT>" <EOL> else : <EOL> raise NotImplementedError ( parts ) <EOL> parts [ <NUM_LIT:0> ] = new_chrom <EOL> return parts <EOL> def get_file ( x , ftp_dir , conn ) : <EOL> if not os . path . exists ( x ) : <EOL> print "<STR_LIT>" % x <EOL> with open ( x , "<STR_LIT:wb>" ) as out_handle : <EOL> conn = ftplib . FTP ( FTP , "<STR_LIT>" , "<STR_LIT>" ) <EOL> conn . cwd ( ftp_dir ) <EOL> conn . retrbinary ( "<STR_LIT>" % x , out_handle . write ) <EOL> return x <EOL> def karyotype_sort ( xs ) : <EOL> """<STR_LIT>""" <EOL> def karyotype_keyfn ( x ) : <EOL> for suffix in [ "<STR_LIT>" ] : <EOL> if x . endswith ( suffix ) : <EOL> x = x [ : - len ( suffix ) ] <EOL> base = os . path . splitext ( os . path . basename ( x ) ) [ <NUM_LIT:0> ] <EOL> for prefix in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> if base . startswith ( prefix ) : <EOL> base = base [ len ( prefix ) : ] <EOL> parts = base . split ( "<STR_LIT:_>" ) <EOL> try : <EOL> parts [ <NUM_LIT:0> ] = int ( parts [ <NUM_LIT:0> ] ) <EOL> except ValueError : <EOL> pass <EOL> if isinstance ( parts [ <NUM_LIT:0> ] , basestring ) and parts [ <NUM_LIT:0> ] . startswith ( ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) ) : <EOL> parts . insert ( <NUM_LIT:0> , "<STR_LIT:z>" ) <EOL> elif parts [ <NUM_LIT:0> ] in [ "<STR_LIT:M>" , "<STR_LIT>" ] : <EOL> parts . insert ( <NUM_LIT:0> , "<STR_LIT:x>" ) <EOL> elif len ( parts ) > <NUM_LIT:1> : <EOL> parts . insert ( <NUM_LIT:0> , "<STR_LIT:y>" ) <EOL> return parts <EOL> return sorted ( xs , key = karyotype_keyfn ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> parser = ArgumentParser ( description = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , choices = REMOTES . keys ( ) , <EOL> help = "<STR_LIT>" ) <EOL> args = parser . parse_args ( ) <EOL> main ( args . org_build ) </s>
