Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
blastp_on_cluster/blast_on_cluster.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
82 lines (61 sloc)
1.93 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from datetime import datetime | |
from math import ceil | |
import shlex | |
import subprocess | |
cluster_template = """#!/bin/bash | |
# | |
#$ -N blast_on_cluster | |
#$ -cwd | |
#$ -j y | |
#$ -S /bin/bash | |
#$ -o OUT_$JOB_NAME.$JOB_ID | |
#$ -e ERR_$JOB_NAME.$JOB_ID | |
module load biotools/ncbi-blast-2.3.0+ | |
date | |
hostname | |
blastp -outfmt 6 -evalue 0.001 -query ${q} -db ${db} -out ${o} -num_threads {t} | |
date | |
""" | |
def check_line(line): | |
""" | |
Checks if a line is a blastp command | |
:param line: string to check | |
:return: True if the line is a blastp command | |
""" | |
return line.startswith("blastp -outfmt") | |
def write_template(filename=None): | |
if filename is None: | |
filename = "blast_on_cluster" + str(ceil(datetime.utcnow().timestamp())) + ".sh" | |
with open(filename, "w") as t: | |
print(cluster_template, file=t) | |
return filename | |
def execute_command_on_cluster(submission_script, cmd, threads=1): | |
""" | |
Parses blastp command reported by OrthoFinder and submits an equivalent job to the cluster | |
:param submission_script: path to the submission script | |
:param cmd: command reported by OrthoFinder | |
:param threads: number of desired thread for each BLAST | |
:return: | |
""" | |
cmd_args = shlex.split(cmd) | |
query, db, output = cmd_args[6], cmd_args[8], cmd_args[10] | |
qsub_cmd = ['qsub', | |
'-pe', 'cores', str(threads), | |
'-v', 'q=%s,db=%s,out=%s,t=%d' % (query, db, output, threads), | |
submission_script] | |
print(qsub_cmd) | |
# subprocess.call(qsub_cmd) | |
def run(): | |
""" | |
Main loop, will get STDIN, parse the line and submit the job using qsub | |
""" | |
print("Writing template") | |
submission_script = write_template() | |
print("Waiting for command to execute...") | |
for line in sys.stdin(): | |
if check_line(line): | |
print("\n\tCommand found! Executing") | |
execute_command_on_cluster(submission_script, line.strip()) | |
if __name__ == "main": | |
run() |