def make_phylip_seq_dist_mat(inseqs, alphabet, tmp_path = None):
    
    commands = "2\ny\n"
    
    with tmp_directory(dir=tmp_path, rm_dir=False) as r_path:
        with push_dir(r_path):
            with open('cmd_file', 'w') as cmd_handle:
                cmd_handle.write(commands)
            cmd_handle = open('cmd_file')
            with open('infile', 'w') as handle:
                trans_names = write_phylip_seqs(inseqs, handle, alphabet = alphabet)
            if alphabet == generic_protein:
                cmd = 'phylip protdist'
            elif alphabet == generic_dna:
                cmd = 'phylip dnadist'
            else:
                raise KeyError, 'Unknown alphabet.'
            cmd = shlex.split(cmd)
            try:
                check_call(cmd, stdin = cmd_handle)
            except:
                print r_path
                raise KeyError
            with open('outfile') as handle:
                dist_data = handle.read()
            
    return trans_names, dist_data
def make_phylip_tree(dist_data, tmp_path = None):
    
    commands = "2\n3\ny\n"
    with tmp_directory(dir=tmp_path, rm_dir=True) as r_path:
        with push_dir(r_path):
            with open('cmd_file', 'w') as cmd_handle:
                cmd_handle.write(commands)
            cmd_handle = open('cmd_file')
            with open('infile', 'w') as handle:
                handle.write(dist_data)
            cmd = shlex.split('phylip neighbor')
            check_call(cmd, stdin = cmd_handle)
            with open('outtree') as handle:
                tree = dendropy.Tree.get_from_stream(handle, schema='newick')
    return tree
Beispiel #3
0
def make_pdf(input_notebook_path, output_pdf):
    """Converts a ipyp notebook to a pdf"""

    notebook_fname = input_notebook_path.rsplit(os.sep, 1)[-1]
    notebook_base = notebook_fname.rsplit('.', 1)[0]
    with tmp_directory() as tmp_path:
        with tmp_changedir(tmp_path):

            new_notebook_path = os.path.join(tmp_path, notebook_fname)
            new_tex_path = os.path.join(tmp_path, notebook_base+'.tex')
            new_pdf_path = os.path.join(tmp_path, notebook_base+'.pdf')
            shutil.copy(input_notebook_path, new_notebook_path)

            run_nbconvert(new_notebook_path)
            run_latex(new_tex_path)

            shutil.move(new_pdf_path, output_pdf)
Beispiel #4
0
def align_with_lastz(input_seqs, ref_seqs):
    """Aligns set of query sequences with a reference."""

    with tmp_directory() as tmp_dir:
        seq_file = os.path.join(tmp_dir, "query.fasta")
        ref_file = os.path.join(tmp_dir, "ref.fasta")
        out_file = os.path.join(tmp_dir, "res.fasta")

        with open(seq_file, "w") as handle:
            fasta_writer(handle, input_seqs)

        with open(ref_file, "w") as handle:
            fasta_writer(handle, ref_seqs)

        call_lastz(seq_file, ref_file, out_file)

        with open(out_file) as handle:
            return list(SAMreader(handle))