<commit_before>#!/usr/bin/env perl

#########################################################################################
#                                       stepCounts.pl
#########################################################################################
# 
#  This program prepares count and summary files in sequential mapping step 
#
#
#########################################################################################
# AUTHORS:
#
# <NAME>, PhD 
# Jul 4, 2014
#########################################################################################


############## LIBRARIES AND PRAGMAS ################

 use List::Util qw[min max];
 use strict;
 use File::Basename;
 use Getopt::Long;
 use Pod::Usage; 

#################### VARIABLES ######################
 my $mapnames         = "";
 my $outdir           = "";
 my $jobsubmit        = "";
 my $indexcmd         = "";
 my $pubdir           = "";
 my $wkey             = "";
 my $bedmake          = "";
 my $gcommondb        = "";
 my $cmd              = ""; 
 my $servicename      = "";
 my $help             = "";
 my $print_version    = "";
 my $version          = "dolphin_counts v1.0.0";

################### PARAMETER PARSING ####################

my $command=$0." ".join(" ",@ARGV); ####command line copy

GetOptions( 
        'mapnames=s'     => \$mapnames,
	'outdir=s'       => \$outdir,
        'cmd=s'          => \$cmd,
        'bedmake=s'      => \$bedmake,
        'indexcmd=s'     => \$indexcmd,
        'gcommondb=s'    => \$gcommondb,
        'pubdir=s'       => \$pubdir,
        'wkey=s'         => \$wkey,
        'servicename=s'  => \$servicename,
        'jobsubmit=s'    => \$jobsubmit,
	'help'           => \$help, 
	'version'        => \$print_version,
) or die("Unrecognized optioins.\nFor help, run this script with -help option.\n");

if($help){
    pod2usage( {
		'-verbose' => 2, 
		'-exitval' => 1,
	} );
}

if($print_version){
  print "Version ".$version."\n";
  exit;
}

pod2usage( {'-verbose' => 0, '-exitval' => 1,} ) if ( ($mapnames eq "") or ($outdir eq "") );	

################### MAIN PROGRAM ####################
#  Prepare count and summary files

my $outd   = "$outdir/counts";
`mkdir -p $outd`;
die "Error 15: Cannot create the directory:".$outd if ($?);
my $puboutdir   = "$pubdir/$wkey";
`mkdir -p $puboutdir`;
die "Error 15: Cannot create the directory:".$puboutdir if ($?);
my ($inputdir, $com)=();

my @mapnames_arr=split(/[,\t\s]+/, $mapnames);
foreach my $mapname (@mapnames_arr)
{ 
  my ($name, $bedfile)=();
  my @arr = split(/:/, $mapname);
  $com="";
  if (scalar(@arr)==2)
  {
  # Custom index, it requires the fasta file in the same directory with index file
     $name=$arr[0];
     $mapname=$name;
     my $ind=$arr[1];
     my $fasta=$ind;
     if(checkFile("$ind.fasta"))
     {
       $fasta.=".fasta";
     }
     elsif(checkFile("$ind.fa"))
     {
       $fasta.=".fa";
     }
     else
     {
      die "Error 64: please check the file: $ind.fa or $ind.fasta "; 
     }
     if(!checkFile("$ind.4.bt2"))
     {
        $com="$indexcmd $fasta $ind"; 
     }
     $bedfile="$ind.bed";
     if(!checkFile($bedfile))
     { 
        $com.=" && " if($com !~/^$/);
        $com.="$bedmake $fasta $name>$bedfile";
     }
  }
  else
  {
    # Common index, it requires the fasta file in the same directory with index file
      $name=$mapname;
      $bedfile="$gcommondb/$mapname/$mapname.bed";
      print "MAPNAME:$mapname # $gcommondb\n\n";
      if ($gcommondb=~/$mapname/) #&& checkFile("$gcommondb/$mapname/piRNA.bed") )
      {
         if (checkFile("$gcommondb/$mapname/piRNAcoor.bed"))
         {
           countCov($mapname, "piRNAcoor", "$gcommondb/$mapname/piRNAcoor.bed" , $outdir, $outd, $cmd, "",$version, $puboutdir, $wkey);
         }
         if (checkFile("$gcommondb/$mapname/miRNAcoor.bed"))
         {
           countCov($mapname, "miRNAcoor", "$gcommondb/$mapname/miRNAcoor.bed" , $outdir, $outd, $cmd, "", $version, $puboutdir, $wkey);
         }
      }
  }

countCov($mapname, $name, $bedfile, $outdir, $outd, $cmd, $com, $version, $puboutdir, $wkey);
}

#Copy count directory to its web accessible area
#`rm -rf $outd/tmp && cp -R $outd $puboutdir`;  
#die "Error 17: Cannot copy the directory:".$puboutdir if ($?);

sub countCov
{
  my ($mapname, $name, $bedfile, $outdir, $outd, $cmd, $precom, $version, $puboutdi, $wkey)=@_; 
  my $inputdir = "$outdir/seqmapping/".lc($mapname);
  print $inputdir."\n";
  my $com=`ls $inputdir/*.sorted.bam 2>&1`;

  print $com."\n";
  if ($com !~/No such file or directory/)
  {<commit_msg>removed comment out from rm and cp line<commit_after>#!/usr/bin/env perl

#########################################################################################
#                                       stepCounts.pl
#########################################################################################
# 
#  This program prepares count and summary files in sequential mapping step 
#
#
#########################################################################################
# AUTHORS:
#
# <NAME>, PhD 
# Jul 4, 2014
#########################################################################################


############## LIBRARIES AND PRAGMAS ################

 use List::Util qw[min max];
 use strict;
 use File::Basename;
 use Getopt::Long;
 use Pod::Usage; 

#################### VARIABLES ######################
 my $mapnames         = "";
 my $outdir           = "";
 my $jobsubmit        = "";
 my $indexcmd         = "";
 my $pubdir           = "";
 my $wkey             = "";
 my $bedmake          = "";
 my $gcommondb        = "";
 my $cmd              = ""; 
 my $servicename      = "";
 my $help             = "";
 my $print_version    = "";
 my $version          = "dolphin_counts v1.0.0";

################### PARAMETER PARSING ####################

my $command=$0." ".join(" ",@ARGV); ####command line copy

GetOptions( 
        'mapnames=s'     => \$mapnames,
	'outdir=s'       => \$outdir,
        'cmd=s'          => \$cmd,
        'bedmake=s'      => \$bedmake,
        'indexcmd=s'     => \$indexcmd,
        'gcommondb=s'    => \$gcommondb,
        'pubdir=s'       => \$pubdir,
        'wkey=s'         => \$wkey,
        'servicename=s'  => \$servicename,
        'jobsubmit=s'    => \$jobsubmit,
	'help'           => \$help, 
	'version'        => \$print_version,
) or die("Unrecognized optioins.\nFor help, run this script with -help option.\n");

if($help){
    pod2usage( {
		'-verbose' => 2, 
		'-exitval' => 1,
	} );
}

if($print_version){
  print "Version ".$version."\n";
  exit;
}

pod2usage( {'-verbose' => 0, '-exitval' => 1,} ) if ( ($mapnames eq "") or ($outdir eq "") );	

################### MAIN PROGRAM ####################
#  Prepare count and summary files

my $outd   = "$outdir/counts";
`mkdir -p $outd`;
die "Error 15: Cannot create the directory:".$outd if ($?);
my $puboutdir   = "$pubdir/$wkey";
`mkdir -p $puboutdir`;
die "Error 15: Cannot create the directory:".$puboutdir if ($?);
my ($inputdir, $com)=();

my @mapnames_arr=split(/[,\t\s]+/, $mapnames);
foreach my $mapname (@mapnames_arr)
{ 
  my ($name, $bedfile)=();
  my @arr = split(/:/, $mapname);
  $com="";
  if (scalar(@arr)==2)
  {
  # Custom index, it requires the fasta file in the same directory with index file
     $name=$arr[0];
     $mapname=$name;
     my $ind=$arr[1];
     my $fasta=$ind;
     if(checkFile("$ind.fasta"))
     {
       $fasta.=".fasta";
     }
     elsif(checkFile("$ind.fa"))
     {
       $fasta.=".fa";
     }
     else
     {
      die "Error 64: please check the file: $ind.fa or $ind.fasta "; 
     }
     if(!checkFile("$ind.4.bt2"))
     {
        $com="$indexcmd $fasta $ind"; 
     }
     $bedfile="$ind.bed";
     if(!checkFile($bedfile))
     { 
        $com.=" && " if($com !~/^$/);
        $com.="$bedmake $fasta $name>$bedfile";
     }
  }
  else
  {
    # Common index, it requires the fasta file in the same directory with index file
      $name=$mapname;
      $bedfile="$gcommondb/$mapname/$mapname.bed";
      print "MAPNAME:$mapname # $gcommondb\n\n";
      if ($gcommondb=~/$mapname/) #&& checkFile("$gcommondb/$mapname/piRNA.bed") )
      {
         if (checkFile("$gcommondb/$mapname/piRNAcoor.bed"))
         {
           countCov($mapname, "piRNAcoor", "$gcommondb/$mapname/piRNAcoor.bed" , $outdir, $outd, $cmd, "",$version, $puboutdir, $wkey);
         }
         if (checkFile("$gcommondb/$mapname/miRNAcoor.bed"))
         {
           countCov($mapname, "miRNAcoor", "$gcommondb/$mapname/miRNAcoor.bed" , $outdir, $outd, $cmd, "", $version, $puboutdir, $wkey);
         }
      }
  }

countCov($mapname, $name, $bedfile, $outdir, $outd, $cmd, $com, $version, $puboutdir, $wkey);
}

#Copy count directory to its web accessible area
`rm -rf $outd/tmp && cp -R $outd $puboutdir`;  
die "Error 17: Cannot copy the directory:".$puboutdir if ($?);

sub countCov
{
  my ($mapname, $name, $bedfile, $outdir, $outd, $cmd, $precom, $version, $puboutdi, $wkey)=@_; 
  my $inputdir = "$outdir/seqmapping/".lc($mapname);
  print $inputdir."\n";
  my $com=`ls $inputdir/*.sorted.bam 2>&1`;

  print $com."\n";
  if ($com !~/No such file or directory/)
  {
{'fraction_non_alphanumeric': 0.2810939874104623, 'fraction_numerical': 0.006511829824180595, 'mean_word_length': 2.792181069958848, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4309877', 'n_tokens_mistral': 3284, 'n_tokens_neox': 3118, 'n_words': 693}