fastq.gz files renaming using python
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
• 2,059 views
•
link
2 answers
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
• 0 views
•
link
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.
• 0 views
•
link
Log in to answer this question.
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?
How many of these files need renaming? If it is only what you listed, a simple
mvcommand may be good enough.mv 389817_001_E01_S49_R1_001.fastq.gz 389817_S49_L001_R1_001.fastq.gz