From 71cb13171bf98f7c0af4141d8e21ff32761ef3a0 Mon Sep 17 00:00:00 2001 From: snewhouse Date: Thu, 16 Jun 2016 10:44:21 +0100 Subject: [PATCH] added python 3 version, renamed files, tested with Python 3.5.1 :: Anaconda 4.0.0 (x86_64) --- KEGG_2015__enrichment.txt | 0 README.md | 0 enrichr_libs.txt | 0 enrichr_libs_list_June_2016.txt | 0 pic.png | Bin query_enrichr_v0.1.py => query_enrichr_py2.py | 0 query_enrichr_py3.py | 128 ++++++++++++++++++ 7 files changed, 128 insertions(+) mode change 100644 => 100755 KEGG_2015__enrichment.txt mode change 100644 => 100755 README.md mode change 100644 => 100755 enrichr_libs.txt mode change 100644 => 100755 enrichr_libs_list_June_2016.txt mode change 100644 => 100755 pic.png rename query_enrichr_v0.1.py => query_enrichr_py2.py (100%) create mode 100755 query_enrichr_py3.py diff --git a/KEGG_2015__enrichment.txt b/KEGG_2015__enrichment.txt old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/enrichr_libs.txt b/enrichr_libs.txt old mode 100644 new mode 100755 diff --git a/enrichr_libs_list_June_2016.txt b/enrichr_libs_list_June_2016.txt old mode 100644 new mode 100755 diff --git a/pic.png b/pic.png old mode 100644 new mode 100755 diff --git a/query_enrichr_v0.1.py b/query_enrichr_py2.py similarity index 100% rename from query_enrichr_v0.1.py rename to query_enrichr_py2.py diff --git a/query_enrichr_py3.py b/query_enrichr_py3.py new file mode 100755 index 0000000..58afe84 --- /dev/null +++ b/query_enrichr_py3.py @@ -0,0 +1,128 @@ +#!/usr/bin/env python + +# python 3 +# Author: Stephen Newhouse +# Date: June 2016 +# see: http://amp.pharm.mssm.edu/Enrichr/help#api for API docs +# +# Usage: python query_enrichr_v0.1.py + +import json +import requests +import sys + +print("Enrichr API : Start") +print("Enrichr API : Reading Command Options") +## get command line args +genelist = sys.argv[1] +listdesrciption = sys.argv[2] +enrichr_library = sys.argv[3] +enrichr_results = sys.argv[4] + +# testing +# genelist = "gene_list.txt" +# enrichr_library = "KEGG_2016" +# listdesrciption = "TEST" +# enrichr_results = "example_enrichment_KEGG_2016" + +## Print options +print('Enrichr API : Input file is:', genelist) +print('Enrichr API : Analysis name: ', listdesrciption) +print('Enrichr API : Enrichr Library: ', enrichr_library) +print('Enrichr API : Enrichr Results File: ', enrichr_results) + +# get gene lits +print("Enrichr API : Reading:",genelist) +f = open(genelist) +genes = f.read() + +## enrichr url +ENRICHR_URL = 'http://amp.pharm.mssm.edu/Enrichr/addList' + +# stick gene list here +genes_str = str(genes) + +# name of analysis or list +description = str(listdesrciption) + +# payload +payload = { + 'list': (None, genes_str), + 'description': (None, description) +} + +# response +print("Enrichr API : requests.post") +response = requests.post(ENRICHR_URL, files=payload) + +if not response.ok: + raise Exception('Error analyzing gene list') + +job_id = json.loads(response.text) + +print('Enrichr API : Job ID:', job_id) + +################################################################################ +# View added gene list +# +ENRICHR_URL_A = 'http://amp.pharm.mssm.edu/Enrichr/view?userListId=%s' + +user_list_id = job_id['userListId'] +#print(user_list_id) + +response_gene_list = requests.get(ENRICHR_URL_A % str(user_list_id)) + +if not response_gene_list.ok: + raise Exception('Error getting gene list') + +print('Enrichr API : View added gene list:', job_id) +added_gene_list = json.loads(response_gene_list.text) +print(added_gene_list) + +################################################################################ +# Get enrichment results +# +ENRICHR_URL = 'http://amp.pharm.mssm.edu/Enrichr/enrich' +query_string = '?userListId=%s&backgroundType=%s' + +## get id data +user_list_id = job_id['userListId'] + +## Libraray +gene_set_library = str(enrichr_library) + +response = requests.get( + ENRICHR_URL + query_string % (str(user_list_id), gene_set_library) + ) +if not response.ok: + raise Exception('Error fetching enrichment results') + +print('Enrichr API : Get enrichment results: Job Id:', job_id) +data = json.loads(response.text) +print(data) + +################################################################################ +## Download file of enrichment results +# +ENRICHR_URL = 'http://amp.pharm.mssm.edu/Enrichr/export' + +query_string = '?userListId=%s&filename=%s&backgroundType=%s' + +user_list_id = str(job_id['userListId']) + +filename = enrichr_results + +gene_set_library = str(enrichr_library) + +url = ENRICHR_URL + query_string % (user_list_id, filename, gene_set_library) + +response = requests.get(url, stream=True) + +print('Enrichr API : Downloading file of enrichment results: Job Id:', job_id) +with open(filename + '.txt', 'wb') as f: + for chunk in response.iter_content(chunk_size=1024): + if chunk: + f.write(chunk) +################################################ +print('Enrichr API : Results written to:', enrichr_results + ".txt") +print("Enrichr API : Done")