Answers.Body: <p>Your file looks like it is <code>CDS</code> feature based, not <code>exon</code> feature based, which <em>seems</em> to be what the script expects.</p>
  <p>There is a tool to convert CDS based GFF to exon based that I've used successfully in the past...</p>
  <ul>
  <li>Could be <a href="https://ccb.jhu.edu/software/stringtie/gff.shtml" rel="nofollow noreferrer">GFF utilities</a>?</li>
  <li>Actually, I think it's <a href="http://genometools.org/tools.html" rel="nofollow noreferrer">GenomeTools</a></li>
  </ul>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-11-08T23:36:25.260Z
Answers.Text: Originally, I had a GTF but I failed with `gffread --gene2exon -E traingenes.gtf -o traingenes.gff3`.  What did I miss?
Answers.UserDisplayName: user977828
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-11-09T13:30:23.073Z
Answers.Text: What do you mean 'failed'?
Answers.UserDisplayName: Dan Bolser
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-11-10T00:34:30.867Z
Answers.Text: Sorry, I meant it still kept CDS rather than changing them to Exon. What did I miss?
Answers.UserDisplayName: user977828
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-11-08T12:23:08.967Z
Answers.LastActivityDate: 2021-11-08T12:23:08.967Z
Answers.UserDisplayName: Dan Bolser
Answers.Body: <p>The easiest is to use <code>agat_convert_sp_gff2zff.pl</code> from <a href="https://github.com/NBISweden/AGAT" rel="nofollow noreferrer">AGAT</a><br />
  (<strong>Edit</strong> Actually you will have to first standardize your file because you do not have gene features, neither gene_id or Parent atttributes in your mRNA features. AGAT will be confused. So, in your case, prior the <em>gff2zff</em> conversion you will have to run <code>agat_convert_sp_gxf2gxf.pl -c geneID --gff file.gff -o file_clean.gff</code>)</p>
  <p>Otherwise you can use your script but you need to add the exon as suggested by @Dan.<br />
  To do so you can just replace the CDS by exon using a sed command, it should work.<br />
  You can also use <code>agat_convert_sp_gxf2gxf.pl</code> (with parameter <code>-c geneID</code>) it will add the gene and exon features.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-11-09T14:41:16.827Z
Answers.LastActivityDate: 2021-11-09T14:47:00.663Z
Answers.LastEditDate: 2021-11-09T14:47:00.663Z
Answers.UserDisplayName: juke34
Body: <p>I have the following GFF3 file which I would like to convert to <a href="https://github.com/KorfLab/SNAP#parameter-estimation" rel="nofollow noreferrer">ZFF</a> file with the below script:</p>
  <pre><code>$ head -n 20 traingenes.gff3 
  ##gff-version 3
  # gffread v0.12.7
  # gffread -E traingenes.gtf -o traingenes.gff3
  NbLab330C00 GeneMark.hmm3   mRNA    74501   76501   .   +   .   ID=1_t;geneID=1_g
  NbLab330C00 GeneMark.hmm3   CDS 74501   74512   .   +   0   Parent=1_t
  NbLab330C00 GeneMark.hmm3   CDS 75568   75999   .   +   0   Parent=1_t
  NbLab330C00 GeneMark.hmm3   CDS 76151   76501   .   +   0   Parent=1_t
  NbLab330C00 GeneMark.hmm3   mRNA    76535   77079   .   +   .   ID=2_t;geneID=2_g
  NbLab330C00 GeneMark.hmm3   CDS 76535   76639   .   +   0   Parent=2_t
  NbLab330C00 GeneMark.hmm3   CDS 76702   77079   .   +   0   Parent=2_t
  NbLab330C00 GeneMark.hmm3   mRNA    93763   100703  .   -   .   ID=3_t;geneID=3_g
  NbLab330C00 GeneMark.hmm3   CDS 93763   93837   .   -   0   Parent=3_t
  NbLab330C00 GeneMark.hmm3   CDS 93915   94031   .   -   0   Parent=3_t
  NbLab330C00 GeneMark.hmm3   CDS 94351   94430   .   -   2   Parent=3_t
  NbLab330C00 GeneMark.hmm3   CDS 95483   95589   .   -   1   Parent=3_t
  NbLab330C00 GeneMark.hmm3   CDS 95697   95746   .   -   0   Parent=3_t
  NbLab330C00 GeneMark.hmm3   CDS 100464  100703  .   -   0   Parent=3_t
  </code></pre>
  <pre class="lang-perl prettyprint-override"><code>#!/usr/bin/env perl
  use strict;
  # 
  # source: https://biowize.wordpress.com/2012/06/01/training-the-snap-ab-initio-gene-predictor/ 
  #
  my @exons;
  my <span class="math-container">$gene_count = 0;
  my $</span>current_seq = &quot;&quot;;
  while (my <span class="math-container">$line = &lt;STDIN&gt;) {
    if ($line =~ m/^###/) {
      flush(\@exons);
      next;
    }
    my @fields = split(/\t/, $line);
    if ($fields[2] eq "mRNA") {
      flush(\@exons);
    } elsif ($fields[2] eq "exon") {
      if ($fields[0] ne $current_seq) {
        $current_seq = $fields[0];
        printf("&gt;%s\n", $</span>current_seq);
      }
      push(@exons, \@fields);
    }
  }
  flush();
   
  sub flush
  {
    my <span class="math-container">$num_exons = scalar(@exons);
    return if($</span>num_exons == 0);
     
    my <span class="math-container">$group = sprintf("%s.%d", $</span>exons[0]-&gt;[0], <span class="math-container">$gene_count);
    $</span>gene_count++;
     
    if (<span class="math-container">$num_exons == 1) {
  my($start, $end) = ($exons[0]-&gt;[3], $exons[0]-&gt;[4]);
  if ($exons[0]-&gt;[6] eq "-") {
    ($start, $end) = ($exons[0]-&gt;[4], $exons[0]-&gt;[3]);
  }
  printf("Esngl\t%lu\t%lu\t%s\n", $start, $end, $group);
    } else {
  @exons = reverse(@exons) if($exons[0]-&gt;[6] eq "-");
  for (my $i = 0; $i &lt; $num_exons; $i++) {
    my $exon_type = "Exon";
    if ($i == 0) {
      $exon_type = "Einit";
    } elsif ($i == $num_exons - 1) {
      $</span>exon_type = &quot;Eterm&quot;;
        }
         
        my(<span class="math-container">$start, $</span>end) = (<span class="math-container">$exons[$</span>i]-&gt;[3], <span class="math-container">$exons[$</span>i]-&gt;[4]);
        if (<span class="math-container">$exons[0]-&gt;[6] eq "-") {
      ($start, $end) = ($exons[$i]-&gt;[4], $exons[$</span>i]-&gt;[3]);
        }
         
        printf(&quot;%s\t%lu\t%lu\t%s\n&quot;, <span class="math-container">$exon_type, $</span>start, <span class="math-container">$end, $</span>group);
      }
    }
    @exons = ();
  }
  </code></pre>
  <p>However, when I run it like <code>perl gff3_to_zff.pl &lt; traingenes.gff3 &gt;&gt; traingenes.ann </code> an empty file get generated.</p>
  <p>What did I miss?</p>
  <p>Thank you in advance,</p>
  <p>Michal</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-11-08T06:35:29.663Z
Comments.Text: Are you going for the short format or long format? Can you provide a spec for what you'd like the output to look like? ZFF is not a common file format.
Comments.UserDisplayName: conchoecia
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-11-08T07:11:07.817Z
Comments.Text: I would go the long format. The specs are described [here](https://github.com/KorfLab/SNAP#parameter-estimation).
Comments.UserDisplayName: user977828
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-09-12T12:21:50.717Z
Comments.Text: Please look at the responses below @user977828 and consider upvoting/accepting an answer
Comments.UserDisplayName: M__
Tags: <perl><gff3>
Title: Perl convert GFF3 file
ThreadText: # Perl convert GFF3 file
  
  (Asked by: username_0 on 2021-11-08)
  
  I have the following GFF3 file which I would like to convert to [ZFF](https://github.com/KorfLab/SNAP#parameter-estimation) file with the below script:
  
  ```
  $ head -n 20 traingenes.gff3 
  ##gff-version 3
  # gffread v0.12.7
  # gffread -E traingenes.gtf -o traingenes.gff3
  NbLab330C00 GeneMark.hmm3   mRNA    74501   76501   .   +   .   ID=1_t;geneID=1_g
  NbLab330C00 GeneMark.hmm3   CDS 74501   74512   .   +   0   Parent=1_t
  NbLab330C00 GeneMark.hmm3   CDS 75568   75999   .   +   0   Parent=1_t
  NbLab330C00 GeneMark.hmm3   CDS 76151   76501   .   +   0   Parent=1_t
  NbLab330C00 GeneMark.hmm3   mRNA    76535   77079   .   +   .   ID=2_t;geneID=2_g
  NbLab330C00 GeneMark.hmm3   CDS 76535   76639   .   +   0   Parent=2_t
  NbLab330C00 GeneMark.hmm3   CDS 76702   77079   .   +   0   Parent=2_t
  NbLab330C00 GeneMark.hmm3   mRNA    93763   100703  .   -   .   ID=3_t;geneID=3_g
  NbLab330C00 GeneMark.hmm3   CDS 93763   93837   .   -   0   Parent=3_t
  NbLab330C00 GeneMark.hmm3   CDS 93915   94031   .   -   0   Parent=3_t
  NbLab330C00 GeneMark.hmm3   CDS 94351   94430   .   -   2   Parent=3_t
  NbLab330C00 GeneMark.hmm3   CDS 95483   95589   .   -   1   Parent=3_t
  NbLab330C00 GeneMark.hmm3   CDS 95697   95746   .   -   0   Parent=3_t
  NbLab330C00 GeneMark.hmm3   CDS 100464  100703  .   -   0   Parent=3_t
  
  ```
  
  ```perl
  #!/usr/bin/env perl
  use strict;
  # 
  # source: https://biowize.wordpress.com/2012/06/01/training-the-snap-ab-initio-gene-predictor/ 
  #
  my @exons;
  my $gene_count = 0;
  my $current_seq = "";
  while (my $line = <STDIN>) {
    if ($line =~ m/^###/) {
      flush(\@exons);
      next;
    }
    my @fields = split(/\t/, $line);
    if ($fields[2] eq "mRNA") {
      flush(\@exons);
    } elsif ($fields[2] eq "exon") {
      if ($fields[0] ne $current_seq) {
        $current_seq = $fields[0];
        printf(">%s\n", $current_seq);
      }
      push(@exons, \@fields);
    }
  }
  flush();
   
  sub flush
  {
    my $num_exons = scalar(@exons);
    return if($num_exons == 0);
     
    my $group = sprintf("%s.%d", $exons[0]->[0], $gene_count);
    $gene_count++;
     
    if ($num_exons == 1) {
  my($start, $end) = ($exons[0]->[3], $exons[0]->[4]);
  if ($exons[0]->[6] eq "-") {
    ($start, $end) = ($exons[0]->[4], $exons[0]->[3]);
  }
  printf("Esngl\t%lu\t%lu\t%s\n", $start, $end, $group);
    } else {
  @exons = reverse(@exons) if($exons[0]->[6] eq "-");
  for (my $i = 0; $i < $num_exons; $i++) {
    my $exon_type = "Exon";
    if ($i == 0) {
      $exon_type = "Einit";
    } elsif ($i == $num_exons - 1) {
      $exon_type = "Eterm";
        }
         
        my($start, $end) = ($exons[$i]->[3], $exons[$i]->[4]);
        if ($exons[0]->[6] eq "-") {
      ($start, $end) = ($exons[$i]->[4], $exons[$i]->[3]);
        }
         
        printf("%s\t%lu\t%lu\t%s\n", $exon_type, $start, $end, $group);
      }
    }
    @exons = ();
  }
  
  ```
  
  However, when I run it like `perl gff3_to_zff.pl < traingenes.gff3 >> traingenes.ann`  an empty file get generated.
  
  What did I miss?
  
  Thank you in advance,
  
  Michal
  
  **Tags:** perl, gff3
  
  > **Comments:**
  > **username_1**: Are you going for the short format or long format? Can you provide a spec for what you'd like the output to look like? ZFF is not a common file format.  
  > **username_0**: I would go the long format. The specs are described [here](https://github.com/KorfLab/SNAP#parameter-estimation).  
  > **username_2**: Please look at the responses below @username_0 and consider upvoting/accepting an answer  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_3 on 2021-11-08. Score: 1
  
  Your file looks like it is `CDS` feature based, not `exon` feature based, which *seems* to be what the script expects.
  
  There is a tool to convert CDS based GFF to exon based that I've used successfully in the past...
  
  * Could be [GFF utilities](https://ccb.jhu.edu/software/stringtie/gff.shtml)?
  * Actually, I think it's [GenomeTools](http://genometools.org/tools.html)
  
  > **Comments:**
  > **username_0**: Originally, I had a GTF but I failed with `gffread --gene2exon -E traingenes.gtf -o traingenes.gff3`.  What did I miss?  
  > **username_3**: What do you mean 'failed'?  
  > **username_0**: Sorry, I meant it still kept CDS rather than changing them to Exon. What did I miss?  
  
  ---
  
  ### Answer by username_4 on 2021-11-09. Score: 1
  
  The easiest is to use `agat_convert_sp_gff2zff.pl` from [AGAT](https://github.com/NBISweden/AGAT)  
  (**Edit** Actually you will have to first standardize your file because you do not have gene features, neither gene\_id or Parent atttributes in your mRNA features. AGAT will be confused. So, in your case, prior the *gff2zff* conversion you will have to run `agat_convert_sp_gxf2gxf.pl -c geneID --gff file.gff -o file_clean.gff`)
  
  Otherwise you can use your script but you need to add the exon as suggested by @Dan.  
  To do so you can just replace the CDS by exon using a sed command, it should work.  
  You can also use `agat_convert_sp_gxf2gxf.pl` (with parameter `-c geneID`) it will add the gene and exon features.
  
  
  ---
  
  