clld-phylogeny-plugin 2.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,37 @@
1
+ """
2
+ A clld plugin to support language Phylogenies.
3
+ """
4
+ from clld.interfaces import IParameter
5
+ from clld.web.adapters.base import Representation
6
+ from clld_phylogeny_plugin.models import Phylogeny
7
+ from clld_phylogeny_plugin.datatables import Phylogenies
8
+ from clld_phylogeny_plugin.interfaces import IPhylogeny, ITree
9
+ from clld_phylogeny_plugin.tree import Tree
10
+
11
+
12
+ def includeme(config): # pylint: disable=C0116
13
+ config.add_static_view('clld-phylogeny-plugin-static', 'clld_phylogeny_plugin:static')
14
+ config.registry.settings['mako.directories'].append(
15
+ 'clld_phylogeny_plugin:templates')
16
+ # We must make sure to register the datatable before registering the resource, to
17
+ # prevent a default DataTable to be registered.
18
+ config.register_datatable('phylogenys', Phylogenies, overwrite=False)
19
+ config.register_resource('phylogeny', Phylogeny, IPhylogeny, with_index=True)
20
+ specs = [
21
+ [
22
+ IParameter,
23
+ Representation,
24
+ 'application/vnd.clld.valuetable+xml',
25
+ 'valuetable.html',
26
+ 'parameter/valuetable_html.mako',
27
+ {'rel': None}],
28
+ [
29
+ IPhylogeny,
30
+ Representation,
31
+ 'application/vnd.clld.description+xml',
32
+ 'description.html',
33
+ 'phylogeny/description_html.mako',
34
+ {'rel': None}],
35
+ ]
36
+ config.register_adapters(specs)
37
+ config.register_utility(Tree, ITree, overwrite=False)
@@ -0,0 +1,9 @@
1
+ from clld.web.datatables.base import DataTable, LinkCol, Col
2
+
3
+
4
+ class Phylogenies(DataTable):
5
+ def col_defs(self):
6
+ return [
7
+ LinkCol(self, 'name'),
8
+ Col(self, 'description'),
9
+ ]
@@ -0,0 +1,11 @@
1
+ from zope.interface import Interface
2
+
3
+
4
+ class IPhylogeny(Interface):
5
+ """marker
6
+ """
7
+
8
+
9
+ class ITree(Interface):
10
+ """marker
11
+ """
@@ -0,0 +1,62 @@
1
+ """
2
+ SQLAlchemy models for the package.
3
+ """
4
+ from zope.interface import implementer
5
+ from sqlalchemy import (
6
+ Column,
7
+ Unicode,
8
+ Integer,
9
+ ForeignKey,
10
+ UniqueConstraint,
11
+ )
12
+ from sqlalchemy.orm import relationship, backref, joinedload
13
+ from sqlalchemy.ext.declarative import declared_attr
14
+
15
+ from clld.db.meta import Base, PolymorphicBaseMixin
16
+ from clld.db.models.common import Language, IdNameDescriptionMixin
17
+
18
+ from clld_phylogeny_plugin.interfaces import IPhylogeny
19
+
20
+
21
+ @implementer(IPhylogeny)
22
+ class Phylogeny(Base, PolymorphicBaseMixin, IdNameDescriptionMixin):
23
+ """
24
+ A Phylogeny represents an evolutionary tree that may be used to map Parameters onto.
25
+ """
26
+ newick = Column(Unicode)
27
+
28
+ @staticmethod
29
+ def refine_factory_query(query): # pylint: disable=C0116
30
+ return query.options(
31
+ joinedload(Phylogeny.treelabels)
32
+ .joinedload(TreeLabel.language_assocs)
33
+ .joinedload(LanguageTreeLabel.language))
34
+
35
+
36
+ class TreeLabel(Base, IdNameDescriptionMixin):
37
+ """
38
+ A Label used for a leaf node in a Phylogeny.
39
+ """
40
+ phylogeny_pk = Column(Integer, ForeignKey('phylogeny.pk'))
41
+
42
+ @declared_attr
43
+ def phylogeny(self) -> Phylogeny: # pylint: disable=C0116,E0213
44
+ return relationship(Phylogeny, backref=backref('treelabels', order_by=[self.pk]))
45
+
46
+ @property
47
+ def languages(self) -> list[Language]: # pylint: disable=C0116
48
+ return [la.language for la in sorted(self.language_assocs, key=lambda la: la.ord)]
49
+
50
+
51
+ class LanguageTreeLabel(Base):
52
+ """
53
+ A TreeLabel may be mapped to multiple Language objects. This many-to-many relationship
54
+ is mediated through LanguageTreeLabel.
55
+ """
56
+ __table_args__ = (UniqueConstraint('language_pk', 'treelabel_pk'),)
57
+
58
+ language_pk = Column(Integer, ForeignKey('language.pk'), nullable=False)
59
+ language = relationship(Language)
60
+ treelabel_pk = Column(Integer, ForeignKey('treelabel.pk'), nullable=False)
61
+ treelabel = relationship(TreeLabel, backref='language_assocs')
62
+ ord = Column(Integer, default=1)
@@ -0,0 +1,159 @@
1
+ CLLD_PHYLOGENY_PLUGIN = {};
2
+
3
+
4
+ CLLD_PHYLOGENY_PLUGIN.marker = function (container, spec, offset, marker_align_offset) {
5
+ var marker, tt, close;
6
+ if (!spec) {
7
+ return;
8
+ }
9
+ if (spec.shape === 'c') {
10
+ marker = container.append("circle")
11
+ .attr("cx", offset * 12 + 6)
12
+ .attr("height", 12)
13
+ .attr("width", 12)
14
+ .attr("r", 5)
15
+ } else if (spec.shape === 's') {
16
+ marker = container.append("path")
17
+ .attr("height", 12)
18
+ .attr("width", 12)
19
+ .attr("d", "M " + (offset * 12 + 1) + " 0 v -5 h 10 v 10 h -10 z")
20
+ } else if (spec.shape === 'd') {
21
+ marker = container.append("path")
22
+ .attr("height", 12)
23
+ .attr("width", 12)
24
+ .attr("d", "M " + (offset * 12) + " 0 l 6 -6 l 6 6 l -6 6 z")
25
+ } else if (spec.shape === 't') {
26
+ marker = container.append("path")
27
+ .attr("height", 12)
28
+ .attr("width", 12)
29
+ .attr("d", "M " + (offset * 12 + 1) + " 5 l 10 0 l -5 -10 z")
30
+ } else if (spec.shape === 'f') {
31
+ marker = container.append("path")
32
+ .attr("height", 12)
33
+ .attr("width", 12)
34
+ .attr("d", "M " + (offset * 12 + 1) + " -5 l 10 0 l -5 10 z")
35
+ } else {
36
+ return
37
+ }
38
+ marker
39
+ .style("fill", spec.color)
40
+ .attr("id", 'm-' + spec.eid);
41
+ if (spec.conflict) {
42
+ marker.style("stroke", "#f00")
43
+ .style("stroke-width", "2.5");
44
+
45
+ } else if (!spec.tooltip) {
46
+ marker.style("stroke", "#fff")
47
+ .style("stroke-width", "0");
48
+ } else {
49
+ marker.style("stroke", "#222")
50
+ .style("stroke-width", "0.5");
51
+ }
52
+ if (spec.tooltip) {
53
+ close = '<button style="float: right;" onclick="$(\'#' + spec.eid + '\').clickover(\'toggle\');">Close</button>';
54
+ marker
55
+ .on("click", function () {
56
+ tt = $('#' + spec.eid);
57
+ if (tt.length) {
58
+ tt.clickover('toggle');
59
+ } else {
60
+ d3.select("body").append("div")
61
+ .attr("class", "clld-tree-tooltip")
62
+ .attr("id", spec.eid)
63
+ .html('')
64
+ .style("visibility", "hidden")
65
+ .style("left", (d3.event.pageX + 70) + "px")
66
+ .style("top", (d3.event.pageY) + "px")
67
+ .style("position", "absolute");
68
+ tt = $('#' + spec.eid);
69
+ tt.clickover({
70
+ 'html': true,
71
+ 'placement': 'right',
72
+ 'title': spec.tooltip_title || undefined,
73
+ 'content': spec.tooltip + close});
74
+ tt.clickover('show');
75
+ }
76
+ });
77
+ }
78
+ if(marker_align_offset > 0){
79
+ marker.attr('transform', 'translate(' + marker_align_offset.toString() + ', 0)');
80
+ }
81
+ return marker;
82
+ };
83
+
84
+ CLLD_PHYLOGENY_PLUGIN.nodeStyler = function (labelSpec, options) {
85
+ return function (element, data) {
86
+ var text, current, sep;
87
+ var init = false;
88
+ if (d3.phylotree.isLeafNode(data)) {
89
+ var marker_align_offset = 0;
90
+ if (options['align-tips']) {
91
+ var line = element.select("line");
92
+ if (!options['hasBranchLengths']) {
93
+ line.attr("class", "branch");
94
+ }
95
+ marker_align_offset = parseFloat(line.attr("x2"));
96
+ }
97
+ text = element.select("text");
98
+ text.attr("fill", "red");
99
+ if (!data.data.org) {
100
+ data.data.org = text.text();
101
+ init = true;
102
+ }
103
+ current = data.data.org;
104
+ if (labelSpec.hasOwnProperty(current)) {
105
+ var values = [];
106
+ sep = ' # ';
107
+ for (i = 0; i < labelSpec[current].length; i++) {
108
+ var lblSpec = labelSpec[current][i];
109
+ if (element.select('#m-' + lblSpec.eid).size() === 0) {
110
+ CLLD_PHYLOGENY_PLUGIN.marker(element, lblSpec, i, marker_align_offset);
111
+ }
112
+ if (init) {
113
+ if (i == 0 && lblSpec.hasOwnProperty('tip_title')) {
114
+ text.text(lblSpec['tip_title']);
115
+ if (lblSpec.hasOwnProperty('tip_values_sep')) {
116
+ sep = lblSpec['tip_values_sep'];
117
+ }
118
+ }
119
+ if (lblSpec.hasOwnProperty('tip_values')) {
120
+ values.push(lblSpec['tip_values']);
121
+ }
122
+ }
123
+ }
124
+ if (init) {
125
+ data.data.name = text.text() + ' ' + values.join(sep);
126
+ }
127
+ text.attr('transform', null)
128
+ .attr('dx', function(d, i) {
129
+ return marker_align_offset + 4 + labelSpec[current].length * 12; }
130
+ );
131
+ }
132
+ }
133
+ }
134
+ };
135
+
136
+ CLLD_PHYLOGENY_PLUGIN.tree = function (eid, newick, labelSpec, options) {
137
+ var container_select_id = '#' + eid;
138
+ var d3_container = d3.select(container_select_id);
139
+
140
+ d3.phylotree = new phylotree.phylotree(newick);
141
+ options['hasBranchLengths'] = d3.phylotree.hasBranchLengths();
142
+ options['node-styler'] = CLLD_PHYLOGENY_PLUGIN.nodeStyler(labelSpec, options);
143
+ options['container'] = container_select_id;
144
+
145
+ // container should be a DIV - a possible custom legacy SVG container will be replaced by a DIV first
146
+ if (d3_container.node().tagName.toLowerCase() === 'svg') {
147
+ var p = d3_container.node().parentNode;
148
+ var org_style = d3_container.attr('style');
149
+ d3_container.remove();
150
+ d3.select(p).append('div')
151
+ .attr('id', eid)
152
+ .attr('style', org_style)
153
+ .attr('class', 'tree-widget');
154
+ }
155
+
156
+ d3_container.node()
157
+ .appendChild(d3.phylotree.render(options).show());
158
+ d3.phylotree.display.update();
159
+ };