[Biopython] when is a SeqRecord not a SeqRecord

Brad Chapman chapmanb at 50mail.com
Wed Jul 18 13:23:27 UTC 2012


Dilara;

> I'm trying to understand what is why when I print filtered_rec I get a
> SeqRecord but if I try to access any particular attribute of a SeqRecord
> such as letter_annotations I sometimes get an attribute error --
> AttributeError: 'NoneType' object has no attribute
> 'letter_annotations.'

> def check_meanQ(record, q_threshold):
>     seqlen=len(record)
>     quality_scores=array(record.letter_annotations["phred_quality"])
>     if round(quality_scores.mean()) <= q_threshold:
>         print "Discarded ", record.id, "because mean Q was",
> round(quality_scores.mean())
>     elif round(quality_scores.mean()) > q_threshold:
>         return record

This function returns different results based on the comparison of
mean quality scores to your threshold:

- When it is below the threshold, it returns None (since you do not
  define an explicit return value)
- When it is above the threshold, it returns a SeqRecord.

> from Bio import SeqIO
> for rec in SeqIO.parse("test.fastq", "fastq"):
>     print rec.id
>     filtered_rec= check_meanQ(rec, q_threshold)
>     #print filtered_rec
>     print filtered_rec.letter_annotations

You are seeing the error since in the filtered cases the function
returns None. You probably want:

filtered_rec= check_meanQ(rec, q_threshold)
if filtered_rec is not None:
   print filtered_rec.letter_annotations

Brad



More information about the Biopython mailing list