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
          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
        • seqadmin
          Strategies for Sequencing Challenging Samples
          by seqadmin


          Despite advancements in sequencing platforms and related sample preparation technologies, certain sample types continue to present significant challenges that can compromise sequencing results. Pedro Echave, Senior Manager of the Global Business Segment at Revvity, explained that the success of a sequencing experiment ultimately depends on the amount and integrity of the nucleic acid template (RNA or DNA) obtained from a sample. “The better the quality of the nucleic acid isolated...
          03-22-2024, 06:39 AM

        ad_right_rmr

        Collapse

        News

        Collapse

        Topics Statistics Last Post
        Started by seqadmin, 04-11-2024, 12:08 PM
        0 responses
        30 views
        0 likes
        Last Post seqadmin  
        Started by seqadmin, 04-10-2024, 10:19 PM
        0 responses
        32 views
        0 likes
        Last Post seqadmin  
        Started by seqadmin, 04-10-2024, 09:21 AM
        0 responses
        28 views
        0 likes
        Last Post seqadmin  
        Started by seqadmin, 04-04-2024, 09:00 AM
        0 responses
        53 views
        0 likes
        Last Post seqadmin  
        Working...
        X