def test_set_values(self, category_data): dcU = DataCategory('A', category_data['attributeListMiss'], category_data['rowListUnicodeMiss']) for i in range(0, dcU.getRowCount()): dcU.setValue('newValue', attributeName='colM1', rowIndex=i) assert dcU.setValue('newValue', attributeName='colM1', rowIndex=dcU.getRowCount() + 5) with pytest.raises(ValueError): dcU.setValue('newValue', 'colX', 0)
def test_compare_values(self, category_data): dcU = DataCategory('A', category_data['attributeList'], category_data['rowListUnicode']) dcM = DataCategory('A', category_data['attributeListMiss'], category_data['rowListUnicodeMiss']) na = dcU.getAttributeList() assert len(na) >= 1 tupL = dcU.cmpAttributeValues(dcU) for tup in tupL: assert tup[1] == True tupL = dcU.cmpAttributeValues(dcM) for tup in tupL: if tup[0] in ['colC', 'colD']: assert tup[1] == False else: assert tup[1] == True # dcX = DataCategory('A', category_data['attributeList'], category_data['rowListUnicode']) assert dcX.setValue( u'134ĆćĈĉĊċČÄ�ÄŽÄ�Ä�Ä‘Ä’Ä“Ä Ä¡Ä¢Ä£Ä¤Ä¥Ä¦Ä§Ä¨xyz', attributeName='colD', rowIndex=dcX.getRowCount() - 2) tupL = dcU.cmpAttributeValues(dcX) for tup in tupL: if tup[0] in ['colD']: assert tup[1] == False else: assert tup[1] == True
def testCompareValues(self): """Test case - compare object values -""" try: dcU = DataCategory("A", self.__attributeList, self.__rowListUnicode) dcM = DataCategory("A", self.__attributeListMiss, self.__rowListUnicodeMiss) na = dcU.getAttributeList() self.assertGreaterEqual(len(na), 1) tupL = dcU.cmpAttributeValues(dcU) for tup in tupL: self.assertEqual(tup[1], True) tupL = dcU.cmpAttributeValues(dcM) for tup in tupL: if tup[0] in ["colC", "colD"]: self.assertEqual(tup[1], False) else: self.assertEqual(tup[1], True) # dcX = DataCategory("A", self.__attributeList, self.__rowListUnicode) self.assertTrue( dcX.setValue(u"134ĆćĈĉĊċČčĎďĐđĒēĠġĢģĤĥĦħĨxyz", attributeName="colD", rowIndex=dcX.getRowCount() - 2)) tupL = dcU.cmpAttributeValues(dcX) for tup in tupL: if tup[0] in ["colD"]: self.assertEqual(tup[1], False) else: self.assertEqual(tup[1], True) except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def testSetValues(self): """Test case - value setters""" try: dcU = DataCategory("A", self.__attributeListMiss, self.__rowListUnicodeMiss) for i in range(0, dcU.getRowCount()): dcU.setValue("newValue", attributeName="colM1", rowIndex=i) self.assertTrue( dcU.setValue("newValue", attributeName="colM1", rowIndex=dcU.getRowCount() + 5)) self.assertRaises(ValueError, dcU.setValue, "newValue", "colX", 0) except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def process_entry(file_in, file_out): try: cif_file = gemmi.cif.read(file_in) # pylint: disable=no-member data_block = cif_file[0] except Exception as e: logger.error("Failed to read cif file in Gemmi") logger.error(e) return 1 logging.info("Finding Centre of Mass") com = get_center_of_mass(data_block) if not com: return 1 try: io = IoAdapterCore() ccL = io.readFile(file_in) except Exception as e: logger.error("Failed to read cif file using IoAdapterCore %s", e) return 1 if len(ccL) == 0: logger.error("No data parsed from file") return 1 # First block only b0 = ccL[0] obj = b0.getObj("struct") # If category does not exist if obj is None: # Need entry.id eid = "XXXX" eobj = b0.getObj("entry") if eobj: if "id" in eobj.getAttributeList(): eid = eobj.getValue("id", 0) obj = DataCategory("struct") obj.appendAttribute("entry_id") obj.setValue(eid, "entry_id", 0) ccL[0].append(obj) newdata = [["pdbx_center_of_mass_x", com.x], ["pdbx_center_of_mass_y", com.y], ["pdbx_center_of_mass_z", com.z]] for [it, val] in newdata: if it not in obj.getAttributeList(): obj.appendAttribute(it) obj.setValue(str(val), it, 0) try: logging.info("Writing mmcif file: %s", file_out) ret = io.writeFile(file_out, ccL) if not ret: logger.info("Writing failed error %s", ret) return 1 except Exception as e: logger.error("Failed to write ccif file in IoAdapater %s", e) return 1 # existing_data = data_block.get_mmcif_category('_struct.') # new_data = { # **existing_data, # 'pdbx_center_of_mass_x': [com.x], # 'pdbx_center_of_mass_y': [com.y], # 'pdbx_center_of_mass_z': [com.z] # } # logging.info("Writing mmcif file: %s", file_out) # try: # data_block.set_mmcif_category('_struct.', new_data) # cif_file.write_file(file_out) # except Exception as e: # logger.error("Failed to write cif file in Gemmi") # logger.error(e) # return 1 return 0
def __extractExtra(self, xrt): """ Separately extract and parse data from the input document related to EM graphs. Args: xrt: ElementTree root element Returns: (list): DataCategory objects """ rL = [] elV = xrt.find("EM_validation") if not elV: return rL # graphDataL = [] # logger.debug("Starting extraExtract -- ") gAtList = [ "graph_data_id", "graph_id", "title", "x_axis_title", "x_axis_scale", "x_axis_units", "y_axis_title", "y_axis_scale", "y_axis_units" ] infObj = DataCategory("pdbx_vrpt_em_2d_graph_info", gAtList) # for el in elV: logger.debug("-- EM element tag %r attrib count (%d): %r", el.tag, len(list(el.attrib.keys())), list(el.attrib.keys())) if el.tag == "RecommendedContourLevel" and "value" in el.attrib: cObj = DataCategory( "pdbx_vrpt_em_details", attributeNameList=["ordinal", "recommended_contour_level"]) cObj.setValue(1, "ordinal", 0) cObj.setValue(el.attrib["value"], "recommended_contour_level", 0) rL.append(cObj) elif el.tag == "map_value_distribution": try: dL = self.__getGraphDataElements(el, graphDataId="d_mvd") cgD = self.__getCommonGraphAttributes(el) if dL and cgD: iRow = infObj.getRowCount() for k, v in cgD.items(): infObj.setValue(v, k, iRow) infObj.setValue("map_value_distribution", "graph_id", iRow) infObj.setValue("d_mvd", "graph_data_id", iRow) cObj = DataCategory( "pdbx_vrpt_em_graph_map_value_distribution", attributeNameList=["graph_id"]) cObj.setValue("map_value_distribution", "graph_id", 0) graphDataL.extend(dL) rL.append(cObj) except Exception as e: logger.exception("Failing with %s", str(e)) elif el.tag == "volume_estimate": try: dL = self.__getGraphDataElements(el, graphDataId="d_ve") cgD = self.__getCommonGraphAttributes(el) if dL and cgD: iRow = infObj.getRowCount() for k, v in cgD.items(): infObj.setValue(v, k, iRow) infObj.setValue("volume_estimate", "graph_id", iRow) infObj.setValue("d_ve", "graph_data_id", iRow) cObj = DataCategory( "pdbx_vrpt_em_graph_volume_estimate", attributeNameList=["graph_id"]) cObj.setValue("volume_estimate", "graph_id", 0) graphDataL.extend(dL) rL.append(cObj) except Exception as e: logger.exception("Failing with %s", str(e)) elif el.tag == "rotationally_averaged_power_spectrum": try: dL = self.__getGraphDataElements(el, graphDataId="d_raps") cgD = self.__getCommonGraphAttributes(el) if dL and cgD: iRow = infObj.getRowCount() for k, v in cgD.items(): infObj.setValue(v, k, iRow) infObj.setValue("rotationally_averaged_power_spectrum", "graph_id", iRow) infObj.setValue("d_raps", "graph_data_id", iRow) cObj = DataCategory( "pdbx_vrpt_em_graph_rotationally_averaged_power_spectrum", attributeNameList=["graph_id"]) cObj.setValue("rotationally_averaged_power_spectrum", "graph_id", 0) graphDataL.extend(dL) rL.append(cObj) except Exception as e: logger.exception("Failing with %s", str(e)) elif el.tag == "atom_inclusion": # backbone or all_atoms try: cObj = None for cN in ["all_atoms", "backbone"]: ch = el.find(cN) abbrev = "aa" if cN == "all_atoms" else "bb" if ch: gId = "atom_inclusion_%s" % cN gdId = "d_ai_%s" % abbrev dL = self.__getGraphDataElements(ch, graphDataId=gdId) cgD = self.__getCommonGraphAttributes(ch) if dL and cgD: iRow = infObj.getRowCount() for k, v in cgD.items(): infObj.setValue(v, k, iRow) infObj.setValue(gId, "graph_id", iRow) infObj.setValue(gdId, "graph_data_id", iRow) # if not cObj: cObj = DataCategory( "pdbx_vrpt_em_graph_atom_inclusion", attributeNameList=["graph_id", "type"]) tRow = cObj.getRowCount() cObj.setValue(gId, "graph_id", tRow) cObj.setValue(cN, "type", tRow) graphDataL.extend(dL) if cObj.getRowCount(): rL.append(cObj) except Exception as e: logger.exception("Failing with %s", str(e)) elif el.tag == "fsc": for ch in el: logger.debug( "-- Child fsc element tag %r attrib count (%d): %r", ch.tag, len(list(ch.attrib.keys())), list(ch.attrib.keys())) if ch.tag == "resolution_intersections": try: atList = [ "ordinal", "resolution_units", "spatial_frequency_units", "correlation", "resolution", "spatial_frequency", "curve", "type" ] rObj = DataCategory( "pdbx_vrpt_em_resolution_intersections", atList) ru = ch.attrib[ "resolution_unit"] if "resolution_unit" in ch.attrib else "?" sfu = ch.attrib[ "spatial_frequency_unit"] if "spatial_frequency_unit" in ch.attrib else "?" ii = 0 for gch in ch: if gch.tag == "intersection": for at in [ "correlation", "resolution", "spatial_frequency", "curve", "type" ]: atV = gch.attrib[ at] if at in gch.attrib else "?" rObj.setValue(atV, at, ii) rObj.setValue(ru, "resolution_units", ii) rObj.setValue(sfu, "spatial_frequency_units", ii) rObj.setValue(ii + 1, "ordinal", ii) ii += 1 if rObj.getRowCount(): rL.append(rObj) except Exception as e: logger.exception("Failing with %s", str(e)) elif ch.tag == "fsc_curves": try: iCount = 0 cObj = None for gch in ch: if gch.tag == "fsc_curve": iCount += 1 gdId = "fsc_%d" % iCount dL = self.__getGraphDataElements( gch, graphDataId=gdId) cgD = self.__getCommonGraphAttributes(gch) curveName = gch.attrib[ "curve_name"] if "curve_name" in gch.attrib else "?" gId = curveName fscType = gch.attrib[ "type"] if "type" in gch.attrib else "?" if dL and cgD and curveName != "?": iRow = infObj.getRowCount() for k, v in cgD.items(): infObj.setValue(v, k, iRow) # infObj.setValue(gId, "graph_id", iRow) infObj.setValue( gdId, "graph_data_id", iRow) # iRow = iCount - 1 if not cObj: cObj = DataCategory( "pdbx_vrpt_em_graph_fsc_curve", attributeNameList=[ "graph_id", "type", "curve_name" ]) cObj.setValue(gId, "graph_id", iRow) cObj.setValue(fscType, "type", iRow) cObj.setValue(curveName, "curve_name", iRow) graphDataL.extend(dL) if cObj: rL.append(cObj) except Exception as e: logger.exception("Failing with %s", str(e)) elif ch.tag == "fsc_indicator_curves": try: iCount = 0 cObj = None for gch in ch: if gch.tag == "fsc_indicator_curve": iCount += 1 gdId = "fsc_i_%d" % iCount dL = self.__getGraphDataElements( gch, graphDataId=gdId) cgD = self.__getCommonGraphAttributes(gch) curveName = gch.attrib[ "curve_name"] if "curve_name" in gch.attrib else "?" gId = curveName fscType = gch.attrib[ "type"] if "type" in gch.attrib else "?" dataCurveName = gch.attrib[ "data_curve"] if "data_curve" in gch.attrib else "?" if dL and cgD and curveName != "?" and dataCurveName != "?": iRow = infObj.getRowCount() for k, v in cgD.items(): infObj.setValue(v, k, iRow) # infObj.setValue(gId, "graph_id", iRow) infObj.setValue( gdId, "graph_data_id", iRow) # iRow = iCount - 1 if not cObj: cObj = DataCategory( "pdbx_vrpt_em_graph_fsc_indicator_curve", attributeNameList=[ "graph_id", "type", "curve_name", "data_curve_name" ]) cObj.setValue(gId, "graph_id", iRow) cObj.setValue(fscType, "type", iRow) cObj.setValue(curveName, "curve_name", iRow) cObj.setValue(dataCurveName, "data_curve_name", iRow) graphDataL.extend(dL) if cObj: rL.append(cObj) except Exception as e: logger.exception("Failing with %s", str(e)) # # -- end of element processing # ------- if infObj.getRowCount(): rL.append(infObj) # dObj = DataCategory( "pdbx_vrpt_em_2d_graph_data", ["ordinal", "graph_data_id", "x_value", "y_value"]) for ii, dD in enumerate(graphDataL): for k, v in dD.items(): dObj.setValue(v, k, ii) dObj.setValue(ii + 1, "ordinal", ii) rL.append(dObj) return rL