#!/usr/bin/perl
#bed.pl refFlat.txt

use strict;
use warnings;

my $file=shift;

open(FILE,$file) or die;
open(RESULT,'>bed-'.$file) or die;

while(<FILE>){
	chomp;
	my @row=split /\t/;
	if($row[2]=~/_/){
	}else{
		my $start=$row[4]-2;
		my $end=$row[5]+2;
		my $type;
		if(($row[7]-$row[6])>0){
			$type='mRNA';
			print RESULT "$row[2]\t$start\t$end\t$row[1]\t$row[8]\t$row[3]\t$_\t$type\n";
		}elsif($row[6]==$row[7]){
			$type='lncRNA';
			print RESULT "$row[2]\t$start\t$end\t$row[1]\t$row[8]\t$row[3]\t$_\t$type\n";
		}
	}
}

close(FILE);
close(RESULT); 
