[Biopython] Best way to change the chain identifiers of a set of residues

Claudia Millán Nebot cmncri at ibmb.csic.es
Wed Nov 25 20:16:32 UTC 2015


Dear all,

I am writing a function that examines a structure, and if there are
discontinuous regions that are smaller than a certain size, they will be
removed from the structure. Then, I would like to write the structure as a
pdb in which the chain identifiers are different for each discontinuous
fragment. For that purpose, I want to change the chain id of certain
residues. ¿What will be the best way to do it? Because right now it is not
working, of course, because I am iterating over something that I am trying
to change at the same time. Maybe I am missing something very obvious or
straightforward, but I do not see what will be the best way to do it...
¿Maybe creating and empty chain and using the set_parent method?

The current code looks like this:
def trimByContinuityLimit(pdb_file,min_size):
    parser=PDBParser()
    structure=parser.get_structure(pdb_file[:-4],pdb_file)
    residues=Selection.unfold_entities(structure,'R')
    list_id="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"
    dictio_chainid={}
    residues_to_remove=[]
    current_listres=[]
    index=0
    for i in range(len(residues)-1):
        res1=residues[i]
        res2=residues[i+1]
        id1=res1.id
        id2=res2.id
        check=Bioinformatics.checkContinuity(res1,res2)
        #print 'check',check
        #print 'list_id[index]',list_id[index]
        if check==True:
            #print "These two residues are consecutive",res1,res2
            if id1 not in current_listres:
                current_listres.append(id1)
            dictio_chainid[id1]=list_id[index]
            if id2 not in current_listres:
                current_listres.append(id2)
            dictio_chainid[id2]=list_id[index]
            #print 'list_id[index]',list_id[index]
            #print 'id1,dictio_chainid[id1]',dictio_chainid[id1],id1
            #print 'id2,dictio_chainid[id2]',dictio_chainid[id2],id2
        elif check==False:
            #print "These two residues are not consecutive",res1,res2
            if id1 not in current_listres:
               current_listres.append(id1)
               dictio_chainid[id1]=list_id[index]
            if len(current_listres)<min_size:
               residues_to_remove.extend(current_listres)
            if i==len(residues)-2 and min_size>1: # If we reach this point,
then the last residue is not continuous so it is single :
               residues_to_remove.append(id2)
            else:
               current_listres=[]
               current_listres.append(id2)
               index=index+1
               dictio_chainid[id2]=list_id[index]
    # Remove the residues and write the pdb
    for model in structure:
       for chain in model:
           for residue in chain:
               id_res=residue.id
               if id_res in residues_to_remove:
                   chain.detach_child(id_res)
               else:
                   chain.id=dictio_chainid[id_res]
    io=PDBIO()
    io.set_structure(structure)
    io.save(pdb_file[:-4]+'_trimmed.pdb',write_end=False)

Thanks in advance :)

Claudia
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.open-bio.org/pipermail/biopython/attachments/20151125/ea5a87de/attachment.html>


More information about the Biopython mailing list