Example #1
0
def to_nex(distances, outfile):
	
	outfile = open(outfile, "w")

	distkeys = distances.keys()
	distkeys.sort()
	
	outfile.write("#nexus\n")
	outfile.write("\n")
	outfile.write("BEGIN Taxa;\n")
	outfile.write("DIMENSIONS ntax="+str(len(distkeys))+";\n")
	outfile.write("TAXLABELS\n")
	
	for template in distkeys:	
		templateName = prettyName(template)
		outfile.write(templateName + "\n")
		
	outfile.write(";\n")
	outfile.write("END; [Taxa]\n")
	
	outfile.write("\n")
	
	outfile.write("BEGIN Distances;\n")
	outfile.write("DIMENSIONS ntax="+str(len(distkeys))+";\n")
	outfile.write("FORMAT labels=left diagonal triangle=lower;\n")
	outfile.write("MATRIX\n")
	
	templateCount =  0
	distkeys = distances.keys()
	distkeys.sort()
	
	for template in distkeys:
		
		tempDistances = distances[template]
	
		distanceString = ""
		gridCount = 0
	
		tempdistkeys = tempDistances.keys()
		tempdistkeys.sort()
	
		for comparisonTemplate in tempdistkeys:
			
			# Prints half grid
			if gridCount > templateCount:
				break
				
			else:
				roundedDistance = round(tempDistances[comparisonTemplate],2)
				distanceString += ("\t" + str(roundedDistance))
				
			gridCount  += 1
	
		# Write out the distances string manually
		outfile.write(template + "\t" + distanceString + "\n")
	
		templateCount += 1
	
	outfile.write(";\n")
	outfile.write("END; [Distances]\n")
Example #2
0
	def add_node(self, node, URI, mother="", predicate=""):
				
		nodeName = node
		
		# Cases may need to be added as new possible "duplicate" possibilities are attested.
		
		# Similar logic applies to all countable nodes. See comments here will help understand others.
		if node == "component":
			if URI in self.components:
				nodeName = self.componentMapping[URI]
			else:
				componentNumber = len(self.components) + 1 # These increments ensure each repeatable node gets its own identifier.
				nodeName = node + str(componentNumber)
				self.components.append(URI)
				self.componentMapping[URI] = nodeName # This is the nodename with the number on it to make sure it can be identified properly for pydot.
				self.core.add_node(nodeName, attrs=[("label", "<<i>component</i>>")]) # Now we add the node to the internal graph, giving it a label without the extra digit.

		elif node == "elastic":
			if URI in self.elastics:
				nodeName = self.elasticMapping[URI]
			else:
				elasticNumber = len(self.elastics) + 1
				nodeName = node + str(elasticNumber)
				self.elastics.append(URI)
				self.elasticMapping[URI] = nodeName
				self.core.add_node(nodeName, attrs=[("label", "<<i>elastic</i>>")])
		
		elif node == "inelastic":		
			
			if URI in self.inelastics:
				nodeName = self.inelasticMapping[URI]
			else:
				inelasticNumber = len(self.inelastics) + 1
				nodeName = node + str(inelasticNumber)
				self.inelastics.append(URI)
				self.inelasticMapping[URI] = nodeName
				self.core.add_node(nodeName, attrs=[("label", "<<i>inelastic</i>>")])

		elif node == "stable":
			if URI in self.stabilities:
				nodeName = self.stabilityMapping[URI]
			else:
				stabilityNumber = len(self.stabilities) + 1
				nodeName = node + str(stabilityNumber)
				self.stabilities.append(URI)
				self.stabilityMapping[URI] = nodeName
				self.core.add_node(nodeName, attrs=[("label", "<<i>stable</i>>")])

		elif node == "unstable":
			if URI in self.unstabilities:
				nodeName = self.unstabilityMapping[URI]
			else:
				unstabilityNumber = len(self.unstabilities) + 1
				nodeName = node + str(unstabilityNumber)
				self.unstabilities.append(URI)
				self.unstabilityMapping[URI] = nodeName
				self.core.add_node(nodeName, attrs=[("label", "<<i>unstable</i>>")])

		elif node == "filled":
			if URI in self.filleds:
				nodeName = self.filledMapping[URI]
			else:
				filledNumber = len(self.filleds) + 1
				nodeName = node + str(filledNumber)
				self.filleds.append(URI)
				self.filledMapping[URI] = nodeName
				self.core.add_node(nodeName, attrs=[("label", "<<i>filled</i>>")])

		elif node == "open":
			if URI in self.opens:
				nodeName = self.openMapping[URI]
			else:
				openNumber = len(self.opens) + 1
				nodeName = node + str(openNumber)
				self.opens.append(URI)
				self.openMapping[URI] = nodeName
				self.core.add_node(nodeName, attrs=[("label", "<<i>open</i>>")])

		elif node == "partiallyFilled":
			if URI in self.partialfilleds:
				nodeName = self.partialfilledMapping[URI]
			else:
				partialfilledNumber = len(self.partialfilleds) + 1
				nodeName = node + str(partialfilledNumber)
				self.partialfilleds.append(URI)
				self.partialfilledMapping[URI] = nodeName
				self.core.add_node(nodeName, attrs=[("label", "<<i>partiallyFilled</i>>")])

		elif node == "final":
			if URI in self.finals:
				nodeName = self.finalMapping[URI]
			else:
				finalNumber = len(self.finals) + 1
				nodeName = node + str(finalNumber)
				self.finals.append(URI)
				self.finalMapping[URI] = nodeName
				self.core.add_node(nodeName, attrs=[("label", "<<i>final</i>>")])

		elif node == "null":
			if URI in self.nulls:
				nodeName = self.nullMapping[URI]
			else:
				nullNumber = len(self.nulls) + 1
				nodeName = node + str(nullNumber)
				self.nulls.append(URI)
				self.nullMapping[URI] = nodeName
				self.core.add_node(nodeName, attrs=[("label", "<<i>null</i>>")])

		elif node == "coherent":
			if URI in self.coherents:
				nodeName = self.coherentMapping[URI]
			else:
				coherentNumber = len(self.coherents) + 1
				nodeName = node + str(coherentNumber)
				self.coherents.append(URI)
				self.coherentMapping[URI] = nodeName
				self.core.add_node(nodeName, attrs=[("label", "<<i>coherent</i>>")])

		elif node == "incoherent":
			if URI in self.incoherents:
				nodeName = self.incoherentMapping[URI]
			else:
				incoherentNumber = len(self.incoherents) + 1
				nodeName = node + str(incoherentNumber)
				self.incoherents.append(URI)
				self.incoherentMapping[URI] = nodeName
				self.core.add_node(nodeName, attrs=[("label", "<<i>incoherent</i>>")])

		elif node == "canonicalLineate":
			if URI in self.canonicals:
				nodeName = self.canonicalMapping[URI]
			else:
				canonicalNumber = len(self.canonicals) + 1
				nodeName = node + str(canonicalNumber)
				self.canonicals.append(URI)
				self.canonicalMapping[URI] = nodeName
				self.core.add_node(nodeName, attrs=[("label", "<<i>canonicalLineate</i>>")])

		# Slightly different scenario for integer values.
		elif node.isdigit():
						
			prettyM = despecification.prettyName(mother)
			prettyP = despecification.prettyName(predicate)
			
			indexName = prettyM + "-" + prettyP
			generalNodeName = prettyP + node
			
			if indexName in self.seenCounts:
				nodeName = self.countMapping[indexName]
				

			else:
				self.seenCounts.append(indexName)
				if self.counts.has_key(generalNodeName):
					self.counts[generalNodeName] = self.counts[generalNodeName] + 1
				else:
					self.counts[generalNodeName] = 1
				nodeName = generalNodeName + "-" + str(self.counts[generalNodeName])
				self.countMapping[indexName] = nodeName
				# If the node is "100" then that is the RDF "shorthand" for infinity. We replace it here for graph display.
				if node == '100':
					self.core.add_node(nodeName,  attrs=[("label", '\xe2\x88\x9e')]) # Some sort of escape sequence for infinity, got it by playing with Python on shell mode.				
				else:
					self.core.add_node(nodeName,  attrs=[("label", node)])

		elif node == "source":
			pass
		
		# If we've made it this far, it's a non-repeatable, generic node.		
		else:
			# make first letter of type name lowercase (needed for names borrowed from GOLD)
			node = node[0].lower() + node[1:]
			self.core.add_node(nodeName, attrs=[("label", "<<i>"+node+"</i>>")])
		return nodeName