Question: unhashable type: 'SeqRecord'
gravatar for Linkrw12
2.5 years ago by
Philadelphia, PA
Linkrw1220 wrote:

Hello everybody,

Using Python, I'm trying to create 30-mers from DNA sequences in a fasta file in order to run a local BLAST analysis on it later. So the end goal is to have a list that contains many lists of these 30-mer sequences that I can turn into a fasta file. To do this, I need to maintain all the fasta information throughout the subsequent code, but when I try to run it, I get an error that states "TypeError: unhashable type: 'SeqRecord''.

The weird thing is that when run the code on my home computer, it handles the code just fine. It's only when I run it on a ssh that links to a Linux server that it gives me this error. Both use Biopython 1.67, but the ssh utilizes Python 3.5 while my home computer runs Python 2.7.

For practical reasons, I cannot run this code on my home computer, so I need to find a way to circumvent or fix this error.

Here is the code that I used and the error message that pops up when running the code from ssh. Thanks!

from Bio import SeqIO

def find_kmers(string, k):

      kmers = []
      n = len(string)

      for i in range(0, n-k+1):

      return list(set(kmers))

ltr_seq = SeqIO.parse(open('HIV_Align_5\'LTR_no_gaps_nor_high_gaps.fasta'), "fasta")
all_kmer_list = []

for i in ltr_seq:
    all_kmer_list.append(find_kmers(i, 30))

File "HIV_5'", line 24, in <module>
    all_kmer_list.append(find_kmers(i, 30))
  File "HIV_5'", line 18, in find_kmers
    return list(set(kmers))
TypeError: unhashable type: 'SeqRecord'
biopython python • 1.1k views
ADD COMMENTlink modified 8 days ago by cronywalls0 • written 2.5 years ago by Linkrw1220
gravatar for WouterDeCoster
2.5 years ago by
WouterDeCoster36k wrote:

It's not a string, it's a seqrecord. Try i.seq in the function call in your example or string.seq in the function body.

ADD COMMENTlink written 2.5 years ago by WouterDeCoster36k

I would like to do that, but unfortunately, I need all the information in the 'ReqSeq' object to funnel the output to a fasta file. On my home computer, the 'ReqSeq' object would know that the function is being applied to the sequence found within the ReqSeq object (without the .seq extension) and maintains the rest of the fasta information while the sequence gets turned into various 30-mers

ADD REPLYlink modified 2.5 years ago • written 2.5 years ago by Linkrw1220

Oh, looks like the problem is in the set() call. Did'nt look long enough at the traceback earlier. Perhaps the implementation of creating a set is different between python3 and python2.7, if else I wouldn't know how to explain the problem.

You want each kmer originating from the same sequence to have the same fasta identifier?

ADD REPLYlink written 2.5 years ago by WouterDeCoster36k

Just extract the fields you want to save into a tuple.

ADD REPLYlink written 2.5 years ago by Matt Shirley8.8k

You're right! I modified the end of the find_kmer function and it worked wonderfully! Thank you :)

ADD REPLYlink written 2.5 years ago by Linkrw1220
gravatar for cronywalls
8 days ago by
cronywalls0 wrote:

The hash() is a built-in python method, used to return a unique number . This can be applied to any user-defined object which won’t get changed once initialized. This property is used mainly in dictionary keys .

TypeError: unhashable type: 'list' usually means that you are trying to use a list as an hash argument. This means that when you try to hash an unhashable object it will result an error. For ex. when you use a list as a key in the dictionary , this cannot be done because lists can't be hashed. The standard way to solve this issue is to cast a list to a tuple .

ADD COMMENTlink written 8 days ago by cronywalls0
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 2051 users visited in the last hour