This is a test version of Biostars. For the public version, visit https://www.biostars.org.
parsing fastq files in python

I have multiple fastq files for different samples. Example : S_Rep1_R1.fastq S_Rep1_R2.fastq S_Rep2_R1.fastq S_Rep2_R2.fastq I want to choose all fastq files corresponding to say S_Rep1 and execute some functions. How can I write a python script which will choose all fastq files belonging to S_Rep1.

next-gen

3 answers

#!usr/bin/env python
import glob

for file in glob.glob('*S_Rep1*.fastq'):
        *do some stuff*

You could also use any standard file parser with python

#!usr/bin/env python
f = open("fastq_file.fastq", "r")

for i in f.readilnes():
    ....

Here you go:

# importing os module
import os
# store the path of current directory in the variable 'cwd'
cwd = os.getcwd()
# loop over all files
for files in os.listdir(cwd):
    # check if file is fastq by extension
    if files.endswith(".fastq"):
        # check if file name contains 'S_Rep1'
        if 'S_Rep1' in files:
            # do something here
            print files

Keep calm and program ;)

Log in to answer this question.