Answers.Body: <p>For the first question try something like:</p>
  
  <pre><code>new_substrings = []
  new_positions = []
  new_list = []
  for pos, sub in zip(positions, substrings): 
     if not pos:
         new_list.append(sub)
     else:
         new_substrings.append(sub)
         new_positions.append(pos)
  </code></pre>
  
  <p>this gives:</p>
  
  <pre><code>&gt;&gt;&gt; new_substrings
  ['TACA']
  &gt;&gt;&gt; new_positions
  ['11, 4']
  &gt;&gt;&gt; new_list
  ['CTAGAAAAG']
  </code></pre>
  
  <p>For the hamming distance you just need to iterate over the values in <code>seq_ref</code> and append the results of the hamming distance function in <code>mismatches</code>.  Based on the given code and the example above you should be able to create that block.  Below is an untested quick example to get you on track.  </p>
  
  <pre><code>mismatches = []
  for seq in seq_ref.keys():
     for value in seq_ref[seq]:
         mismatches.append(HammingDistance(seq, value))
  </code></pre>
  
  <p>As far as the CSV and sorting is concerned I suggest that you look into pandas or numpy.  Both of those packages provided functions to help with this.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-17T13:10:15.410Z
Answers.Text: Just a note to say that your hamming distance calculation is pretty slow. When we've had to do this calculation before on many millions of sequences we've ended up cythonising the code. But friends who are better coders have even better algorithms for calculating these things.
Answers.UserDisplayName: Ian Sudbery
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-15T14:43:14.977Z
Answers.LastActivityDate: 2018-10-15T14:43:14.977Z
Answers.UserDisplayName: Bioathlete
Body: <p>I have a string, a list of substrings, and a list of positions where substrings are found in string.</p>
  
  <pre><code>string = 'ATGGTACACGCTACGAGCTAG'
  substrings = ['TACA', 'CTAGAAAAG']
  positions = ['11, 4', '']
  </code></pre>
  
  <p>Substring <code>'TACA'</code> appears at positions 11 and 4 in string (i.e. <code>'11, 4'</code>).</p>
  
  <p>Substring <code>'CTAGAAAAG'</code> does not appear in string (i.e. <code>''</code>).</p>
  
  <p>I need to remove <code>'CTAGAAAAG'</code> from <code>substrings</code> and I need to remove <code>''</code> from <code>positions</code>, while creating a new list with <code>'CTAGAAAAG'</code>. <strong>I need your help here to create code which will give the following results:</strong></p>
  
  <pre><code>new_substrings = ['TACA']
  new_positions = ['11, 4']
  new_list = ['CTAGAAAAG']
  string = 'ATGGTACACGCTACGAGCTAG' (obtained from above)
  </code></pre>
  
  <p>Once this is complete, the <strong>given</strong> code:</p>
  
  <pre><code>new_positions = [map(int, x.split(',')) for x in new_positions]
  seq_pos = dict(zip(new_substrings,new_positions))
  seq_ref = {k: [string[start:start+length] for start in starts] for k, starts, length in zip(new_substrings,new_positions,[len(x) for x in new_substrings])}
  </code></pre>
  
  <p>Gives:</p>
  
  <pre><code>seq_pos = {'TACA': [11, 4]}
  seq_ref = {'TACA': ['TACG', 'TACA']}
  </code></pre>
  
  <p>Then, from seq_ref, I need to create a list of integers which represents the number of mismatches between the key: <code>'TACA'</code> and both of its values: <code>'TACG'</code> and <code>'TACA'</code>.</p>
  
  <pre><code>mismatches = [[1, 0]]
  </code></pre>
  
  <p>The code to find the # of mismatches between two strings is provided:</p>
  
  <pre><code>def HammingDistance(p, q):
      dist = 0
      for i in range(len(p)):
          if p[i] != q[i]:
              dist += 1
      return dist
  </code></pre>
  
  <p>Finally, I need to save my data in a CSV file (i.e. Excel or Numbers).</p>
  
  <pre><code>new_positions = ['11, 4'] (obtained above)
  new_substrings = ['TACA'] (obtained above)
  actual_substrings = [['TACG', 'TACA']] (values of seq_ref)
  mismatches = [[1, 0]] (obtained above)
  </code></pre>
  
  <p>Info needs to be sorted based on # of mismatches.</p>
  
  <p><a href="https://i.sstatic.net/LdAa1.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/LdAa1.png" alt="enter image description here"></a></p>
  
  <p>I understand this is a multi-step problem but any help would be appreciated.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-15T12:37:09.747Z
Comments.Text: You posted a lot of given code, but what have you tried so far? To be honest this reads a lot like a homework assignment and while most users here are happy to help this is not a site to get your work done for you.
Comments.UserDisplayName: voiDnyx
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-15T14:21:13.950Z
Comments.Text: Where did the blocks of "given" code come from?  I agree this seems like homework and many of the questions you are trying to answer have similar concepts in the "given" code.  Have you tried using or understand some of the functions in the "given" code to see how they apply to the missing steps you have?
Comments.UserDisplayName: Bioathlete
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-15T14:26:28.827Z
Comments.Text: This is not part of a homework assignment.  I've never taken a coding or bioinformatics class before and I just need some assistance.  This is a unique problem that I have created from scratch.  The given code is my own work and I uploaded it so that it may provide a better understanding of the problem at hand.  I need help removing a blank string element from a list and I need help iterating the HammingDistance() function for elements in my list.  Also, I am unfamiliar with csv files, so I am just reaching out to see if anyone can help.
Comments.UserDisplayName: Thomas Ribeiro
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-15T14:46:21.000Z
Comments.Text: Ok I posted a solution.  It is confusing because most of what you are stuck on short of the CSV are concepts that you are using in the "given" code, including zip, dictionaries, list comprehension / iteration.
Comments.UserDisplayName: Bioathlete
Tags: <python>
Title: Store contigs and genome substrings based on mismatches
ThreadText: # Store contigs and genome substrings based on mismatches
  
  (Asked by: username_0 on 2018-10-15)
  
  I have a string, a list of substrings, and a list of positions where substrings are found in string.
  
  ```
  string = 'ATGGTACACGCTACGAGCTAG'
  substrings = ['TACA', 'CTAGAAAAG']
  positions = ['11, 4', '']
  
  ```
  
  Substring `'TACA'` appears at positions 11 and 4 in string (i.e. `'11, 4'`).
  
  Substring `'CTAGAAAAG'` does not appear in string (i.e. `''`).
  
  I need to remove `'CTAGAAAAG'` from `substrings` and I need to remove `''` from `positions`, while creating a new list with `'CTAGAAAAG'`. **I need your help here to create code which will give the following results:**
  
  ```
  new_substrings = ['TACA']
  new_positions = ['11, 4']
  new_list = ['CTAGAAAAG']
  string = 'ATGGTACACGCTACGAGCTAG' (obtained from above)
  
  ```
  
  Once this is complete, the **given** code:
  
  ```
  new_positions = [map(int, x.split(',')) for x in new_positions]
  seq_pos = dict(zip(new_substrings,new_positions))
  seq_ref = {k: [string[start:start+length] for start in starts] for k, starts, length in zip(new_substrings,new_positions,[len(x) for x in new_substrings])}
  
  ```
  
  Gives:
  
  ```
  seq_pos = {'TACA': [11, 4]}
  seq_ref = {'TACA': ['TACG', 'TACA']}
  
  ```
  
  Then, from seq\_ref, I need to create a list of integers which represents the number of mismatches between the key: `'TACA'` and both of its values: `'TACG'` and `'TACA'`.
  
  ```
  mismatches = [[1, 0]]
  
  ```
  
  The code to find the # of mismatches between two strings is provided:
  
  ```
  def HammingDistance(p, q):
      dist = 0
      for i in range(len(p)):
          if p[i] != q[i]:
              dist += 1
      return dist
  
  ```
  
  Finally, I need to save my data in a CSV file (i.e. Excel or Numbers).
  
  ```
  new_positions = ['11, 4'] (obtained above)
  new_substrings = ['TACA'] (obtained above)
  actual_substrings = [['TACG', 'TACA']] (values of seq_ref)
  mismatches = [[1, 0]] (obtained above)
  
  ```
  
  Info needs to be sorted based on # of mismatches.
  
  [![enter image description here](https://i.sstatic.net/LdAa1.png)](https://i.sstatic.net/LdAa1.png)
  
  I understand this is a multi-step problem but any help would be appreciated.
  
  **Tags:** python
  
  > **Comments:**
  > **username_1**: You posted a lot of given code, but what have you tried so far? To be honest this reads a lot like a homework assignment and while most users here are happy to help this is not a site to get your work done for you.  
  > **username_2**: Where did the blocks of "given" code come from?  I agree this seems like homework and many of the questions you are trying to answer have similar concepts in the "given" code.  Have you tried using or understand some of the functions in the "given" code to see how they apply to the missing steps you have?  
  > **username_0**: This is not part of a homework assignment.  I've never taken a coding or bioinformatics class before and I just need some assistance.  This is a unique problem that I have created from scratch.  The given code is my own work and I uploaded it so that it may provide a better understanding of the problem at hand.  I need help removing a blank string element from a list and I need help iterating the HammingDistance() function for elements in my list.  Also, I am unfamiliar with csv files, so I am just reaching out to see if anyone can help.  
  > **username_2**: Ok I posted a solution.  It is confusing because most of what you are stuck on short of the CSV are concepts that you are using in the "given" code, including zip, dictionaries, list comprehension / iteration.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2018-10-15. Score: 1
  
  For the first question try something like:
  
  ```
  new_substrings = []
  new_positions = []
  new_list = []
  for pos, sub in zip(positions, substrings): 
     if not pos:
         new_list.append(sub)
     else:
         new_substrings.append(sub)
         new_positions.append(pos)
  
  ```
  
  this gives:
  
  ```
  >>> new_substrings
  ['TACA']
  >>> new_positions
  ['11, 4']
  >>> new_list
  ['CTAGAAAAG']
  
  ```
  
  For the hamming distance you just need to iterate over the values in `seq_ref` and append the results of the hamming distance function in `mismatches`. Based on the given code and the example above you should be able to create that block. Below is an untested quick example to get you on track.
  
  ```
  mismatches = []
  for seq in seq_ref.keys():
     for value in seq_ref[seq]:
         mismatches.append(HammingDistance(seq, value))
  
  ```
  
  As far as the CSV and sorting is concerned I suggest that you look into pandas or numpy. Both of those packages provided functions to help with this.
  
  > **Comments:**
  > **username_3**: Just a note to say that your hamming distance calculation is pretty slow. When we've had to do this calculation before on many millions of sequences we've ended up cythonising the code. But friends who are better coders have even better algorithms for calculating these things.  
  
  ---
  
  