Seqanswers Leaderboard Ad

Collapse

Announcement

Collapse
No announcement yet.
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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

  • #2
    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
    [B]cp "$file" "$newname"."fq"[/B]
    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

    Comment


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

      Code:
      $ for f in *.fq; do [B][U]echo[/U][/B] 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.

      Comment


      • #4
        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
        [B]cp "$file" "$newname"."fq"[/B]
        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!!

        Comment

        Latest Articles

        Collapse

        • seqadmin
          Essential Discoveries and Tools in Epitranscriptomics
          by seqadmin




          The field of epigenetics has traditionally concentrated more on DNA and how changes like methylation and phosphorylation of histones impact gene expression and regulation. However, our increased understanding of RNA modifications and their importance in cellular processes has led to a rise in epitranscriptomics research. “Epitranscriptomics brings together the concepts of epigenetics and gene expression,” explained Adrien Leger, PhD, Principal Research Scientist...
          04-22-2024, 07:01 AM
        • seqadmin
          Current Approaches to Protein Sequencing
          by seqadmin


          Proteins are often described as the workhorses of the cell, and identifying their sequences is key to understanding their role in biological processes and disease. Currently, the most common technique used to determine protein sequences is mass spectrometry. While still a valuable tool, mass spectrometry faces several limitations and requires a highly experienced scientist familiar with the equipment to operate it. Additionally, other proteomic methods, like affinity assays, are constrained...
          04-04-2024, 04:25 PM

        ad_right_rmr

        Collapse

        News

        Collapse

        Topics Statistics Last Post
        Started by seqadmin, Yesterday, 11:49 AM
        0 responses
        15 views
        0 likes
        Last Post seqadmin  
        Started by seqadmin, 04-24-2024, 08:47 AM
        0 responses
        16 views
        0 likes
        Last Post seqadmin  
        Started by seqadmin, 04-11-2024, 12:08 PM
        0 responses
        62 views
        0 likes
        Last Post seqadmin  
        Started by seqadmin, 04-10-2024, 10:19 PM
        0 responses
        60 views
        0 likes
        Last Post seqadmin  
        Working...
        X