Question: unhashable type: 'SeqRecord'
gravatar for Linkrw12
4.4 years ago by
Philadelphia, PA
Linkrw1230 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 • 2.0k views
ADD COMMENTlink modified 23 months ago by cronywalls10 • written 4.4 years ago by Linkrw1230
gravatar for WouterDeCoster
4.4 years ago by
WouterDeCoster45k 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 4.4 years ago by WouterDeCoster45k

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 4.4 years ago • written 4.4 years ago by Linkrw1230

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 4.4 years ago by WouterDeCoster45k

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

ADD REPLYlink written 4.4 years ago by Matt Shirley9.5k

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

ADD REPLYlink written 4.4 years ago by Linkrw1230
gravatar for cronywalls
23 months ago by
cronywalls10 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 23 months ago by cronywalls10
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: 2111 users visited in the last hour