<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">You can pass a quick class to the IO method that takes residue objects:<div class=""><br class=""></div><div class=""><div class=""><font face="Courier" class="">def trimByContinuityLimit(pdb_file,min_size):</font></div><div class=""><font face="Courier" class=""> parser=PDBParser()</font></div><div class=""><font face="Courier" class=""> structure=parser.get_structure(pdb_file[:-4],pdb_file)</font></div><div class=""><font face="Courier" class=""> residues=Selection.unfold_entities(structure,'R')</font></div><div class=""><font face="Courier" class=""> list_id="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"</font></div><div class=""><font face="Courier" class=""> dictio_chainid={}</font></div><div class=""><font face="Courier" class=""> residues_to_remove=[]</font></div><div class=""><font face="Courier" class=""> current_listres=[]</font></div><div class=""><font face="Courier" class=""> index=0</font></div><div class=""><font face="Courier" class=""> for i in range(len(residues)-1):</font></div><div class=""><font face="Courier" class=""> res1=residues[i]</font></div><div class=""><font face="Courier" class=""> res2=residues[i+1]</font></div><div class=""><font face="Courier" class=""> id1=res1.id</font></div><div class=""><font face="Courier" class=""> id2=res2.id</font></div><div class=""><font face="Courier" class=""> check=Bioinformatics.checkContinuity(res1,res2)</font></div><div class=""><font face="Courier" class=""> #print 'check',check</font></div><div class=""><font face="Courier" class=""> #print 'list_id[index]',list_id[index]</font></div><div class=""><font face="Courier" class=""> if check==True:</font></div><div class=""><font face="Courier" class=""> #print "These two residues are consecutive",res1,res2</font></div><div class=""><font face="Courier" class=""> if id1 not in current_listres:</font></div><div class=""><font face="Courier" class=""> current_listres.append(id1)</font></div><div class=""><font face="Courier" class=""> dictio_chainid[id1]=list_id[index]</font></div><div class=""><font face="Courier" class=""> if id2 not in current_listres:</font></div><div class=""><font face="Courier" class=""> current_listres.append(id2)</font></div><div class=""><font face="Courier" class=""> dictio_chainid[id2]=list_id[index]</font></div><div class=""><font face="Courier" class=""> #print 'list_id[index]',list_id[index]</font></div><div class=""><font face="Courier" class=""> #print 'id1,dictio_chainid[id1]',dictio_chainid[id1],id1</font></div><div class=""><font face="Courier" class=""> #print 'id2,dictio_chainid[id2]',dictio_chainid[id2],id2</font></div><div class=""><font face="Courier" class=""> elif check==False:</font></div><div class=""><font face="Courier" class=""> #print "These two residues are not consecutive",res1,res2</font></div><div class=""><font face="Courier" class=""> if id1 not in current_listres:</font></div><div class=""><font face="Courier" class=""> current_listres.append(id1)</font></div><div class=""><font face="Courier" class=""> dictio_chainid[id1]=list_id[index]</font></div><div class=""><font face="Courier" class=""> if len(current_listres)<min_size:</font></div><div class=""><font face="Courier" class=""> residues_to_remove.extend(current_listres)</font></div><div class=""><font face="Courier" class=""> 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 :</font></div><div class=""><font face="Courier" class=""> residues_to_remove.append(id2)</font></div><div class=""><font face="Courier" class=""> else:</font></div><div class=""><font face="Courier" class=""> current_listres=[]</font></div><div class=""><font face="Courier" class=""> current_listres.append(id2)</font></div><div class=""><font face="Courier" class=""> index=index+1</font></div><div class=""><font face="Courier" class=""> dictio_chainid[id2]=list_id[index]</font></div><div class=""><font face="Courier" class=""><span class="Apple-tab-span" style="white-space:pre"> </span>class MyAccept(Select):</font></div><div class=""><font face="Courier" class=""><span class="Apple-tab-span" style="white-space:pre"> </span>def accept_residue(self, residue):</font></div><div class=""><font face="Courier" class=""><span class="Apple-tab-span" style="white-space:pre"> </span>if residue.id not in residues_to_remove:</font></div><div class=""><font face="Courier" class=""> return 1</font></div><div class=""><font face="Courier" class=""> else:</font></div><div class=""><font face="Courier" class=""> return 0</font></div><div class=""><font face="Courier" class=""><br class=""></font></div><div class=""><font face="Courier" class=""> io=PDBIO()</font></div><div class=""><font face="Courier" class=""> io.set_structure(structure)</font></div><div class=""><font face="Courier" class=""> io.save(pdb_file[:-4]+'_trimmed.pdb',MyAccept())</font></div></div><div class=""><pre class="python" style="padding: 1em; border: 1px dashed rgb(47, 111, 171); line-height: 1.3em; widows: 1; background-color: rgb(249, 249, 249);"><br class=""></pre><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Nov 25, 2015, at 12:16 PM, Claudia Millán Nebot <<a href="mailto:cmncri@ibmb.csic.es" class="">cmncri@ibmb.csic.es</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class=""><div class=""><div class="">Dear all,<br class=""><br class=""></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 class=""><br class="">The current code looks like this:<br class="">def trimByContinuityLimit(pdb_file,min_size):<br class=""> parser=PDBParser()<br class=""> structure=parser.get_structure(pdb_file[:-4],pdb_file)<br class=""> residues=Selection.unfold_entities(structure,'R')<br class=""> list_id="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"<br class=""> dictio_chainid={}<br class=""> residues_to_remove=[]<br class=""> current_listres=[]<br class=""> index=0<br class=""> for i in range(len(residues)-1):<br class=""> res1=residues[i]<br class=""> res2=residues[i+1]<br class=""> id1=<a href="http://res1.id/" class="">res1.id</a><br class=""> id2=<a href="http://res2.id/" class="">res2.id</a><br class=""> check=Bioinformatics.checkContinuity(res1,res2)<br class=""> #print 'check',check<br class=""> #print 'list_id[index]',list_id[index]<br class=""> if check==True:<br class=""> #print "These two residues are consecutive",res1,res2<br class=""> if id1 not in current_listres:<br class=""> current_listres.append(id1)<br class=""> dictio_chainid[id1]=list_id[index]<br class=""> if id2 not in current_listres:<br class=""> current_listres.append(id2)<br class=""> dictio_chainid[id2]=list_id[index]<br class=""> #print 'list_id[index]',list_id[index]<br class=""> #print 'id1,dictio_chainid[id1]',dictio_chainid[id1],id1<br class=""> #print 'id2,dictio_chainid[id2]',dictio_chainid[id2],id2<br class=""> elif check==False:<br class=""> #print "These two residues are not consecutive",res1,res2<br class=""> if id1 not in current_listres:<br class=""> current_listres.append(id1)<br class=""> dictio_chainid[id1]=list_id[index]<br class=""> if len(current_listres)<min_size:<br class=""> residues_to_remove.extend(current_listres)<br class=""> 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 class=""> residues_to_remove.append(id2)<br class=""> else:<br class=""> current_listres=[]<br class=""> current_listres.append(id2)<br class=""> index=index+1<br class=""> dictio_chainid[id2]=list_id[index]<br class=""><br class=""> io=PDBIO()<br class=""> io.set_structure(structure)<br class=""> io.save(pdb_file[:-4]+'_trimmed.pdb',write_end=False)<br class=""><br class=""></div>Thanks in advance :)<br class=""><br class=""></div>Claudia<br class=""></div>
_______________________________________________<br class="">Biopython mailing list - <a href="mailto:Biopython@mailman.open-bio.org" class="">Biopython@mailman.open-bio.org</a><br class=""><a href="http://mailman.open-bio.org/mailman/listinfo/biopython" class="">http://mailman.open-bio.org/mailman/listinfo/biopython</a></div></blockquote></div><br class=""></div></div></body></html>