#!/usr/bin/perl -w

use strict;


my $seq="";
my @v;
my $o=0;
my @t;
my $trozo;
my $p;
my $i;
my $r;
my $z;
my $fichero=$ARGV[0];
my $s=0;

$i=0;
$r=0;


open(OUTPUT,"> output.txt");

open (ADRI," < $fichero");

while (<ADRI>) {
    chomp $_;

    if ($_ =~ /^>/) {
	$s=$s + 1;

	if (!($seq eq "")) {
	    
	    
	    my $m=0;
	    $r=0;
	    @v = split(//,$seq);
	    
	    while ($m < scalar(@v)) {
		
		 $trozo = substr ($seq,$m);
		

		if ($trozo =~ m/^($ARGV[1])/i) {
		    
	      
		    my $p= $m + length($1);
		    $r=$r + 1;
		    if ($r == 1){
			$i= $i + 1;
			$z = $o - 1;
		    }
			print OUTPUT "$t[$z]\t";
			         
			print OUTPUT scalar(@v) . "\t";      #### total
		    	    
		    print OUTPUT "$m\t";
		    print OUTPUT "$1\t";
                    print OUTPUT "$p\n";  
		}
		$m =$m + 1;
	    }
	    
	}
	
	
	$t[$o]= $_;
	$o= $o + 1;
	$seq="";    
}
    else {
	$seq =$seq . $_;
    }
}

	

my $m=0;
$r=0;	    
	    @v = split(//,$seq);
	    
	    while ($m < scalar(@v)) {
		
		 $trozo = substr ($seq,$m);
		

		if ($trozo =~ m/^($ARGV[1])/i) {
		    		 
		  
		    $p= $m + length($1);
		   
		 $r= $r + 1;
		    if ($r == 1) {
			$i= $i + 1;
			$z = $o - 1;		 
		    }
		        print OUTPUT "$t[$z]\t"; 

		    print OUTPUT scalar(@v) . "\t";
		    print OUTPUT "$m\t";
		    print OUTPUT "$1\t";
                    print OUTPUT "$p\n"; 
		
		}
		$m =$m +1;
	    }






print OUTPUT "numero de secuencias introducidas con el motivo =$i \n";
print OUTPUT "numero total de secuencias introducidas= $s\n";
    close (ADRI);

close(OUTPUT);
