|
| 1 | +import os |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import pandas as pd |
| 5 | + |
| 6 | + |
| 7 | +def Create_Annovar_input(args): |
| 8 | + hasepath = args.path |
| 9 | + studyname = args.study_name |
| 10 | + savepath = args.out |
| 11 | + |
| 12 | + if os.path.exists(hasepath + '/probes/' + studyname + '_selected.h5'): |
| 13 | + probes = pd.read_hdf(hasepath + '/probes/' + studyname + '_selected.h5', mode="r") |
| 14 | + else: |
| 15 | + probes = pd.read_hdf(hasepath + '/probes/' + studyname + '.h5', mode="r") |
| 16 | + print(probes.shape) |
| 17 | + |
| 18 | + if os.path.exists(hasepath + '/probes/' + studyname + '_hash_table.csv.gz'): |
| 19 | + hashtable = pd.read_csv(hasepath + '/probes/' + studyname + '_hash_table.csv.gz', compression="gzip", sep='\t') |
| 20 | + else: |
| 21 | + hashtable = pd.read_csv(hasepath + '/probes/' + studyname + '_hash_table.csv', sep='\t') |
| 22 | + |
| 23 | + hashtable['allele1'] = hashtable['keys'] |
| 24 | + unhashed_probes = probes.merge(hashtable, on='allele1', how="left") |
| 25 | + unhashed_probes = unhashed_probes.drop(columns=["keys", "allele1"]) |
| 26 | + unhashed_probes = unhashed_probes.rename(columns={'allele': 'allele1'}) |
| 27 | + |
| 28 | + # reload hashtable for other allele |
| 29 | + |
| 30 | + if os.path.exists(hasepath + '/probes/' + studyname + '_hash_table.csv.gz'): |
| 31 | + hashtable = pd.read_csv(hasepath + '/probes/' + studyname + '_hash_table.csv.gz', compression="gzip", sep='\t') |
| 32 | + else: |
| 33 | + hashtable = pd.read_csv(hasepath + '/probes/' + studyname + '_hash_table.csv', sep='\t') |
| 34 | + |
| 35 | + hashtable['allele2'] = hashtable['keys'] |
| 36 | + unhashed_probes = unhashed_probes.merge(hashtable, on='allele2', how="left") |
| 37 | + unhashed_probes = unhashed_probes.drop(columns=["keys", "allele2"]) |
| 38 | + unhashed_probes = unhashed_probes.rename(columns={'allele': 'allele2'}) |
| 39 | + |
| 40 | + # clean |
| 41 | + annovar_input = unhashed_probes.drop(columns=["ID", "distance"]) |
| 42 | + annovar_input["bp2"] = annovar_input["bp"] |
| 43 | + annovar_input["index_col"] = annovar_input.index |
| 44 | + annovar_input = annovar_input[['CHR', 'bp', "bp2", "allele1", "allele2", "index_col"]] |
| 45 | + |
| 46 | + # print('Shape', annovar_input.shape) |
| 47 | + # if args.variants is None: |
| 48 | + # pass |
| 49 | + # else: |
| 50 | + # used_indices = pd.read_csv(args.variants, header=None) |
| 51 | + # used_indices = used_indices.index.values[used_indices.values.flatten()] |
| 52 | + # annovar_input = annovar_input.loc[annovar_input['index_col'].isin(used_indices)] |
| 53 | + # annovar_input['index_col'] = np.arange(len(annovar_input)) # after splitting out the unused variants the numbering needs to be reset to match the genotype matrix |
| 54 | + |
| 55 | + print('Number of variants', annovar_input.shape) |
| 56 | + |
| 57 | + annovar_input_path = savepath + '/annovar_input_' + studyname + '.csv' |
| 58 | + annovar_input.to_csv(annovar_input_path, sep="\t", index=False, header=False) |
| 59 | + |
| 60 | + print('\n') |
| 61 | + print('Annovar input files ready \n') |
| 62 | + print("Install annovar: https://doc-openbio.readthedocs.io/projects/annovar/en/latest/user-guide/download/") |
| 63 | + print("Navigate to annovar, e.g cd /home/charlesdarwin/annovar/") |
| 64 | + print("Update annovar:\n perl annotate_variation.pl -buildver hg19 -downdb -webfrom annovar refGene humandb/") |
| 65 | + print("Run:\n perl annotate_variation.pl -geneanno -dbtype refGene -buildver hg19 " + str( |
| 66 | + savepath) + "/annovar_input_" + str(studyname) + ".csv humandb --outfile " + str(savepath) + "/" + str( |
| 67 | + studyname) + "_RefGene") |
| 68 | + print('\n') |
| 69 | + print( |
| 70 | + 'After obtaining the Annovar annotations, run topology create_gene_network to get the topology file for the SNPs-gene-output network:') |
| 71 | + |
| 72 | + |
| 73 | +def Create_gene_network_topology(args): |
| 74 | + datapath = args.path + '/' |
| 75 | + studyname = args.study_name |
| 76 | + savepath = args.out + '/' |
| 77 | + |
| 78 | + print(args.study_name) |
| 79 | + |
| 80 | + gene_annotation = pd.read_csv(datapath + str(studyname) + "_RefGene.variant_function", sep='\t', header=None) |
| 81 | + gene_annotation.columns = ['into/exonic', 'gene', 'chr', 'bps', 'bpe', "mutation1", "mutation2", 'index_col'] |
| 82 | + gene_annotation['gene'] = gene_annotation['gene'].str.replace(r"\,.*", "") |
| 83 | + # gene_annotation['dist'] = gene_annotation['gene'].str.extract(r"(?<=dist\=)(.*)(?=\))") |
| 84 | + gene_annotation['gene'] = gene_annotation['gene'].str.replace(r"\(.*\)", "") |
| 85 | + gene_annotation['gene'] = gene_annotation['gene'].str.replace(r"\(.*", "") |
| 86 | + gene_annotation['gene'] = gene_annotation['gene'].str.replace(r"\;.*", "") |
| 87 | + gene_annotation = gene_annotation[(gene_annotation['gene'] != "NONE")] |
| 88 | + gene_annotation = gene_annotation.dropna() |
| 89 | + |
| 90 | + gene_list = gene_annotation.drop_duplicates("gene") |
| 91 | + gene_list = gene_list.sort_values(by=["chr", "bps"], ascending=[True, True]) |
| 92 | + gene_list["gene_id"] = np.arange(len(gene_list)) |
| 93 | + gene_list = gene_list[["gene", "gene_id"]] |
| 94 | + |
| 95 | + gene_annotation = gene_annotation.merge(gene_list, on="gene") |
| 96 | + gene_annotation = gene_annotation.sort_values(by="index_col", ascending=True) |
| 97 | + |
| 98 | + gene_annotation = gene_annotation.assign( |
| 99 | + chrbp='chr' + gene_annotation.chr.astype(str) + ':' + gene_annotation.bps.astype(str)) |
| 100 | + gene_annotation.to_csv(savepath + "/gene_network_description.csv") |
| 101 | + |
| 102 | + topology = gene_annotation[["chr", "index_col", "chrbp", "gene_id", "gene"]] |
| 103 | + print(topology['index_col'].max()) |
| 104 | + topology.columns = ['chr', 'layer0_node', 'layer0_name', 'layer1_node', 'layer1_name'] |
| 105 | + |
| 106 | + |
| 107 | + topology.to_csv(savepath + "/topology.csv") |
| 108 | + |
| 109 | + print('Topology file saved:', savepath + "/topology.csv") |
| 110 | + |
| 111 | + |
| 112 | +def topology(args): |
| 113 | + if args.type == 'create_annovar_input': |
| 114 | + Create_Annovar_input(args) |
| 115 | + elif args.type == 'create_gene_network': |
| 116 | + Create_gene_network_topology(args) |
| 117 | + else: |
| 118 | + print("invalid type:", args.type) |
| 119 | + exit() |
0 commit comments