#!/usr/bin/perl
# August 2002; RHWade.
# Kwik.pl updated 2006
# ======================================================================
# Landmark Patterns for PROTEIN sequences
# user provides motifs and motif "names" in one file and a sequence list
# in the another; output to screen.
# the two files :
# 	1) sequence list PearsonFasta format ending with >
# 	2) motif list; name1;motif1;name2;motif2;etc. on separate lines
# ======================================================================
#
   print "\n infile = ";
   $SEQ = <STDIN>;
   chomp $SEQ;
#
   print "\n motif file = ";
   $MOT = <STDIN>;
   chomp $MOT;
#
   $j=0;
   $i=0;
   $k=0;
	open MOT;			# read in motifs
   while(<MOT>) {
   if ( /^#/ ) {
	}
	else {
	chomp;
	$test=$j % 2;
   if ($test > 0) {
	 $motif[$i]=$_;
	 $i=$i+1;	  
	} else {
	 $name[$k]=$_;
	 $k=$k+1;
	}
	 $j=$j+1;
	}
	}

	print "\n"," NAME","          LEN   ",;
	$i=0;
	while($i <= $k) {
      print $name[$i],"  ",;
	$i=$i+1;
	}
      print "\n";
#
	$k=$k-1;
	$result=join("(.*?)",@motif);	# motifs strung together
#	print "\n", $result."\n";
#
	$count=-1;			# read sequences
	open SEQ;
	$otitle="first round";
	while(<SEQ>) {
   if ( /^>/ ) {
      $count=$count+1;
      $ntitle=$_;
      chomp($ntitle);
#      $ntitle2=substr($ntitle,16,5);
      $ntitle2=substr($ntitle,1,11);
	@d=(0,0,0,0,0,0,0,0,0,0);
#
      $_=$s;
# -------------------------------------------------------------------------
# (Nter)@d[0]-name1-@d[1]-name2-@d[2]-name3-@d[2]-name4-@d[3]-name4-etc.           
	s/(.*?)$result//;		# obtain inter-motif counts
	$i=0;
	while ($i <= $k) {
	$a=$i+1;
	@d[$i]=length($$a);
	$i=$i+1;
	}
# -------------------------------------------------------------------------
      if (@d[0] >= 1) {
#
	print "\n"," ", $otitle,"   ",length($s),;
#
	$i=0;
	while($i <= $k) {
	if ($i == 0) {
      print "    ",$d[$i],;
	} else {
      print "    ",$d[$i],;	
	}
	$i=$i+1;
	}
#      print "\n";
#
	} else {
	if ($otitle ne $ntitle2 and $otitle ne "first round") {
     print "\n",$otitle," problem : check sequence"."\n";
      }
	}
#
      $s="";
   } else {
      chomp;
      $s.=$_;
      $otitle=$ntitle2;
   }
    }
      print "\n";
	\n;