#!/usr/bin/perl -w

use strict;

########################################################################
## This scrips is used to obtain predicted U12 introns from predicted ## 
##       U12-intron-flanking exons, that are stored in a GFF file.    ##
########################################################################

# we define de variables

my @gff = "";              # an array to read the first line

my @previousgff = "";      # an array to read the second line

my @introngff = "";        # an array to print the output

my $GROUP = "";            

my $entrada = $ARGV[0];    

my $llegint = 0;          # it will go line by line


open (FILE,$entrada) || die "Can't find $entrada\n";      # open or die!!!


while (<FILE>) {                             # the program will read line by line

next if m/^#/;                               # if it finds a line starting with the symbol # (a comment) 
                                             # it goes to the next line directly.

chomp $_;

@gff = split (/\t/,$_);                      # the script splits the line into an array (@gff) using 
                                             # the tabs (every position of the array is one column).

    if ($llegint > 0){                       # When the script is reading two lines at the same time 
                                             #($llegint > 0) we will be able to creat a predicted intron

	 $GROUP = $previousgff[8];           # We only want to create a new intron if we are in the 
                                             # same chromosome and in the same gene, so we should compare                                              # column 9

       if (($gff[8] eq $GROUP)              # We are in the same gene. Flanking exons are predicted U12-
                                            # intron-flanking exons (third column should contain the word
                                            #¡ÈU12¡É).Check the symbol (- or +) of the strand because the
                                            # extremes can be reversed
	   && 
	   (
	    (($gff[6] eq '-') && ($previousgff[2]=~/^U12/) && ($gff[2]=~/U12/)) 
	       ||
	    (($gff[6] eq '+') && ($gff[2]=~/^U12/) && ($previousgff[2]=~/U12/))
	   )
       ){

	 $introngff[0] = $gff[0];

	 $introngff[1] = $gff[1];          # in third column we will print the kind of predicted U12 
                                           # intron

		if ($gff[2] =~ /atac/) {

			$introngff[2] = "U12atac-intron";
		}

		else {

			$introngff[2] = "U12gtag-intron";
		}
 
	 $introngff[3] = (($previousgff[4]) + 1);         # The start of the intron would be the end of
                                                          # the left flanking exon plus one and its end
                                                          # would be the start of the next exon minus one
	 $introngff[4] = (($gff[3]) - 1);
 
	 $introngff[5] = (($gff[5]) + ($previousgff[5]))/2;       # The new score would be the average of
                                                                  # the score of the two predicted U12-
                                                                  # intron-flanking exons.  
 
	 $introngff[6] = $gff[6];
 
	 $introngff[7] = $gff[7];
 
	 $introngff[8] = $gff[8];
 

         print join("\t",@introngff)."\n";        # print the output (the predicted U12 intron) into an 
                                                  # array (@introngff) preserving GFF format
 	        
      }
 
 
    }
 
	@previousgff = @gff;                      # We want to read two lines at the same time, so we 
                                                  # have to create a new array (@previousgff)
 
        $llegint = $llegint + 1;                  # go to next line

}

close(FILE);
