{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"### запрос с символами генов в списке:","metadata":{}},{"cell_type":"code","source":"queries = [\n    \n'CD86',\n'CD274',\n'TNFRSF14',\n'PVR',\n'NECTIN2',\n'CD47',\n'CD40',\n'CD52',\n'CD3D',\n'CD8A',\n'NCAM1',\n'CD19',\n'CD33',\n'ITGAX',\n'HLA-A',\n'PTPRC',\n'IL3RA',\n'ENG',\n'ITGA6',\n'CD4',\n'CD44',\n'CD14',\n'FCGR3A',\n'IL2RA',\n'PTPRC',\n'PDCD1',\n'TIGIT',\n'MS4A1',\n'NCR1',\n'PECAM1',\n'PDPN',\n'MCAM',\n'IGHM',\n'FCGR2A',\n'ITGAE',\n'CD69',\n'CTLA4',\n'LAMP1',\n'FAS',\n'HLA-DRA',\n'ITGAM',\n'FCGR1A',\n'THBD',\n'CD58',\n'ENTPD1',\n'CX3CR1',\n'ITGAL',\n'CD79B',\n'ITGB7',\n'GP1BA',\n'ICAM1',\n'SELP',\n'IFNGR1',\n'IL2RB',\n'FCER1A',\n'ITGA2B',\n'TNFRSF9',\n'CD83',\n'IL4R',\n'ANPEP',\n'ITGB1',\n'ITGA2',\n'CD81',\n'ITGB2',\n'CD28',\n'CD38',\n'IL7R',\n'PTPRC',\n'CD22',\n'TFRC',\n'DPP4',\n'CSF1R',\n'CD63',\n'NRP1',\n'CD36',\n'SIRPA',\n'CD72',\n'CD93',\n'ITGA1',\n'ITGA4',\n'NT5E',\n'CD9',\n'OLR1',\n'KIR2DL3',\n'SLAMF7',\n'SLAMF6',\n'SELPLG',\n'SIGLEC7',\n'HLA-E',\n'CD82',\n'C5AR1',\n'GGT1'\n\n\n]","metadata":{"execution":{"iopub.status.busy":"2022-12-12T06:56:22.101670Z","iopub.execute_input":"2022-12-12T06:56:22.102400Z","iopub.status.idle":"2022-12-12T06:56:22.124995Z","shell.execute_reply.started":"2022-12-12T06:56:22.102326Z","shell.execute_reply":"2022-12-12T06:56:22.123497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### размер окрестности:","metadata":{}},{"cell_type":"code","source":"neighbourhood = 1","metadata":{"execution":{"iopub.status.busy":"2022-12-12T06:56:22.131313Z","iopub.execute_input":"2022-12-12T06:56:22.131660Z","iopub.status.idle":"2022-12-12T06:56:22.137267Z","shell.execute_reply.started":"2022-12-12T06:56:22.131621Z","shell.execute_reply":"2022-12-12T06:56:22.136244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-12-12T06:56:22.139251Z","iopub.execute_input":"2022-12-12T06:56:22.139889Z","iopub.status.idle":"2022-12-12T06:56:22.157299Z","shell.execute_reply.started":"2022-12-12T06:56:22.139806Z","shell.execute_reply":"2022-12-12T06:56:22.155906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fn = '/kaggle/input/ppisim100neighbors/BIOGRID-ALL-4.3.195.tab3.txt'\ndf_ppi = pd.read_csv(fn, sep ='\\t')","metadata":{"execution":{"iopub.status.busy":"2022-12-12T06:56:22.160382Z","iopub.execute_input":"2022-12-12T06:56:22.160779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_ppi.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_ppi['Official Symbol Interactor A'].replace('-', np.nan, inplace=True)\n\ndf_ppi['Official Symbol Interactor A'].replace('nan', np.nan, inplace=True)\n\ndf_ppi = df_ppi[df_ppi['Official Symbol Interactor A'].notna()]\n\ndf_ppi = df_ppi[df_ppi['Official Symbol Interactor A'].notnull()]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_ppi['Official Symbol Interactor B'].replace('-', np.nan, inplace=True)\n\ndf_ppi['Official Symbol Interactor B'].replace('nan', np.nan, inplace=True)\n\ndf_ppi = df_ppi[df_ppi['Official Symbol Interactor B'].notna()]\n\ndf_ppi = df_ppi[df_ppi['Official Symbol Interactor B'].notnull()]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import networkx as nx","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"M = nx.from_pandas_edgelist(df_ppi, source='Official Symbol Interactor A', target='Official Symbol Interactor B', create_using=nx.MultiGraph())","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(nx.info(M))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"G = nx.Graph()\nfor u,v,data in M.edges(data=True):\n    w = data['weight'] if 'weight' in data else 1.0\n    if G.has_edge(u,v):\n        G[u][v]['weight'] += w\n    else:\n        G.add_edge(u, v, weight=w)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(nx.info(G))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\nfrom community import community_louvain\nimport matplotlib.pyplot as plt\n%matplotlib inline","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for query in tqdm(queries):\n    try:\n        neigbors = nx.single_source_shortest_path_length(G, query, neighbourhood)\n        neigbors_occurence = [n for n in neigbors]\n        k = G.subgraph(neigbors_occurence)\n        fig = plt.figure(figsize=(15, 15))\n        d = dict(k.degree)\n        communities =community_louvain.best_partition(k)\n        community_id = [communities[node] for node in k.nodes()]\n        pos = nx.spring_layout(k)\n        nx.draw(\n        k,\n        with_labels=True,\n        edge_color=['silver'] * len(k.edges()),\n        cmap=plt.cm.tab20,\n        node_color=community_id,\n        nodelist=d.keys(), \n        node_size=[v * 100 for v in d.values()]\n        )\n        plt.savefig(f\"./{query}.png\", dpi=300, bbox_inches='tight')\n        plt.title(f\"{query} PPI neighbors\")\n        plt.show()\n    except:\n        print(f'symbol {query} not in graph, please try aliases')\n        continue","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}