def main(path): ## load structure struct = FileIO.LoadStructure(file=path) num_atoms = 0 mean_B = 0.0 sigma_B = 0.0 min_B = 1000.0 max_B = 0.0 for atm in struct.iter_all_atoms(): num_atoms += 1 mean_B += atm.temp_factor min_B = min(min_B, atm.temp_factor) max_B = max(max_B, atm.temp_factor) if num_atoms > 0: mean_B = mean_B / num_atoms print "mean B: ", mean_B print "max B: ", max_B print "min B: ", min_B else: print "No Atoms Found"
def main(pdb_path, tls_out_path, calc_tls): struct = FileIO.LoadStructure(file=pdb_path) ## calculate one set of TLS tensors from all the amino acid atoms if calc_tls == True: tls = TLS.TLSGroup() for res in struct.iter_amino_acids(): for atm in res.iter_atoms(): tls.append(atm) tls.origin = tls.calc_centroid() tls.calc_tls_tensors() print_TLSGroup(tls) else: tls_file = TLS.TLSFile() ## get TLS groups from REMARK statments in PDB file if tls_out_path == None: tls_file.set_file_format(TLS.TLSFileFormatPDB()) try: tls_file.load(open(pdb_path, "r"), pdb_path) except IOError, e: print "[Error] %s: %s" % (str(e), pdb_path) ## or get TLS groups from REFMAC TLSOUT file else:
def main(path): ## load structure print "Loading ", path struct = FileIO.LoadStructure(file=path) ## list of anisotropic values anisou_list = [] ## iterate over all atoms in the structure for res in struct.iter_amino_acids(): for atm in res.iter_atoms(): anisou = atm.calc_anisotropy() anisou_list.append(anisou) ## this function counts the number of anisotropic values in ## anisou_list in the range amin to amax def count(amin, amax): c = 0 for x in anisou_list: if x >= amin and x < amax: c += 1 return c bin_size = 0.05 amax = bin_size while amax <= 1.001: amin = amax - bin_size print "Ansotropy Range (%f,%f): %d atoms " % (amin, amax, count(amin, amax)) amax += bin_size
def save_verify(struct, stats): """Save structure in all supported formats, then reload it and compare structures. """ ## pdb print "[temp.pdb]" FileIO.SaveStructure(fil="temp.pdb", struct=struct, format="PDB") pdb_struct = FileIO.LoadStructure(fil="temp.pdb", library_bonds=True) cmp_struct(struct, pdb_struct) ## mmCIF print "[temp.cif]" FileIO.SaveStructure(fil="temp.cif", struct=struct, format="CIF") cif_struct = FileIO.LoadStructure(fil="temp.cif", library_bonds=True) cmp_struct(struct, cif_struct)
def main(path, verbose): struct = FileIO.LoadStructure(file=path) print struct print "Alpha Helicies:" for ahelix in struct.iter_alpha_helicies(): print " ", ahelix if verbose == True: print " ", ahelix.segment print "Beta Sheets:" for bsheet in struct.iter_beta_sheets(): print " ", bsheet if verbose == True: for strand in bsheet.iter_strands(): print " ", strand print " ", strand.segment print "Sites:" for site in struct.iter_sites(): print " ", site if verbose == True: for frag in site.iter_fragments(): print " ", frag
def LoadStructure(struct_source): """Loads Structure, chooses a unique struct_id string. Also, search the REMARK records for TLS group records. If they are found, then add the TLS group ADP magnitude to the B facors of the ATOM records. """ ## determine the argument type if isinstance(struct_source, str): file_path = struct_source console.kvformat("LOADING STRUCTURE", file_path) fobj = open(file_path, "r") elif hasattr(struct_source, "__iter__") and hasattr(struct_source, "seek"): console.kvformat("LOADING STRUCTURE", str(struct_source)) fobj = struct_source else: raise ValueError ## load struct struct = FileIO.LoadStructure(file = fobj, distance_bonds = True) console.kvformat("HEADER", struct.header) console.kvformat("TITLE", struct.title) console.kvformat("EXPERIMENTAL METHOD", struct.experimental_method) ## set the structure ID if conf.globalconf.struct_id is not None: struct_id = conf.globalconf.struct_id else: struct_id = struct.structure_id conf.globalconf.struct_id = struct_id struct.structure_id = struct_id console.endln() ## if there are REFMAC5 TLS groups in the REMARK records of ## the PDB file, then add those in tls_file = TLS.TLSFile() tls_file.set_file_format(TLS.TLSFileFormatPDB()) ## return to the beginning of the file and read the REMARK/TLS records fobj.seek(0) tls_file.load(fobj) if len(tls_file.tls_desc_list) > 0: console.stdoutln("ADDING TLS GROUP Bequiv TO ATOM TEMPERATURE FACTORS") console.stdoutln(" NUM TLS GROUPS: %d" % (len(tls_file.tls_desc_list))) ## assume REFMAC5 groups where Utotal = Utls + Biso(temp_factor) for tls_desc in tls_file.tls_desc_list: tls_group = tls_desc.construct_tls_group_with_atoms(struct) console.stdoutln(" TLS GROUP: %s" % (tls_group.name)) for atm, Utls in tls_group.iter_atm_Utls(): bresi = atm.temp_factor atm.temp_factor = bresi + (Constants.U2B * numpy.trace(Utls) / 3.0) atm.U = (Constants.B2U * bresi * numpy.identity(3, float)) + Utls console.endln() return struct
def testmain(): import tlsmd_analysis struct = FileIO.LoadStructure( file="/home/jpaint/mymmlib/struct/movedb/domain-hinge/groel/1KP8.pdb") chain = tlsmd_analysis.ConstructSegmentForAnalysis(struct.get_chain("A")) cpartition = opt_containers.ChainPartition(chain, 3) groups = [[("2", "135"), ("411", "525")], [("136", "190"), ("375", "410")], [("191", "374")]] for segment_ranges in groups: tls = opt_containers.TLSSegment(segment_ranges) cpartition.add_tls_segment(tls) cpartition.fit_residual() partitions = ChainPartitionList(cpartition) print partitions, cpartition.residual() for part in partitions: print "PARTITION ", part part.move_left(10) for x in range(20): try: part.move_right() except ValueError: print "reached limit" print str(partitions).replace(" ", ""), cpartition.residual() part.move_left(10) return for x in range(60): try: partitions[2].move_right() except ValueError: print "reached limit" print str(partitions).replace(" ", ""), cpartition.residual() return print "Move 1 right" for x in range(15): try: partitions[0].move_right() except ValueError: print "reached limit" print partitions, cpartition.residual()
def refmac_pure_tls_prep(xyzin, tlsin_list, wilson, xyzout, tlsout): """Use TLS model (without Uiso) for each atom. Output xyzout with the B-factors reset to either the Bmean or the Wilson B value. """ ## load structure struct = FileIO.LoadStructure(fil=xyzin) ## load and construct TLS groups tls_group_list = [] tls_file = TLS.TLSFile() tls_file.set_file_format(TLS.TLSFileFormatPureTLSOUT()) tls_file_format = TLS.TLSFileFormatPureTLSOUT() for tlsin in tlsin_list: tls_desc_list = tls_file_format.load(open(tlsin, "r")) for tls_desc in tls_desc_list: tls_file.tls_desc_list.append(tls_desc) tls_group = tls_desc.construct_tls_group_with_atoms(struct) tls_group.tls_desc = tls_desc tls_group_list.append(tls_group) ## set the extra Uiso for each atom for tls_group in tls_group_list: Bmean = 0.0 sum_Biso = 0.0 num_atms = 0 for atm, Utls in tls_group.iter_atm_Utls(): num_atms += 1 sum_Biso += atm.temp_factor ## EAM Aug 2011: num_atms goes to zero if there are atom selection problems. ## The output files will contain empty groups, but at least it doesn't crash. if num_atms > 0: Bmean = sum_Biso / num_atms ## reset the TLS tensor values in the TLSDesc object so they can be ## saved tls_group.tls_desc.set_tls_group(tls_group) ## reset atm.temp_factor to the Bmean for this TLS group for atm, Utls in tls_group.iter_atm_Utls(): atm.temp_factor = Bmean atm.temp_factor = wilson ## save the new xyzout file with temp_factors reset to "wilson" value FileIO.SaveStructure(fil=xyzout, struct=struct) ## save the REFMAC-format file, but without T, L, S, and ORIGIN values tls_file.save(open(tlsout, "w"))
def load_struct(self, path): """Loads the requested structure. """ info("loading: %s" % (path)) try: struct = FileIO.LoadStructure(fil=path, distance_bonds=True) except IOError: error("file not found: %s" % (path)) return None struct_desc = {} struct_desc["struct"] = struct struct_desc["path"] = path glstruct = self.glv_add_struct(struct) return struct
def phenix_prep(xyzin, phenix_tlsin_list, phenix_tlsout): """PHENIX input file. Tells 'phenix.refine' what the TLS groups are. """ ## load structure struct = FileIO.LoadStructure(fil=xyzin) ## load and construct TLS groups tls_group_list = [] tls_file = TLS.TLSFile() tls_file.set_file_format(TLS.TLSFileFormatPHENIX()) tls_file_format = TLS.TLSFileFormatPHENIX() for tlsin in phenix_tlsin_list: tls_desc_list = tls_file_format.load(open(tlsin, "r")) for tls_desc in tls_desc_list: tls_file.tls_desc_list.append(tls_desc) tls_group = tls_desc.construct_tls_group_with_atoms(struct) tls_group.tls_desc = tls_desc tls_group_list.append(tls_group) ## now save the PHENIX file in phenix.refine format tls_file.save(open(phenix_tlsout, "w"))
def load_struct(self, path = None): try: struct = FileIO.LoadStructure( fil = path, library_bonds = True, distance_bonds = True ) except IOError: error( "file not found: %s" % (path) ) return None struct_desc = {} struct_desc["struct"] = struct struct_desc["path"] = path self.glstruct = self.glv_add_struct(struct) self.prop_editor = GLPropertyBrowserDialog( glo_root = self.glstruct ) return struct
def SumperimposeHomologousStructure(analysis): """ """ import structcmp target_struct = FileIO.LoadStructure(fil = analysis.struct2_file_path) target_chain = target_struct.get_chain(analysis.struct2_chain_id) if target_chain is None: console.stderrln( "UNABLE TO LOAD TARGET STRUCTURE/CHAIN: %s:%s" % ( target_struct, target_chain)) return analysis.target_chain = target_chain for chain in analysis.iter_chains(): console.endln() console.kvformat("Superimposing Chain", chain.chain_id) hyp = structcmp.TLSConformationPredctionHypothosis(chain, target_chain) for ntls, cpartition in chain.partition_collection.iter_ntls_chain_partitions(): console.endln() console.stdoutln("Number of TLS Segments........: %d" % (ntls)) hyp.add_conformation_prediction_to_chain_partition(cpartition)
def main(tlsin, struct_in, struct_out): struct = FileIO.LoadStructure(file=struct_in) ## make the TLS groups fil = open(tlsin, "r") tls_file = TLS.TLSFile() tls_file.set_file_format(TLS.TLSFileFormatTLSOUT()) tls_file.load(fil) tls_group_list = [] for tls_desc in tls_file.tls_desc_list: tls = tls_desc.construct_tls_group_with_atoms(struct) tls.tls_desc = tls_desc tls_group_list.append(tls) print tls.origin for tls in tls_group_list: for atm, Utls in tls.iter_atm_Utls(): atm.U = Utls + (Constants.B2U * atm.temp_factor * numpy.identity(3, float)) atm.temp_factor = Constants.U2B * (numpy.trace(atm.U) / 3.0) ## save the struct FileIO.SaveStructure(file=struct_out, struct=struct)
def main(walk_path, start_path): print "Running Python Macromolecular Library Test Program" print "--------------------------------------------------" print "This program will throw a AssertionError (or worse!) if it" print "runs into any problems." print for path in test_util.walk_pdb_cif(walk_path, start_path): print "[%s]" % (path) time1 = time.time() stats = Stats() struct = FileIO.LoadStructure(fil=path, build_properties=("library_bonds", )) ## track memory leaks sref = weakref.ref(struct, weakref_callback) WEAKREF_PATH[sref] = path WEAKREF_LIST.append(sref) ## test the mmLib.Structure object API and ## with massive sanity checking print "[loaded struct]" try: run_structure_tests(struct, stats) except AssertionError: print "*** AssertionError while testing: %s ***" % (str( stats["testing"])) raise except: print "*** Error while testing: %s ***" % (str(stats["testing"])) raise stats.print_stats() ## copy the structure and re-run those tests print "[copy struct]" struct_cp = copy.deepcopy(struct) cmp_struct(struct, struct_cp) ## verify the number of atoms in the mmLib.Structure object ## matches the number of atoms in the source file file_verify(path, struct, stats) ## test file saving from mmLib.CIF import DataBlock if (hasattr(struct, "cif_data") and isinstance(struct.cif_data, DataBlock)): print "[save verify skipped]" else: print "[save verify]" save_verify(struct, stats) time2 = time.time() print "Tests Time (sec)-----:", int(time2 - time1) ## dereference! struct = None struct_cp = None stats = None stats_cp = None ## force garbage collection gc.collect() print
def main(path, opt_dict): struct = FileIO.LoadStructure(file = path) tls_group_list = [] ## make the TLS groups if opt_dict.has_key("-t"): try: fil = open(opt_dict["-t"], "r") except IOError: print "ERROR: TLSIN File not found %s" % (opt_dict["-t"]) sys.exit(-1) tls_file = TLS.TLSFile() tls_file.set_file_format(TLS.TLSFileFormatTLSOUT()) tls_file.load(fil) for tls_desc in tls_file.tls_desc_list: #tls = tls_desc.generate_tls_group(struct) ## old def tls = tls_desc.construct_tls_group_with_atoms(struct) tls.tls_desc = tls_desc tls_group_list.append(tls) print tls.name else: tls_desc = TLS.TLSGroupDesc() tls_desc.add_range("A", "1", "A", "25", "ALL") tls_group = tls_desc.construct_tls_group_with_atoms(struct) tls_group_list.append(tls_group) tls_group.tls_desc = tls_desc tls_group.origin = tls_group.calc_centroid() tls_group.T = rt_random(T1) tls_group.L, tls_group.S = rt_random2(L1, S1) tls_desc = TLS.TLSGroupDesc() tls_desc.add_range("A", "26", "A", "35", "ALL") tls_group = tls_desc.construct_tls_group_with_atoms(struct) tls_group_list.append(tls_group) tls_group.tls_desc = tls_desc tls_group.origin = tls_group.calc_centroid() tls_group.T = rt_random(T2) tls_group.L, tls_group.S = rt_random2(L2, S1) tls_desc = TLS.TLSGroupDesc() tls_desc.add_range("A", "36", "A", "52", "ALL") tls_group = tls_desc.construct_tls_group_with_atoms(struct) tls_group_list.append(tls_group) tls_group.tls_desc = tls_desc tls_group.origin = tls_group.calc_centroid() tls_group.T = rt_random(T3) tls_group.L, tls_group.S = rt_random2(L3, S1) for atm in struct.iter_all_atoms(): atm.U = None ## write out ideal TLS ANISOU records if opt_dict.has_key("-p"): for tls_group in tls_group_list: for atm, Utls in tls_group.iter_atm_Utls(): atm.temp_factor = Constants.U2B * (numpy.trace(Utls) / 3.0) atm.U = Utls ## save the struct FileIO.SaveStructure(file = opt_dict["-p"], struct = struct)
def set_structure_file(self, struct_file_data): """ Creates job directory, saves structure file to the job directory, sets this job up to run. """ if not os.path.isdir(settings.TLSMD_WORK_DIR): raise TlsmdJobException( 'settings.TLSMD_WORK_DIR=%s directory does not exist' % settings.TLSMD_WORK_DIR) if os.path.exists(self.get_job_dir()): self.remove_job_dir() try: os.mkdir(self.get_job_dir()) except OSError: raise TlsmdJobException('cannot make directory=%s' % self.get_job_dir()) try: open(self.get_structure_path(), 'w').write(struct_file_data) except IOError: raise TlsmdJobException('cannot write file=%s' % self.get_structure_path()) ## load the structure with mmLib, extract data from the structure from mmLib import FileIO try: struct = FileIO.LoadStructure(fil=self.get_structure_path()) except: raise TlsmdJobException( 'The Python Macromolecular Library was unable to load your structure file.' ) ## set self.structure_id if necessary if not self.structure_id and struct.structure_id: self.structure_id = struct.structure_id if not self.structure_id: self.structure_id = 'XXXX' ## Select Chains for Analysis num_atoms = 0 num_aniso_atoms = 0 largest_chain_seen = 0 ## catelog all the chains of the structure chains = [] for chain in struct.iter_chains(): job_chain = TlsmdJobChain(tlsmd_job=self, chain_id=chain.chain_id) job_chain.set_chain(chain) num_atoms += job_chain.num_atoms num_aniso_atoms += job_chain.num_aniso_atoms largest_chain_seen = max(job_chain.length, largest_chain_seen) if num_atoms < 1: self.remove_job() raise TlsmdJobException( 'Your submitted structure contained no atoms') if largest_chain_seen > settings.MAX_CHAIN_LENGTH: self.remove_job() raise TlsmdJobException( 'Your submitted structure contained a chain exceeding the %d residue limit' % settings.MAX_CHAIN_LENGTH) try: aniso_ratio = float(num_aniso_atoms) / float(num_atoms) except ZeroDivisionError: raise TlsmdJobException( 'Your submitted structure contained no atoms') if aniso_ratio > 0.90: self.tls_model = 'ANISO' self.save()
def SetStructureFile(webtlsmdd, job_id, struct_bin): """Creates job directory, saves structure file to the job directory, and sets all jdict defaults. """ if not mysql.job_exists(job_id): return False try: os.chdir(conf.TLSMD_WORK_DIR) except OSError: return "Unable to change to conf.TLSMD_WORK_DIR = '%s'" % ( conf.TLSMD_WORK_DIR) ## NOTE: This is the first place the webserver creates the job directory try: os.mkdir(job_id) except OSError: return "Unable to make job directory %s" % (job_id) job_dir = os.path.join(conf.TLSMD_WORK_DIR, job_id) os.chdir(job_dir) try: os.mkdir("ANALYSIS") except OSError: return "Unable to make ANALYSIS sub-directory for job_id: %s" % ( job_id) ## Copy sanity.png from "All atoms" sanity check (in tlsmdlib/webtlsmd.py) ## to job_dir try: src_png_file = "%s/%s.png" % (conf.WEBTMP_PATH, job_id) dst_png_file = "%s/%s/sanity.png" % (conf.TLSMD_WORK_DIR, job_id) if os.path.exists(src_png_file): shutil.copy(src_png_file, dst_png_file) except OSError: return "Unable to copy sanity.png for job_id: %s" % job_id #mysql.job_set_id(job_id) #mysql.job_set_header_id(job_id, "test") ## DEBUG ## save PDB file pdb_filename = conf.PDB_FILENAME filobj = open(pdb_filename, "w") filobj.write(struct_bin.data) filobj.close() ## Generate summary/thumb 'struct.png' image if conf.THUMBNAIL: misc.render_struct(job_dir) ## Generate 'struct.r3d' for Raster3D if conf.GEN_RAW_GREY: misc.generate_raw_grey_struct(job_dir) ## set basic properties of the job job_url = "%s/%s" % (conf.TLSMD_WORK_URL, job_id) log_url = "%s/log.txt" % (job_url) log_file = "%s/log.txt" % (job_dir) if not os.path.exists(log_file): open(log_file, 'w').close() ## touch log.txt #tarball_url = "%s/%s.tar.gz" % (job_url, job_id) analysis_dir = "%s/ANALYSIS" % (job_dir) analysis_base_url = "%s/ANALYSIS" % (job_url) analysis_url = "%s/ANALYSIS/index.html" % (job_url) ## TODO: Add version to MySQL status_page table #mysql.job_set_version(job_id, const.VERSION) ## submission time and initial state submit_time = time.time() mysql.job_set_state(job_id, "submit1") mysql.job_set_submit_time(job_id, submit_time) ## This is for internal use only tm_struct = time.localtime(submit_time) submit_date = time.strftime("%Y-%m-%d %H:%M:%S", tm_struct) mysql.job_set_submit_date(job_id, submit_date) ## now load the structure and build the submission form try: struct = FileIO.LoadStructure(fil=pdb_filename) except: return "The Python Macromolecular Library was unable to load your structure file." if not struct.structure_id: struct.structure_id = "XXXX" mysql.job_set_structure_id(job_id, struct.structure_id) ## Select Chains for Analysis num_atoms = 0 num_aniso_atoms = 0 largest_chain_seen = 0 chain_descriptions = "" chains = [] for chain in struct.iter_chains(): naa = chain.count_amino_acids() nna = chain.count_nucleic_acids() ota = chain.count_fragments() num_frags = 0 ## minimum number of residues (amino/nucleic) per chain ## TODO: Does this work better? 2009-07-24 if naa > 0: if naa < conf.MIN_AMINO_PER_CHAIN: continue num_frags = naa largest_chain_seen = max(naa, largest_chain_seen) elif nna > 0: if nna < conf.MIN_NUCLEIC_PER_CHAIN: continue num_frags = nna largest_chain_seen = max(nna, largest_chain_seen) elif naa == 0 and nna == 0: ## The chain has neither amino or nucleic acid atoms, so assign ## num_frags = ota -> "other atom" types num_frags = ota ## this chain has nucleic acids in it, so generate r3d file for ## just the sugars misc.generate_bases_r3d(job_dir, chain.chain_id) misc.generate_sugars_r3d(job_dir, chain.chain_id) ## TODO: Allow for MIN_NUCLEIC_PER_CHAIN and MIN_AMINO_PER_CHAIN diffs, 2009-07-19 ## TODO: Record ignored chains (because too small) in logfile, 2009-07-19 #if num_frags < conf.MIN_RESIDUES_PER_CHAIN: #if naa < conf.MIN_AMINO_PER_CHAIN or nna < conf.MIN_NUCLEIC_PER_CHAIN: #if (naa > 0 and naa < conf.MIN_AMINO_PER_CHAIN) or\ # (nna > 0 and nna < conf.MIN_NUCLEIC_PER_CHAIN): # #log_file = open(log_file, 'w+') # #log_file.write("Ignoring chain %s; too small" % chain.chain_id) # #log_file.close() # continue ## create chain description labels ## E.g., chains_descriptions = "A:10:0:aa;B:20:1:na;C:30:0:na;" chain_descriptions = chain_descriptions + chain.chain_id + ":" if naa > 0: chain_descriptions = chain_descriptions + str(num_frags) + ":1:aa;" elif nna > 0: chain_descriptions = chain_descriptions + str(num_frags) + ":1:na;" else: chain_descriptions = chain_descriptions + str(num_frags) + ":0:ot;" for atm in chain.iter_all_atoms(): num_atoms += 1 if atm.U is not None: num_aniso_atoms += 1 if num_atoms < 1: webtlsmdd.remove_job(job_id) return 'Your submitted structure contained no atoms' if largest_chain_seen > conf.LARGEST_CHAIN_ALLOWED: webtlsmdd.remove_job(job_id) return 'Your submitted structure contained a chain exceeding the %s residue limit' % ( conf.LARGEST_CHAIN_ALLOWED) mysql.job_set_chain_sizes(job_id, chain_descriptions) ## set defaults mysql.job_set_user_name(job_id, "") mysql.job_set_email(job_id, "") mysql.job_set_user_comment(job_id, conf.globalconf.user_comment) mysql.job_set_plot_format(job_id, "PNG") mysql.job_set_nparts(job_id, conf.globalconf.nparts) mysql.job_set_via_pdb(job_id, "0") mysql.job_set_private_job(job_id, "0") mysql.job_set_jmol_view(job_id, "0") mysql.job_set_jmol_animate(job_id, "0") mysql.job_set_histogram(job_id, "0") mysql.job_set_cross_chain_analysis(job_id, "0") if conf.PRIVATE_JOBS: mysql.job_set_private_job(job_id, "1") if conf.globalconf.generate_jmol_view: mysql.job_set_jmol_view(job_id, "1") if conf.globalconf.generate_jmol_animate: mysql.job_set_jmol_animate(job_id, "1") if conf.globalconf.generate_histogram: mysql.job_set_histogram(job_id, "1") if conf.globalconf.cross_chain_analysis: mysql.job_set_cross_chain_analysis(job_id, "1") try: aniso_ratio = float(num_aniso_atoms) / float(num_atoms) except ZeroDivisionError: return 'Your submitted structure contained no atoms' if aniso_ratio > conf.ANISO_RATIO: mysql.job_set_tls_model(job_id, "ANISO") else: mysql.job_set_tls_model(job_id, "ISOT") mysql.job_set_weight_model(job_id, "NONE") mysql.job_set_include_atoms(job_id, "ALL") return ""
def refmac5_prep(xyzin, tlsin_list, xyzout, tlsout): """Use TLS model + Uiso for each atom. Output xyzout with the residual Uiso only. """ ## load structure struct = FileIO.LoadStructure(fil = xyzin) ## load and construct TLS groups tls_group_list = [] tls_file = TLS.TLSFile() tls_file.set_file_format(TLS.TLSFileFormatTLSOUT()) tls_file_format = TLS.TLSFileFormatTLSOUT() for tlsin in tlsin_list: tls_desc_list = tls_file_format.load(open(tlsin, "r")) for tls_desc in tls_desc_list: tls_file.tls_desc_list.append(tls_desc) tls_group = tls_desc.construct_tls_group_with_atoms(struct) tls_group.tls_desc = tls_desc tls_group_list.append(tls_group) ## set the extra Uiso for each atom for tls_group in tls_group_list: ## minimal/maximal amount of Uiso which has to be added ## to the group's atoms to to make Uiso == Uiso_tls min_Uiso = 0.0 max_Uiso = 0.0 for atm, Utls in tls_group.iter_atm_Utls(): tls_tf = numpy.trace(Utls) / 3.0 ref_tf = numpy.trace(atm.get_U()) / 3.0 if ref_tf > tls_tf: max_Uiso = max(ref_tf - tls_tf, max_Uiso) else: min_Uiso = max(tls_tf - ref_tf, min_Uiso) ## reduce the TLS group T tensor by min_Uiso so that ## a PDB file can be written out where all atoms ## Uiso == Uiso_tls ## we must rotate the T tensor to its primary axes before ## subtracting min_Uiso magnitude from it (T_eval, TR) = numpy.linalg.eig(tls_group.T) T = numpy.dot(TR, numpy.dot(tls_group.T, numpy.transpose(TR))) assert numpy.allclose(T[0,1], 0.0) assert numpy.allclose(T[0,2], 0.0) assert numpy.allclose(T[1,2], 0.0) T[0,0] = T[0,0] - min_Uiso T[1,1] = T[1,1] - min_Uiso T[2,2] = T[2,2] - min_Uiso ## now take some of the smallest principal component of T and ## move it into the individual atomic temperature factors min_T = min(T[0,0], min(T[1,1], T[2,2])) sub_T = min_T * 0.50 add_Uiso = min_T - sub_T T[0,0] = T[0,0] - sub_T T[1,1] = T[1,1] - sub_T T[2,2] = T[2,2] - sub_T ## rotate T back to original orientation tls_group.T = numpy.dot( numpy.transpose(TR), numpy.dot(T, TR)) ## reset the TLS tensor values in the TLSDesc object so they can be saved tls_group.tls_desc.set_tls_group(tls_group) ## set atm.temp_factor for atm, Utls in tls_group.iter_atm_Utls(): tls_tf = numpy.trace(Utls) / 3.0 ref_tf = numpy.trace(atm.get_U()) / 3.0 if ref_tf > tls_tf: atm.temp_factor = ((add_Uiso) + ref_tf - tls_tf)*Constants.U2B atm.U = None else: atm.temp_factor = (add_Uiso) * Constants.U2B atm.U = None FileIO.SaveStructure(fil = xyzout, struct = struct) tls_file.save(open(tlsout, "w"))
def main(path): print "mmLib.LoadStructure(fil=%s)" % (path) struct = FileIO.LoadStructure(fil=path)
print "AUTHORS" print " Jay Painter <*****@*****.**>" if __name__ == '__main__': if "-h" in sys.argv or "--help" in sys.argv: usage() sys.exit(0) cif = "-" pdb = "-" if len(sys.argv) == 1: pass elif len(sys.argv) == 2: cif = sys.argv[1] elif len(sys.argv) == 3: cif = sys.argv[1] pdb = sys.argv[2] else: usage() sys.exit(1) if cif == "-": cif = sys.stdin if pdb == "-": pdb = sys.stdout struct = FileIO.LoadStructure(file=cif, format="CIF") FileIO.SaveStructure(file=pdb, structure=struct, format="PDB")
def SetStructureFile(webtlsmdd, job_id, struct_bin): """Creates job directory, saves structure file to the job directory, and sets all jdict defaults. """ if not webtlsmdd.job_exists(job_id): return False try: os.chdir(settings.TLSMD_WORK_DIR) except OSError: return "Unable to change to settings.TLSMD_WORK_DIR = '%s'" % ( settings.TLSMD_WORK_DIR) try: os.mkdir(job_id) except OSError: return "Unable to make job directory %s" % (job_id) job_dir = os.path.join(settings.TLSMD_WORK_DIR, job_id) os.chdir(job_dir) webtlsmdd.jobdb.job_data_set(job_id, "job_dir", job_dir) ## save PDB file pdb_filename = "struct.pdb" webtlsmdd.jobdb.job_data_set(job_id, "pdb_filename", pdb_filename) filobj = open(pdb_filename, "w") filobj.write(struct_bin.data) filobj.close() ## set basic properties of the job job_url = "%s/%s" % (settings.TLSMD_WORK_URL, job_id) webtlsmdd.jobdb.job_data_set(job_id, "job_url", job_url) log_url = "%s/log.txt" % (job_url) webtlsmdd.jobdb.job_data_set(job_id, "log_url", log_url) analysis_dir = "%s/ANALYSIS" % (job_dir) webtlsmdd.jobdb.job_data_set(job_id, "analysis_dir", analysis_dir) analysis_base_url = "%s/ANALYSIS" % (job_url) webtlsmdd.jobdb.job_data_set(job_id, "analysis_base_url", analysis_base_url) analysis_url = "%s/ANALYSIS/index.html" % (job_url) webtlsmdd.jobdb.job_data_set(job_id, "analysis_url", analysis_url) webtlsmdd.jobdb.job_data_set(job_id, "version", settings.VERSION) ## submission time and initial state webtlsmdd.jobdb.job_data_set(job_id, "state", "submit1") webtlsmdd.jobdb.job_data_set(job_id, "submit_time", time.time()) ## now load the structure and build the submission form try: struct = FileIO.LoadStructure(fil=pdb_filename) except: return "The Python Macromolecular Library was unable to load your structure file." if not struct.structure_id: struct.structure_id = "XXXX" webtlsmdd.jobdb.job_data_set(job_id, "structure_id", struct.structure_id) ## Select Chains for Analysis num_atoms = 0 num_aniso_atoms = 0 largest_chain_seen = 0 chains = [] for chain in struct.iter_chains(): naa = chain.count_amino_acids() nna = chain.count_nucleic_acids() num_frags = 0 if naa > 0: num_frags = naa elif nna > 0: num_frags = nna if num_frags < 10: continue largest_chain_seen = max(num_frags, largest_chain_seen) ## form name cb_name = 'CHAIN%s' % (chain.chain_id) ## create chain description label cb_desc if naa > 0: cb_desc = 'Chain %s (%d Amino Acid Residues)' % (chain.chain_id, num_frags) elif nna > 0: cb_desc = 'Chain %s (%d Nucleic Acid Residues)' % (chain.chain_id, num_frags) else: continue for atm in chain.iter_all_atoms(): num_atoms += 1 if atm.U is not None: num_aniso_atoms += 1 listx = [] i = 0 for frag in chain.iter_fragments(): i += 1 if i > 5: break listx.append(frag.res_name) cb_preview = string.join(listx, " ") cdict = {} chains.append(cdict) cdict["chain_id"] = chain.chain_id cdict["length"] = num_frags cdict["name"] = cb_name cdict["desc"] = cb_desc cdict["preview"] = cb_preview cdict["selected"] = True if num_atoms < 1: webtlsmdd.remove_job(job_id) return 'Your submitted structure contained no atoms' if largest_chain_seen > 1700: webtlsmdd.remove_job(job_id) return 'Your submitted structure contained a chain exceeding the 1700 residue limit' webtlsmdd.jobdb.job_data_set(job_id, "chains", chains) ## defaults webtlsmdd.jobdb.job_data_set(job_id, "user", "") webtlsmdd.jobdb.job_data_set(job_id, "passwd", "") webtlsmdd.jobdb.job_data_set(job_id, "email", "") webtlsmdd.jobdb.job_data_set(job_id, "comment", "") webtlsmdd.jobdb.job_data_set(job_id, "private_job", False) webtlsmdd.jobdb.job_data_set(job_id, "plot_format", "PNG") try: aniso_ratio = float(num_aniso_atoms) / float(num_atoms) except ZeroDivisionError: return 'Your submitted structure contained no atoms' if aniso_ratio > 0.90: webtlsmdd.jobdb.job_data_set(job_id, "tls_model", "ANISO") else: webtlsmdd.jobdb.job_data_set(job_id, "tls_model", "ISOT") webtlsmdd.jobdb.job_data_set(job_id, "weight", "") webtlsmdd.jobdb.job_data_set(job_id, "include_atoms", "ALL") return ""
def main(path, opt_dict): struct = FileIO.LoadStructure(file=path) tls_group_list = [] ## make the TLS groups if opt_dict.has_key("-t"): try: fil = open(opt_dict["-t"], "r") except IOError: print "[ERROR]: TLSIN File not found %s" % (opt_dict["-t"]) sys.exit(-1) tls_file = TLS.TLSFile() tls_file.set_file_format(TLS.TLSFileFormatTLSOUT()) tls_file.load(fil) for tls_desc in tls_file.tls_desc_list: tls = tls_desc.construct_tls_group_with_atoms(struct) tls.tls_desc = tls_desc tls_group_list.append(tls) else: ## create one TLS group per chain by default for chain in struct.iter_chains(): if chain.count_amino_acids() < 10: continue try: chain_id1 = chain.chain_id frag_id1 = chain[0].fragment_id frag_id2 = chain[-1].fragment_id except IndexError: continue tls_desc = TLS.TLSGroupDesc() tls_desc.add_range(chain_id1, frag_id1, chain_id1, frag_id2, "ALL") tls = tls_desc.construct_tls_group_with_atoms(struct) tls_group_list.append(tls) tls.tls_desc = tls_desc print "Creating TLS Group: %s" % (tls.name) ## fit TLS groups and write output tls_file = TLS.TLSFile() tls_file.set_file_format(TLS.TLSFileFormatTLSOUT()) ## preform a LSQ fit if necessary for tls in tls_group_list: print "[TLS GROUP %s]" % (tls.name) ## if the TLS group is null, then perform a LSQ-TLS fit if tls.is_null(): print "Null Group: Running TLS-LSQ" if len(tls) < 20: print "ERROR: Not Enough Atoms in TLS Group." continue tls.origin = tls.calc_centroid() lsq_residual = tls.calc_TLS_least_squares_fit() tls.shift_COR() tls.tls_desc.set_tls_group(tls) tls_file.tls_desc_list.append(tls.tls_desc) if opt_dict.has_key("-o"): print "Saving TLSIN: %s" % (opt_dict["-o"]) tls_file.save(open(opt_dict["-o"], "w")) ## write out a PDB file with 0.0 tempature factors for all ## atoms in TLS groups if opt_dict.has_key("-p"): for tls in tls_group_list: for atm, Utls in tls.iter_atm_Utls(): if opt_dict.has_key("-s"): atm.temp_factor = 0.0 atm.U = None else: atm.temp_factor = Constants.U2B * numpy.trace(Utls) / 3.0 atm.U = Utls ## save the struct print "Saving XYZIN: %s" % (opt_dict["-p"]) FileIO.SaveStructure(file=opt_dict["-p"], struct=struct)
print "AUTHORS" print " Jay Painter <*****@*****.**>" if __name__ == '__main__': if "-h" in sys.argv or "--help" in sys.argv: usage() sys.exit(0) infil = "-" outfil = "-" if len(sys.argv) == 1: pass elif len(sys.argv) == 2: infil = sys.argv[1] elif len(sys.argv) == 3: infil = sys.argv[1] outfil = sys.argv[2] else: usage() sys.exit(1) if infil == "-": infil = sys.stdin if outfil == "-": outfil = sys.stdout struct = FileIO.LoadStructure(file=infil, format="PDB") FileIO.SaveStructure(file=outfil, structure=struct, format="CIF")