-1

I'm trying to use this code from https://github.com/dayedepps/q30 and I encountered some issues. I tried fixing some of the issues except for one.

def stat(filename):
    reader = fastq.read(filename)
    total_count = 0
    q20_count = 0
    q30_count = 0
    while True:
        read = reader.nextRead()
        if read == None:
            break
        total_count += len(read[3])
        q20, q30 = qual_stat(read[3])
        q20_count += q20
        q30_count += q30

Whenever I try to run the script, it results to:

File "/home/user/folder/q30.py", line 25, in stat
    read = reader.nextRead()
           ^^^^^^^^^^^^^^^
AttributeError: 'generator' object has no attribute 'nextRead'

Is there an alternative to nextRead so that the script would proceed? I tried changing it to just next() or read() but still the same result. I'm not yet adept in python so I'm not sure how to fix this issue. Thank you.

apcreyes
  • 1
  • 1
  • 1
    Did you mean `reader = fastq.Reader(filename)`? – Paul M. Jan 23 '23 at 02:55
  • Sorry, I meant read = reader.nextRead(). When that runs, it results to AttributeError: 'generator' object has no attribute 'nextRead'. – apcreyes Jan 23 '23 at 03:02
  • 1
    Yes, I understand. I'm saying that you're getting that error because `reader` was initialized incorrectly. I believe `reader = fastq.read(filename)` should instead be `reader = fastq.Reader(filename)` – Paul M. Jan 23 '23 at 03:03
  • Welcome to Stack Overflow. "Is there an alternative to nextRead so that the script would proceed?" This isn't understandable, because you have not explained: what should this code **do**? – Karl Knechtel Jan 23 '23 at 03:20

1 Answers1

0

The correct code in the git provided is this:

def stat(filename):
    reader = fastq.Reader(filename)
    total_count = 0
    q20_count = 0
    q30_count = 0
    while True:
        read = reader.nextRead()
        if read == None:
            break
        total_count += len(read[3])
        q20, q30 = qual_stat(read[3])
        q20_count += q20
        q30_count += q30

    print("total bases:", total_count)
    print("q20 bases:", q20_count)
    print("q30 bases:", q30_count)
    print("q20 percents:", 100 * float(q20_count)/float(total_count))
    print("q30 percents:", 100 * float(q30_count)/float(total_count))

I tried running the script directly and it works perfectly. Note that the first line of the stat function is reader = fastq.Reader(filename) instead of reader = fastq.read(filename). In fact, fastq.read doesn't seem to exist in the latest version of that repository. That line reader = fastq.read(filename) should've thrown an error if we were using the same code. It's possible that you are not using the files in the git you provided. Could you please elaborate on the details?

If you still encounter this error, would you please try re-cloning the files from the git and re-running it again? Here are the steps: (replace <fastq_file> with the path to your file)

git clone https://github.com/dayedepps/q30
cd q30
python q30.py <fastq_file>
Muhammad Nizami
  • 904
  • 1
  • 5
  • 9