Skip to content
Permalink
f5d0a39359
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
82 lines (61 sloc) 1.93 KB
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()