Seqanswers Leaderboard Ad

Collapse

Announcement

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

  • 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;

  • #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


    • #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


      • #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


        • #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

          • 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
          • seqadmin
            Techniques and Challenges in Conservation Genomics
            by seqadmin



            The field of conservation genomics centers on applying genomics technologies in support of conservation efforts and the preservation of biodiversity. This article features interviews with two researchers who showcase their innovative work and highlight the current state and future of conservation genomics.

            Avian Conservation
            Matthew DeSaix, a recent doctoral graduate from Kristen Ruegg’s lab at The University of Colorado, shared that most of his research...
            03-08-2024, 10:41 AM

          ad_right_rmr

          Collapse

          News

          Collapse

          Topics Statistics Last Post
          Started by seqadmin, Yesterday, 06:37 PM
          0 responses
          10 views
          0 likes
          Last Post seqadmin  
          Started by seqadmin, Yesterday, 06:07 PM
          0 responses
          9 views
          0 likes
          Last Post seqadmin  
          Started by seqadmin, 03-22-2024, 10:03 AM
          0 responses
          51 views
          0 likes
          Last Post seqadmin  
          Started by seqadmin, 03-21-2024, 07:32 AM
          0 responses
          67 views
          0 likes
          Last Post seqadmin  
          Working...
          X