<commit_before>#include "SWEMBL.h"

struct param get_parameters(char infile[1000], char outfile[1000], char reffile[1000], char wigfile[1000], char contigfile[1000], char compfile[1000], char compoverlapfile[1000], char regoverlapfile[1000], int argc, char **argv)
{
  struct param par;
  par.bg = 0;
  par.posbg = 0.01;
  par.longbg = 0.02;
  par.min_above_bg = 8;
  par.threshold = 5;
  par.seqlength = 0;
  par.qualcutoff = 0;
  par.resultcutoff = 0;
  par.pen_inc = 70;
  par.maxtotal = 0;
  par.fraglength = 0;
  par.ref = 0;
  par.reffraglength = 0;
  par.refseqlength = 0;
  par.refpen = 1;
  par.refthreshold = 0;
  par.refqualcutoff = 0;
  par.killifnoref = 0;
  par.paired = 0;
  par.wig = 0;
  par.contigrows = 200;
  par.comp = 0;
  overlap.print = 0;
  par.proportion = 1;
  par.totalreads = 0;
  par.totalrefreads = 0;
  par.quietmode = 0;
  par.overlapmode = 0;
  par.bootstrap = 0;
  par.binom_p = 1;
  par.newpeakversion=0;
  /*outfile = '';*/
  
  //unsigned int totalreads = 0;
  double relativebg = 0;

  opterr = 0;
  int c;
  int index;
 
  while((c = getopt (argc, argv, "i:o:r:w:a:b:p:P:m:t:j:l:f:q:c:d:s:x:G:n:N:K:R:u:D:AOCBEMSFhezvygTV")) != -1)
   switch(c)
     {
     case 'i': strcpy(infile,optarg); break;
     case 'o': strcpy(outfile,optarg); break;
     case 'r': strcpy(reffile,optarg); break;
     case 'w': strcpy(wigfile,optarg); break;
     case 'a': strcpy(compfile,optarg); break;
     case 'A': strcpy(filemode,"a"); break;
     case 'b': par.bg = atof(optarg); break;
     case 'p': par.posbg = atof(optarg); break;
     case 'P': par.longbg = atof(optarg); break;
     case 'm': par.min_above_bg = atoi(optarg); break;
     case 't': par.threshold = atof(optarg); break;
     case 'j': par.refthreshold = atof(optarg); break;
     case 'l': par.seqlength = atoi(optarg); break;
     case 'f': par.fraglength = atoi(optarg); break;
     case 'N': par.totalreads = atoi(optarg); break;
     case 'K': par.totalrefreads = atoi(optarg); break;
     case 'R': relativebg = atof(optarg); break;
       /*case 'C': countfile = 1; break;
	 case 'B': bedfile = 1; break;
	 case 'E': elandfile = 1; break;
	 case 'M': maqfile = 1; break;*/
     case 'C': strcpy(filetype,"C"); break;
     case 'B': strcpy(filetype,"B"); break;
     case 'E': strcpy(filetype,"E"); break;
     case 'M': strcpy(filetype,"M"); break;
     case 'S': strcpy(filetype,"S"); break;
     case 'F': strcpy(filetype,"F"); break;
     case 'q': par.qualcutoff = atof(optarg);  break;
     case 'c': par.resultcutoff = atof(optarg); break;
     case 'd': par.pen_inc = atoi(optarg); break;
     case 'n': par.maxtotal = atof(optarg); break;
     case 'x': par.refpen = atof(optarg); break;       
     case 'z': zip = 1; break;
     case 'e': par.paired = 1; break;
     case 'V': par.newpeakversion = 1; break;
     case 's': strcpy(contigfile,optarg); break;
     case 'G': par.contigrows = atoi(optarg); break;
     case 'O': overlap.print = 1; break; 
     case 'u': par.proportion = atof(optarg); break;
     case 'y': par.quietmode = 1; break;
     case 'g': par.overlapmode = 1; break;
     case 'T': par.bootstrap = 1; break;
     case 'D': rseed = atoi(optarg); break;
     case 'v': printf("SWEMBL version 3.6\n"); exit (0);
     case 'h': print_help();
     case '?': 
       if (optopt == 'i')
	 fprintf(stderr, "Option -%c requires an argument.\n", optopt);
       else if (isprint(optopt))
	 fprintf(stderr, "Unknown option '-%c'.\n", optopt);
       else
	 fprintf(stderr,"Unknown option character '\\x%x'.\n", optopt);
       exit(1);
     default: 
       abort();
     }

 for (index = optind; index < argc; index++)
   printf ("Non-option argument %s\n", argv[index]);

 //If Input file is not specified, print help, then exit.

 if(!strcmp(infile,""))
   {
     print_help();
   }

 if(!strcmp(outfile,""))
    {
      strcpy(outfile,infile);
      strcat(outfile, ".SWEMBL.3.6.txt");
    }

 if(strcmp(reffile,""))
   {
     par.ref = 1;
     if(!par.refthreshold) { par.refthreshold = par.threshold; }
   }

 if(strcmp(wigfile,""))
   {
     par.wig = 1;
   }

 if(strcmp(compfile,""))
   {
     par.comp = 1;
   }

 if(!strcmp(filetype,""))
   {
     par.overlapmode ? strcpy(filetype,"B") : strcpy(filetype,"F");
   }

 if(zip && !strcmp(filetype,"F"))
   {
     printf("Cannot open a gzipped BAM file!\n");
     exit(1);
   }

 if(!par.comp)
   {
     overlap.print = 0;
   }

 if(overlap.print)
   {    
     strcpy(compoverlapfile, outfile);
     strcat(compoverlapfile, ".overlapscomp");     
     strcpy(regoverlapfile, outfile);
     strcat(regoverlapfile, ".overlapsregion");
   } 
 
 if(par.proportion > 1 || par.proportion <= 0)
   {
     printf("Invalid proportion entered : %f !\n", par.proportion);
     exit(1);
   }

 par.refpen *= par.proportion;

 if(relativebg && !par.totalreads)
   {
     par.totalreads = count_sample_lines(&par, infile, 1);
   }

 if(par.ref && relativebg && !par.totalrefreads)
   {
     par.totalrefreads = count_sample_lines(&par, reffile, 0);
   }

 if(relativebg && (par.totalreads <= 0 || (par.ref && par.totalrefreads <= 0)))
   {
     printf("Number of reads must be greater than zero.\n");
     exit(1);
   }
 
 if(par.totalreads && relativebg)
   {
     par.posbg = relativebg * par.totalreads * par.proportion / 3000000;
     par.longbg = par.posbg * 5;
     par.maxtotal = relativebg * 300 * par.proportion;
     par.min_above_bg = ((relativebg * par.totalreads * par.proportion / 7500) > 6) ? ceil(relativebg * par.totalreads *par.proportion / 7500) : 6;
     if(par.ref)
       {
	 par.refpen *= ((double)par.totalreads /(double)par.totalrefreads);
	 par.refthreshold /= par.refpen;
       }
   }

 if(par.overlapmode)
   {
     par.posbg = 0.9;
     par.longbg = 100;
     par.min_above_bg = 1; 
     par.paired = 1;
     overlap.mode = 1;
   }

 return(par);
}

void print_help()
{
  printf("\nProgram to call peaks from genomic and other data.\n\nMandatory command line options:\n-i Path to input file\n\nOptional command line options taking no arguments:\n-e Paired end reads\n-V New peak version, extending peaks only as far as evidence permits\n-M MAQ format [default]\n-B BED format\n-E ELAND format\n-S SAM format\n-F BAM format\n-C count format\n-z File is gzipped\n-O Create files of nearest features with comparison file (-a)\n-g Overlap mode for two bed files\n-T Bootstrap input\n-y Quiet mode, only print warnings\n-A Append output file\n-v Print version number and exit\n-h This help page\n\nOptional command line options taking arguments [defaults in square brackets]:\n-o Path to output file [Input file.'.SWEMBL.3.6.txt']\n-r Path to reference (Input) file\n-w Path to wiggle track file\n-a Path to comparison file\n-s Path to coordinate file\n-G Number of rows in coordinate file [200]\n-b Penalty applied to read count [0]\n-p Penalty applied to a gap of one base pair [0.01]\n-l Read length [calculated from first read in file for BED and MAQ formats]\n-f Fragment length for extending fragments [Read length]\n-d Gap at which penalty per base pair increases [70]\n-P Penalty per base pair applied after gap specifed above [0.02]\n-t Threshold value for number of sample reads starting at one base pair [5]\n-j Threshold value for number of reference reads starting at one base pair [0 = -t value]\n-q Mapping quality filter value (MAQ only) [0]\n-m Minimum read count in a peak [8]\n-c Minimum score for a peak [0]\n-n Maximum score threshold [0 = infinite]\n-N Number of input reads [0]\n-R Relative background (depends on -N) [0]\n-K Number of reference reads [0]\n-x Penalty factor for reference reads [1]\n-u Proportion of reads sampled [1]\n-D Random seed (only used for sampling and bootstrapping) [time]\n\n"); exit(1);
}


void open_files(int ref, int wig, int comp, char *infile, char *outfile, char *reffile, char *wigfile, char *contigfile, char *compfile, char *compoverlapfile, char *regoverlapfile)
{
  if(zip)
    {
      if((in_fp = gzopen(infile, "r")) == NULL)
	{
	  printf("\n\n*** %s does not exist ***\n", infile);
	  exit(1);<commit_msg>Can now overwrite individual -R parameters.<commit_after>#include "SWEMBL.h"

struct param get_parameters(char infile[1000], char outfile[1000], char reffile[1000], char wigfile[1000], char contigfile[1000], char compfile[1000], char compoverlapfile[1000], char regoverlapfile[1000], int argc, char **argv)
{
  struct param par;
  par.bg = 0;
  par.posbg = 0;
  par.longbg = 0;
  par.min_above_bg = 0;
  par.threshold = 5;
  par.seqlength = 0;
  par.qualcutoff = 0;
  par.resultcutoff = 0;
  par.pen_inc = 70;
  par.maxtotal = -1;
  par.fraglength = 0;
  par.ref = 0;
  par.reffraglength = 0;
  par.refseqlength = 0;
  par.refpen = 1;
  par.refthreshold = 0;
  par.refqualcutoff = 0;
  par.killifnoref = 0;
  par.paired = 0;
  par.wig = 0;
  par.contigrows = 200;
  par.comp = 0;
  overlap.print = 0;
  par.proportion = 1;
  par.totalreads = 0;
  par.totalrefreads = 0;
  par.quietmode = 0;
  par.overlapmode = 0;
  par.bootstrap = 0;
  par.binom_p = 1;
  par.newpeakversion=0;
  /*outfile = '';*/
  
  //unsigned int totalreads = 0;
  double relativebg = 0;

  opterr = 0;
  int c;
  int index;
 
  while((c = getopt (argc, argv, "i:o:r:w:a:b:p:P:m:t:j:l:f:q:c:d:s:x:G:n:N:K:R:u:D:AOCBEMSFhezvygTV")) != -1)
   switch(c)
     {
     case 'i': strcpy(infile,optarg); break;
     case 'o': strcpy(outfile,optarg); break;
     case 'r': strcpy(reffile,optarg); break;
     case 'w': strcpy(wigfile,optarg); break;
     case 'a': strcpy(compfile,optarg); break;
     case 'A': strcpy(filemode,"a"); break;
     case 'b': par.bg = atof(optarg); break;
     case 'p': par.posbg = atof(optarg); break;
     case 'P': par.longbg = atof(optarg); break;
     case 'm': par.min_above_bg = atoi(optarg); break;
     case 't': par.threshold = atof(optarg); break;
     case 'j': par.refthreshold = atof(optarg); break;
     case 'l': par.seqlength = atoi(optarg); break;
     case 'f': par.fraglength = atoi(optarg); break;
     case 'N': par.totalreads = atoi(optarg); break;
     case 'K': par.totalrefreads = atoi(optarg); break;
     case 'R': relativebg = atof(optarg); break;
       /*case 'C': countfile = 1; break;
	 case 'B': bedfile = 1; break;
	 case 'E': elandfile = 1; break;
	 case 'M': maqfile = 1; break;*/
     case 'C': strcpy(filetype,"C"); break;
     case 'B': strcpy(filetype,"B"); break;
     case 'E': strcpy(filetype,"E"); break;
     case 'M': strcpy(filetype,"M"); break;
     case 'S': strcpy(filetype,"S"); break;
     case 'F': strcpy(filetype,"F"); break;
     case 'q': par.qualcutoff = atof(optarg);  break;
     case 'c': par.resultcutoff = atof(optarg); break;
     case 'd': par.pen_inc = atoi(optarg); break;
     case 'n': par.maxtotal = atof(optarg); break;
     case 'x': par.refpen = atof(optarg); break;       
     case 'z': zip = 1; break;
     case 'e': par.paired = 1; break;
     case 'V': par.newpeakversion = 1; break;
     case 's': strcpy(contigfile,optarg); break;
     case 'G': par.contigrows = atoi(optarg); break;
     case 'O': overlap.print = 1; break; 
     case 'u': par.proportion = atof(optarg); break;
     case 'y': par.quietmode = 1; break;
     case 'g': par.overlapmode = 1; break;
     case 'T': par.bootstrap = 1; break;
     case 'D': rseed = atoi(optarg); break;
     case 'v': printf("SWEMBL version 3.6\n"); exit (0);
     case 'h': print_help();
     case '?': 
       if (optopt == 'i')
	 fprintf(stderr, "Option -%c requires an argument.\n", optopt);
       else if (isprint(optopt))
	 fprintf(stderr, "Unknown option '-%c'.\n", optopt);
       else
	 fprintf(stderr,"Unknown option character '\\x%x'.\n", optopt);
       exit(1);
     default: 
       abort();
     }

 for (index = optind; index < argc; index++)
   printf ("Non-option argument %s\n", argv[index]);

 //If Input file is not specified, print help, then exit.

 if(!strcmp(infile,""))
   {
     print_help();
   }

 if(!strcmp(outfile,""))
    {
      strcpy(outfile,infile);
      strcat(outfile, ".SWEMBL.3.6.txt");
    }

 if(strcmp(reffile,""))
   {
     par.ref = 1;
     if(!par.refthreshold) { par.refthreshold = par.threshold; }
   }

 if(strcmp(wigfile,""))
   {
     par.wig = 1;
   }

 if(strcmp(compfile,""))
   {
     par.comp = 1;
   }

 if(!strcmp(filetype,""))
   {
     par.overlapmode ? strcpy(filetype,"B") : strcpy(filetype,"F");
   }

 if(zip && !strcmp(filetype,"F"))
   {
     printf("Cannot open a gzipped BAM file!\n");
     exit(1);
   }

 if(!par.comp)
   {
     overlap.print = 0;
   }

 if(overlap.print)
   {    
     strcpy(compoverlapfile, outfile);
     strcat(compoverlapfile, ".overlapscomp");     
     strcpy(regoverlapfile, outfile);
     strcat(regoverlapfile, ".overlapsregion");
   } 
 
 if(par.proportion > 1 || par.proportion <= 0)
   {
     printf("Invalid proportion entered : %f !\n", par.proportion);
     exit(1);
   }

 par.refpen *= par.proportion;

 if(relativebg && !par.totalreads)
   {
     par.totalreads = count_sample_lines(&par, infile, 1);
   }

 if(par.ref && relativebg && !par.totalrefreads)
   {
     par.totalrefreads = count_sample_lines(&par, reffile, 0);
   }

 if(relativebg && (par.totalreads <= 0 || (par.ref && par.totalrefreads <= 0)))
   {
     printf("Number of reads must be greater than zero.\n");
     exit(1);
   }
 
 if(par.totalreads && relativebg)
   {
     par.posbg = (par.posbg == 0) ? relativebg * par.totalreads * par.proportion / 3000000 : par.posbg;
     par.longbg = (par.longbg == 0) ? relativebg * par.totalreads * par.proportion / 600000 : par.longbg;
     par.maxtotal = (par.maxtotal == -1) ? relativebg * 300 * par.proportion : par.maxtotal;
     par.min_above_bg = (par.min_above_bg == 0) ? (((relativebg * par.totalreads * par.proportion / 7500) > 6) ? ceil(relativebg * par.totalreads *par.proportion / 7500) : 6) : par.min_above_bg;
     if(par.ref)
       {
	 par.refpen *= ((double)par.totalreads /(double)par.totalrefreads);
	 par.refthreshold /= par.refpen;
       }
   }
 else
   {
     par.posbg = (par.posbg == 0) ? 0.01 : par.posbg;
     par.longbg = (par.longbg == 0) ? 0.02 : par.longbg;
     par.maxtotal = (par.maxtotal == -1) ? 0 : par.maxtotal;
     par.min_above_bg = (par.min_above_bg == 0) ? 8 : par.min_above_bg;
   }

 if(par.overlapmode)
   {
     par.posbg = 0.9;
     par.longbg = 100;
     par.min_above_bg = 1; 
     par.paired = 1;
     overlap.mode = 1;
   }

 return(par);
}

void print_help()
{
  printf("\nProgram to call peaks from genomic and other data.\n\nMandatory command line options:\n-i Path to input file\n\nOptional command line options taking no arguments:\n-e Paired end reads\n-V New peak version, extending peaks only as far as evidence permits\n-M MAQ format [default]\n-B BED format\n-E ELAND format\n-S SAM format\n-F BAM format\n-C count format\n-z File is gzipped\n-O Create files of nearest features with comparison file (-a)\n-g Overlap mode for two bed files\n-T Bootstrap input\n-y Quiet mode, only print warnings\n-A Append output file\n-v Print version number and exit\n-h This help page\n\nOptional command line options taking arguments [defaults in square brackets]:\n-o Path to output file [Input file.'.SWEMBL.3.6.txt']\n-r Path to reference (Input) file\n-w Path to wiggle track file\n-a Path to comparison file\n-s Path to coordinate file\n-G Number of rows in coordinate file [200]\n-b Penalty applied to read count [0]\n-p Penalty applied to a gap of one base pair [0.01]\n-l Read length [calculated from first read in file for BED and MAQ formats]\n-f Fragment length for extending fragments [Read length]\n-d Gap at which penalty per base pair increases [70]\n-P Penalty per base pair applied after gap specifed above [0.02]\n-t Threshold value for number of sample reads starting at one base pair [5]\n-j Threshold value for number of reference reads starting at one base pair [0 = -t value]\n-q Mapping quality filter value (MAQ only) [0]\n-m Minimum read count in a peak [8]\n-c Minimum score for a peak [0]\n-n Maximum score threshold [0 = infinite]\n-N Number of input reads [0]\n-R Relative background (depends on -N) [0]\n-K Number of reference reads [0]\n-x Penalty factor for reference reads [1]\n-u Proportion of reads sampled [1]\n-D Random seed (only used for sampling and bootstrapping) [time]\n\n"); exit(1);
}


void open_files(int ref, int wig, int comp, char *infile, char *outfile, char *reffile, char *wigfile, char *contigfile, char *compfile, char *compoverlapfile, char *regoverlapfile)
{
  if(zip)
    {
      if((in_fp = gzopen(infile, "r")) == NULL)
	{
	  printf("\n\n*** %s does not exist ***\n", infile);
	  exit(1);
{'fraction_non_alphanumeric': 0.15576436573906757, 'fraction_numerical': 0.020298759185640284, 'mean_word_length': 3.1352428393524283, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, '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': '2411694', 'n_tokens_mistral': 6439, 'n_tokens_neox': 5976, 'n_words': 1649}