<div dir="ltr"><div><div><div>Dear all,<br><br></div>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?<br><br>The current code looks like this:<br>def trimByContinuityLimit(pdb_file,min_size):<br>    parser=PDBParser()<br>    structure=parser.get_structure(pdb_file[:-4],pdb_file)<br>    residues=Selection.unfold_entities(structure,'R')<br>    list_id="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"<br>    dictio_chainid={}<br>    residues_to_remove=[]<br>    current_listres=[]<br>    index=0<br>    for i in range(len(residues)-1):<br>        res1=residues[i]<br>        res2=residues[i+1]<br>        id1=<a href="http://res1.id">res1.id</a><br>        id2=<a href="http://res2.id">res2.id</a><br>        check=Bioinformatics.checkContinuity(res1,res2)<br>        #print 'check',check<br>        #print 'list_id[index]',list_id[index]<br>        if check==True:<br>            #print "These two residues are consecutive",res1,res2<br>            if id1 not in current_listres:<br>                current_listres.append(id1)<br>            dictio_chainid[id1]=list_id[index]<br>            if id2 not in current_listres:<br>                current_listres.append(id2)<br>            dictio_chainid[id2]=list_id[index]<br>            #print 'list_id[index]',list_id[index]<br>            #print 'id1,dictio_chainid[id1]',dictio_chainid[id1],id1<br>            #print 'id2,dictio_chainid[id2]',dictio_chainid[id2],id2<br>        elif check==False:<br>            #print "These two residues are not consecutive",res1,res2<br>            if id1 not in current_listres:<br>               current_listres.append(id1)<br>               dictio_chainid[id1]=list_id[index]<br>            if len(current_listres)<min_size:<br>               residues_to_remove.extend(current_listres)<br>            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 :<br>               residues_to_remove.append(id2)<br>            else:<br>               current_listres=[]<br>               current_listres.append(id2)<br>               index=index+1<br>               dictio_chainid[id2]=list_id[index]<br>    # Remove the residues and write the pdb<br>    for model in structure:<br>       for chain in model:<br>           for residue in chain:<br>               id_res=<a href="http://residue.id">residue.id</a><br>               if id_res in residues_to_remove:<br>                   chain.detach_child(id_res)<br>               else:<br>                   <a href="http://chain.id">chain.id</a>=dictio_chainid[id_res]<br>    io=PDBIO()<br>    io.set_structure(structure)<br>    io.save(pdb_file[:-4]+'_trimmed.pdb',write_end=False)<br><br></div>Thanks in advance :)<br><br></div>Claudia<br></div>