Skip to content

Commit

Permalink
added phylogeny
Browse files Browse the repository at this point in the history
  • Loading branch information
xrotwang committed Mar 22, 2018
1 parent a6ab300 commit 4101243
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 0 deletions.
1 change: 1 addition & 0 deletions grambank/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def main(global_config, **settings):
config = Configurator(settings=settings)
config.include('clldmpg')
config.include('clld_glottologfamily_plugin')
config.include('clld_phylogeny_plugin')

config.registry.settings['home_comp'].append('coverage')
config.add_route('coverage', pattern='/coverage')
Expand Down
9 changes: 9 additions & 0 deletions grambank/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
from clld.web.adapters.geojson import GeoJsonParameter
from clld.db.meta import DBSession
from clld.db.models.common import ValueSet, Value, DomainElement
from clld_phylogeny_plugin.interfaces import ITree
from clld_phylogeny_plugin.tree import Tree


class GrambankTree(Tree):
def get_marker(self, valueset):
res = valueset.values[0].domainelement.jsondata['icon']
return res[0], '#' + res[1:]


class GrambankGeoJsonParameter(GeoJsonParameter):
Expand All @@ -21,6 +29,7 @@ def feature_iterator(self, ctx, req):


def includeme(config):
config.registry.registerUtility(GrambankTree, ITree)
config.register_adapter(
GrambankGeoJsonParameter,
interfaces.IParameter,
Expand Down
53 changes: 53 additions & 0 deletions grambank/scripts/global_tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# coding: utf8
"""
Recreate glottolog data files from the current version published at http://glottolog.org
"""
from __future__ import unicode_literals
import re

from ete3 import Tree
from pyglottolog.api import Glottolog


def tree(glottocodes, gl_repos):
label_pattern = re.compile("'[^\[]+\[([a-z0-9]{4}[0-9]{4})[^']*'")

def rename(n):
n.name = label_pattern.match(n.name).groups()[0]
n.length = 1

glottocodes = set(glottocodes)
glottocodes_in_global_tree = set()
languoids = {}
families = []
for lang in Glottolog(gl_repos).languoids():
if not lang.lineage: # a top-level node
if not lang.category.startswith('Pseudo '):
families.append(lang)
languoids[lang.id] = lang

glob = Tree()
glob.name = 'glottolog_global'

for family in sorted(families):
node = family.newick_node(nodes=languoids)
node.visit(rename)
langs_in_tree = set(n.name for n in node.walk())
langs_selected = glottocodes.intersection(langs_in_tree)
if not langs_selected:
continue

tree = Tree("({0});".format(node.newick), format=3)
tree.name = 'glottolog_{0}'.format(family.id)
if family.level.name == 'family':
tree.prune([n.encode('ascii') for n in langs_selected])
glottocodes_in_global_tree = glottocodes_in_global_tree.union(
set(n.name for n in tree.traverse()))
else:
glottocodes_in_global_tree = glottocodes_in_global_tree.union(langs_in_tree)
glob.add_child(tree)

# global
nodes = glottocodes_in_global_tree.intersection(glottocodes)
glob.prune([n.encode('ascii') for n in nodes])
return glob.write(format=9), nodes
12 changes: 12 additions & 0 deletions grambank/scripts/initializedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@
from clld.db.meta import DBSession
from clld.db.models import common
from clld.db.util import compute_language_sources
from clldutils.path import read_text, Path
from clld_glottologfamily_plugin.models import Family
from clld_glottologfamily_plugin.util import load_families
from clld_phylogeny_plugin.models import Phylogeny, LanguageTreeLabel, TreeLabel
from pyglottolog.api import Glottolog

import grambank
from grambank.scripts.util import (
import_gb20_features, import_cldf, get_clf_paths, get_names,
GLOTTOLOG_REPOS, GRAMBANK_REPOS,
)
from grambank.scripts.global_tree import tree

from stats_util import grp, grp2, feature_stability, feature_incidence, feature_dependencies, feature_diachronic_dependencies, dependencies_graph #, havdist, deep_families
from grambank.models import Dependency, Transition, Stability, Feature, GrambankLanguage #, DeepFamily, Support, HasSupport
Expand Down Expand Up @@ -99,6 +102,15 @@ def checkpoint(s, msg=None):
print(n - s, msg or '')
return n

newick, _ = tree([l.id for l in DBSession.query(common.Language)], GLOTTOLOG_REPOS)
phylo = Phylogeny(
id='p',
name='glottolog global tree',
newick=newick)
for l in DBSession.query(common.Language):
LanguageTreeLabel(
language=l, treelabel=TreeLabel(id=l.id, name=l.id, phylogeny=phylo))
DBSession.add(phylo)

dump()

Expand Down
76 changes: 76 additions & 0 deletions grambank/templates/phylogeny/detail_html.mako
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<%inherit file="../${context.get('request').registry.settings.get('clld.app_template', 'app.mako')}"/>
<%namespace name="util" file="../util.mako"/>
<%! active_menu_item = "phylogenys" %>
<%block name="title">Phylogeny ${ctx.name}</%block>
<%! from clld_phylogeny_plugin.tree import Tree %>
<%! from clld_phylogeny_plugin.interfaces import ITree %>
<% tree = req.registry.queryUtility(ITree)(ctx, req) %>

<%block name="head">
${Tree.head(req)|n}
<link href="${request.static_url('clld:web/static/css/select2.css')}"
rel="stylesheet">
<script src="${request.static_url('clld:web/static/js/select2.js')}"></script>
</%block>

<div class="row-fluid">
<div class="span8">
<h2>${title()}</h2>

% if ctx.description:
<div class="alert alert-info">${ctx.description}</div>
% endif
% if tree.parameters:
<div class="alert alert-success">
The tree has been pruned to only contain leaf nodes with values for
the selected variables. For the full tree click
<a href="${req.resource_url(ctx)}">here</a>.
</div>
% endif
<div>
<form action="${req.resource_url(ctx)}">
<fieldset>
<p>
You may combine these ${_('Parameters').lower()} with another
one.
Start typing the ${_('Parameter').lower()} name or number in
the field below.
</p>
${ms(request, combination=tree).render()|n}
<button class="btn" type="submit">Submit</button>
</fieldset>
</form>
</div>
${tree.render()}
</div>

<div class="span4">
<% ca = h.get_adapter(h.interfaces.IRepresentation, ctx, req, ext='description.html') %>
% if ca:
<div class="well well-small">
${ca.render(ctx, req)|n}
</div>
% endif
% if tree.parameters:
<div class="accordion" id="values-acc" data-spy="affix" data-offset-top="0"
style="margin-right: 10px;">
% for parameter in tree.parameters:
<div class="accordion-group">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse"
data-parent="#values-acc" href="#acc-${parameter.id}">
${parameter.name}
</a>
</div>
<div id="acc-${parameter.id}"
class="accordion-body collapse${' in' if loop.first else ''}">
<div class="accordion-inner">
${h.get_adapter(h.interfaces.IRepresentation, parameter, req, ext='valuetable.html').render(parameter, req)|n}
</div>
</div>
</div>
% endfor
</div>
% endif
</div>
</div>
7 changes: 7 additions & 0 deletions grambank/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from clld_glottologfamily_plugin.models import Family
from clld.web.icon import SHAPES
from clld.interfaces import IIcon
from clld.web.util.multiselect import CombinationMultiSelect

from grambank.maps import DeepFamilyMap
from grambank.models import Dependency, Transition, GrambankLanguage
Expand All @@ -30,6 +31,12 @@
]


def phylogeny_detail_html(request=None, context=None, **kw):
return {
'ms': CombinationMultiSelect,
}


def table_incidence(*ds):
values = set([x for d in ds for x in d.keys()])
totals = [sum(d.values()) for d in ds]
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
'clld~=4.0',
'clldmpg~=3.1.0',
'clld-glottologfamily-plugin>=2.0.0',
'clld-phylogeny-plugin>=1.1.0',
'pyglottolog>=0.3.1',
'sqlalchemy',
'waitress',
Expand Down

0 comments on commit 4101243

Please sign in to comment.