Writing fastq files with Python!

Hello!

I am trying to write a file in fastq format. Does anyone know if this can simply be done by specifying the fastq format with open? I have all the attributes of a fastq while (i.e ID, sequence, quality score), but I am not sure how to make a proper fastq file by simply passing this information to open while indicating fastq as the format. I hope I explained that well enough. But do ask questions if anything is unclear. Also, here is my pseudo code:

with open('dictionaryfile', '.fastq',"w") as outfile:
            for read in dictionaryfile:
                read_name = '>'+read.split('/')[0]
		quality = read.split('/')[2 or 3?]
                outfile.write(read_name+'\n'
                outfile.write(read.split('/')[1]+'\n'
		another line hear for writing the quality scores? (i.g outfile.write(quality))
										

Hi Taylor,

Your code sample is not possible because the open() function takes only two (2) parameters, file, and mode. However, here is a possible solution to save the data to in proper format

Sample FastQ Data:

@MN00537:51:000H2K25G:1:11101:2213:1092 1:N:0:9
CTCCAGTCCTTACTCCCATATCTAACCTCTTACCCCTACNTCATAGGTANACATTTTAATGAAT
+
FFFFFFFFFFFFAFFFFFFFF=FFFFAFFFFFFF/AFFF#FFFFFFFFF#FFFFFFFF

.
You can use the Template class from the string module:

from string import Template

identifier = '@MN00537:51:000H2K25G:1:11101:2213:1092 1:N:0:9'
sequence = 'CTCCAGTCCTTACTCCCATATCTAACCTCTTACCCCTACNTCATAGGTANACATTTTAATGAAT'
plus_sign = '+'
quality_score = 'FFFFFFFFFFFFAFFFFFFFF=FFFFAFFFFFFF/AFFF#FFFFFFFFF#FFFFFFFF'

fastq_template = Template(f'$identifier\n$sequence\n$plus_sign\n$quality_score')

with open(file='fastq', mode='w') as file:
    file.write(fastq_template.substitute(identifier=identifier,
                                         sequence=sequence,
                                         plus_sign=plus_sign,
                                         quality_score=quality_score))

Let me know if this produces the outcome you’re looking for!