Unconfigured Ad

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chariko
    Member
    • Jun 2010
    • 56

    #1

    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
  • GenoMax
    Senior Member
    • Feb 2008
    • 7142

    #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

    • GenoMax
      Senior Member
      • Feb 2008
      • 7142

      #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

      • chariko
        Member
        • Jun 2010
        • 56

        #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

        • SEQadmin2
          16S/ITS Profiling vs. Shotgun Metagenomics: What Changes in Your Workflow?
          by SEQadmin2




          NGS has revolutionized clinical microbiology by offering new ways to identify and surveil pathogens without prior knowledge of the target. One popular approach is 16S rRNA and ITS amplicon sequencing, which targets specific microbial markers. But studies are increasingly incorporating shotgun metagenomics, a technique that sequences all DNA from a sample without targeting specific microbial markers. This approach allows more detailed taxonomic classification, detection...
          Today, 06:46 AM
        • SEQadmin2
          Multiomic Liquid Biopsy: What DNA/RNA Panels Reveal That DNA Alone Can’t
          by SEQadmin2



          Tumors contain complex mixtures of cell populations that evolve in response to treatment and host-specific factors. Tissue biopsies provide only static snapshots of a tumor’s state. But what if we could follow that evolution over time using a minimally invasive technique?

          Liquid biopsies can detect tumor-derived material circulating in the bloodstream, providing a way to study treatment response, detect emerging resistance, and discover new biomarkers. Circulating
          ...
          10-14-2026, 09:55 AM

        ad_right_rmr

        Collapse

        News

        Collapse

        Topics Statistics Last Post
        Started by SEQadmin2, 10-28-2026, 09:57 AM
        0 responses
        22 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, 10-28-2026, 09:51 AM
        0 responses
        14 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, 10-22-2026, 11:13 AM
        0 responses
        68 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, 10-22-2026, 10:48 AM
        0 responses
        48 views
        0 reactions
        Last Post SEQadmin2  
        Working...