def runAsMedianMedianProblemTest(medianHistory): """Runs AsMedian, requires to be installed. I got it from: https://sites.google.com/site/andrewweixu/Home/software/asmedian """ #Dump to disk tempFile = os.path.join(os.getcwd(), "simulatedGenomeTempFile.txt") fileHandle = open(tempFile, 'w') fileHandle.write(medianHistory.getLeafGenomeString()) fileHandle.close() #-cp /Users/benedictpaten/Desktop/ASMedian-1.0 popenCatch("java BIOMedian %s" % tempFile) os.remove(tempFile) #Parse in fileHandle = open(tempFile + ".rst", 'r') input = fileHandle.readlines() fileHandle.close() os.remove(tempFile + ".rst") asMedianMedianGenome = Genome(chromosomeNumber=0, elementNumber=0) for line in input[1:]: if line[0] == '#': break asMedianChromosome = Chromosome() for element in line.split()[1:]: asMedianChromosome.append(int(element)) asMedianMedianGenome.addChromosome(asMedianChromosome) return asMedianMedianGenome
def runReferenceMedianProblemTest(medianHistory, greedyIterations, theta): """Runs the reference problem for a given median history """ #Make adjacencies stubNumber = 2 nodeNumber = len( medianHistory.getMedianGenome().getElements()) * 2 + stubNumber weights = {} for genome in medianHistory.getLeafGenomes(): for node1, node2, distance in genome.getTransitiveAdjacencies(): if (node1, node2) in weights: weights[(node1, node2)] += weightFn(distance, theta) else: weights[(node1, node2)] = weightFn(distance, theta) def translateLeftSideOfElementToNode(element): assert element != 0 if element < 0: return abs(element) * 2 return element * 2 + 1 def translateLeftNodeToElement(node): assert node >= stubNumber assert node < nodeNumber element = node / 2 if (node % 2) == 0: element *= -1 return element #Now print out the input = "%i\t%i\t%i\t%i\t%s" % ( greedyIterations, nodeNumber, stubNumber, len(weights.keys()), "\t".join([ "%i\t%i\t%f" % (translateLeftSideOfElementToNode(-node1), translateLeftSideOfElementToNode(node2), weights[(node1, node2)]) for (node1, node2) in weights.keys() ])) tempPath = getTempFile() with open(tempPath, 'w') as tempFile: tempFile.write(input) #Command command = os.path.join( os.path.split( os.path.abspath( matchingAndOrdering.tests.simulatedGenome.__file__))[0], "testBin", "referenceMedianProblemTest2") output = popenCatch(command + " < %s" % tempPath) os.remove(tempPath) medianChromosome = Chromosome() for adjacency in output.split(): medianChromosome.append(translateLeftNodeToElement(int(adjacency))) medianGenome = Genome(chromosomeNumber=0, elementNumber=0) medianGenome.addChromosome(medianChromosome) assert medianGenome.getElements() == medianHistory.getMedianGenome( ).getElements() return medianGenome
def testChromosome(self): """Test basic functions of a chromosome. """ c = Chromosome() #Append method c.append(1) c.append(2) #Test string generator self.assertEquals(c.getOrderedElements(), [1, 2]) self.assertEqual(str(c), "1 2") #Test get reverse d = c.getReverse() self.assertEqual(str(c), "1 2") self.assertEqual(str(d), "-2 -1") #Test fuse e = c.fuse(d) self.assertEqual(str(c), "1 2") self.assertEqual(str(d), "-2 -1") self.assertEqual(str(e), "1 2 -2 -1") #Test fuse of null chromosome e = e.fuse(Chromosome()) self.assertEquals(e.getOrderedElements(), [1, 2, -2, -1]) self.assertEqual(str(e), "1 2 -2 -1") #Test random breakpoint for i in xrange(10): f, g = e.getRandomBreakpoint() self.assertEqual(str(f.fuse(g)), "1 2 -2 -1") self.assertEqual(str(d), str(d.clone()))
def testChromosome(self): """Test basic functions of a chromosome. """ c = Chromosome() #Append method c.append(1) c.append(2) #Test string generator self.assertEquals(c.getOrderedElements(), [ 1, 2 ]) self.assertEqual(str(c), "1 2") #Test get reverse d = c.getReverse() self.assertEqual(str(c), "1 2") self.assertEqual(str(d), "-2 -1") #Test fuse e = c.fuse(d) self.assertEqual(str(c), "1 2") self.assertEqual(str(d), "-2 -1") self.assertEqual(str(e), "1 2 -2 -1") #Test fuse of null chromosome e = e.fuse(Chromosome()) self.assertEquals(e.getOrderedElements(), [ 1, 2, -2, -1 ]) self.assertEqual(str(e), "1 2 -2 -1") #Test random breakpoint for i in xrange(10): f, g = e.getRandomBreakpoint() self.assertEqual(str(f.fuse(g)), "1 2 -2 -1") self.assertEqual(str(d), str(d.clone()))
def runReferenceMedianProblemTest(medianHistory, greedyIterations,theta): """Runs the reference problem for a given median history """ #Make adjacencies stubNumber = 2 nodeNumber = len(medianHistory.getMedianGenome().getElements()) * 2 + stubNumber; weights = {} for genome in medianHistory.getLeafGenomes(): for node1, node2, distance in genome.getTransitiveAdjacencies(): if (node1, node2) in weights: weights[(node1, node2)] += weightFn(distance, theta) else: weights[(node1, node2)] = weightFn(distance, theta) def translateLeftSideOfElementToNode(element): assert element != 0 if element < 0: return abs(element) * 2 return element * 2 + 1 def translateLeftNodeToElement(node): assert node >= stubNumber assert node < nodeNumber element = node / 2 if (node % 2) == 0: element *= -1 return element #Now print out the input = "%i\t%i\t%i\t%i\t%s" % (greedyIterations, nodeNumber, stubNumber, len(weights.keys()), "\t".join([ "%i\t%i\t%f" % (translateLeftSideOfElementToNode(-node1), translateLeftSideOfElementToNode(node2), weights[(node1, node2)]) for (node1, node2) in weights.keys()])) #Command command = os.path.join(os.path.split(os.path.abspath(matchingAndOrdering.tests.simulatedGenome.__file__))[0], "testBin", "referenceMedianProblemTest2") output = popenCatch(command, input) medianChromosome = Chromosome() for adjacency in output.split(): medianChromosome.append(translateLeftNodeToElement(int(adjacency))) medianGenome = Genome(chromosomeNumber=0, elementNumber=0) medianGenome.addChromosome(medianChromosome) assert medianGenome.getElements() == medianHistory.getMedianGenome().getElements() return medianGenome