Rename multiple fastq.gz files
1
0
Entering edit mode
8 months ago
Jérémie • 0

Hi everyone,

I have little experience in bash and would like to rename multiple fastq.gz files. I know this question has already been asked multiple times, but nothing I have tried has worked so far, unfortunately.

Original names are as follows:

JM1-44589688_S693_L001_R1_001.fastq.gz
JM110-44589731_S719_L001_R2_001.fastq.gz

I would the final names like this:

JM1_1.fastq.gz
JM110_2.fastq.gz

I tried to use the rename command but I didn't manage to find a way to delete only de central part of the label using this command.

I also tried this:

rename '-' '_' *fastq.gz
for i in *.fastq
   do
   mv $i $(echo $i | awk '{split($1,a,/_/); print a[1]"_"a[5]}')
   done

But this way I lose the suffix .fastq.gz.

Your help will be very much appreciated!

fastq next-gen • 946 views
ADD COMMENT
0
Entering edit mode

Here's another safe solution using brename (download). Personally, I usually avoid batch renaming files with mv, which might overwrite existing files by accident.

$ brename -p '^(\w+).+_R([12])_\d+.fastq.gz' -r '${1}_$2.fastq.gz' -d
Searching for paths to rename...

  [OK] JM1-44589688_S693_L001_R1_001.fastq.gz -> JM1_1.fastq.gz
  [OK] JM110-44589731_S719_L001_R2_001.fastq.gz -> JM110_2.fastq.gz

2 path(s) to be renamed
ADD REPLY
1
Entering edit mode
8 months ago
bk11 ★ 3.0k

This will be a very rough way of doing it-

#listing the fastq files
ls *fastq.gz
JM1-44589688_S693_L001_R1_001.fastq.gz  JM110-44589731_S719_L001_R2_001.fastq.gz

#changing the names
ls *fastq.gz |awk -F [-_] '{print $1"_"$5".fastq.gz"}' |sed 's/R1/1/g' |sed 's/R2/2/g'
JM1_1.fastq.gz
JM110_2.fastq.gz
ADD COMMENT
0
Entering edit mode

Thank you! Just a stupid question, when I run this command it gives me the list of the names expected but does not actually change the names of the files in the directory. Do I have to change anything in the command line to effectively rename the files?

ADD REPLY
0
Entering edit mode

I just gave hint in the comment above. Please try this, it should work for you.

#!/bin/bash
for file in `ls *fastq.gz`; do 
    filename=$(basename "$file")
    new_name=`echo ${filename} |awk -F [-_] '{print $1"_"$5".fastq.gz"}' |sed 's/R1/1/g' |sed 's/R2/2/g'`
    mv ${file} ${new_name};
done
ADD REPLY
0
Entering edit mode

Sorry, I misunderstood. Thank you so much for this.

ADD REPLY
0
Entering edit mode

Of course! If it worked, accept the answer.

ADD REPLY

Login before adding your answer.

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