Biopython: Cant use .count() for biopython


My goal here is to receive the amount of time 'g' appears in a DNA sequence.

I imported a DNA sequence via Biopython using list comprehension

seq = [record for record in SeqIO.parse('sequences/hiv.gbk.rtf', 'fasta')]

I then tried using the .count() method on the newly created list comp variable


I get an error that reads

NotImplementedError: SeqRecord comparison is deliberately not implemented. Explicitly compare the attributes of interest.

Anyone know what the dealio is? Biopython's manual says all standard python methods should work.


You are trying to apply count to a list. You would to need to apply it to the sequence of each element, e.g.


or if you want to get the sum of all sequences

print(sum([s.seq.count('g') for s in seq]))

Here is a minimal working example

from Bio import SeqIO

txt = """>gnl|TC-DB|O60669|2.A.1.13.5 Monocarboxylate transporter 2 - Homo sapiens (Human).
>gnl|TC-DB|O60706|3.A.1.208.23 ATP-binding cassette sub-family C member 9 OS=Homo sapiens GN=ABCC9 PE=1 SV=2
>gnl|TC-DB|O60721|3.A.1.208.23 Sodium/potassium/calcium exchanger 1 OS=Homo sapiens GN=SLC24A1 PE=1 SV=1
>gnl|TC-DB|O60779|2.A.1.13.5 Thiamine transporter 1 (THTR-1) (ThTr1) (Thiamine carrier 1) (TC1) - Homo sapiens (Human).

filename = 'sequences.fa'
with open(filename, 'w') as f:

seqs = [record for record in SeqIO.parse(filename, 'fasta')]

print(sum([s.seq.count('P') for s in seqs]))    
>>> 21

>>> 9

Recent Tips

  1. How can I find out the token balance of an address?
  2. ref value is undefined in vue (modal, textarea, $refs)
  3. Azure - HDInsight Hbase Data Insertion Failed
  4. SignalR overwriting OnConnected(), OnDisconnected()
  5. DatePickerDialog displays with two borders
  6. "type 'double' is not a subtype of type 'int' in type cast" error in flutter. What should i do?
  7. hiding the autocomplete list when user click outside the textbox is not working as expected
  8. JSF IceFaces basic problem with redisplaying input value
  9. How to validate material ui TextField in reactjs?
  10. Go and MongoDB connection won't work with panic log "no reachable server"
  11. WordPress Posts Pagination Not Working
  12. F# sprintf won't print in interactive console
  13. Spring Integration get FTP files recursively with outbound-gateway
  14. Jade mixins not getting working from external file
  15. Can not access defined exports from the webpack bundle?
  16. Completely new to Node.js - API Programming
  17. Formatting Compare-Object Ouput
  18. Add dynamically added textbox value from User Control to main form
  19. Create a ByteBuf in Netty 4.0
  20. Is it possible to do computation before super() in the constructor?
  21. Q-learning Updating Frequency
  22. Wrong reload order when using Gulp and browserSync
  23. I use hugo build static page. But don't have content
  24. How to change background color and set bar colors based on conditional formatting in VBA?
  25. Problem when comparing two numeric values in SAS
  26. Is ACE reactor timer managment thread safe?
  27. Why Express res.render dumps the render output (EJS template) in console?
  28. Define generic typescript sort function of a certain type
  29. Eclipse RCP: TableViewer setInput from another view
  30. Migrating data from RDBMS to Arango DB: Bulk upload