<commit_before>#!/usr/local/ensembl/bin/perl  -w

############################################################
# script to run the ClusterMerge Algorithm (<NAME>)
#
# written by <NAME> (<EMAIL>)
#
# You may distribute this module under the same terms as perl itself
#
############################################################

use strict;
use Getopt::Long;
use Bio::EnsEMBL::Pipeline::Runnable::ClusterMerge;
use Bio::EnsEMBL::Transcript;
use Bio::EnsEMBL::Pipeline::Tools::TranscriptUtils;
use Bio::EnsEMBL::Exon;

my $gff_format = 1;
my $input;
my $output;

############################################################
# deafult options

my $comp_level        = 5;
my $splice_mismatch   = 10;
my $intron_mismatch   = 0;
my $min_order         = 1;
my $internal_splice_overlap = 0;
my $sets;
my $help;

############################################################

&GetOptions( 
	    #'gff_format:s'        => \$gff_format,
	     'input:s'                   => \$input,
	     'output:s'                  => \$output,
	     'comp_level:n'              => \$comp_level,
	     'splice_mismatch:n'         => \$splice_mismatch,
	     'intron_mismatch:n'         => \$intron_mismatch,
	     'internal_splice_overlap:n' => \$internal_splice_overlap,
	     'sets'                      => \$sets,
	     'help'                      => \$help,
	     );

if ( $help || !$input || !$output){
  &usage;
  exit(0);
}

  
############################################################

open ( IN, "<$input") || die("could not open input file $input");

#my $gff2exon = Bio::EnsEMBL::Utils::GFF2Exon->new( -gff_version => $gff_format );

my @transcripts;
my $trans_tag;
my %trans;

INPUT:
while(<IN>){
  
  ############################################################
  # parse gff
  #print STDERR "gff: $_";
  #chomp;
  
  chomp;
  my %trans_tag;
  my $exon = &exon_from_gff( $_ , \%trans_tag );
  #print STDERR "trans_tag[ exon ] = $trans_tag{$exon}\n";
  if ( $exon && $exon->start && $exon->end ){
    push( @{$trans{$trans_tag{$exon}}}, $exon );
  }
}

foreach my $tag ( keys %trans ){
  my $transcript = Bio::EnsEMBL::Transcript->new();
  $transcript->dbID( $tag );
  foreach my $exon ( @{ $trans{$tag} } ){
    $transcript->add_Exon($exon);
  }
  push( @transcripts, $transcript );
}

#print STDERR "transcripts created\n";
#foreach my $t ( @transcripts ){
#    print STDERR "transcript: $t\n";
#    Bio::EnsEMBL::Pipeline::Tools::TranscriptUtils->_print_Transcript($t);
#}


############################################################

print STDERR "running ClusterMerge with parameters:\n";
print STDERR "comp_level              = $comp_level\n";
print STDERR "splice_mismatch         = $splice_mismatch\n";
print STDERR "intron_mismatch         = $intron_mismatch\n"; 
print STDERR "min_order               = $min_order\n";
print STDERR "internal_splice_overlap = $internal_splice_overlap\n";

############################################################

my $cluster_merge = 
    Bio::EnsEMBL::Pipeline::Runnable::ClusterMerge->new(
							-transcripts                   => \@transcripts,
							-comparison_level              => $comp_level,
							-splice_mismatch               => $splice_mismatch,
							-intron_mismatch               => $intron_mismatch,
							-minimum_order                 => $min_order,
							-internal_splice_overlap       => $internal_splice_overlap,
						       );

  


$cluster_merge->run;

open ( OUT, ">$output") || die("could not open input file $output");

############################################################
# can retrieve the non-redundant sets:
if ( $sets ){
  
  # list of listrefs, each one cointaining a list of transcript objects
  my @sets = $cluster_merge->sub_clusters;
  
  my $count = 0;
  foreach my $set ( @sets ){
    $count++;
    print OUT "set $count:\n";
    foreach my $transcript ( @$set ){
      foreach my $exon ( @{$transcript->get_all_Exons} ){
	print OUT &gff_string($exon,$transcript);
      }
    }
  } 
}

############################################################
# or the merged transcripts:
else{<commit_msg>latest version with commented out timing<commit_after>#!/usr/local/ensembl/bin/perl  -w

############################################################
# script to run the ClusterMerge Algorithm (<NAME>)
#
# written by <NAME> (<EMAIL>)
#
# You may distribute this module under the same terms as perl itself
#
############################################################

use strict;
use Getopt::Long;
use Bio::EnsEMBL::Pipeline::Runnable::ClusterMerge;
use Bio::EnsEMBL::Transcript;
use Bio::EnsEMBL::Pipeline::Tools::TranscriptUtils;
use Bio::EnsEMBL::Exon;

my $gff_format = 1;
my $input;
my $output;

############################################################
# deafult options

my $comp_level        = 5;
my $splice_mismatch   = 10;
my $intron_mismatch   = 0;
my $min_order         = 1;
my $internal_splice_overlap = 0;
my $sets;
my $help;

############################################################

&GetOptions( 
	    #'gff_format:s'        => \$gff_format,
	     'input:s'                   => \$input,
	     'output:s'                  => \$output,
	     'comp_level:n'              => \$comp_level,
	     'splice_mismatch:n'         => \$splice_mismatch,
	     'intron_mismatch:n'         => \$intron_mismatch,
	     'internal_splice_overlap:n' => \$internal_splice_overlap,
	     'sets'                      => \$sets,
	     'help'                      => \$help,
	     );

if ( $help || !$input || !$output){
  &usage;
  exit(0);
}

  
############################################################

open ( IN, "<$input") || die("could not open input file $input");

#my $gff2exon = Bio::EnsEMBL::Utils::GFF2Exon->new( -gff_version => $gff_format );

my @transcripts;
my $trans_tag;
my %trans;

INPUT:
while(<IN>){
  
  ############################################################
  # parse gff
  #print STDERR "gff: $_";
  #chomp;
  
  chomp;
  my %trans_tag;
  my $exon = &exon_from_gff( $_ , \%trans_tag );
  #print STDERR "trans_tag[ exon ] = $trans_tag{$exon}\n";
  if ( $exon && $exon->start && $exon->end ){
    push( @{$trans{$trans_tag{$exon}}}, $exon );
  }
}

foreach my $tag ( keys %trans ){
  my $transcript = Bio::EnsEMBL::Transcript->new();
  $transcript->dbID( $tag );
  foreach my $exon ( @{ $trans{$tag} } ){
    $transcript->add_Exon($exon);
  }
  push( @transcripts, $transcript );
}

#print STDERR "transcripts created\n";
#foreach my $t ( @transcripts ){
#    print STDERR "transcript: $t\n";
#    Bio::EnsEMBL::Pipeline::Tools::TranscriptUtils->_print_Transcript($t);
#}


############################################################

print STDERR "running ClusterMerge with parameters:\n";
print STDERR "comp_level              = $comp_level\n";
print STDERR "splice_mismatch         = $splice_mismatch\n";
print STDERR "intron_mismatch         = $intron_mismatch\n"; 
print STDERR "min_order               = $min_order\n";
print STDERR "internal_splice_overlap = $internal_splice_overlap\n";

############################################################

my $cluster_merge = 
    Bio::EnsEMBL::Pipeline::Runnable::ClusterMerge->new(
							-transcripts                   => \@transcripts,
							-comparison_level              => $comp_level,
							-splice_mismatch               => $splice_mismatch,
							-intron_mismatch               => $intron_mismatch,
							-minimum_order                 => $min_order,
							-internal_splice_overlap       => $internal_splice_overlap,
						       );

  

#my $t1 = time;
$cluster_merge->run;
#my $t2 = time;
#print STDERR "TIME TO RUN CLUSTERMERGE: ".($t2-$t1)." *********************\n";

open ( OUT, ">$output") || die("could not open input file $output");

############################################################
# can retrieve the non-redundant sets:
if ( $sets ){
  
  # list of listrefs, each one cointaining a list of transcript objects
  my @sets = $cluster_merge->sub_clusters;
  
  my $count = 0;
  foreach my $set ( @sets ){
    $count++;
    print OUT "set $count:\n";
    foreach my $transcript ( @$set ){
      foreach my $exon ( @{$transcript->get_all_Exons} ){
	print OUT &gff_string($exon,$transcript);
      }
    }
  } 
}

############################################################
# or the merged transcripts:
else{
{'fraction_non_alphanumeric': 0.27866682709661894, 'fraction_numerical': 0.003128384069305739, 'mean_word_length': 3.4189261031366294, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7530312', 'n_tokens_mistral': 2918, 'n_tokens_neox': 2645, 'n_words': 554}