LSHTM_analysis/mcsm_na/submit_mcsm_na.py

84 lines
2.8 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 19 14:33:51 2020
@author: tanu
"""
#%% load packages
import os,sys
import subprocess
import argparse
import requests
import re
import time
from bs4 import BeautifulSoup
import pandas as pd
from pandas.api.types import is_string_dtype
from pandas.api.types import is_numeric_dtype
#%%#####################################################################
def submit_mcsm_na(host_url
, pdb_file
, mutation_list
, nuc_type
, prediction_url
, output_dir
, outfile_suffix
):
"""
Makes a POST request for mcsm_na predictions.
@param host_url: valid host url for submitting the job
@type string
@param pdb_file: valid path to pdb structure
@type string
@param mutation_list: list of mutations (1 per line) of the format:{chain} {WT}<POS>{Mut} [A X1Z}
@type string
@param nuc_type: Nucleic acid type
@type string
@param prediction_url: mcsm_na url for prediction
@type string
@param output_dir: output dir
@type string
@param outfile_suffix: outfile_suffix
@type string
@return writes a .txt file containing url for the snps processed with user provided suffix in filename
@type string
"""
with open(pdb_file, "rb") as pdb_file, open (mutation_list, "rb") as mutation_list:
files = {"wild": pdb_file
, "mutation_list": mutation_list}
body = {"na_type": nuc_type
,"pred_type": 'list',
"pdb_code": ''} # apparently needs it even though blank!
response = requests.post(prediction_url, files = files, data = body)
print(response.status_code)
if response.history:
print('\nPASS: valid submission. Fetching result url')
url_match = re.search('/mcsm_na/results_prediction/.+(?=")', response.text)
url = host_url + url_match.group()
print('\nURL for snp batch no ', str(outfile_suffix), ':', url)
#===============
# writing file: result urls
#===============
mcsm_na_temp_dir = output_dir + '/mcsm_na_temp' # creates a temp dir within output_dir
if not os.path.exists(mcsm_na_temp_dir):
print('\nCreating mcsm_na_temp in output_dir', output_dir )
os.makedirs(mcsm_na_temp_dir)
out_url_file = mcsm_na_temp_dir + '/mcsm_na_result_url_' + str(outfile_suffix) + '.txt'
print('\nWriting output url file:', out_url_file)
myfile = open(out_url_file, 'a')
myfile.write(url)
myfile.close()
#%%#####################################################################