How to edit a text file (.fastq) in python

I have a file like a small example below. Every 4 lines are associated with one ID. the second line of each ID starts with N. I want to remove the N at the beginning of the thoses lines and everything else will remain the same. I want to do this in python. do you know how to do it?

Example:

@SRR2163140.1 HISEQ:148:C670LANXX:3:1101:1302:1947 length=50
NGCGACCTCAGATCAGACGTGGCGACC
+SRR2163140.1 HISEQ:148:C670LANXX:3:1101:1302:1947 length=50
#<<ABGGGGGGGGGGGGGGGGGGGGGG
@SRR2163140.3 HISEQ:148:C670LANXX:3:1101:1381:1997 length=50
NGCCGACATCGAAGGATCAA
+SRR2163140.3 HISEQ:148:C670LANXX:3:1101:1381:1997 length=50
#<<ABFGGGGGGGGGGGGGG
@SRR2163140.4 HISEQ:148:C670LANXX:3:1101:1705:1940 length=50
NACAAACCCTTGTGTCGAGGGC
+SRR2163140.4 HISEQ:148:C670LANXX:3:1101:1705:1940 length=50
#=ABBGGGGGGGGGGGGGGGGG
@SRR2163140.7 HISEQ:148:C670LANXX:3:1101:1704:1965 length=50
NGGGACATGACAGCCTGGACCATCG
+SRR2163140.7 HISEQ:148:C670LANXX:3:1101:1704:1965 length=50
#=ABBGGGGGGGGGGGGGGGGGGGG

      

output:

@SRR2163140.1 HISEQ:148:C670LANXX:3:1101:1302:1947 length=50
GCGACCTCAGATCAGACGTGGCGACC
+SRR2163140.1 HISEQ:148:C670LANXX:3:1101:1302:1947 length=50
#<<ABGGGGGGGGGGGGGGGGGGGGGG
@SRR2163140.3 HISEQ:148:C670LANXX:3:1101:1381:1997 length=50
GCCGACATCGAAGGATCAA
+SRR2163140.3 HISEQ:148:C670LANXX:3:1101:1381:1997 length=50
#<<ABFGGGGGGGGGGGGGG
@SRR2163140.4 HISEQ:148:C670LANXX:3:1101:1705:1940 length=50
ACAAACCCTTGTGTCGAGGGC
+SRR2163140.4 HISEQ:148:C670LANXX:3:1101:1705:1940 length=50
#=ABBGGGGGGGGGGGGGGGGG
@SRR2163140.7 HISEQ:148:C670LANXX:3:1101:1704:1965 length=50
GGGACATGACAGCCTGGACCATCG
+SRR2163140.7 HISEQ:148:C670LANXX:3:1101:1704:1965 length=50
#=ABBGGGGGGGGGGGGGGGGGGGG

      

+3


source to share


1 answer


If I did exactly what you ask (remove the leading N from each sequence), then that would leave the FASTQ file in an inconsistent state.

Every fourth line of the FASTQ file contains quality values ​​for the sequence two lines earlier. Therefore, if you remove the first character from the sequence, you also need to remove the first character from the string with quality values.

You can do something very simple in pure Python like

with open("example.fastq") as f:
    for idx, line in enumerate(f.read().splitlines()):
        if idx % 2:
            print(line[1:])
        else:
            print(line)

      

but if you are going to work with biological data on a regular basis, you should really start using a bioinformatics module like BioPython . It will warn you if you try to do something that leaves the file in an inconsistent form or that it doesn't make sense.



The solution then looks like this:

from Bio import SeqIO
from Bio import Seq

new_records = []
for record in SeqIO.parse("example.fastq", "fastq"):
    sequence = str(record.seq)
    letter_annotations = record.letter_annotations

    # You first need to empty the existing letter annotations
    record.letter_annotations = {}

    new_sequence = sequence[1:]
    record.seq = Seq.Seq(new_sequence)


    new_letter_annotations = {'phred_quality': letter_annotations['phred_quality'][1:]}
    record.letter_annotations = new_letter_annotations

    new_records.append(record)


with open('without_starting_N.fastq', 'w') as output_handle:
    SeqIO.write(new_records, output_handle, "fastq")

      

which outputs

@SRR2163140.1 HISEQ:148:C670LANXX:3:1101:1302:1947 length=50
GCGACCTCAGATCAGACGTGGCGACC
+
<<ABGGGGGGGGGGGGGGGGGGGGGG
@SRR2163140.3 HISEQ:148:C670LANXX:3:1101:1381:1997 length=50
GCCGACATCGAAGGATCAA
+
<<ABFGGGGGGGGGGGGGG
@SRR2163140.4 HISEQ:148:C670LANXX:3:1101:1705:1940 length=50
ACAAACCCTTGTGTCGAGGGC
+
=ABBGGGGGGGGGGGGGGGGG
@SRR2163140.7 HISEQ:148:C670LANXX:3:1101:1704:1965 length=50
GGGACATGACAGCCTGGACCATCG
+
=ABBGGGGGGGGGGGGGGGGGGGG

      

(the "+" character on every third line is not necessarily followed by the same sequence identifier and description from the two lines earlier)

+4


source







All Articles