示例#1
0
    def write_tlsout_file(self, chain, cpartition):
        """Writes the TLSOUT file for the segmentation.
        """
        basename = "%s_CHAIN%s_NTLS%d" % (self.struct_id, chain.chain_id,
                                          cpartition.num_tls_segments())
        tlsout_path = "%s.tlsout" % (basename)

        struct_id = self.struct_id
        chain_id = chain.chain_id

        tls_file = TLS.TLSFile()
        tls_file.set_file_format(TLS.TLSFileFormatTLSOUT())

        for tls in cpartition.iter_tls_segments():
            ## don't write out bypass edges
            if tls.method != "TLS":
                continue

            tls_desc = TLS.TLSGroupDesc()
            tls_file.tls_desc_list.append(tls_desc)
            tls_desc.set_tls_group(tls.tls_group)
            for frag_id1, frag_id2 in tls.iter_segment_ranges():
                tls_desc.add_range(chain_id, frag_id1, chain_id, frag_id2,
                                   "ALL")

        tls_file.save(open(tlsout_path, "w"))

        return tlsout_path
示例#2
0
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:
示例#3
0
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
示例#4
0
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"))
示例#5
0
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"))
示例#6
0
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)
示例#7
0
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)
示例#8
0
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)
示例#9
0
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"))