text: user856: I have a fasta file that contains sequence reads and sequence id file that needed to be removed from the fasta file. I have done this earlier, but since id contains a space my piece of code is not working and I don't know how to change it since I'm still learning. fasta file looks like this.
  >m64041_200717_231916/74/12723_24868 id=30
  CCGCACCTCCTCAATCTGCAGCAGTTGAGGCCACTACCCTTCTGCTCAATGGTTCCTGCAGACTTTATCATAGTCACTCACACTTGTCCA
  >m64041_200717_231916/77/1941_50622 id=3115
  TGAGCCGCACCTCCTCAATCTGCAGCAGTTGAGGCCACTACCCTTCTGCTCAATGGTTCCTGCAGACTTTATCATAGTCACTCACACTTGTCCATGAG
  >m64041_200717_231916/105/20691_65844 id=488
  AGGCCACTACCCTTCTGCTCAATGGTTCCTGCAGACTTTATCATAGTCACTCACACTTGTCCAAGACTTTAT
  >m64041_200717_231916/108/17414_67048 id=4956
  TGAGGCCACTACCCTTCTGCTCAATGGTTCCTGCAGACTTTATCATAGTCACTCACACTTGTCCATGAGGCAGACTTTATCATAGTCACTCACACTTG
  >m64041_200717_231916/162/0_6615 id=857
  CAGACTTTATCATAGTCACTCACACTTGTCCATGAGGCAGACTTTATCATAGTCACTCACACTTG
  
  IDList file is like this.
  m64041_200717_231916/74/12723_24868 id=30
  m64041_200717_231916/108/17414_67048 id=4956
  m64041_200717_231916/105/20691_65844 id=488
  
  I need to remove these sequences from that fasta file. I have a code I used earlier. But it is not working since id contains a space (I think). Previous code is like this.
  # use as follows
  # ./filter_seq.bash <fastafile> <list_of_ids_you_want_to_remove>
  # ./filter_seq.bash all.fasta bacterial_IDlist
  
  grep ">" $1 | sed 's/>//g' | sort | uniq > all_del
  sort $2 | uniq > query_del
  comm -23 all_del query_del | sort | uniq > filtered_del
  perl -ne 'if(/^>(\S+)/){$c=$i{$1}}$c?print:chomp;$i{$_}=1 if @ARGV' filtered_del $1 > filtered.fasta
  rm *_del
  
  I would be grateful if someone can help me.
  
  user7153: Install (pip) biopython if u don't have it.
  from Bio import SeqIO
  with open('/path/ids.txt', 'r') as fin:
      to_keep = set([])
      for line in fin:
          to_keep.add(line.strip().split(' ')[0])
  with open('/path/output.fa', 'w') as fout:
      for record in SeqIO.parse('/path/sequences.fa','fasta'):
          if record.id in to_keep:
              SeqIO.write(record, fout, 'fasta')
  
  
original_text: 856: <p>I have a fasta file that contains sequence reads and sequence id file that needed to be removed from the fasta file. I have done this earlier, but since id contains a space my piece of code is not working and I don't know how to change it since I'm still learning. fasta file looks like this.</p>
  <pre><code>&gt;m64041_200717_231916/74/12723_24868 id=30
  CCGCACCTCCTCAATCTGCAGCAGTTGAGGCCACTACCCTTCTGCTCAATGGTTCCTGCAGACTTTATCATAGTCACTCACACTTGTCCA
  &gt;m64041_200717_231916/77/1941_50622 id=3115
  TGAGCCGCACCTCCTCAATCTGCAGCAGTTGAGGCCACTACCCTTCTGCTCAATGGTTCCTGCAGACTTTATCATAGTCACTCACACTTGTCCATGAG
  &gt;m64041_200717_231916/105/20691_65844 id=488
  AGGCCACTACCCTTCTGCTCAATGGTTCCTGCAGACTTTATCATAGTCACTCACACTTGTCCAAGACTTTAT
  &gt;m64041_200717_231916/108/17414_67048 id=4956
  TGAGGCCACTACCCTTCTGCTCAATGGTTCCTGCAGACTTTATCATAGTCACTCACACTTGTCCATGAGGCAGACTTTATCATAGTCACTCACACTTG
  &gt;m64041_200717_231916/162/0_6615 id=857
  CAGACTTTATCATAGTCACTCACACTTGTCCATGAGGCAGACTTTATCATAGTCACTCACACTTG
  </code></pre>
  <p>IDList file is like this.</p>
  <pre><code>m64041_200717_231916/74/12723_24868 id=30
  m64041_200717_231916/108/17414_67048 id=4956
  m64041_200717_231916/105/20691_65844 id=488
  </code></pre>
  <p>I need to remove these sequences from that fasta file. I have a code I used earlier. But it is not working since id contains a space (I think). Previous code is like this.</p>
  <pre><code># use as follows
  # ./filter_seq.bash &lt;fastafile&gt; &lt;list_of_ids_you_want_to_remove&gt;
  # ./filter_seq.bash all.fasta bacterial_IDlist
  
  grep &quot;&gt;&quot; <span class="math-container">$1 | sed 's/&gt;//g' | sort | uniq &gt; all_del
  sort $</span>2 | uniq &gt; query_del
  comm -23 all_del query_del | sort | uniq &gt; filtered_del
  perl -ne 'if(/^&gt;(\S+)/){<span class="math-container">$c=$</span>i{<span class="math-container">$1}}$</span>c?print:chomp;<span class="math-container">$i{$_}=1 if @ARGV' filtered_del $</span>1 &gt; filtered.fasta
  rm *_del
  </code></pre>
  <p>I would be grateful if someone can help me.</p>
   7153: <p>Install (pip) biopython if u don't have it.</p>
  <pre><code>from Bio import SeqIO
  with open('/path/ids.txt', 'r') as fin:
      to_keep = set([])
      for line in fin:
          to_keep.add(line.strip().split(' ')[0])
  with open('/path/output.fa', 'w') as fout:
      for record in SeqIO.parse('/path/sequences.fa','fasta'):
          if record.id in to_keep:
              SeqIO.write(record, fout, 'fasta')
  </code></pre>
  