#!/usr/bin/perl -w
use strict;

########################### Variables #################################

my $threshold = 300;  # An ORF (open reading frame) with this many bases might
                      # be a gene.

my $genome;           # The genome we're investigating.

my @orfs;             # Lists the ORFs we find in the genome.  Each row of
                      # @orfs is a triple
                      #
                      #    [$start, $end, $direction]
                      #
                      # where $start tells us the beginning of the ORF in
                      # the genome, $end tells us the last position, and
                      # $direction is either "d" (for direct), meaning the
                      # ORF came from the genome as given, or "c" (for
                      # complement), meaning the ORF came from the reverse
                      # complement of the genome.

############################# Files ###################################

$genome  = get_genome_sequence("Synecho.nt");

########################## Main Program ###############################

find_orfs();
sort_orfs_by_starting_position();
print_orfs();

######################## Subroutines ##################################

sub find_orfs {
   my @direct_orfs;    # ORFs found in the genome as given
   my @reverse_orfs;   # ORFs found in the reverse complement

   @direct_orfs = ([23, 42, "d"]);

   @reverse_orfs = ([0, 1, "c"]);

   @orfs = (@direct_orfs, @reverse_orfs);
}   

sub sort_orfs_by_starting_position {
   @orfs = sort { $$a[0] <=> $$b[0] } @orfs;
}

sub print_orfs {
   foreach my $orf (@orfs) {
      my ($start, $end, $direction) = @$orf;
      $start = $start + 1;
      $end = $end + 1;
      print "$start\t$end\t$direction\n";
   }
}

# Read in a FASTA-format sequence
#
sub get_genome_sequence {
   my ($file_path) = @_;
   open GENOME, "<$file_path" or die "Can't open $file_path: $!\n";
   $_ = <GENOME>; # discard first line
   my $sequence = "";
   while (<GENOME>) {
      chomp($_);                  # Remove line break
      s/\r//;                     # And carriage return
      $_ = uc $_;                 # Make the line uppercase;
      $sequence = $sequence . $_; # Append the line to the sequence
   }
   return $sequence;
}
