136"""Generator that yields CDAO.Tree instances from an RDF model."""137ifgraphisNone:138graph=self.graph139140# look up branch lengths/TUs for all nodes141self.get_node_info(graph,context=context)142143forroot_nodeinself.tree_roots:144clade=self.parse_children(root_node)145146yieldCDAO.Tree(root=clade,rooted=self.rooted)

149"""Returns a CDAO.Clade object for a given named node."""150result=self.node_info[node]151152kwargs={}153if'branch_length'inresult:154kwargs['branch_length']=result['branch_length']155if'label'inresult:156kwargs['name']=result['label'].replace('_',' ')157if'confidence'inresult:158kwargs['confidence']=result['confidence']159160clade=CDAO.Clade(**kwargs)161162returnclade

165"""Creates a dictionary containing information about all nodes in the tree."""166self.node_info={}167self.obj_info={}168self.children={}169self.nodes=set()170self.tree_roots=set()171172assignments={173qUri('cdao:has_Parent'):'parent',174qUri('cdao:belongs_to_Edge_as_Child'):'edge',175qUri('cdao:has_Annotation'):'annotation',176qUri('cdao:has_Value'):'value',177qUri('cdao:represents_TU'):'tu',178qUri('rdfs:label'):'label',179qUri('cdao:has_Support_Value'):'confidence',180}181182fors,v,oingraph:183# process each RDF triple in the graph sequentially184185s,v,o=str(s),str(v),str(o)186187ifsnotinself.obj_info:188self.obj_info[s]={}189this=self.obj_info[s]190191try:192# if the predicate is one we care about, store information for193# later194this[assignments[v]]=o195exceptKeyError:196pass197198ifv==qUri('rdf:type'):199ifoin(qUri('cdao:AncestralNode'),qUri('cdao:TerminalNode')):200# this is a tree node; store it in set of all nodes201self.nodes.add(s)202ifv==qUri('cdao:has_Root'):203# this is a tree; store its root in set of all tree roots204self.tree_roots.add(o)205206fornodeinself.nodes:207# for each node, look up all information needed to create a208# CDAO.Clade209self.node_info[node]={}210node_info=self.node_info[node]211212obj=self.obj_info[node]213if'edge'inobj:214# if this object points to an edge, we need a branch length from215# the annotation on that edge216edge=self.obj_info[obj['edge']]217if'annotation'inedge:218annotation=self.obj_info[edge['annotation']]219if'value'inannotation:220node_info['branch_length']=float(annotation['value'])221222if'tu'inobj:223# if this object points to a TU, we need the label of that TU224tu=self.obj_info[obj['tu']]225if'label'intu:226node_info['label']=tu['label']227228if'parent'inobj:229# store this node as a child of its parent, if it has one,230# so that the tree can be traversed from parent to children231parent=obj['parent']232ifparentnotinself.children:233self.children[parent]=[]234self.children[parent].append(node)

324"""recursively generate triples describing a tree of clades."""325self.node_counter+=1326clade.uri='node%s'%str(self.node_counter).zfill(ZEROES)327ifparent:328clade.ancestors=parent.ancestors+[parent.uri]329else:330clade.ancestors=[]331332nUri=lambdas:rdflib.URIRef(s)333pUri=lambdas:rdflib.URIRef(qUri(s))334tree_id=nUri('')335336statements=[]337338ifrootisnotFalse:339# create a cdao:RootedTree with reference to the tree root340tree_type=pUri('cdao:RootedTree')ifself.rootedelsepUri(341'cdao:UnrootedTree')342343statements+=[344(tree_id,pUri('rdf:type'),tree_type),345(tree_id,pUri('cdao:has_Root'),nUri(clade.uri)),346]347348try:349tree_attributes=root.attributes350exceptAttributeError:351tree_attributes=[]352353forpredicate,objintree_attributes:354statements.append((tree_id,predicate,obj))355356ifclade.name:357# create TU358self.tu_counter+=1359tu_uri='tu%s'%str(self.tu_counter).zfill(ZEROES)360361statements+=[362(nUri(tu_uri),pUri('rdf:type'),pUri('cdao:TU')),363(nUri(clade.uri),pUri(364'cdao:represents_TU'),nUri(tu_uri)),365(nUri(tu_uri),pUri('rdfs:label'),366rdflib.Literal(format_label(clade.name))),367]368369try:370tu_attributes=clade.tu_attributes371exceptAttributeError:372tu_attributes=[]373374forpredicate,objintu_attributes:375yield(nUri(tu_uri),predicate,obj)376377# create this node378node_type='cdao:TerminalNode'ifclade.is_terminal(379)else'cdao:AncestralNode'380statements+=[381(nUri(clade.uri),pUri('rdf:type'),pUri(node_type)),382(nUri(clade.uri),pUri(383'cdao:belongs_to_Tree'),tree_id),384]385386ifparentisnotNone:387# create edge from the parent node to this node388self.edge_counter+=1389edge_uri='edge%s'%str(self.edge_counter).zfill(ZEROES)390391statements+=[392(nUri(edge_uri),pUri('rdf:type'),pUri('cdao:DirectedEdge')),393(nUri(edge_uri),pUri(394'cdao:belongs_to_Tree'),tree_id),395(nUri(edge_uri),pUri('cdao:has_Parent_Node'),396nUri(parent.uri)),397(nUri(edge_uri),pUri('cdao:has_Child_Node'),398nUri(clade.uri)),399(nUri(clade.uri),pUri(400'cdao:belongs_to_Edge_as_Child'),nUri(edge_uri)),401(nUri(clade.uri),pUri('cdao:has_Parent'),402nUri(parent.uri)),403(nUri(parent.uri),pUri(404'cdao:belongs_to_Edge_as_Parent'),nUri(edge_uri)),405]406407ifhasattr(clade,'confidence')andclade.confidenceisnotNone:408confidence=rdflib.Literal(409clade.confidence,datatype='http://www.w3.org/2001/XMLSchema#decimal')410411statements+=[(nUri(clade.uri),412pUri('cdao:has_Support_Value'),confidence)]413414ifself.record_complete_ancestryandlen(clade.ancestors)>0:415statements+=[(nUri(clade.uri),pUri('cdao:has_Ancestor'),nUri(ancestor))416forancestorinclade.ancestors]417418ifclade.branch_lengthisnotNone:419# add branch length420edge_ann_uri='edge_annotation%s'%str(421self.edge_counter).zfill(ZEROES)422423branch_length=rdflib.Literal(clade.branch_length,datatype=rdflib.URIRef(424'http://www.w3.org/2001/XMLSchema#decimal'))425statements+=[426(nUri(edge_ann_uri),pUri('rdf:type'),427pUri('cdao:EdgeLength')),428(nUri(edge_uri),pUri('cdao:has_Annotation'),429nUri(edge_ann_uri)),430(nUri(edge_ann_uri),431pUri('cdao:has_Value'),branch_length),432]433434try:435edge_attributes=clade.edge_attributes436exceptAttributeError:437edge_attributes=[]438439forpredicate,objinedge_attributes:440yield(nUri(edge_uri),predicate,obj)441442forstmtinstatements:443yieldstmt444445try:446clade_attributes=clade.attributes447exceptAttributeError:448clade_attributes=[]449450forpredicate,objinclade_attributes:451yield(nUri(clade.uri),predicate,obj)452453ifnotclade.is_terminal():454fornew_cladeinclade.clades:455forstmtinself.process_clade(new_clade,parent=clade,root=False):456yieldstmt