#!/usr/bin/perl 
# (C) M.chandrasekhar, Dept. of EE, IIT Bombay,
#     chandra_m_sekhar@hotmail.com, 
#     Created on February 4th, 2006


# This code generates features (arff format) from the 
# sequences(203 ATG window -> upstream 100, ATG, downstream 100). Total number of features
# upstream: monomers, dimers,trimers, tetramers, pentamers, codons
# downstream: monomers, dimers,trimers, tetramers, pentamers, codons
# Counter shows the processing sequence number
#
# Default: all sequence classtype is set as positive
# so if you generate training file care should be taken to 
# make only 500 position and rest all negative for each full length sequence.
# This can be obtained from map file mentioned in getvalidsequences.pl
# since array index starts with 0 the position is 500, in the original full length
# sequence its 501 position.
use strict;
#use entropy;
#use pickrandom;
$|++;
my $windowlength = 200;
my $ITERATIONS=1;
my @monomers = ("A","C","G","T");
my @dimers = ("AA","AC","AG","AT",
              "CA","CC","CG","CT",
              "GA","GC","GG","GT",
              "TA","TC","TG","TT");

my @trimers = ("AAA","AAC","AAG","AAT","ACA","ACC","ACG","ACT","AGA","AGC",
               "AGG","AGT","ATA","ATC","ATG","ATT","CAA","CAC","CAG","CAT",
               "CCA","CCC","CCG","CCT","CGA","CGC","CGG","CGT","CTA","CTC",
               "CTG","CTT","GAA","GAC","GAG","GAT","GCA","GCC","GCG","GCT",
               "GGA","GGC","GGG","GGT","GTA","GTC","GTG","GTT","TAA","TAC",
               "TAG","TAT","TCA","TCC","TCG","TCT","TGA","TGC","TGG","TGT",
               "TTA","TTC","TTG","TTT"); 

my @tetramers = ("AAAA","AAAC","AAAG","AAAT","AACA","AACC","AACG","AACT","AAGA","AAGC",
                "AAGG","AAGT","AATA","AATC","AATG","AATT","ACAA","ACAC","ACAG","ACAT",
                "ACCA","ACCC","ACCG","ACCT","ACGA","ACGC","ACGG","ACGT","ACTA","ACTC",
                "ACTG","ACTT","AGAA","AGAC","AGAG","AGAT","AGCA","AGCC","AGCG","AGCT",
                "AGGA","AGGC","AGGG","AGGT","AGTA","AGTC","AGTG","AGTT","ATAA","ATAC",
                "ATAG","ATAT","ATCA","ATCC","ATCG","ATCT","ATGA","ATGC","ATGG","ATGT",
                "ATTA","ATTC","ATTG","ATTT","CAAA","CAAC","CAAG","CAAT","CACA","CACC",
                "CACG","CACT","CAGA","CAGC","CAGG","CAGT","CATA","CATC","CATG","CATT",
                "CCAA","CCAC","CCAG","CCAT","CCCA","CCCC","CCCG","CCCT","CCGA","CCGC",
                "CCGG","CCGT","CCTA","CCTC","CCTG","CCTT","CGAA","CGAC","CGAG","CGAT",
                "CGCA","CGCC","CGCG","CGCT","CGGA","CGGC","CGGG","CGGT","CGTA","CGTC",
                "CGTG","CGTT","CTAA","CTAC","CTAG","CTAT","CTCA","CTCC","CTCG","CTCT",
                "CTGA","CTGC","CTGG","CTGT","CTTA","CTTC","CTTG","CTTT","GAAA","GAAC",
                "GAAG","GAAT","GACA","GACC","GACG","GACT","GAGA","GAGC","GAGG","GAGT",
                "GATA","GATC","GATG","GATT","GCAA","GCAC","GCAG","GCAT","GCCA","GCCC",
                "GCCG","GCCT","GCGA","GCGC","GCGG","GCGT","GCTA","GCTC","GCTG","GCTT",
                "GGAA","GGAC","GGAG","GGAT","GGCA","GGCC","GGCG","GGCT","GGGA","GGGC",
                "GGGG","GGGT","GGTA","GGTC","GGTG","GGTT","GTAA","GTAC","GTAG","GTAT",
                "GTCA","GTCC","GTCG","GTCT","GTGA","GTGC","GTGG","GTGT","GTTA","GTTC",
                "GTTG","GTTT","TAAA","TAAC","TAAG","TAAT","TACA","TACC","TACG","TACT",
                "TAGA","TAGC","TAGG","TAGT","TATA","TATC","TATG","TATT","TCAA","TCAC",
                "TCAG","TCAT","TCCA","TCCC","TCCG","TCCT","TCGA","TCGC","TCGG","TCGT",
                "TCTA","TCTC","TCTG","TCTT","TGAA","TGAC","TGAG","TGAT","TGCA","TGCC",
                "TGCG","TGCT","TGGA","TGGC","TGGG","TGGT","TGTA","TGTC","TGTG","TGTT",
                "TTAA","TTAC","TTAG","TTAT","TTCA","TTCC","TTCG","TTCT","TTGA","TTGC",
                "TTGG","TTGT","TTTA","TTTC","TTTG","TTTT");

my @pentamers = ("AAAAA","AAAAC","AAAAG","AAAAT","AAACA","AAACC","AAACG","AAACT","AAAGA","AAAGC",
         "AAAGG","AAAGT","AAATA","AAATC","AAATG","AAATT","AACAA","AACAC","AACAG","AACAT",
         "AACCA","AACCC","AACCG","AACCT","AACGA","AACGC","AACGG","AACGT","AACTA","AACTC",
         "AACTG","AACTT","AAGAA","AAGAC","AAGAG","AAGAT","AAGCA","AAGCC","AAGCG","AAGCT",
         "AAGGA","AAGGC","AAGGG","AAGGT","AAGTA","AAGTC","AAGTG","AAGTT","AATAA","AATAC",
         "AATAG","AATAT","AATCA","AATCC","AATCG","AATCT","AATGA","AATGC","AATGG","AATGT",
         "AATTA","AATTC","AATTG","AATTT","ACAAA","ACAAC","ACAAG","ACAAT","ACACA","ACACC",
         "ACACG","ACACT","ACAGA","ACAGC","ACAGG","ACAGT","ACATA","ACATC","ACATG","ACATT",
         "ACCAA","ACCAC","ACCAG","ACCAT","ACCCA","ACCCC","ACCCG","ACCCT","ACCGA","ACCGC",
         "ACCGG","ACCGT","ACCTA","ACCTC","ACCTG","ACCTT","ACGAA","ACGAC","ACGAG","ACGAT",
         "ACGCA","ACGCC","ACGCG","ACGCT","ACGGA","ACGGC","ACGGG","ACGGT","ACGTA","ACGTC",
         "ACGTG","ACGTT","ACTAA","ACTAC","ACTAG","ACTAT","ACTCA","ACTCC","ACTCG","ACTCT",
         "ACTGA","ACTGC","ACTGG","ACTGT","ACTTA","ACTTC","ACTTG","ACTTT","AGAAA","AGAAC",
         "AGAAG","AGAAT","AGACA","AGACC","AGACG","AGACT","AGAGA","AGAGC","AGAGG","AGAGT",
         "AGATA","AGATC","AGATG","AGATT","AGCAA","AGCAC","AGCAG","AGCAT","AGCCA","AGCCC",
         "AGCCG","AGCCT","AGCGA","AGCGC","AGCGG","AGCGT","AGCTA","AGCTC","AGCTG","AGCTT",
         "AGGAA","AGGAC","AGGAG","AGGAT","AGGCA","AGGCC","AGGCG","AGGCT","AGGGA","AGGGC",
         "AGGGG","AGGGT","AGGTA","AGGTC","AGGTG","AGGTT","AGTAA","AGTAC","AGTAG","AGTAT",
         "AGTCA","AGTCC","AGTCG","AGTCT","AGTGA","AGTGC","AGTGG","AGTGT","AGTTA","AGTTC",
         "AGTTG","AGTTT","ATAAA","ATAAC","ATAAG","ATAAT","ATACA","ATACC","ATACG","ATACT",
         "ATAGA","ATAGC","ATAGG","ATAGT","ATATA","ATATC","ATATG","ATATT","ATCAA","ATCAC",
         "ATCAG","ATCAT","ATCCA","ATCCC","ATCCG","ATCCT","ATCGA","ATCGC","ATCGG","ATCGT",
         "ATCTA","ATCTC","ATCTG","ATCTT","ATGAA","ATGAC","ATGAG","ATGAT","ATGCA","ATGCC",
         "ATGCG","ATGCT","ATGGA","ATGGC","ATGGG","ATGGT","ATGTA","ATGTC","ATGTG","ATGTT",
         "ATTAA","ATTAC","ATTAG","ATTAT","ATTCA","ATTCC","ATTCG","ATTCT","ATTGA","ATTGC",
         "ATTGG","ATTGT","ATTTA","ATTTC","ATTTG","ATTTT","CAAAA","CAAAC","CAAAG","CAAAT",
         "CAACA","CAACC","CAACG","CAACT","CAAGA","CAAGC","CAAGG","CAAGT","CAATA","CAATC",
         "CAATG","CAATT","CACAA","CACAC","CACAG","CACAT","CACCA","CACCC","CACCG","CACCT",
         "CACGA","CACGC","CACGG","CACGT","CACTA","CACTC","CACTG","CACTT","CAGAA","CAGAC",
         "CAGAG","CAGAT","CAGCA","CAGCC","CAGCG","CAGCT","CAGGA","CAGGC","CAGGG","CAGGT",
         "CAGTA","CAGTC","CAGTG","CAGTT","CATAA","CATAC","CATAG","CATAT","CATCA","CATCC",
         "CATCG","CATCT","CATGA","CATGC","CATGG","CATGT","CATTA","CATTC","CATTG","CATTT",
         "CCAAA","CCAAC","CCAAG","CCAAT","CCACA","CCACC","CCACG","CCACT","CCAGA","CCAGC",
         "CCAGG","CCAGT","CCATA","CCATC","CCATG","CCATT","CCCAA","CCCAC","CCCAG","CCCAT",
         "CCCCA","CCCCC","CCCCG","CCCCT","CCCGA","CCCGC","CCCGG","CCCGT","CCCTA","CCCTC",
         "CCCTG","CCCTT","CCGAA","CCGAC","CCGAG","CCGAT","CCGCA","CCGCC","CCGCG","CCGCT",
         "CCGGA","CCGGC","CCGGG","CCGGT","CCGTA","CCGTC","CCGTG","CCGTT","CCTAA","CCTAC",
         "CCTAG","CCTAT","CCTCA","CCTCC","CCTCG","CCTCT","CCTGA","CCTGC","CCTGG","CCTGT",
         "CCTTA","CCTTC","CCTTG","CCTTT","CGAAA","CGAAC","CGAAG","CGAAT","CGACA","CGACC",
         "CGACG","CGACT","CGAGA","CGAGC","CGAGG","CGAGT","CGATA","CGATC","CGATG","CGATT",
         "CGCAA","CGCAC","CGCAG","CGCAT","CGCCA","CGCCC","CGCCG","CGCCT","CGCGA","CGCGC",
         "CGCGG","CGCGT","CGCTA","CGCTC","CGCTG","CGCTT","CGGAA","CGGAC","CGGAG","CGGAT",
         "CGGCA","CGGCC","CGGCG","CGGCT","CGGGA","CGGGC","CGGGG","CGGGT","CGGTA","CGGTC",
         "CGGTG","CGGTT","CGTAA","CGTAC","CGTAG","CGTAT","CGTCA","CGTCC","CGTCG","CGTCT",
         "CGTGA","CGTGC","CGTGG","CGTGT","CGTTA","CGTTC","CGTTG","CGTTT","CTAAA","CTAAC",
         "CTAAG","CTAAT","CTACA","CTACC","CTACG","CTACT","CTAGA","CTAGC","CTAGG","CTAGT",
         "CTATA","CTATC","CTATG","CTATT","CTCAA","CTCAC","CTCAG","CTCAT","CTCCA","CTCCC",
         "CTCCG","CTCCT","CTCGA","CTCGC","CTCGG","CTCGT","CTCTA","CTCTC","CTCTG","CTCTT",
         "CTGAA","CTGAC","CTGAG","CTGAT","CTGCA","CTGCC","CTGCG","CTGCT","CTGGA","CTGGC",
         "CTGGG","CTGGT","CTGTA","CTGTC","CTGTG","CTGTT","CTTAA","CTTAC","CTTAG","CTTAT",
         "CTTCA","CTTCC","CTTCG","CTTCT","CTTGA","CTTGC","CTTGG","CTTGT","CTTTA","CTTTC",
         "CTTTG","CTTTT","GAAAA","GAAAC","GAAAG","GAAAT","GAACA","GAACC","GAACG","GAACT",
         "GAAGA","GAAGC","GAAGG","GAAGT","GAATA","GAATC","GAATG","GAATT","GACAA","GACAC",
         "GACAG","GACAT","GACCA","GACCC","GACCG","GACCT","GACGA","GACGC","GACGG","GACGT",
         "GACTA","GACTC","GACTG","GACTT","GAGAA","GAGAC","GAGAG","GAGAT","GAGCA","GAGCC",
         "GAGCG","GAGCT","GAGGA","GAGGC","GAGGG","GAGGT","GAGTA","GAGTC","GAGTG","GAGTT",
         "GATAA","GATAC","GATAG","GATAT","GATCA","GATCC","GATCG","GATCT","GATGA","GATGC",
         "GATGG","GATGT","GATTA","GATTC","GATTG","GATTT","GCAAA","GCAAC","GCAAG","GCAAT",
         "GCACA","GCACC","GCACG","GCACT","GCAGA","GCAGC","GCAGG","GCAGT","GCATA","GCATC",
         "GCATG","GCATT","GCCAA","GCCAC","GCCAG","GCCAT","GCCCA","GCCCC","GCCCG","GCCCT",
         "GCCGA","GCCGC","GCCGG","GCCGT","GCCTA","GCCTC","GCCTG","GCCTT","GCGAA","GCGAC",
         "GCGAG","GCGAT","GCGCA","GCGCC","GCGCG","GCGCT","GCGGA","GCGGC","GCGGG","GCGGT",
         "GCGTA","GCGTC","GCGTG","GCGTT","GCTAA","GCTAC","GCTAG","GCTAT","GCTCA","GCTCC",
         "GCTCG","GCTCT","GCTGA","GCTGC","GCTGG","GCTGT","GCTTA","GCTTC","GCTTG","GCTTT",
         "GGAAA","GGAAC","GGAAG","GGAAT","GGACA","GGACC","GGACG","GGACT","GGAGA","GGAGC",
         "GGAGG","GGAGT","GGATA","GGATC","GGATG","GGATT","GGCAA","GGCAC","GGCAG","GGCAT",
         "GGCCA","GGCCC","GGCCG","GGCCT","GGCGA","GGCGC","GGCGG","GGCGT","GGCTA","GGCTC",
         "GGCTG","GGCTT","GGGAA","GGGAC","GGGAG","GGGAT","GGGCA","GGGCC","GGGCG","GGGCT",
         "GGGGA","GGGGC","GGGGG","GGGGT","GGGTA","GGGTC","GGGTG","GGGTT","GGTAA","GGTAC",
         "GGTAG","GGTAT","GGTCA","GGTCC","GGTCG","GGTCT","GGTGA","GGTGC","GGTGG","GGTGT",
         "GGTTA","GGTTC","GGTTG","GGTTT","GTAAA","GTAAC","GTAAG","GTAAT","GTACA","GTACC",
         "GTACG","GTACT","GTAGA","GTAGC","GTAGG","GTAGT","GTATA","GTATC","GTATG","GTATT",
         "GTCAA","GTCAC","GTCAG","GTCAT","GTCCA","GTCCC","GTCCG","GTCCT","GTCGA","GTCGC",
         "GTCGG","GTCGT","GTCTA","GTCTC","GTCTG","GTCTT","GTGAA","GTGAC","GTGAG","GTGAT",
         "GTGCA","GTGCC","GTGCG","GTGCT","GTGGA","GTGGC","GTGGG","GTGGT","GTGTA","GTGTC",
         "GTGTG","GTGTT","GTTAA","GTTAC","GTTAG","GTTAT","GTTCA","GTTCC","GTTCG","GTTCT",
         "GTTGA","GTTGC","GTTGG","GTTGT","GTTTA","GTTTC","GTTTG","GTTTT","TAAAA","TAAAC",
         "TAAAG","TAAAT","TAACA","TAACC","TAACG","TAACT","TAAGA","TAAGC","TAAGG","TAAGT",
         "TAATA","TAATC","TAATG","TAATT","TACAA","TACAC","TACAG","TACAT","TACCA","TACCC",
         "TACCG","TACCT","TACGA","TACGC","TACGG","TACGT","TACTA","TACTC","TACTG","TACTT",
         "TAGAA","TAGAC","TAGAG","TAGAT","TAGCA","TAGCC","TAGCG","TAGCT","TAGGA","TAGGC",
         "TAGGG","TAGGT","TAGTA","TAGTC","TAGTG","TAGTT","TATAA","TATAC","TATAG","TATAT",
         "TATCA","TATCC","TATCG","TATCT","TATGA","TATGC","TATGG","TATGT","TATTA","TATTC",
         "TATTG","TATTT","TCAAA","TCAAC","TCAAG","TCAAT","TCACA","TCACC","TCACG","TCACT",
         "TCAGA","TCAGC","TCAGG","TCAGT","TCATA","TCATC","TCATG","TCATT","TCCAA","TCCAC",
         "TCCAG","TCCAT","TCCCA","TCCCC","TCCCG","TCCCT","TCCGA","TCCGC","TCCGG","TCCGT",
         "TCCTA","TCCTC","TCCTG","TCCTT","TCGAA","TCGAC","TCGAG","TCGAT","TCGCA","TCGCC",
         "TCGCG","TCGCT","TCGGA","TCGGC","TCGGG","TCGGT","TCGTA","TCGTC","TCGTG","TCGTT",
         "TCTAA","TCTAC","TCTAG","TCTAT","TCTCA","TCTCC","TCTCG","TCTCT","TCTGA","TCTGC",
         "TCTGG","TCTGT","TCTTA","TCTTC","TCTTG","TCTTT","TGAAA","TGAAC","TGAAG","TGAAT",
         "TGACA","TGACC","TGACG","TGACT","TGAGA","TGAGC","TGAGG","TGAGT","TGATA","TGATC",
         "TGATG","TGATT","TGCAA","TGCAC","TGCAG","TGCAT","TGCCA","TGCCC","TGCCG","TGCCT",
         "TGCGA","TGCGC","TGCGG","TGCGT","TGCTA","TGCTC","TGCTG","TGCTT","TGGAA","TGGAC",
         "TGGAG","TGGAT","TGGCA","TGGCC","TGGCG","TGGCT","TGGGA","TGGGC","TGGGG","TGGGT",
         "TGGTA","TGGTC","TGGTG","TGGTT","TGTAA","TGTAC","TGTAG","TGTAT","TGTCA","TGTCC",
         "TGTCG","TGTCT","TGTGA","TGTGC","TGTGG","TGTGT","TGTTA","TGTTC","TGTTG","TGTTT",
         "TTAAA","TTAAC","TTAAG","TTAAT","TTACA","TTACC","TTACG","TTACT","TTAGA","TTAGC",
         "TTAGG","TTAGT","TTATA","TTATC","TTATG","TTATT","TTCAA","TTCAC","TTCAG","TTCAT",
         "TTCCA","TTCCC","TTCCG","TTCCT","TTCGA","TTCGC","TTCGG","TTCGT","TTCTA","TTCTC",
         "TTCTG","TTCTT","TTGAA","TTGAC","TTGAG","TTGAT","TTGCA","TTGCC","TTGCG","TTGCT",
         "TTGGA","TTGGC","TTGGG","TTGGT","TTGTA","TTGTC","TTGTG","TTGTT","TTTAA","TTTAC",
         "TTTAG","TTTAT","TTTCA","TTTCC","TTTCG","TTTCT","TTTGA","TTTGC","TTTGG","TTTGT",
         "TTTTA","TTTTC","TTTTG","TTTTT");

my @polymers=(@monomers,@dimers,@trimers,@tetramers,@pentamers);



#my $start_data = 0;
#my $end_data = scalar @data - 1;
#print scalar $end_data."\n";
#print scalar @data."\n";
#my @slice;
#for (my $i=$start_data; $i<=$end_data; $i++){

#    @slice = @{$data[$i]};

#    foreach (@slice){
#       my ($letter,$height) = ($_ =~ /^(.{1})(\S+)/);
#        print $letter." ".$height."\n";
#    }
#}
#

my $last_status_len = 0;

sub print_status
{
   my $new_status = shift();
   $last_status_len = 0 if( !defined($last_status_len));
   print STDOUT "\rSequence number: $new_status";

   if(length($new_status) < $last_status_len)
   {
      print STDOUT (' ' x ($last_status_len - length ($new_status)));
   }
   $last_status_len = length($new_status);
}

sub countoccurences
{
   my $qstr=shift; # sequence
   my $sstr=shift; # search character or substring
   my $slen=length($sstr);
   my $qlen=length($qstr);

   if( $slen == 1 )
   {
      my $cnt = 0;
      $cnt = ($qstr =~ s/$sstr//g);
      return $cnt;
   }elsif( $slen > 1 ){
      my $cnts = 0;
      my $currentx=0;

      while ($currentx < $qlen)
      {
        my $tmpstr=substr($qstr,$currentx,$slen);
        if($sstr eq $tmpstr)
        {
         $cnts++;
        }
        $currentx++ ;
      }
      return $cnts;
   }else{
      die "Error unable to find string length of $sstr in $qstr";
   }
}

sub countnonoverlapoccurences
{
   my $qstr=shift; # sequence
   my $sstr=shift; # search character or substring
   my $pstart=shift; # preference to start...upstream 1, downstream 0
   my $slen=length($sstr);
   my $qlen=length($qstr);
   

   if( $slen == 1 )
   {
      my $cnt = 0;
      $cnt = ($qstr =~ s/$sstr//g);
      return $cnt;
   }elsif( $slen > 1 ){
      my $cnts = 0;
      my $currentx = $pstart;

      while ($currentx < ($qlen - $slen + 1))
      {
        my $tmpstr=substr($qstr,$currentx,$slen);
        if($sstr eq $tmpstr)
        {
         $cnts++;
        }
        $currentx = $currentx + $slen ;
      }
      return $cnts;
   }else{
      die "Error unable to find string length of $sstr in $qstr";
   }
}



# each entry in the hash array is Nx1 vector..N = no. of sequences
# initxarrays
sub initxarrays
{
   my $seq1 = shift; #upstream
   my $seq2 = shift; #downstream
   my $numseqlenu = shift; #sequence number
   
   my %abundanceUP=();
   my %abundanceDN=();
   my %codonUP100=();
   my %codonDN100=();

   #initialize abundance and codon
    #monomers,dimers,trimers

      foreach my $dim (@polymers)
      {
        my $a1 = &countoccurences($seq1,$dim);
        $abundanceUP{$dim} = $a1;
      }
      foreach my $dimm (@trimers)
      {
        my $a2=&countnonoverlapoccurences($seq1,$dimm,1);
        $codonUP100{$dimm} = $a2;
      }
         
      my $instring1 = $seq1;
      $instring1 =~ s/\s+//g; # just to be safe
      my @rtmat1 = split("",$instring1);
      my $tl = scalar @rtmat1;
      die "Error RAW feature length mismatch at seq $numseqlenu with upstream length ($tl,100)\n" if ($tl != ($windowlength/2) );
      my $RAWu = join(",",@rtmat1);

      foreach my $dim (@polymers)
      {
        my $a11=&countoccurences($seq2,$dim);
        $abundanceDN{$dim} = $a11;
      }
      foreach my $dimm (@trimers)
      {
        my $a22=&countnonoverlapoccurences($seq2,$dimm,0);
        $codonDN100{$dimm} = $a22;
      }
      
      my $instring2 = $seq2;
      $instring2 =~ s/\s+//g; # just to be safe
      my @rtmat2 = split("",$instring2);
      my $tl2 = scalar @rtmat2;
      die "Error RAW feature length mismatch with upstream length\n" if ($tl != ($windowlength/2) );
      my $RAWd = join(",",@rtmat2);

      return(\%abundanceUP,\%codonUP100,\%abundanceDN,\%codonDN100,$RAWu,$RAWd);
}

# writexdata
sub writeattrib
{
   my $relname = shift;
   my $attributefilename = shift;

      open(ATHANDLE,">$attributefilename") || die "error writing $attributefilename $!";

      print ATHANDLE "\@relation $relname\n\n";
      
      foreach my $dimx (@polymers){
         print ATHANDLE "\@attribute $dimx-au real\n";
      }
      
      foreach my $dimx (@trimers){
              print ATHANDLE "\@attribute $dimx-cu real\n";
      }
      
      foreach my $dimx (@polymers){
              print ATHANDLE "\@attribute $dimx-ad real\n";
      }
      
      foreach my $dimx (@trimers){
              print ATHANDLE "\@attribute $dimx-cd real\n";
      }
      
      for(my $rw=0;$rw < $windowlength; $rw++)
      {
         print ATHANDLE "\@attribute raw$rw {A,C,G,T}\n";
      }

      print ATHANDLE "\@attribute class {positive,negative}\n\n";
      print ATHANDLE "\@data\n\n";
      close ATHANDLE;
}

sub writexdata
{
   my $attributefilename = shift;
   my $classtype = shift;

   my $aUP=shift;
   my $cUP=shift;
   my $aDN=shift;
   my $cDN=shift;
   my $RAWu=shift;
   my $RAWd=shift;

      # write in append mode
      open(ATHANDLE,">>$attributefilename") || die "error append writing $attributefilename $!";

   
#    foreach my $dim (@monomers){
#        printf(STDOUT "%1.4f %1.4f",$pssmdataUP{$dim}->[$i],$pssmdataDN{$dim}->[$i]);
#        }
        foreach my $dimx (@polymers){
           printf(ATHANDLE "%d,",$aUP->{$dimx});
        }
        
        foreach my $dimx (@trimers){
           printf(ATHANDLE "%d,",$cUP->{$dimx});
        }

        foreach my $dimx (@polymers){
           printf(ATHANDLE "%d,",$aDN->{$dimx});
        }
        
        foreach my $dimx (@trimers){
           printf(ATHANDLE "%d,",$cDN->{$dimx});
        }
        
        printf(ATHANDLE "%s,",$RAWu);
        printf(ATHANDLE "%s,",$RAWd);

        printf(ATHANDLE "$classtype\n");

   close ATHANDLE;
}

sub calcstart
{
         #my %heightparams = (
         #               smallsamplecorrection => 1,
         #               input_kind => 1,
         #               stretch => 0
         #               );
         #my ($entropy_dataU, $desc_rU, $kindU,$freqDU) = entropy::getFREQ(\@inputdataUP,\%heightparams);
         #my ($entropy_dataD, $desc_rD, $kindD,$freqDD) = entropy::getFREQ(\@inputdataDN,\%heightparams);

         #my @dataU = @$entropy_dataU;
         #my @dataD = @$entropy_dataD;

         #my %pssmdataUP = %$freqDU;
         #my %pssmdataDN = %$freqDD;
         my $inputfilename = shift;
         my $appendflag = shift;
         my $relname = shift;
         my $featurefilename = shift;
         my $attributefilename = shift;
         my $classtype = shift;
         my $noseqs=0;
         
         open (ATGCFILEHANDLE,"$inputfilename") || die ("Error finding file: $inputfilename\n");
        
         print "writing to file $attributefilename\n";

         &writeattrib($relname,$attributefilename);
         while(<ATGCFILEHANDLE>)
         {
            chomp;

            my $inputdata=$_;
            my $inputdataUP=substr($inputdata,0,100);
            my $inputdataDN=substr($inputdata,103,100);

            my $numseqlenu=length($inputdataUP);
            my $numseqlend=length($inputdataDN);
            die "Mismatch: Upstream = $numseqlenu and Downstream = $numseqlend entries" if( $numseqlenu != $numseqlend);
            my ($abup,$cdup,$abdn,$cddn,$rawu,$rawd)=&initxarrays($inputdataUP,$inputdataDN,$noseqs);
            &writexdata($attributefilename,$classtype,$abup,$cdup,$abdn,$cddn,$rawu,$rawd);
            &print_status($noseqs+1);
            $noseqs++;
         }
         close (ATGCFILEHANDLE);
         return $noseqs;
}

#&calcstart("negative",0,"plasm-1","f-1-negative","plasm-1.arff","negative");
#&calcstart("positive",1,"plasm-1","f-1-positive","plasm-1.arff","positive");

sub makeattribfile
{
   my $tfile = shift; # input sequences 203 length each
   my $relname = shift; # arff file to write
   my $logfile=shift;
   my $initime = time;
   my $starttime;
   my $looptime;

   my (@ts1) = (times,time);
#  my $relname = "plasm-sporoN-dataset";
            
                  my $attributefilename = "$relname.arff";
                  my $featurefilename = "f-$tfile";
                  
                  my $numseqs = &calcstart($tfile,0,$relname,$featurefilename,$attributefilename,"positive");
                  print "Number of sequences = $numseqs\n";
   
                  my @ts3 = (times,time);

                  for(@ts3)
                  {
                     $_ -= shift @ts1;
                  }
                  
                  warn(sprintf "user_time:%.2f s, system_time:%.2f s, (%.2f %.2f), looptime:%d s\n", @ts3);
}




if((not defined $ARGV[0]) or (not defined $ARGV[1]))
{
   my $cmd1 = $0;
   if($cmd1 !~ /\.\//)
   {
    warn "Usage: perl $cmd1 <sequences-file> <output-arff-file>\n";
   }else{
    warn "Usage: $cmd1 <sequences-file> <output-arff-file>\n";
   }
   exit(1);
}

my $tf = $ARGV[0];
my $arfffile = $ARGV[1];
my $logfile="output.log";

&makeattribfile($tf,$arfffile,$logfile);
1;
