parsing fastq files in python
3
0
Entering edit mode
6.4 years ago

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.4k views
ADD COMMENT
2
Entering edit mode
6.4 years ago
st.ph.n ★ 2.7k
#!usr/bin/env python
import glob

for file in glob.glob('*S_Rep1*.fastq'):
        *do some stuff*
ADD COMMENT
1
Entering edit mode
6.4 years ago
Hussain Ather ▴ 990

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():
    ....
ADD COMMENT
0
Entering edit mode
6.4 years ago

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 ;)

ADD COMMENT

Login before adding your answer.

Traffic: 3001 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6