fastq.gz files renaming using python
2
1
Entering edit mode
4.5 years ago
mukil.biotek ▴ 10

Hi all, I have fastq.gz files and I need to rename it

The read names in my files are

389817_001_E01_S49_R1_001.fastq.gz
389817_001_E01_S49_R2_001.fastq.gz
389818_001_A01_S1_R1_001.fastq.gz
389818_001_A01_S1_R2_001.fastq.gz

And i want to transform them in the format:

389817_S49_L001_R1_001.fastq.gz
389817_S49_L001_R2_001.fastq.gz
389818_S1_L001_R1_001.fastq.gz
389818_S1_L001_R2_001.fastq.gz

I tried using python but not working..see below for codes

import os
for i in os.listdir():
    file_name, file_ext = os.path.splitext(i)
    print(os.path.splitext(i))
    file_name, file_ext = os.path.splitext(i)
    print(file_name)    
    file_name1, file_ext1 = os.path.splitext(file_name)
    file_name1
    file_ext1
    print(file_name1.split('_'))
    f_0, f_1, f_2, f_3, f_4, f_5 = file_name1.split('_')
    new_name = print(os.path.join(f_0+'_'+f_3+'_'+'L001'+'_'+f_4+'_'+f_5+file_ext1+file_ext))
    path = '/Users/mukilkavi/Desktop/NAMING'
    os.rename('/Users/mukilkavi/Desktop/NAMING/', new_name)

Any advice will be appreciated...Thanks in advance

sequencing • 1.4k views
ADD COMMENT
0
Entering edit mode

I don't know python, but shouldn't that last line concatenate the path and the 'i' file name, and that's the input into os.rename?

ADD REPLY
0
Entering edit mode

How many of these files need renaming? If it is only what you listed, a simple mv command may be good enough.

mv 389817_001_E01_S49_R1_001.fastq.gz 389817_S49_L001_R1_001.fastq.gz

ADD REPLY
0
Entering edit mode
4.5 years ago

how about 'just' using bash.

 ls *.fastq.gz | awk -F '_' '{printf("mv \"%s\" \"%s_%s_L001_%s_%s\"\n",$0,$1,$4,$5,$6);}' > script.bash

check script.bash and then

bash script.bash
ADD COMMENT
0
Entering edit mode
4.5 years ago
Ram 43k
for f in *.fastq.gz
    newname=$(echo -n $f | awk -F "_" -vOFS="_" '{print $1,$4,"L001",$5,$6}')
    echo "mv $f $newname"
done

Untested, but should work. Once you test it and everything looks OK, change the echo statement to just mv $f $newname and the files should be renamed.

ADD COMMENT

Login before adding your answer.

Traffic: 3271 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