code: def sequence_weights(aln, scaling='none', gap_chars='-.'):
      expectk = [0.0, 1.0, 1.953, 2.861, 3.705, 4.524, 5.304, 6.026, 6.724, 7.397,
                 8.04, 8.622, 9.191, 9.739, 10.264, 10.758, 11.194, 11.635,
                 12.049, 12.468, 12.806, 13.185, 13.539, 13.863, 14.177, 14.466,
                 14.737, 15.005, 15.245, 15.491, 15.681, 15.916, 16.12, 16.301,
                 16.485, 16.671, 16.831, 16.979, 17.151, 17.315, 17.427, 17.559,
                 17.68, 17.791, 17.914, 18.009, 18.113, 18.203, 18.298, 18.391,
                 18.46, 18.547, 18.617, 18.669, 18.77, 18.806, 18.858, 18.934,
                 18.978, 19.027, 19.085, 19.119, 19.169, 19.202, 19.256, 19.291,
                 19.311, 19.357, 19.399, 19.416, 19.456, 19.469, 19.5, 19.53,
                 19.553, 19.562, 19.602, 19.608, 19.629, 19.655, 19.67, 19.681,
                 19.7, 19.716, 19.724, 19.748, 19.758, 19.765, 19.782, 19.791,
                 19.799, 19.812, 19.82, 19.828, 19.844, 19.846, 19.858, 19.863,
                 19.862, 19.871, 19.882]
      def col_weight(column):
          min_nongap = max(2, .2*len(column))
          if len([c for c in column if c not in gap_chars]) < min_nongap:
              return ([0] * len(column), 0)
          counts = Counter(column)
          n_residues = len(counts)
          freqs = dict((aa, 1.0 / (n_residues * count))
                       for aa, count in counts.iteritems())
          weights = [freqs[aa] for aa in column]
          return (weights, n_residues)
      seq_weights = [0] * len(aln)
      tot_nres = 0.0
      for col in zip(*aln):
          wts, nres = col_weight(col)
          assert sum(wts) <= 20
          tot_nres += expectk[nres] if nres < len(expectk) else 20
          for idx, wt in enumerate(wts):
              seq_weights[idx] += wt
      if scaling == 'none':
          avg_seq_len = tot_nres / len(aln)
          return [wt/avg_seq_len for wt in seq_weights]
      if scaling == 'max1':
          scale = 1.0 / max(seq_weights)
      elif scaling == 'sum1':
          scale = 1.0 / sum(seq_weights)
      elif scaling == 'avg1':
          scale = len(aln) / sum(seq_weights)
      elif scaling == 'andy':
          scale = len(aln) / sum(seq_weights)
          return [min(scale * wt, 1.0) for wt in seq_weights]
      else:
          raise ValueError("Unknown scaling scheme '%s'" % scaling)
      return [scale * wt for wt in seq_weights]