<commit_before>#########################################################################################
#
# author: <NAME>
# email: <EMAIL>
#
# Reference: <NAME>, <NAME>, <NAME>. 
#            "Predicting evolution from the shape of genealogical trees"
#
#########################################################################################
#
# tree_utils.py
# a collection of functions to build and manipulate trees.
#  
#########################################################################################


import numpy as np
import time,os
from matplotlib import cm

# define fasttree binary used for tree building, 
# if no fasttree binary is found, no trees can be built
from socket import gethostname
hostname = gethostname()
if hostname in ['rneher-iMac', 'ag-neher-xps13']:
    fasttree_bin = '/ebio/ag-neher/share/programs/bin/fasttree_imac'
else:
    fasttree_bin = '/ebio/ag-neher/share/programs/bin/fasttree_cluster'
if not os.path.exists(fasttree_bin):
    if os.system('fasttree')==0:
        fasttree_bin = 'fasttree'
        print 'using default fasttree binary'
    else:
        fasttree_bin = None
        print 'no fasttree binary found'
else:
    print 'using fasttree binary:', fasttree_bin

verbose = 1
my_favorite_cmap = cm.jet

##################################################
# UTILITY FUNCTION FOR TREE BUILDING, TREE LABELING AND TREE MANIPULATION
##################################################

def calculate_tree(aln, outgroup, ancestral=True):
    '''
    takes a biopython alignment and an outgroup sequences, writes a temporary alignment
    and builds a tree using fasttree
    aln         --biopython alignment
    outgroup    -- outgroup sequence (biopython)
    '''
    import subprocess, gzip
    from StringIO import StringIO
    from Bio import SeqIO,Phylo, AlignIO
    import ancestral

    # write sequences into a temporary alignment file
    tmp_file_name = 'tmp_'+str(np.random.randint(1000000000))+'.fasta.gz'
    with gzip.open(tmp_file_name, 'w') as outfile:
        SeqIO.write(outgroup, outfile, 'fasta')
        for seq in aln:
            SeqIO.write(seq, outfile, 'fasta')

    print "building tree of file", tmp_file_name
    fasttree_cmd = [fasttree_bin,"-nt"]
    ps = subprocess.Popen(('gunzip', '-c', tmp_file_name), stdout=subprocess.PIPE)
    fasttree_output = subprocess.check_output(fasttree_cmd, stdin=ps.stdout)
    ps.wait()

    #all fasttree output is returned as byte string, parse and write tree to file
    biopython_tree = Phylo.read(StringIO(fasttree_output), 'newick')
    with gzip.open(tmp_file_name, 'r') as infile:
        tmp_aln = AlignIO.read(infile, 'fasta')
    # determine the root clade, root, and ladderize
    outgroup_clade = [z for z in biopython_tree.get_terminals() 
                      if z.name.startswith(outgroup.name)][0]
    biopython_tree.root_with_outgroup(outgroup_clade)
    biopython_tree.ladderize()

    #infer ancestral sequences in place
    print "set_alignment: inferring ancestral sequences..."
    anc_rec = ancestral.ancestral_sequences(biopython_tree, tmp_aln, copy_tree=False)
    anc_rec.calc_ancestral_sequences()        

    biopython_tree.prune(outgroup_clade)
    biopython_tree.root.branch_length = 0.001
    os.remove(tmp_file_name)
    return biopython_tree<commit_msg>manually added branch length to root of biopython tree.<commit_after>#########################################################################################
#
# author: <NAME>
# email: <EMAIL>
#
# Reference: <NAME>, <NAME>, <NAME>. 
#            "Predicting evolution from the shape of genealogical trees"
#
#########################################################################################
#
# tree_utils.py
# a collection of functions to build and manipulate trees.
#  
#########################################################################################


import numpy as np
import time,os
from matplotlib import cm

# define fasttree binary used for tree building, 
# if no fasttree binary is found, no trees can be built
from socket import gethostname
hostname = gethostname()
if hostname in ['rneher-iMac', 'ag-neher-xps13']:
    fasttree_bin = '/ebio/ag-neher/share/programs/bin/fasttree_imac'
else:
    fasttree_bin = '/ebio/ag-neher/share/programs/bin/fasttree_cluster'
if not os.path.exists(fasttree_bin):
    if os.system('fasttree')==0:
        fasttree_bin = 'fasttree'
        print 'using default fasttree binary'
    else:
        fasttree_bin = None
        print 'no fasttree binary found'
else:
    print 'using fasttree binary:', fasttree_bin

verbose = 1
my_favorite_cmap = cm.jet

##################################################
# UTILITY FUNCTION FOR TREE BUILDING, TREE LABELING AND TREE MANIPULATION
##################################################

def calculate_tree(aln, outgroup, ancestral=True):
    '''
    takes a biopython alignment and an outgroup sequences, writes a temporary alignment
    and builds a tree using fasttree
    aln         --biopython alignment
    outgroup    -- outgroup sequence (biopython)
    '''
    import subprocess, gzip
    from StringIO import StringIO
    from Bio import SeqIO,Phylo, AlignIO
    import ancestral

    # write sequences into a temporary alignment file
    tmp_file_name = 'tmp_'+str(np.random.randint(1000000000))+'.fasta.gz'
    with gzip.open(tmp_file_name, 'w') as outfile:
        SeqIO.write(outgroup, outfile, 'fasta')
        for seq in aln:
            SeqIO.write(seq, outfile, 'fasta')

    print "building tree of file", tmp_file_name
    fasttree_cmd = [fasttree_bin,"-nt"]
    ps = subprocess.Popen(('gunzip', '-c', tmp_file_name), stdout=subprocess.PIPE)
    fasttree_output = subprocess.check_output(fasttree_cmd, stdin=ps.stdout)
    ps.wait()

    #all fasttree output is returned as byte string, parse and write tree to file
    biopython_tree = Phylo.read(StringIO(fasttree_output), 'newick')
    biopython_tree.root.branch_length = 0.000001
    with gzip.open(tmp_file_name, 'r') as infile:
        tmp_aln = AlignIO.read(infile, 'fasta')
    # determine the root clade, root, and ladderize
    outgroup_clade = [z for z in biopython_tree.get_terminals() 
                      if z.name.startswith(outgroup.name)][0]
    biopython_tree.root_with_outgroup(outgroup_clade)
    biopython_tree.ladderize()

    #infer ancestral sequences in place
    print "set_alignment: inferring ancestral sequences..."
    anc_rec = ancestral.ancestral_sequences(biopython_tree, tmp_aln, copy_tree=False)
    anc_rec.calc_ancestral_sequences()        

    biopython_tree.prune(outgroup_clade)
    biopython_tree.root.branch_length = 0.001
    os.remove(tmp_file_name)
    return biopython_tree
{'fraction_non_alphanumeric': 0.1924854629491576, 'fraction_numerical': 0.006709408081109289, 'mean_word_length': 4.120610687022901, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3923466', 'n_tokens_mistral': 2095, 'n_tokens_neox': 2012, 'n_words': 525}