SEQanswers

Go Back   SEQanswers > Bioinformatics > Bioinformatics



Similar Threads
Thread Thread Starter Forum Replies Last Post
One line fasta file with overlapped scaffold illinu Bioinformatics 4 09-18-2013 06:43 AM
How to join the line wraps in FASTA file Fad2012 Bioinformatics 22 03-21-2013 07:52 AM
GTF file with one line per gene? cnyh RNA Sequencing 3 03-06-2013 09:36 AM
BFAST fasta2brg -f filename kursuni Bioinformatics 0 05-10-2011 12:59 PM
Add a new line after the last file recording Wei-HD Bioinformatics 2 04-15-2010 01:13 AM

Reply
 
Thread Tools
Old 05-22-2014, 03:13 PM   #1
chariko
Member
 
Location: Spain

Join Date: Jun 2010
Posts: 56
Default Way for putting as filename the first line of the file

I have 1200 fasta file which are named x*. Does anybody know how to change the name of the file so that the new name is the first name of the file itself?

Example
xcv.txt which has written:

>contig1
CGGGTCCC

I want the xcv file to be renamed as contig1

and so on for the 1200 fasta files...

Thanks
chariko is offline   Reply With Quote
Old 05-22-2014, 04:25 PM   #2
GenoMax
Senior Member
 
Location: East Coast USA

Join Date: Feb 2008
Posts: 7,080
Default

WARNING: Please make sure you have a copy of the files in a secure location before using the script below. If something goes wrong then you could lose a lot of data. Test with a small subset of files first.

1. Grab a list of file names you want to convert into a file (example below assumes file names end in .fq).

Code:
$ ls -1 x*.fq > filelist
2. Put the following code in a file in the same directory with filelist/rest of the files. (this script will take whatever is in ID line and use it as a file name, would be best to ensure that there are no spaces or other strange characters in the fasta ID line).

Code:
for file in `cat ./filelist` 
do
# Avoid renaming diretories!
if [ -f "$file" ]
then
newname=`head -1 $file | cut -d">" -f2`
if [ -f "$newname" ]
then
echo "Cannot rename $file to $newname - file already exists"
else
cp "$file" "$newname"."fq"
fi
fi
done
In this example I am copying the files so the original stays intact (bold highlight) and am using the same file extension (you could replace the extension with something else, if you want).

You can use "mv" instead of "cp" once you are sure script works to rename the files.

There must be several other ways of doing this. This is just one of them

Modified using code found here: https://gist.github.com/dylan-k/6531959
GenoMax is offline   Reply With Quote
Old 05-22-2014, 05:29 PM   #3
GenoMax
Senior Member
 
Location: East Coast USA

Join Date: Feb 2008
Posts: 7,080
Default

One-line variation. WARNING in my previous post still applies.

Code:
$ for f in *.fq; do echo mv "$f" "`head -1 $f|cut -d">" -f2`.fq"; done
Note 1: If the commands look correct then you can remove the word "echo" to actually do the conversion.

Note 2: If you do not want to rename then "cp" can be substituted for "mv" retaining a copy of the original files.
GenoMax is offline   Reply With Quote
Old 05-27-2014, 06:04 AM   #4
chariko
Member
 
Location: Spain

Join Date: Jun 2010
Posts: 56
Default

Quote:
Originally Posted by GenoMax View Post
WARNING: Please make sure you have a copy of the files in a secure location before using the script below. If something goes wrong then you could lose a lot of data. Test with a small subset of files first.

1. Grab a list of file names you want to convert into a file (example below assumes file names end in .fq).

Code:
$ ls -1 x*.fq > filelist
2. Put the following code in a file in the same directory with filelist/rest of the files. (this script will take whatever is in ID line and use it as a file name, would be best to ensure that there are no spaces or other strange characters in the fasta ID line).

Code:
for file in `cat ./filelist` 
do
# Avoid renaming diretories!
if [ -f "$file" ]
then
newname=`head -1 $file | cut -d">" -f2`
if [ -f "$newname" ]
then
echo "Cannot rename $file to $newname - file already exists"
else
cp "$file" "$newname"."fq"
fi
fi
done
In this example I am copying the files so the original stays intact (bold highlight) and am using the same file extension (you could replace the extension with something else, if you want).

You can use "mv" instead of "cp" once you are sure script works to rename the files.

There must be several other ways of doing this. This is just one of them

Modified using code found here: https://gist.github.com/dylan-k/6531959
Thank you very much!! it worked perfect!!
chariko is offline   Reply With Quote
Reply

Tags
command line, fasta, rename

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off




All times are GMT -8. The time now is 04:18 PM.


Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2021, vBulletin Solutions, Inc.
Single Sign On provided by vBSSO