<s> import os <EOL> import sys <EOL> import sqlalchemy as sql <EOL> import compression as Z <EOL> from collections import defaultdict <EOL> from gemini . config import read_gemini_config <EOL> import gemini_utils as util <EOL> from gemini_constants import * <EOL> def get_pathways ( args ) : <EOL> version_dic = defaultdict ( ) <EOL> version_dic = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> config = read_gemini_config ( args = args ) <EOL> path_dirname = config [ "<STR_LIT>" ] <EOL> if args . version in version_dic : <EOL> path_file = os . path . join ( path_dirname , version_dic [ args . version ] ) <EOL> else : <EOL> sys . exit ( "<STR_LIT>" ) <EOL> agn_paths = defaultdict ( list ) <EOL> hgnc_paths = defaultdict ( list ) <EOL> ensembl_paths = defaultdict ( list ) <EOL> for line in open ( path_file , '<STR_LIT:r>' ) : <EOL> fields = line . strip ( ) . split ( "<STR_LIT:\t>" ) <EOL> uniprot = fields [ <NUM_LIT:0> ] <EOL> agn = fields [ <NUM_LIT:1> ] <EOL> hgnc = fields [ <NUM_LIT:2> ] <EOL> ensid = fields [ <NUM_LIT:3> ] <EOL> ens_transcript = fields [ <NUM_LIT:4> ] <EOL> hsa = fields [ <NUM_LIT:5> ] <EOL> path = fields [ <NUM_LIT:6> ] if fields [ <NUM_LIT:6> ] != '<STR_LIT:None>' else None <EOL> if path is not None and path . startswith ( "<STR_LIT>" ) : <EOL> path = path [ <NUM_LIT:5> : ] <EOL> path = path . replace ( "<STR_LIT:;>" , "<STR_LIT::>" ) <EOL> agn_paths [ ( agn , ens_transcript ) ] . append ( path ) <EOL> hgnc_paths [ ( hgnc , ens_transcript ) ] . append ( path ) <EOL> ensembl_paths [ ( ensid , ens_transcript ) ] . append ( path ) <EOL> return agn_paths , hgnc_paths , ensembl_paths <EOL> def _get_pathways ( gene , transcript , pathways , allow_none = True ) : <EOL> pathways = set ( pathways ) <EOL> if None in pathways : <EOL> if len ( pathways ) > <NUM_LIT:1> or allow_none is False : <EOL> pathways . remove ( None ) <EOL> return pathways <EOL> def _report_variant_pathways ( res , args , idx_to_sample ) : <EOL> ( agn_paths , hgnc_paths , ensembl_paths ) = get_pathways ( args ) <EOL> unpack = Z . unpack_genotype_blob <EOL> for r in res : <EOL> try : <EOL> gt_types = unpack ( r [ '<STR_LIT>' ] ) <EOL> gts = unpack ( r [ '<STR_LIT>' ] ) <EOL> except : <EOL> unpack = Z . snappy_unpack_blob <EOL> gt_types = unpack ( r [ '<STR_LIT>' ] ) <EOL> gts = unpack ( r [ '<STR_LIT>' ] ) <EOL> gene = str ( r [ '<STR_LIT>' ] ) <EOL> trans = str ( r [ '<STR_LIT>' ] ) <EOL> pathways = [ ] <EOL> if ( gene , trans ) in agn_paths : <EOL> pathways = _get_pathways ( gene , trans , agn_paths [ ( gene , trans ) ] , <EOL> allow_none = False ) <EOL> elif ( gene , trans ) in hgnc_paths : <EOL> pathways = _get_pathways ( gene , trans , hgnc_paths [ ( gene , trans ) ] , <EOL> allow_none = False ) <EOL> elif ( gene , trans ) in ensembl_paths : <EOL> pathways = _get_pathways ( gene , trans , ensembl_paths [ ( gene , trans ) ] , <EOL> allow_none = False ) <EOL> pathlist = "<STR_LIT:U+002C>" . join ( pathways ) <EOL> for idx , gt_type in enumerate ( gt_types ) : <EOL> if ( gt_type == HET or gt_type == HOM_ALT ) and len ( pathways ) > <NUM_LIT:0> : <EOL> print "<STR_LIT:\t>" . join ( [ r [ '<STR_LIT>' ] , str ( r [ '<STR_LIT:start>' ] ) , str ( r [ '<STR_LIT:end>' ] ) , r [ '<STR_LIT>' ] , r [ '<STR_LIT>' ] , r [ '<STR_LIT>' ] , idx_to_sample [ idx ] , gts [ idx ] , gene , trans , pathlist ] ) <EOL> def get_ind_pathways ( conn , metadata , args ) : <EOL> idx_to_sample = util . map_indices_to_samples ( metadata ) <EOL> query = "<STR_LIT>" <EOL> res = conn . execute ( sql . text ( query ) ) <EOL> print '<STR_LIT:\t>' . join ( [ '<STR_LIT>' , '<STR_LIT:start>' , '<STR_LIT:end>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> _report_variant_pathways ( res , args , idx_to_sample ) <EOL> def get_ind_lof_pathways ( conn , metadata , args ) : <EOL> idx_to_sample = util . map_indices_to_samples ( metadata ) <EOL> query = "<STR_LIT>" <EOL> res = conn . execute ( sql . text ( query ) ) <EOL> print '<STR_LIT:\t>' . join ( [ '<STR_LIT>' , '<STR_LIT:start>' , '<STR_LIT:end>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> _report_variant_pathways ( res , args , idx_to_sample ) <EOL> def pathways ( parser , args ) : <EOL> import database <EOL> conn , metadata = database . get_session_metadata ( args . db ) <EOL> if ( not args . lof ) : <EOL> get_ind_pathways ( conn , metadata , args ) <EOL> else : <EOL> get_ind_lof_pathways ( conn , metadata , args ) </s>
