diff options
author | Hao Chen | 2019-05-17 19:10:08 -0500 |
---|---|---|
committer | Hao Chen | 2019-05-17 19:10:08 -0500 |
commit | eb51746ce69fb43450b6896e4a71e5052df51115 (patch) | |
tree | d87e36cbf05611010fa19fd78b94b120095cf25a /topGene_step2_cnt_sentences.py | |
parent | a6376a94e137eea0e0d326d6524fe9c2177b1b34 (diff) | |
download | genecup-eb51746ce69fb43450b6896e4a71e5052df51115.tar.gz |
find top genes
Diffstat (limited to 'topGene_step2_cnt_sentences.py')
-rwxr-xr-x | topGene_step2_cnt_sentences.py | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/topGene_step2_cnt_sentences.py b/topGene_step2_cnt_sentences.py new file mode 100755 index 0000000..fe97cdd --- /dev/null +++ b/topGene_step2_cnt_sentences.py @@ -0,0 +1,59 @@ +#!/bin/env python3 +import os +import re +import time +from nltk.tokenize import sent_tokenize +from ratspub_keywords import * + +def undic(dic): + return "|".join(dic.values()) + +def findWholeWord(w): + return re.compile(r'\b({0})\b'.format(w), flags=re.IGNORECASE).search + +def getSentences(query, genes): + abstracts = os.popen("esearch -db pubmed -query " + query + " | efetch -format uid |fetch-pubmed -path /run/media/hao/PubMed/Archive/ | xtract -pattern PubmedArticle -element MedlineCitation/PMID,ArticleTitle,AbstractText|sed \"s/-/ /g\"").read() + gene_syno=genes.split("|") + symb=gene_syno[0] + out=str() + for row in abstracts.split("\n"): + tiab=row.split("\t") + pmid = tiab.pop(0) + tiab= " ".join(tiab) + sentences = sent_tokenize(tiab) + ## keep the sentence only if it contains the gene + for sent in sentences: + for gene in gene_syno: + if findWholeWord(gene)(sent): + sent=re.sub(r'\b(%s)\b' % gene, r'<strong>\1</strong>', sent, flags=re.I) + for drug0 in drug_d: + if findWholeWord(drug_d[drug0])(sent) : + sent=sent.replace("<b>","").replace("</b>","") + sent=re.sub(r'\b(%s)\b' % drug_d[drug0], r'<b>\1</b>', sent, flags=re.I) + out+=symb+"\t"+"drug\t" + drug0+"\t"+pmid+"\t"+sent+"\n" + for add0 in addiction_d: + if findWholeWord(addiction_d[add0])(sent) : + sent=sent.replace("<b>","").replace("</b>","") + sent=re.sub(r'\b(%s)\b' % addiction_d[add0], r'<b>\1</b>', sent, flags=re.I) + out+=symb+"\t"+"addiction\t"+add0+"\t"+pmid+"\t"+sent+"\n" + return(out) + +addiction=undic(addiction_d) +drug=undic(drug_d) + + +out=open("gene_addiction_sentences.tab", "w+") +cnt=0 +with open ("./ncbi_gene_symb_syno_name_txid9606_absCnt_sorted_absCnt_sorted_absCnt_sorted.txt", "r") as f: + for line in f: + (genes, abstractCount)=line.strip().split("\t") + if int(abstractCount)>20: + symb=genes.split("|")[0] + print(symb+"-->"+genes) + q="\'(\"" + addiction.replace("|", "\"[tiab] OR \"") + "\") AND (\"" + drug.replace("|", "\"[tiab] OR \"", ) + "\") AND (\"" + genes.replace("|", "\"[tiab] OR \"", ) + "\")\'" + sentences=getSentences(q,genes) + out.write(sentences) +out.close() + +os.system("cut -f 1,4 gene_addiction_sentences.tab |uniq |cut -f 1 |uniq -c |sort -rn > topGeneAbstractCount.tab") + |