text: user9740: I'm trying to programmatically replace a set of amino acid residues on an MSA with a "Z" from a list of unaligned positions. Any ideas on how I could do this?
  Input: a list of unaligned positions for each sequence + an MSA
  Output: an MSA with those unaligned positions on the aligned MSA replaced with a Z for each sequence.
  I have a multiple sequence alignment i'm working with and a list of sites on that alignment that are glycosylated. I was trying to run a script that would take the input of the positions as a list of characters along with the MSA to replace the Nth character with a "Z" to easily visualize the sites. So essentially if I feed it the following sequence
  M----KFLAFLCLLGFANAQ-------------D-GKCG----TLSNKSPS--------------------K
  
  and positions
  2, 5, 10, 11, 14
  
  it should spit back
  M----ZFLZFLCLZZFAZAQ-------------D-GKCG----TLSNKSPS--------------------K
  
  
  user3848: The code is
  import re
  
  pos = [2, 5, 10, 11, 14]
  newseq = []
  prot = 'M----KFLAFLCLLGFANAQ-------------D-GKCG----TLSNKSPS--------------------K'
  count = 0
  for n,s in enumerate(prot):    
      if str(s) == '-':
          count += 1
          newseq.append('-')
      else:
          if (n + 1 - count) in pos:
              s = re.sub(r'[A-Za-z]', 'Z', s)
          newseq.append(s)
  print(''.join(newseq))
  
  output
  
  M----ZFLZFLCLZZFAZAQ-------------D-GKCG----TLSNKSPS--------------------K
  
  Precisely as requested, apologies for the delay.
  Happy to explain why it works - its just a couple of Python tricks and a method. It must be run under Python 3 (unlikely to work in Python 2) and probably >3.6.
  BTW I normally avoid re for complicated reasons but its easy, hence its here.
  
  As @gaspanic has pointed out re is not only unnecessary it will slow the code (re is very inefficient). It is in fact one of the 'legendary inefficiencies' of Python. Thus removing it ...
  # variable declarations omitted
  newseq = list()
  count = 0
  for n,s in enumerate(prot):    
      if str(s) == '-':
          count += 1
          newseq.append('-')
      else:
          if (n + 1 - count) in pos:
              s = 'Z'
          newseq.append(s)
  print(''.join(newseq))
  
  Same output.
  
  user11919: Another alternative, without the need for re:
  seq = 'M----KFLAFLCLLGFANAQ-------------D-GKCG----TLSNKSPS--------------------K'
  pos = [2, 5, 10, 11, 14]
  
  new_seq = []
  count = 0
  for s in seq:
      if s == "-":
          new_seq.append(s)
      else:
          count += 1
          if count in pos:
              new_seq.append('Z')
          else:
              new_seq.append(s)
  
  print(''.join(new_seq))
  
  Result:
  M----ZFLZFLCLZZFAZAQ-------------D-GKCG----TLSNKSPS--------------------K
  
  
original_text: 9740: <p>I'm trying to programmatically replace a set of amino acid residues on an MSA with a &quot;Z&quot; from a list of unaligned positions. Any ideas on how I could do this?</p>
  <p>Input: a list of unaligned positions for each sequence + an MSA<br />
  Output: an MSA with those unaligned positions on the aligned MSA replaced with a Z for each sequence.</p>
  <p>I have a multiple sequence alignment i'm working with and a list of sites on that alignment that are glycosylated. I was trying to run a script that would take the input of the positions as a list of characters along with the MSA to replace the Nth character with a &quot;Z&quot; to easily visualize the sites. So essentially if I feed it the following sequence</p>
  <pre><code>M----KFLAFLCLLGFANAQ-------------D-GKCG----TLSNKSPS--------------------K
  </code></pre>
  <p>and positions</p>
  <pre><code>2, 5, 10, 11, 14
  </code></pre>
  <p>it should spit back</p>
  <pre><code>M----ZFLZFLCLZZFAZAQ-------------D-GKCG----TLSNKSPS--------------------K
  </code></pre>
   3848: <p>The code is</p>
  <pre><code>import re
  
  pos = [2, 5, 10, 11, 14]
  newseq = []
  prot = 'M----KFLAFLCLLGFANAQ-------------D-GKCG----TLSNKSPS--------------------K'
  count = 0
  for n,s in enumerate(prot):    
      if str(s) == '-':
          count += 1
          newseq.append('-')
      else:
          if (n + 1 - count) in pos:
              s = re.sub(r'[A-Za-z]', 'Z', s)
          newseq.append(s)
  print(''.join(newseq))
  </code></pre>
  <p>output</p>
  <blockquote>
  <p>M----ZFLZFLCLZZFAZAQ-------------D-GKCG----TLSNKSPS--------------------K</p>
  </blockquote>
  <p>Precisely as requested, apologies for the delay.</p>
  <p>Happy to explain why it works - its just a couple of Python tricks and a method. It must be run under Python 3 (unlikely to work in Python 2) and probably &gt;3.6.</p>
  <p>BTW I normally avoid <code>re</code> for complicated reasons but its easy, hence its here.</p>
  <hr />
  <p>As @gaspanic has pointed out <code>re</code> is not only unnecessary it will slow the code (<code>re</code> is very inefficient). It is in fact one of the 'legendary inefficiencies' of Python. Thus removing it ...</p>
  <pre><code># variable declarations omitted
  newseq = list()
  count = 0
  for n,s in enumerate(prot):    
      if str(s) == '-':
          count += 1
          newseq.append('-')
      else:
          if (n + 1 - count) in pos:
              s = 'Z'
          newseq.append(s)
  print(''.join(newseq))
  </code></pre>
  <p>Same output.</p>
   11919: <p>Another alternative, without the need for <code>re</code>:</p>
  <pre><code>seq = 'M----KFLAFLCLLGFANAQ-------------D-GKCG----TLSNKSPS--------------------K'
  pos = [2, 5, 10, 11, 14]
  
  new_seq = []
  count = 0
  for s in seq:
      if s == &quot;-&quot;:
          new_seq.append(s)
      else:
          count += 1
          if count in pos:
              new_seq.append('Z')
          else:
              new_seq.append(s)
  
  print(''.join(new_seq))
  </code></pre>
  <p>Result:</p>
  <pre><code>M----ZFLZFLCLZZFAZAQ-------------D-GKCG----TLSNKSPS--------------------K
  </code></pre>
  