Beispiel #1
0
def add(alignment, sequence, timeout, logger, wd, threads):
    """Align sequence(s) to an alignment using mafft (external
program)"""
    alignment_file = "alignment_in.fasta"
    sequence_file = "sequence_in.fasta"
    output_file = "alignment_out.fasta" + ".fasta"
    command_line = "{0} --auto --thread {1} --add {2} {3} > {4}".format(
        mafft, threads, sequence_file, alignment_file, output_file
    )
    with open(os.path.join(wd, sequence_file), "w") as file:
        SeqIO.write(sequence, file, "fasta")
    with open(os.path.join(wd, alignment_file), "w") as file:
        AlignIO.write(alignment, file, "fasta")
    pipe = TerminationPipe(command_line, timeout=timeout, cwd=wd)
    pipe.run()
    os.remove(os.path.join(wd, alignment_file))
    os.remove(os.path.join(wd, sequence_file))
    if not pipe.failure:
        try:
            res = AlignIO.read(os.path.join(wd, output_file), "fasta")
        except:
            logger.info(pipe.output)
            raise MafftError()
        else:
            os.remove(os.path.join(wd, output_file))
    else:
        logger.debug(".... add timeout ....")
        return genNonAlignment(len(alignment) + 1, len(alignment.get_alignment_length()))
    return res
Beispiel #2
0
def align(command, sequences, timeout, logger, wd, threads):
    """Adapted pG function: Align sequences using mafft (external
program)"""
    input_file = "sequences_in.fasta"
    output_file = "alignment_out.fasta"
    command_line = "{0} --thread {1} {2} > {3}".format(command, threads, input_file, output_file)
    with open(os.path.join(wd, input_file), "w") as file:
        SeqIO.write(sequences, file, "fasta")
    logger.debug(command_line)
    pipe = TerminationPipe(command_line, timeout=timeout, cwd=wd)
    pipe.run()
    os.remove(os.path.join(wd, input_file))
    if not pipe.failure:
        try:
            res = AlignIO.read(os.path.join(wd, output_file), "fasta")
        except:
            logger.info(pipe.output)
            raise MafftError()
        else:
            os.remove(os.path.join(wd, output_file))
    else:
        # if pipe.failure, runtime error, return non-alignment
        logger.debug(".... align timeout ....")
        return genNonAlignment(len(sequences), len(sequences[0]))
    return res
Beispiel #3
0
def align(command, sequences, timeout, logger, wd, threads):
    """Adapted pG function: Align sequences using mafft (external
program)"""
    input_file = "sequences_in.fasta"
    output_file = "alignment_out.fasta"
    command_line = '{0} --thread {1} {2} > {3}'.format(command, threads,
                                                       input_file, output_file)
    with open(os.path.join(wd, input_file), "w") as file:
        SeqIO.write(sequences, file, "fasta")
    logger.debug(command_line)
    pipe = TerminationPipe(command_line, timeout=timeout, cwd=wd)
    pipe.run()
    os.remove(os.path.join(wd, input_file))
    if not pipe.failure:
        try:
            res = AlignIO.read(os.path.join(wd, output_file), 'fasta')
        except:
            logger.info(pipe.output)
            raise MafftError()
        else:
            os.remove(os.path.join(wd, output_file))
    else:
        # if pipe.failure, runtime error, return non-alignment
        logger.debug('.... align timeout ....')
        return genNonAlignment(len(sequences), len(sequences[0]))
    return res
Beispiel #4
0
def add(alignment, sequence, timeout, logger, wd, threads):
    """Align sequence(s) to an alignment using mafft (external
program)"""
    alignment_file = "alignment_in.fasta"
    sequence_file = "sequence_in.fasta"
    output_file = "alignment_out.fasta" + '.fasta'
    command_line = '{0} --auto --thread {1} --add {2} {3} > {4}'.\
                   format(mafft, threads, sequence_file, alignment_file,
                          output_file)
    with open(os.path.join(wd, sequence_file), "w") as file:
        SeqIO.write(sequence, file, "fasta")
    with open(os.path.join(wd, alignment_file), "w") as file:
        AlignIO.write(alignment, file, "fasta")
    pipe = TerminationPipe(command_line, timeout=timeout, cwd=wd)
    pipe.run()
    os.remove(os.path.join(wd, alignment_file))
    os.remove(os.path.join(wd, sequence_file))
    if not pipe.failure:
        try:
            res = AlignIO.read(os.path.join(wd, output_file), 'fasta')
        except:
            logger.info(pipe.output)
            raise MafftError()
        else:
            os.remove(os.path.join(wd, output_file))
    else:
        logger.debug('.... add timeout ....')
        return genNonAlignment(
            len(alignment) + 1, len(alignment.get_alignment_length()))
    return res
Beispiel #5
0
def RAxML(alignment, wd, logger, threads, outgroup=None, partitions=None,
          constraint=None, timeout=999999999):
    """Adapted pG function: Generate phylogeny from alignment using
RAxML (external program)."""
    # TODO: too complex, consider breaking up
    input_file = 'phylogeny_in.phylip'
    output_file = 'phylogeny_out'
    file_line = ' -s ' + input_file + ' -n ' + output_file
    options = ' -p ' + str(random.randint(0, 10000000))
    # only add -T arg if pthreads version
    if pthreads:
        options += ' -T ' + str(threads)
    if outgroup:
        options += ' -o ' + outgroup
    with open(os.path.join(wd, input_file), "w") as file:
        AlignIO.write(alignment, file, "phylip-relaxed")
    # only use GTRCAT for more than 100 taxa (ref RAxML manual)
    if len(alignment) > 100:
        dnamodel = ' -m GTRCAT'
    else:
        dnamodel = ' -m GTRGAMMA'
    if partitions:
        options += partitions
    if constraint:
        options += constraint
    command_line = raxml + file_line + dnamodel + options
    logger.debug(command_line)
    pipe = TerminationPipe(command_line, silent=True, cwd=wd)
    pipe.run()
    if not pipe.failure:
        try:
            with open(os.path.join(wd, 'RAxML_bestTree.' + output_file), "r") \
                    as file:
                tree = Phylo.read(file, "newick")
        except IOError:
            return None
        finally:
            if constraint:
                os.remove(os.path.join(wd, 'constraint.tre'))
            if partitions:
                os.remove(os.path.join(wd, "partitions.txt"))
            os.remove(os.path.join(wd, input_file))
            all_files = os.listdir(wd)
            for each in all_files:
                if re.search("(RAxML)", each):
                    os.remove(os.path.join(wd, each))
                if re.search("\.reduced$", each):
                    os.remove(os.path.join(wd, each))
        return tree
    else:
        raise RuntimeError()