My program evaluates fastq files. The fastq file is just a huge text file (about 1,5gb in my case), which looks like this:

[email protected] 1 length=490
+SRR587217.1 1 length=490
BT[^^^^^caccccacffffffhhhgghhhhefeggghhghghgfgggghfgggeghhhhhgggghhgggggggggggggggdgggggfffffffffffffddfffffbfffffffffffffffffffffffMFFFHOIIPHMWHOIIHHMM^IFTMFHOHIHMHWMWFMFFFTHHIIIHIOIHIIIHMFFFFFFOIIIHHIOH^IIIHMSFOIHMHMFPYIIIIIIIIFFMFMOWIIIPIHFFMOIYIII^^``^aaaccccccccffffffghghhggghghgghghhhggggefgfededbe`cgeghggegaefgghegggggggggccdggggceeggfT``dddfffdfffffac]Zbffdfdbdbdfffddd^^dfdfdfddffffdbdbdfffffffbdddf``c`b[]ccQW`MSHYbdffff]cbbdbbbdYI^bd^[WdbbbfMXbbbf^b   ^^b^bdbdddIX^d]W]QW^dd^^dcfW
@SRR587217.4 4 length=502
+SRR587217.4 4 length=502

Each sequence is divided into 4 lines. First line shows us the sequence name. Second line shows us the sequence itself. Third line shows us some comments (irrelevant for this programm) and the fourth line shows as the quality of the bases in ascii character (Phred score)

First of all i subdivide the three necessary lines (line1, line2 and line4)

Line one (sequence names) are saved in one list. Line two are saved in another list as well. In line four, I convert the ASCII character into numbers and save these numbers for each line in a different list. Example:

"abcdefghijkl \n
list of numbers=[[1,2,3,4,5,6,7,8][1,2,3,4,5,6,7,8]]

with those lists i calculate means, medians, variance for some plotting.

The code works fine with smaller "test.files" but if I use the big one it overloads my PC. Is there a way to compress the code? Should I use less lists/dicts?

def main():
    parser = ArgumentParser(prog='fastq', description=desc)
    parser.add_argument('dateien', metavar='Files', nargs='*',
                    default=['sys.stdin'], help='Input of the file')
    parser.add_argument('-p', '--phred',dest='phred',nargs=  '?',action='store',
                    choices= [33 ,64 ], required=True, type=int ) 
    parser.add_argument('-c', '--cutoff',dest = "cutoff",nargs='?',
                    action='store', default=1, type=int,
                    help='deletes whole Reads with bad quality')
    parser.add_argument('-t', '--trim',dest = 'trim',nargs='?',
                    action='store', default=1, type=int,
                    help='Trimming Readends with bad quality')
    #parser.add_argument('-v','--version',action='version', version="{prog}: {version}".format(prog=args.prog, version='0.1'))

    args = parser.parse_args()

    names = [] ; x=0 ;names1 = ""
    seq_dict={} ; mean_dict={} ; qual_dict={}
    qual_liste=[] ; i=0 ; sequenz=[] ; seq_name=[]
    mean1=[] ; summe=0
    with open('test.fastq', 'r') as seq:
      for line in seq:                
        rest=i%4                   #modulo to focus on specific lines

        if rest==0:     #line including the sequenz name
            name=line[:-13]    #some embellishment
            name2=name1.strip("length=" or "length" or "lengl" or "leng")
            seq_name.append(name3) #List which includes lists of the seq names
        elif rest == 1:  #Line including sequenz nucleotides
            seq=line[:-args.trim] #trimming the sequenz
        elif rest ==3:    #line including quality score for sequenz
            if args.phred == 33:
                quality_code=[[(ord(ii)-33) for ii in i] for i in qual.split('\n')]
            elif args.phred == 64:
                quality_code=[[(ord(ii)-64) for ii in i] for i in qual.split('\n')]
            for list in quality_code:

            if mean >= int(args.cutoff):
                mean_dict[name3]=int(mean)   #filter/cutoff of reads with worse quality

The whole code includes some more lines about plotting to show the results.

  • \$\begingroup\$ What do you use sequenz for? You are storing every second line of the file in a list; if the file has 1.5GB, that means using hundreds of MBs of memory even if Python stored them very efficiently. It's also not very CPU friendly, since the list is constantly having to be expanded. \$\endgroup\$ Commented Feb 15, 2018 at 9:13
  • \$\begingroup\$ You might want to look into this project: github.com/JohnLonginotto/SeQC \$\endgroup\$
    – aghast
    Commented Mar 17, 2018 at 12:37

1 Answer 1


This won't solve your problem, but here are some suggestions regarding the organization of the code:

  • You can use a function like this to fetch four lines at once, which I think makes your code easier to understand than using rest:

    with open('test.fastq', 'r') as seq:
        for line1, line2, line3, line4 in group_it(4, seq):
  • Instead of doing if args.phred == 33: you can just use the variable in the calculation, avoiding the repetition:

    quality_code=[[(ord(ii)-args.phred) for ii in i] for i in qual.split('\n')]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.