Unconfigured Ad

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kennels
    Senior Member
    • Feb 2011
    • 149

    #1

    script help to format gff file

    Hi,

    I have a gff3 file which contains ID numbers in the name attribute. I want to replace these ID numbers with transcript names. I have another file containing the ID numbers in column 1, and the corresponding transcript name in column 2 (inputheaderfile), so I want to use this as the input file for to guide replacement.

    I've tried using perl script to do a system call of sed to replace the ID numbers in file, but I've calculated this to take a very long time and I don't have the scripting skills to write something better. Could anyone help or provide a better way of doing this? I've pasted what I've written below and I realize how newbie it is but hope to get some help.
    Thanks in advance!

    Code:
    # $usage = "$0 <inputheaderfile> <gfffile>"; 
    
    $headfile = $ARGV[0];
    $gfffile = $ARGV[1];
    
    open(GFF, $gfffile) or die "cannot open $gfffile\n";
    
    ## for every line the GFF file, scan through the entire input header file and replace the name attribute from ID number to transcript name, but only where the name attribute is at the end of line (type = gene)
    
    while ($line = <GFF>) { 
    		
    		print $.,"\n"; 
    
    		open(HEADER, $headfile) or die "cannot open $headfile\n";
    			
    		while ($line2 = <HEADER>) {
    			
    			chomp $line2;
    			@cols = split("\t", $line2);
    						
                            # if line in gff file contains 'Name=IDnumber' at end of line, then replace this with 'Name=transcriptname' based on the inputheaderfile
    
    			if ( $line =~ /Name=$cols[0]\n/ ) {
    				
    				`sed -i 's/Name=$cols[0]\$/Name=$cols[1]/g' $gfffile`;
    			} else {
    				next;
    			}
    
    		}
    		
    }
    
    exit;
  • arvid
    Senior Member
    • Jul 2011
    • 156

    #2
    A simple and relatively fast script would be the following, assuming a sorted dictionary file (dict.txt) containing the search/replace pairs AND that the "Name" GFF field is the last on each line to be replaced:

    Code:
    cat input.gff | sed $'s/Name=/Name=\t/' | sort -k 10 | join -a 1 -t $'\t' -1 10 - dict.txt | cut -f 2-11 | sed $'s/Name=\t/Name=/' > output.gff
    The slowest part here is the sort (necessary for the join) so if you have gigabytes of GFF you might run into trouble. Also, this method doesn't keep the original line order - however this could be fixed by an extra awk command in the beginning (adding line numbers) and sort (on that line number), cut (to remove the line number) in the end.
    Last edited by arvid; 06-13-2012, 12:55 AM. Reason: more info

    Comment

    • Kennels
      Senior Member
      • Feb 2011
      • 149

      #3
      Thanks arvid.

      Because the original gff had lines that don't end with Name, I did the following (for reference for others):

      1. Added line numbers with awk NR option
      Code:
      awk ' { print NR"\t"$0} ' input.gff > inputwithlineno.gff
      2. pulled out lines with the parent gene features that I wanted to modify
      Code:
      awk ' { if ($4 == "gene") print $0 } ' inputwithlineno.gff > genesonly.gff
      3. Use the script you provided to modify the ID into the names
      Code:
      cat genesonly.gff | sed $'s/Name=/Name\t/' | sort -k 11 | join -a 1 -t $'\t' -1 11 -2 1 - dict.sorted | cut -f 2-12 | sed $'s/Name=\t/Name=/' > genesedited.gff
      4. Merged the modfied gene lines with the original
      Code:
      sort -k 1,1 genesedited.gff > genesedited.sorted
      sort -k 1,1 inputwithlineno.gff > inputwithlineno.sorted
      join -a 2 -t $'\t' -1 1 -2 1 genesedited.sorted inputwithlineno.sorted | cut -f 1-10 | sort -g -k 1,1 | cut -f 2-10 > final.gff
      The output seems correct, so I hope the above is alright.

      One question - what does the dollar sign before the quote do? e.g. sed $'s/Name=/Name\t/'
      I looked it up and it appears to be string expansion, but I'm finding it hard to grasp the significance - why not just use without it, it seems to work.

      Many thanks for the help, I feel like I've just climbed over a very high wall.

      Comment

      • arvid
        Senior Member
        • Jul 2011
        • 156

        #4
        Some versions of sed/awk won't interpret \t as a tab character, that's why I prefer to use shell string expansion in scripts to make them work - $'\t' would be expanded to a tab character by the shell and passed on correctly to sed/awk. Otherwise you could also type a tab character by pressing "Ctrl-V <tab>", but I like the more explicit $'\t'...

        Comment

        • arvid
          Senior Member
          • Jul 2011
          • 156

          #5
          There is one caveat with the original script line I posted; if there is no corresponding entry in the dictionary file for a given "Name", it will be replaced by an empty string - which might not be what you intended. You can check for such lines with e.g.
          Code:
          grep "Name=$" final.gff

          Comment

          Latest Articles

          Collapse

          • 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
          • SEQadmin2
            New Genomics Technologies Take Aim at Long-Standing Limits
            by SEQadmin2


            Researchers using sequencing and genomics tools often have to make trade-offs. They can choose between speed or scale, short reads or long-range information, or targeted panels or a view of the whole transcriptome. New technologies that have been released this year are built to address those tough choices.

            We asked six companies the same four questions to learn about their latest products. The new technologies bring a lot to the table, including rethinking sequencing
            ...
            09-28-2026, 10:25 AM

          ad_right_rmr

          Collapse

          News

          Collapse

          Topics Statistics Last Post
          Started by SEQadmin2, Today, 11:13 AM
          0 responses
          12 views
          0 reactions
          Last Post SEQadmin2  
          Started by SEQadmin2, Today, 10:48 AM
          0 responses
          11 views
          0 reactions
          Last Post SEQadmin2  
          Started by SEQadmin2, 10-15-2026, 10:52 AM
          0 responses
          32 views
          0 reactions
          Last Post SEQadmin2  
          Started by SEQadmin2, 10-14-2026, 05:39 AM
          0 responses
          40 views
          0 reactions
          Last Post SEQadmin2  
          Working...