<s> """<STR_LIT>""" <EOL> import os <EOL> import subprocess <EOL> import sys <EOL> FTP_DIR = "<STR_LIT>" <EOL> VERSION = "<STR_LIT>" <EOL> BCBIO_NEXTGEN_BASE = "<STR_LIT>" <EOL> def main ( ) : <EOL> work_dir = "<STR_LIT>" <EOL> if not os . path . exists ( work_dir ) : <EOL> os . makedirs ( work_dir ) <EOL> os . chdir ( work_dir ) <EOL> ref_file = BCBIO_NEXTGEN_BASE + "<STR_LIT>" <EOL> fnames = [ reorder_reference ( x , ref_file ) for x in get_cosmic_files ( ) ] <EOL> grc_cosmic = combine_cosmic ( fnames , ref_file ) <EOL> hg_cosmic = map_coords_to_ucsc ( grc_cosmic , ref_file ) <EOL> for ready_file in [ bgzip_vcf ( x ) for x in [ grc_cosmic , hg_cosmic ] ] : <EOL> upload_to_s3 ( ready_file ) <EOL> upload_to_s3 ( ready_file . replace ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> upload_to_s3 ( ready_file + "<STR_LIT>" ) <EOL> def upload_to_s3 ( fname ) : <EOL> upload_script = os . path . join ( os . path . dirname ( __file__ ) , "<STR_LIT>" ) <EOL> subprocess . check_call ( [ sys . executable , upload_script , fname , "<STR_LIT>" , <EOL> "<STR_LIT>" % os . path . basename ( fname ) , "<STR_LIT>" ] ) <EOL> def map_coords_to_ucsc ( grc_cosmic , ref_file ) : <EOL> hg19_ref_file = ref_file . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> out_file = grc_cosmic . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if not os . path . exists ( out_file ) : <EOL> tmp_file = "<STR_LIT>" % os . path . splitext ( out_file ) <EOL> with open ( tmp_file , "<STR_LIT:w>" ) as out_handle : <EOL> with open ( grc_cosmic ) as in_handle : <EOL> for line in in_handle : <EOL> if line . startswith ( "<STR_LIT:#>" ) and not line . startswith ( "<STR_LIT>" ) : <EOL> out_handle . write ( line ) <EOL> with open ( grc_cosmic ) as in_handle : <EOL> for line in in_handle : <EOL> if line . startswith ( "<STR_LIT>" ) : <EOL> line = _rename_to_ucsc ( line ) <EOL> out_handle . write ( line ) <EOL> with open ( grc_cosmic ) as in_handle : <EOL> for line in in_handle : <EOL> if not line . startswith ( ( "<STR_LIT>" , "<STR_LIT:#>" ) ) : <EOL> line = _rename_to_ucsc ( line ) <EOL> out_handle . write ( line ) <EOL> subprocess . check_call ( [ "<STR_LIT>" , "<STR_LIT>" , hg19_ref_file , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , tmp_file , "<STR_LIT>" , out_file ] ) <EOL> return out_file <EOL> def _rename_to_ucsc ( line ) : <EOL> chrom , rest = line . split ( "<STR_LIT:\t>" , <NUM_LIT:1> ) <EOL> if chrom == "<STR_LIT>" : <EOL> new_chrom = "<STR_LIT>" <EOL> else : <EOL> new_chrom = "<STR_LIT>" % chrom <EOL> return "<STR_LIT>" % ( new_chrom , rest ) <EOL> def combine_cosmic ( fnames , ref_file ) : <EOL> out_file = "<STR_LIT>" % VERSION <EOL> if not os . path . exists ( out_file ) : <EOL> cmd = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , ref_file , "<STR_LIT>" , out_file , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:null>" ] <EOL> for v in fnames : <EOL> cmd += [ "<STR_LIT>" , v ] <EOL> subprocess . check_call ( cmd ) <EOL> return out_file <EOL> def reorder_reference ( fname , ref_file ) : <EOL> """<STR_LIT>""" <EOL> out_file = "<STR_LIT>" % os . path . splitext ( fname ) <EOL> bcbiov_jar = BCBIO_NEXTGEN_BASE + "<STR_LIT>" <EOL> if not os . path . exists ( out_file ) : <EOL> cmd = [ "<STR_LIT>" , "<STR_LIT>" , bcbiov_jar , "<STR_LIT>" , "<STR_LIT>" , fname , ref_file , "<STR_LIT>" ] <EOL> subprocess . check_call ( cmd ) <EOL> return out_file <EOL> def get_cosmic_files ( ) : <EOL> fnames = [ ] <EOL> for ctype in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> to_get = "<STR_LIT>" % ( FTP_DIR , ctype , VERSION ) <EOL> fname_gz = os . path . basename ( to_get ) <EOL> fname = os . path . splitext ( fname_gz ) [ <NUM_LIT:0> ] <EOL> if not os . path . exists ( fname ) : <EOL> if not os . path . exists ( fname_gz ) : <EOL> subprocess . check_call ( [ "<STR_LIT>" , to_get ] ) <EOL> subprocess . check_call ( [ "<STR_LIT>" , fname_gz ] ) <EOL> fnames . append ( fname ) <EOL> return fnames <EOL> def bgzip_vcf ( in_file ) : <EOL> out_file = in_file + "<STR_LIT>" <EOL> if not os . path . exists ( out_file ) : <EOL> subprocess . check_call ( "<STR_LIT>" % ( in_file , out_file ) , shell = True ) <EOL> tabix_file = out_file + "<STR_LIT>" <EOL> if not os . path . exists ( tabix_file ) : <EOL> subprocess . check_call ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , out_file ] ) <EOL> return out_file <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
