#!/usr/bin/perl
use strict;

### NOTE: To determine the 'tip' anchor, look at the last line in the record; however, what if that portion doe snot overlap an exon?
### BETTER: for each line, check whether the 'tip' of the alignment (to if +, from if -) is the portoin that overlaps the intron; if none, simply skip;
## Reverse complement match
#chr1    164746  165913  ERR188081.2415473/2     0       -       164746  165913  0,0,0   2       45,30,  0,1137, chr1    165884  165942  "ENST00000466557.5";    29
#chr1    164746  165913  ERR188081.2415473/2     0       -       164746  165913  0,0,0   2       45,30,  0,1137, chr1    164263  164791  "ENST00000466557.5";    45
#chr1    164746  165913  ERR188081.2415473/2     0       -       164746  165913  0,0,0   2       45,30,  0,1137, chr1    165889  165942  "ENST00000491962.1";    24
#chr1    164746  524509  ERR188081.2415473/2     0       -       164746  524509  0,0,0   2       45,30,  0,359733,       chr1    164263  164791  "ENST00000466557.5";    45
#chr1    165912  168008  ERR188081.38994639/1    0       -       165912  168008  0,0,0   2       30,45,  0,2051, chr1    165884  165942  "ENST00000466557.5";    30
#chr1    165912  168008  ERR188081.38994639/1    0       -       165912  168008  0,0,0   2       30,45,  0,2051, chr1    165889  165942  "ENST00000491962.1";    30
### Forward match
#chr1    827707  829009  ERR188081.2073561/1     50      +       827707  829009  0,0,0   2       68,7,   0,1295, chr1    829003  829104  "ENST00000623808.2";    6
#chr1    827707  829009  ERR188081.2073561/1     50      +       827707  829009  0,0,0   2       68,7,   0,1295, chr1    827608  827775  "ENST00000445118.5";    68
#chr1    827707  829009  ERR188081.2073561/1     50      +       827707  829009  0,0,0   2       68,7,   0,1295, chr1    829003  829104  "ENST00000445118.5";    6
#chr1    827707  829009  ERR188081.2073561/1     50      +       827707  829009  0,0,0   2       68,7,   0,1295, chr1    827657  827775  "ENST00000448975.5";    68
#chr1    827707  829009  ERR188081.2073561/1     50      +       827707  829009  0,0,0   2       68,7,   0,1295, chr1    829003  829104  "ENST00000448975.5";    6
#chr1    827707  829009  ERR188081.2073561/1     50      +       827707  829009  0,0,0   2       68,7,   0,1295, chr1    827661  827775  "ENST00000610067.4";    68
#chr1    827707  829009  ERR188081.2073561/1     50      +       827707  829009  0,0,0   2       68,7,   0,1295, chr1    829003  829104  "ENST00000610067.4";    6
#chr1    827707  829009  ERR188081.2073561/1     50      +       827707  829009  0,0,0   2       68,7,   0,1295, chr1    827661  827775  "ENST00000608189.3";    68
#chr1    827707  829009  ERR188081.2073561/1     50      +       827707  829009  0,0,0   2       68,7,   0,1295, chr1    829003  829104  "ENST00000608189.3";    6


while (<>) {
   # chr1	90937	91012	ERR188081.10684278/1	1	+	90937	91012	0,0,0	1	75,	0,	chr1	89295	91629	"ENST00000466430.4";	75
   /^(\S+)\t(\d+)\t(\d+)\t(\S+)\t\d+\t(\S)\t\d+\t\d+\t\S+\t(\d+)\t(\S+)\t\S+\t\S+\t(\d+)\t(\d+)\t/ or die "died. $_";
   my ($chrom,$rfrom,$rto,$readid,$ori,$nblocks,$blocklens,$afrom,$ato) = ($1,$2,$3,$4,$5,$6,$7,$8,$9);
 
   if (($ori eq "+") && ($afrom<=$rto) && ($rto<=$ato)) { print $_; }
   if (($ori eq "-") && ($afrom<=$rfrom) && ($rfrom<=$ato)) { print $_; }
}

#   if ($nblocks==1) { print $_; next; }
#
#   while (--$nblocks) {
#      $_ = <>; 
#      /^(\S+)\t(\d+)\t(\d+)\t/ or die "died. $_"; 
#      (($1 eq $chrom) && ($2 == $rfrom) && ($3 == $rto)) or die "something wrong at $_";
#   }
#   print $_;
#}
