#!/usr/bin/perl -w

use strict;

#my @organisms = ('Caenorhabditis_elegans');  ## Name of the query organism
#my @organisms = ('Anopheles_gambiae', 'Bos_taurus', 'Caenorhabditis_elegans', 'Canis_familiaris', 'Ciona_intestinalis', 'Danio_rerio', 'Drosophila_melanogaster', 'Equus_caballus', 'Gallus_gallus', 'Homo_sapiens', 'Macaca_mulatta', 'Monodelphis_domestica', 'Mus_musculus', 'Ornithorhynchus_anatinus', 'Oryzias_latipes' ,'Pan_troglodytes', 'Pongo_pygmaeus', 'Rattus_norvegicus', 'Saccharomyces_cerevisiae', 'Tetraodon_nigroviridis');  ## Name of the query organism
my @organisms = ('Anopheles_gambiae','Bos_taurus', 'Canis_familiaris', 'Ciona_intestinalis', 'Danio_rerio', 'Drosophila_melanogaster', 'Equus_caballus', 'Gallus_gallus', 'Macaca_mulatta', 'Monodelphis_domestica', 'Ornithorhynchus_anatinus', 'Oryzias_latipes' ,'Pan_troglodytes', 'Pongo_pygmaeus', 'Rattus_norvegicus', 'Saccharomyces_cerevisiae', 'Tetraodon_nigroviridis');

my $ensembl_version = '50';

foreach my $organism (@organisms) {

    my $ensembl_org = $organism."_EnsEMBL_".$ensembl_version;

    ###
    ### Retrieve-ensembl-seq parameters
    ###

    ## Feature types
    foreach my $feattype ('mRNA', 'CDS', 'intron', 'firstintron', 'utr') {

	## Type
	my $type;
	my $from;
	my $to;
	if (($feattype eq 'mRNA') || ($feattype eq 'CDS')) {
	    $type = 'upstream';  ## The -type option value; other example:'-type downstream'
	    $from = -2000;  ## Start position of the sequence
	    $to = -1;  ## End position of the sequence
	} else {
	    $type = '';
	    $from = '';
	    $to = '';
	}

	my $maskcoding = '-maskcoding';

	foreach my $rm ('', '-rm') {
	    foreach my $noov ('-ovlp', '-noov') {
		foreach my $str ('-1str', '-2str') {
		    foreach my $oligo_size ('1', '2', '3', '4', '5', '6', '7', '8') {

			my $input_file_name = $organism."_".$type."_".$feattype.$from.$to.$maskcoding.$rm.".fasta";
			$input_file_name =~ s/__/_/;

			my $output_file_name = $oligo_size."nt_".$type."_".$feattype.$from.$to.$maskcoding.$rm."_".$organism.$noov.$str.".freq";
			$output_file_name =~ s/__/_/;

			print "Saving result to file $output_file_name\n";

			my $noov_cw;
			if ($noov eq '-ovlp') {
			    $noov_cw = '';
			} else {
			    $noov_cw = '-noov';
			}
			my $command = "count-words -i /bio/rsa-tools/data/genomes/$ensembl_org/genome/$input_file_name -l $oligo_size $str $noov_cw > /bio/rsa-tools/data/genomes/$ensembl_org/oligo-frequencies/$output_file_name";
			`$command`;
		    }
		}
	    }
	}
    }
}
