Skip to content

Instantly share code, notes, and snippets.

View jhcepas's full-sized avatar

Jaime Huerta-Cepas jhcepas

View GitHub Profile
@jhcepas
jhcepas / print_table.py
Last active June 6, 2020 18:20
nice table printing in python
def print_table(items, header=None, wrap=True, max_col_width=20, wrap_style="wrap", row_line=False, fix_col_width=False):
''' Prints a matrix of data as a human readable table. Matrix
should be a list of lists containing any type of values that can
be converted into text strings.
Two different column adjustment methods are supported through
the *wrap_style* argument:
wrap: it will wrap values to fit max_col_width (by extending cell height)
cut: it will strip values to max_col_width
@jhcepas
jhcepas / Newick2JSON.py
Last active May 6, 2022 19:37
Converts a newick file into JSON format for d3/TreeWidget javascript component
import sys
from ete2 import Tree
import random
def get_json(node):
# Read ETE tag for duplication or speciation events
if not hasattr(node, 'evoltype'):
dup = random.sample(['N','Y'], 1)[0]
elif node.evoltype == "S":
dup = "N"
@jhcepas
jhcepas / etree2orthoxml
Last active August 29, 2015 14:21
Newick tree to OrthoXML converter
#!/usr/bin/python
import sys
from ete2 import PhyloTree, orthoxml
import argparse
__DESCRIPTION__ = """
etree2orthoxml is a python script that extracts evolutionary events
(speciation and duplication) from a newick tree and exports them as an
OrthoXML file.
@jhcepas
jhcepas / Newick2FastTreeConstraints.py
Last active August 29, 2015 14:23
Converts a tree in newick format into a FastTree-compatible constraint alignment. It allows for multifurcated nodes.
#!/usr/bin/env python
import sys
from collections import defaultdict
from ete2 import Tree
try:
t = Tree(sys.argv[1])
except IndexError:
print >>sys.stderr, 'you need to provide a newick tree file as first argument\n\n'
print >>sys.stderr, 'Usage: Newick2FastTreeConstraints.py tree.nw > constraints.fa'
sys.exit(1)
# pp = painter
# node_links = a list of node links
# a = node a
# b = node b
# a_mode, b_mode: mode 0= node to node. mode 1: if node is internal cover also the descendants
# bg = background color
# lw = linewidth
# opa = opacity
# text, fsize, fcolor, ftype = defines text following the curve
@jhcepas
jhcepas / plot_mutations.py
Last active August 30, 2019 12:05
given a tree and its associated alignment, plot column changes for each internal branch
from collections import defaultdict
from ete3 import PhyloTree, TreeStyle, SeqMotifFace, TextFace, RectFace
alg = """
>Dme_001
MAEIPDETIQQFMALT---HNIAVQYLSEFGDLNEAL--YYASQTDDIKDRREEAH
>Dme_002
MAEIPDATIQQFMALTNVSHNIAVQY--EFGDLNEALNSYYAYQTDDQKDRREEAH
>Cfa_001
MAEIPDATIQ---ALTNVSHNIAVQYLSEFGDLNEALNSYYASQTDDQPDRREEAH
@jhcepas
jhcepas / ancestral_seq.py
Last active November 18, 2016 10:39
Some tweaks and ideas to render ancestral sequences aligned to leaf-sequences using ETE3. Resulting image not prefect, but good for data exploration.
from collections import defaultdict
from ete3 import PhyloTree, TreeStyle, SeqMotifFace, TextFace, RectFace, AttrFace
alg = """
>Dme_001
MAEIPDETIQQFMALT---HNIAVQYLSEFGDLNEAL--YYASQTDDIKDRREEAH
>Dme_002
MAEIPDATIQQFMALTNVSHNIAVQY--EFGDLNEALNSYYAYQTDDQKDRREEAH
>Cfa_001
MAEIPDATIQ---ALTNVSHNIAVQYLSEFGDLNEALNSYYASQTDDQPDRREEAH
@jhcepas
jhcepas / sort_alg_by_tree.py
Created March 15, 2017 14:48
Sort a fasta alignment by the order of branches in a phylogenetic tree
import re
from ete3 import SeqGroup, Tree
import sys
alg_file = sys.argv[1] # in fasta format
tree_file = sys.argv[2] # in newick format
alg = SeqGroup(alg_file)
for k,v in alg.name2id.items():
# converts ilegal newick chars from alg names.
@jhcepas
jhcepas / dist_matrix.py
Created October 18, 2017 08:51
calculates cophenetic matrix of a tree
import sys
import itertools
from ete3 import Tree
try:
t = Tree()
t.populate(int(sys.argv[1]), random_branches=True)
except ValueError:
print >>sys.stderr, 'loading', sys.argv[1]
t = Tree(sys.argv[1])