<p><strong>It's a bug in Biopython 1.78.</strong></p>
<p>What those three sequences have in common is that they're less than 61 characters long.  When you print the record it implicitly calls <code>__str__</code> for the record, which calls <code>__repr__</code> for the Seq.  DBSeq gets its method from Seq, which looks like:</p>
<pre><code>    def __repr__(self):
        &quot;&quot;&quot;Return (truncated) representation of the sequence for debugging.&quot;&quot;&quot;
        if len(self) &gt; 60:
            # Shows the last three letters as it is often useful to see if
            # there is a stop codon at the end of a sequence.
            # Note total length is 54+3+3=60
            return f&quot;{self.__class__.__name__}('{str(self[:54])}...{str(self[-3:])}')&quot;
        else:
            return f&quot;{self.__class__.__name__}({self._data!r})&quot;
</code></pre>
<p>...and then it only tries to access the nonexistent <code>_data</code> for the short sequences.</p>
<p>So there's a mismatch between DBSeq's lack of a <code>_data</code> attribute and its use of <code>Seq.__repr__</code>.  I don't see a DBSeq class at all in the latest Biopython, so maybe this is fixed already.  But there's no tag for 1.79 yet that I can see and I can't get the very latest code to import successfully so I'm not sure.  In the meantime if you avoid letting <code>repr(record)</code> happen you should be able to work around it.</p>

CC BY-SA 4.0
2021-03-17T18:33:20.867Z
2021-03-17T18:33:20.867Z
Jesse
<p>Hi I am playing with Biopython and BioSQL,</p>
<p>I manage to create an MySQL database using <a href="https://biopython.org/wiki/BioSQL" rel="nofollow noreferrer">Managing local biological databases with the BioSQL module</a> tutorial:</p>
<p>and load 2909 fasta sequences in it through:</p>
<pre><code>db = server[&quot;test_1&quot;]

count = db.load(SeqIO.parse(&quot;Newtextfile-03.txt&quot;, &quot;fasta&quot;))
        print(&quot;Loaded %i records&quot; % count)
        server.commit()
</code></pre>
<p>from this file: <a href="https://easyupload.io/us14gl" rel="nofollow noreferrer">Newtextfile-03.txt</a></p>
<p>problem is trying just to print the database index list with:</p>
<pre><code>for i in db:
    print(i)
</code></pre>
<p>works , while trying print element by element:</p>
<pre><code>for i in db:
    print(i, db[i])
</code></pre>
<p>or with:</p>
<pre><code>for key, record in db.items():
            print (&quot;Key %r maps to a sequence record  %s&quot; % (key, record))
</code></pre>
<p>gives an error:</p>
<pre><code>2557 Traceback (most recent call last):
  File &quot;./try_001.py&quot;, line 142, in &lt;module&gt;
    f()
  File &quot;./try_001.py&quot;, line 120, in f
    print(i, db[i])
  File &quot;......../lib/python3.8/site-packages/Bio/SeqRecord.py&quot;, line 652, in __str__
    lines.append(repr(self.seq))
  File &quot;........//lib/python3.8/site-packages/Bio/Seq.py&quot;, line 109, in __repr__
    return f&quot;{self.__class__.__name__}({self._data!r})&quot;

AttributeError: 'DBSeq' object has no attribute '_data'

</code></pre>
<p>the error occurs for three records namely : [2164, 2522, 2557]</p>
<p>inspecting the <a href="https://easyupload.io/us14gl" rel="nofollow noreferrer">Newtextfile-03.txt</a> file results in finding fasta sequences of less than
100 aminoacids.</p>
<p>Is there any way to resolve the error ? It sounds strange to me that I can load the records into the database but not retrieving them. Is anything I am missing about the record load ? or similar ?</p>
<p>I am working with the database using:</p>
<pre><code> server = BioSeqDatabase.open_database(
    driver='MySQLdb' , 
    user=password.userz,
    password=password.pazzword,
    host=&quot;XXXXXXXX&quot;,
    db=&quot;bio-data-first&quot;)

db = server.new_database(&quot;test_1&quot;, description=&quot;Just for testing&quot;)
        server.commit()
</code></pre>
<p>using  driver=&quot;MySQLdb&quot;, wasn't able to have driver='mysql.connector' to work,</p>
<p>biopython is version 1.78</p>

CC BY-SA 4.0
2021-03-13T16:04:34.130Z
This does look like a bug but the code has been refactored a lot recently. Try running with biopython 1.79 (it's not released yet so you'll need to install directly from the github)
Chris_Rands
CC BY-SA 4.0
2021-03-13T16:06:01.213Z
so its about 'DBSeq' object ?
pippo1980
CC BY-SA 4.0
2021-03-13T16:09:29.433Z
thanks @Chris_Rands I'll give it a try as soon as I figure out about virtual enviroments and how to install modules from code
pippo1980
CC BY-SA 4.0
2021-03-13T16:22:55.160Z
found out that: for key, record in db.items():
        print(key)
        seq_record = db.lookup(gi=record.id)
        print(seq_record.seq) works for all the elements
pippo1980
<fasta><python><database>
Biopython BioSQL error : AttributeError: 'DBSeq' object has no attribute '_data'
# Biopython BioSQL error : AttributeError: 'DBSeq' object has no attribute '_data'

(Asked by: username_0 on 2021-03-13)

Hi I am playing with Biopython and BioSQL,
I manage to create an MySQL database using Managing local biological databases with the BioSQL module tutorial:
and load 2909 fasta sequences in it through:
db = server["test_1"]

count = db.load(SeqIO.parse("Newtextfile-03.txt", "fasta"))
        print("Loaded %i records" % count)
        server.commit()

from this file: Newtextfile-03.txt
problem is trying just to print the database index list with:
for i in db:
    print(i)

works , while trying print element by element:
for i in db:
    print(i, db[i])

or with:
for key, record in db.items():
            print ("Key %r maps to a sequence record  %s" % (key, record))

gives an error:
2557 Traceback (most recent call last):
  File "./try_001.py", line 142, in 
    f()
  File "./try_001.py", line 120, in f
    print(i, db[i])
  File "......../lib/python3.8/site-packages/Bio/SeqRecord.py", line 652, in __str__
    lines.append(repr(self.seq))
  File "........//lib/python3.8/site-packages/Bio/Seq.py", line 109, in __repr__
    return f"{self.__class__.__name__}({self._data!r})"

AttributeError: 'DBSeq' object has no attribute '_data'


the error occurs for three records namely : [2164, 2522, 2557]
inspecting the Newtextfile-03.txt file results in finding fasta sequences of less than
100 aminoacids.
Is there any way to resolve the error ? It sounds strange to me that I can load the records into the database but not retrieving them. Is anything I am missing about the record load ? or similar ?
I am working with the database using:
 server = BioSeqDatabase.open_database(
    driver='MySQLdb' , 
    user=password.userz,
    password=password.pazzword,
    host="XXXXXXXX",
    db="bio-data-first")

db = server.new_database("test_1", description="Just for testing")
        server.commit()

using  driver="MySQLdb", wasn't able to have driver='mysql.connector' to work,
biopython is version 1.78

**Tags:** fasta, python, database
> **Comments:**
> **username_1**: This does look like a bug but the code has been refactored a lot recently. Try running with biopython 1.79 (it's not released yet so you'll need to install directly from the github)  
> **username_0**: so its about 'DBSeq' object ?  
> **username_0**: thanks @Chris_Rands I'll give it a try as soon as I figure out about virtual enviroments and how to install modules from code  
> **username_0**: found out that: for key, record in db.items():         print(key)         seq_record = db.lookup(gi=record.id)         print(seq_record.seq) works for all the elements  

---

## 1 answers

### Answer by username_2 on 2021-03-17. Score: 1 | Accepted answer

It's a bug in Biopython 1.78.
What those three sequences have in common is that they're less than 61 characters long.  When you print the record it implicitly calls __str__ for the record, which calls __repr__ for the Seq.  DBSeq gets its method from Seq, which looks like:
    def __repr__(self):
        """Return (truncated) representation of the sequence for debugging."""
        if len(self) > 60:
            # Shows the last three letters as it is often useful to see if
            # there is a stop codon at the end of a sequence.
            # Note total length is 54+3+3=60
            return f"{self.__class__.__name__}('{str(self[:54])}...{str(self[-3:])}')"
        else:
            return f"{self.__class__.__name__}({self._data!r})"

...and then it only tries to access the nonexistent _data for the short sequences.
So there's a mismatch between DBSeq's lack of a _data attribute and its use of Seq.__repr__.  I don't see a DBSeq class at all in the latest Biopython, so maybe this is fixed already.  But there's no tag for 1.79 yet that I can see and I can't get the very latest code to import successfully so I'm not sure.  In the meantime if you avoid letting repr(record) happen you should be able to work around it.


---

