#for the results from paired end
#The two inputs are the results from the two ends
use strict;
my $read_size = $ARGV[2];
open(input1, $ARGV[0]);
open(input2, $ARGV[1]);
open(output, ">$ARGV[0].fragsize");
#open(fusefile,">$ARGV[0].fuse");

#my $LongMarker="L";
#my $ShortMarker="S";

while(my $line1=<input1>)
{
	my $line2=<input2>;
	chomp($line1);
	chomp($line2);
	next if($line1=~/$\NM/ or $line2=~/$\NM/ or $line1=~/$\MT/ or $line2=~/$\MT/);
	my @array1 = split("\t",$line1);
	#my $read_size=length($array1[1]);
	my @array2 = split("\t",$line2);
	my $match1=$array1[3];
	my $match2=$array2[3];
#	my $marker=$LongMarker.$ShortMarker;
	my @sizes=();
	#while($match1=~/\/(\S[^,]*\[[$marker]\])\S[^,]*:(\d*)[RF]/g)
	while($match1=~/\/(\S[^,]*\[\w+\])\S[^,]*:(\d*)[RF]/g)
	{
		my $name=$1;
		my $posa=$2;
		#print $name,"\n";
		
		if($match2=~/\Q$name\E\S[^,]*:(\d*)[RF]/)
		{
			#print "match\n";
			my $posb=$1;
			push @sizes, abs($posb-$posa)+$read_size;
			
		}
		
	}
	my %saw;
	@saw{@sizes}=();
	my @keya= keys %saw;
	#print scalar(@keya),"\n";
	if(scalar(@keya)==1)
	{
		print output $keya[0],"\n";
	}

}
#close(fusefile);
close(output);
close(input2);
close(input1);
