def test_create_nested_json(self): """Test that we can create nested json.""" property_tree = GenomePropertiesTree(*self.properties) json_data = property_tree.create_nested_json(as_dict=True) root_id = json_data['id'] """Root could be either GenProp0002 or GenProp0003. See Note 1 in test_find_root_node().""" self.assertIn(root_id, ['GenProp0002', 'GenProp0003']) tree_level_one_children = json_data['children'] self.assertEqual(len(tree_level_one_children), 1) level_one_child = tree_level_one_children[0] self.assertEqual(level_one_child['id'], 'GenProp0066') tree_level_two_children = level_one_child['children'] self.assertEqual(len(tree_level_two_children), 2) level_two_child_one = tree_level_two_children[0] level_two_child_two = tree_level_two_children[1] self.assertIn(level_two_child_one['id'], ['GenProp0089', 'GenProp0092']) self.assertIn(level_two_child_two['id'], ['GenProp0089', 'GenProp0092']) self.assertNotEqual(level_two_child_one['id'], level_two_child_two['id']) self.assertEqual(level_two_child_one['children'], []) self.assertEqual(level_two_child_two['children'], [])
def test_json_string_creation_nodes_and_links(self): property_tree = GenomePropertiesTree(*self.properties) test_json = property_tree.to_json(nodes_and_links=True) expected_json = '''{ "nodes": [{"id": "GenProp0002", "name": "Coenzyme F420 utilization", "type": "GUILD", "description": null, "notes": null}, {"id": "GenProp0003", "name": "Coenzyme F420 utilization", "type": "GUILD", "description": null, "notes": null}, {"id": "GenProp0066", "name": "Coenzyme F420 utilization", "type": "GUILD", "description": null, "notes": null}, {"id": "GenProp0089", "name": "Coenzyme F420 utilization", "type": "GUILD", "description": null, "notes": null}, {"id": "GenProp0092", "name": "Coenzyme F420 utilization", "type": "GUILD", "description": null, "notes": null}], "links": [{"parent": "GenProp0002", "child": "GenProp0066"}, {"parent": "GenProp0003", "child": "GenProp0066"}, {"parent": "GenProp0066", "child": "GenProp0089"}, {"parent": "GenProp0066", "child": "GenProp0092"}]}''' test_json_parsed = json.loads(test_json) expected_json_parsed = json.loads(expected_json) test_json_nodes = test_json_parsed['nodes'] expected_json_nodes = expected_json_parsed['nodes'] test_json_links = test_json_parsed['links'] expected_json_links = expected_json_parsed['links'] self.assertCountEqual(test_json_nodes, expected_json_nodes) self.assertCountEqual(test_json_links, expected_json_links)
def setUpClass(cls): """Set up testing data for testing.""" """ Test Properties Rooted DAG Structure: --> GenProp0089 GenProp0066 --> GenProp0092 """ property_rows_one = [('AC', 'GenProp0066'), ('DE', 'Coenzyme F420 utilization'), ('TP', 'GUILD'), ('--', ''), ('SN', '1'), ('ID', 'Selfish genetic elements'), ('RQ', '0'), ('EV', 'GenProp0089;')] property_rows_two = [('AC', 'GenProp0089'), ('DE', 'Coenzyme F420 utilization'), ('TP', 'GUILD'), ('--', ''), ('SN', '1'), ('ID', 'LLM-family F420-associated subfamilies'), ('RQ', '0'), ('EV', 'IPR019910; TIGR03564; sufficient;')] property_one = parse_genome_property(property_rows_one) property_two = parse_genome_property(property_rows_two) cls.tree = GenomePropertiesTree(property_one, property_two)
def test_build_genome_property_connections(self): """Test that we can add child and parent genome properties.""" test_properties = self.properties property_one = test_properties[0] property_two = test_properties[1] property_three = test_properties[2] property_four = test_properties[3] property_five = test_properties[4] property_tree = GenomePropertiesTree(*test_properties) self.assertEqual(property_tree[property_one.id].children[0], property_three) self.assertEqual(property_tree[property_two.id].children[0], property_three) self.assertCountEqual(property_tree[property_three.id].parents, [property_one, property_two]) self.assertCountEqual(property_tree[property_three.id].children, [property_four, property_five]) self.assertEqual(property_tree[property_four.id].parents[0], property_three) self.assertEqual(property_tree[property_five.id].parents[0], property_three)
def test_find_leaf_nodes(self): """Test we can find the right leaf nodes.""" property_tree = GenomePropertiesTree(*self.properties) leaf_ids = [leaf.id for leaf in property_tree.leafs] self.assertCountEqual(leaf_ids, ['GenProp0089', 'GenProp0092'])
def test_get_property_identifiers(self): """Test that we can get a set of all property identifiers in the tree.""" property_tree = GenomePropertiesTree(*self.properties) self.assertEqual( property_tree.genome_property_identifiers, { 'GenProp0002', 'GenProp0089', 'GenProp0066', 'GenProp0003', 'GenProp0092' })
def test_find_root_node(self): """Test that we can find the correct genome property root.""" property_tree = GenomePropertiesTree(*self.properties) root = property_tree.root """ Note 2: The root could be GenProp0002 or GenProp0003 depending on which one is stored first in the property tree. Since we are using a dict not an OrderedDict inside of GenomePropertyTree we cannot guarantee that GenProp0002 will always be returned as root. Thus we check if the root node is either property. """ self.assertIn(root.id, ['GenProp0002', 'GenProp0003'])
def test_json_string_creation(self): property_tree = GenomePropertiesTree(*self.properties) test_json = property_tree.to_json() expected_json_one = '''{"id": "GenProp0002", "name": "Coenzyme F420 utilization", "type": "GUILD", "description": null, "notes": null, "children": [{"id": "GenProp0066", "name": "Coenzyme F420 utilization", "type": "GUILD", "description": null, "notes": null, "children": [ {"id": "GenProp0089", "name": "Coenzyme F420 utilization", "type": "GUILD", "description": null, "notes": null, "children": []}, {"id": "GenProp0092", "name": "Coenzyme F420 utilization", "type": "GUILD", "description": null, "notes": null, "children": []}]}]}''' test_json_parsed = json.loads(test_json) expected_json_parsed_one = json.loads(expected_json_one) """Root could be either GenProp0002 or GenProp0003. See Note 1 in test_find_root_node().""" expected_json_parsed_two = deepcopy(expected_json_parsed_one) expected_json_parsed_two['id'] = 'GenProp0003' self.assertIn(test_json_parsed, [expected_json_parsed_one, expected_json_parsed_two])
def test_create_json_graph_nodes(self): """Test that we can create nodes json.""" property_tree = GenomePropertiesTree(*self.properties) json_nodes = property_tree.create_graph_nodes_json(as_list=True) ids = {node['id'] for node in json_nodes} names = {node['name'] for node in json_nodes} types = {node['type'] for node in json_nodes} descriptions = {node['description'] for node in json_nodes} notes = {node['notes'] for node in json_nodes} self.assertCountEqual( ids, { 'GenProp0002', 'GenProp0003', 'GenProp0066', 'GenProp0089', 'GenProp0092' }) self.assertEqual(names, {'Coenzyme F420 utilization'}) self.assertEqual(types, {'GUILD'}) self.assertEqual(descriptions, {None}) self.assertEqual(notes, {None})
def test_create_json_graph_links(self): """Test that we can create parent child link json.""" property_tree = GenomePropertiesTree(*self.properties) json_links = property_tree.create_graph_links_json(as_list=True) predicted_links = [{ 'parent': 'GenProp0002', 'child': 'GenProp0066' }, { 'parent': 'GenProp0003', 'child': 'GenProp0066' }, { 'parent': 'GenProp0066', 'child': 'GenProp0089' }, { 'parent': 'GenProp0066', 'child': 'GenProp0092' }] self.assertCountEqual(json_links, predicted_links)
def setUpClass(cls): """Set up testing data for testing.""" prebuilt_cache = AssignmentCache() prebuilt_cache.cache_property_assignment('GenProp0053', 'YES') prebuilt_cache.cache_property_assignment('GenProp0052', 'NO') prebuilt_cache.cache_property_assignment('GenProp0051', 'PARTIAL') prebuilt_cache.cache_step_assignment('GenProp0053', 1, 'YES') prebuilt_cache.cache_step_assignment('GenProp0053', 2, 'NO') prebuilt_cache.cache_step_assignment('GenProp0053', 3, 'YES') cls.cache = prebuilt_cache """ Test Properties Rooted DAG Structure: --> GenProp0089 GenProp0066 --> GenProp0092 """ property_rows_one = [('AC', 'GenProp0066'), ('DE', 'Coenzyme F420 utilization'), ('TP', 'GUILD'), ('--', ''), ('SN', '1'), ('ID', 'Selfish genetic elements'), ('RQ', '0'), ('EV', 'GenProp0089;'), ('--', ''), ('SN', '2'), ('ID', 'Selfish genetic elements'), ('RQ', '0'), ('EV', 'GenProp0092;')] property_rows_two = [('AC', 'GenProp0089'), ('DE', 'Coenzyme F420 utilization'), ('TP', 'GUILD'), ('--', ''), ('SN', '1'), ('ID', 'LLM-family F420-associated subfamilies'), ('RQ', '0'), ('EV', 'IPR019910; TIGR03564; sufficient;')] property_rows_three = [('AC', 'GenProp0092'), ('DE', 'Coenzyme F420 utilization'), ('TP', 'GUILD'), ('--', ''), ('SN', '1'), ('ID', 'LLM-family F420-associated subfamilies'), ('RQ', '0'), ('EV', 'IPR019910; TIGR03565; sufficient;')] property_one = parse_genome_property(property_rows_one) property_two = parse_genome_property(property_rows_two) property_three = parse_genome_property(property_rows_three) raw_properties = [property_one, property_two, property_three] cls.tree = GenomePropertiesTree(*raw_properties)
def test_get_step_genome_properties(self): """Test that we can get child genome properties directly from a step.""" property_tree = GenomePropertiesTree(*self.properties) steps = property_tree.root.steps step_one_genome_properties = steps[0].genome_properties step_two_genome_properties = steps[1].genome_properties self.assertEquals(len(step_one_genome_properties), 1) self.assertEquals(step_one_genome_properties[0].id, 'GenProp0089') self.assertEquals(len(step_two_genome_properties), 1) self.assertEquals(step_two_genome_properties[0].id, 'GenProp0092')
def parse_genome_properties_flat_file(genome_property_file): """ A parses a genome property flat file. :param genome_property_file: A genome property file handle object. :return: A GenomePropertyTree object. """ genome_properties = [] current_genome_property_record = [] for line in genome_property_file: if not line.strip() == '//': current_genome_property_record.append(create_marker_and_content(line)) else: collapsed_genome_property_record = unwrap_genome_property_record(current_genome_property_record) new_genome_property = parse_genome_property(collapsed_genome_property_record) genome_properties.append(new_genome_property) current_genome_property_record = [] genome_properties_tree = GenomePropertiesTree(*genome_properties) return genome_properties_tree
def test_get_consortium_identifiers(self): """Test that we can get a set of all consortium identifiers used as evidence by the genome properties tree.""" property_tree = GenomePropertiesTree(*self.properties) self.assertEqual(property_tree.consortium_identifiers, {'TIGR03564', 'TIGR03565'})
def test_get_interpro_identifiers(self): """Test that we can get a set of all InterPro identifiers used as evidence by the genome properties tree.""" property_tree = GenomePropertiesTree(*self.properties) self.assertEqual(property_tree.interpro_identifiers, {'IPR019910', 'IPR019911'})