def replace_seqres(self, pdb, update_atoms = True):
    """Replace SEQRES lines with a new sequence, optionally removing 
    mutated sidechains"""
  
    newpdb = PDB()
    inserted_seqres = False
    entries_before_seqres = set(["HEADER", "OBSLTE", "TITLE",  "CAVEAT", "COMPND", "SOURCE", 
                                 "KEYWDS", "EXPDTA", "AUTHOR", "REVDAT", "SPRSDE", "JRNL", 
                                 "REMARK", "DBREF",  "SEQADV"])
  
    mutated_resids = {}
    
    if update_atoms:
      old_seqs = ChainSequences()
      chainresnums = old_seqs.parse_atoms(pdb)
    
      assert self.keys() == old_seqs.keys()
      
      for chain in self.keys():
        assert len(self[chain]) == len(old_seqs[chain])
        for i in xrange(len(self[chain])):
          if self[chain][i] != old_seqs[chain][i]:
            resid = chain + chainresnums[chain][i]
            mutated_resids[resid] = self[chain][i]
    
    for line in pdb.lines:
    
      entry = line[0:6]
      if (not inserted_seqres) and entry not in entries_before_seqres:
        inserted_seqres = True
        newpdb.lines += self.seqres_lines()
      
      if update_atoms and entry == "ATOM  ":
        resid = line[21:27]
        atom = line[12:16].strip()
        if not mutated_resids.has_key(resid):
          newpdb.lines += [line]
        else:
          newpdb.lines += [line[:17] + mutated_resids[resid] + line[20:]]
      elif entry != "SEQRES":
        newpdb.lines += [line]
    
    if update_atoms:
      newpdb.remove_nonbackbone_atoms(mutated_resids.keys())
    
    return newpdb
Replace SEQRES lines with a new sequence, optionally removing 
    mutated sidechains