From ed537af0fa953d03f8d77a6af809b50b17adfd48 Mon Sep 17 00:00:00 2001 From: sepro Date: Wed, 10 May 2017 11:54:00 +0200 Subject: [PATCH] build step implemented --- detector.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 3 deletions(-) diff --git a/detector.py b/detector.py index 8e71208..4ec5c6a 100644 --- a/detector.py +++ b/detector.py @@ -1,5 +1,16 @@ import click +import os +import tempfile + +import subprocess +import shlex + + +BLAST_MODULE = 'biotools/ncbi-blast-2.3.0+' + +BUILD_DB_CMD = 'makeblastdb -dbtype prot -in %s -out %s' + @click.group() def cli(): @@ -18,13 +29,61 @@ def cli(): @cli.command() @click.argument('path', type=click.Path()) -def build(path): +@click.argument('db', type=click.Path()) +def build(path, db): """ Builds the blast database. - usage: detector build PATH (the directory with .fasta files to build the database) + usage: detector build PATH DB + + Fasta files in PATH should have the extension .fasta ! + The output from makeblastdb will be written to DB. """ - click.echo("Building the database ... ") + click.echo("Building the database from FASTA files... ") click.echo("Input path: %s" % path) + full_fasta = tempfile.mktemp() + fasta_count = 0 + + # find fasta files and concatenate them to a temporary file + with open(full_fasta, 'w') as f_out: + for file in os.listdir(path): + if file.lower().endswith('.fasta'): + click.echo("\tFound file: %s" % file) + fasta_count += 1 + with (open(os.path.join(path, file), 'r')) as f_in: + for line in f_in: + f_out.write(line) + + click.echo("Concatenated %d fasta files in temporary file %s" % (fasta_count, full_fasta)) + click.echo("\nCreating Blast database") + + cmd = BUILD_DB_CMD % (full_fasta, db) + click.echo("Executing command: %s" % cmd) + subprocess.call(shlex.split(cmd)) + + # delete temporary file + os.remove(full_fasta) + + # done + click.echo("All done!") + + +@cli.command() +@click.argument('db', type=click.Path()) +@click.argument('output', type=click.Path()) +def blast(db, output): + """ + Runs Blast + """ + pass + + +@cli.command() +@click.argument('input', type=click.Path()) +def analyze(intput): + """ + Analyzes blast output + """ + pass