<commit_before>/*  -*- c-file-style:"linux" c-basic-offset:3 tab-width:3 -*-  */
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <math.h>
#include <string.h>

#define ORDER 0

const char usage[]= "\nUsage: [-b bases] input.gbk [start end [window_size step [period_of_frames]]]\nDefaults: start= 1; end= end of genome sequence; window_size= 201; step= 51; period_of_frames= 3.\n";


int mapfile(char* filename, char** addr, size_t* length) {
   struct stat sb;
   int fd;

   fd = open(filename, O_RDONLY);
   if (fd == -1) {
      fprintf(stderr, "ERROR: Error opening '%s': ", filename);
      perror(NULL);
      return 1;
   }

   if (fstat (fd, &sb) == -1) {
      perror("fstat");
      return 1;
   }

   if (!S_ISREG (sb.st_mode)) {
      fprintf (stderr, "%s is not a file\n", filename);
      return 1;
   }
   *length = sb.st_size;
   *addr = mmap (NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
   if (*addr == MAP_FAILED) {
      perror ("mmap");
      return 1;
   }

   if (close (fd) == -1) {
      perror ("close");
      return 1;
   }
   return 0;
}

void calculateProfile(const char* origin, const size_t length,
                      const char* bases, const int start, const int end,
                      const int window, const int step, const int period) {

   char *box, base;
   int offset =0 , j, baseidx=0, n=0;
   size_t *S;
   double normfactor = 100.0/(double)(window/period);

   fprintf(stderr,
           "Calculating [%s]-profile of bases %d-%d with window %d nt, step %d nt and period %d nt.\n",
           bases, start, end, window, step, period);

   box = (char *)calloc(window, sizeof(char));
   S = (size_t *)calloc(period, sizeof(size_t));


   /* skip up till start, but don't move offset passed start. */
   while (offset < length) {
      base = origin[offset];
      if(base >= 'a' && base <= 'z') {
         if(baseidx + 1 == start)
            break;
         baseidx++;
      }
      offset++;
   }

   for(; offset < length && baseidx < end; offset++) {
      base = origin[offset];
      if(base >= 'a' && base <= 'z') {
         ++baseidx;
         if(strchr(bases, base)) {
            if(! box[n % window]) {
               //if the flag wasn't set, then set it and increase the count.
               ++S[(baseidx-1) % period];
               box[n % window] = 1;
            }
         }
         else {
            if(box[n % window]) {
               //if the flag was set, clear it and decrease the count.
               --S[(baseidx-1) % period];
               box[n % window] = 0;
            }

            //unexpected base, print warning.
            if(! strchr("actgu", base))
               fprintf(stderr,"\nBase %c found at position %d\n",base,baseidx);
         }
         ++n;

         if(n >= window && ((n-window) % step) == 0 && baseidx < end) {
            fprintf(stdout, "%8d", baseidx-window/2);
            for(j=0; j < period; ++j)
               fprintf(stdout, "%8.1f", normfactor*S[j]);
            fprintf(stdout,"\n");
         }
      }
   }
   free(box);
   free(S);
   fprintf(stderr,"Bases %d-%d read (%d nt)\n", start, end, n);
}

int countBases(const char* origin, const size_t length) {
   /* how many bases from there */
   int i, tot=0;
   for(i=0; i < length; i++)
      if(origin[i] >= 'a' && origin[i] <= 'z')
         ++tot;
   return tot;
}

int main(int argc, char *argv[]) {
   int start=1,end=0,window=201,step=51,period=3,tot=0;
   int i;
   char* mapped_file;
   char* origin;
   size_t length, olength;
   int argi = 1;
   char* filename;
   char* bases = "cg";


   if(strcmp(argv[argi], "-b") == 0) {
      bases = (char*) argv[++argi];
      for (i = 0; bases[i]; i++)
         if (bases[i] < 'a')
            bases[i] = bases[i] + 'a' - 'A';
      argi++;
   }
   filename = argv[argi++];


   if ( mapfile(filename, &mapped_file, &length) )
      exit(1);


   fprintf(stderr, "Searching for coding sequence in '%s'... ", filename);
   /* Skip until we see ORIGIN (the line that starts the coding seq)
    * If the file doesn't contain ORIGIN this may raise a segfault;
    * but at that point exiting with error is all we can do anyways.
    */
   origin = strstr(mapped_file, "\nORIGIN");
   /* then save a pointer at the start of the next line */
   origin = strchr(origin + strlen("\nORIGIN"), '\n') + 1;<commit_msg>Added translation of upper case into lower case in nprofile.c<commit_after>/*  -*- c-file-style:"linux" c-basic-offset:3 tab-width:3 -*-  */
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <math.h>
#include <string.h>

#define ORDER 0

const char usage[]= "\nUsage: [-b bases] input.gbk [start end [window_size step [period_of_frames]]]\nDefaults: start= 1; end= end of genome sequence; window_size= 201; step= 51; period_of_frames= 3.\n";


int mapfile(char* filename, char** addr, size_t* length) {
   struct stat sb;
   int fd;

   fd = open(filename, O_RDONLY);
   if (fd == -1) {
      fprintf(stderr, "ERROR: Error opening '%s': ", filename);
      perror(NULL);
      return 1;
   }

   if (fstat (fd, &sb) == -1) {
      perror("fstat");
      return 1;
   }

   if (!S_ISREG (sb.st_mode)) {
      fprintf (stderr, "%s is not a file\n", filename);
      return 1;
   }
   *length = sb.st_size;
   *addr = mmap (NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
   if (*addr == MAP_FAILED) {
      perror ("mmap");
      return 1;
   }

   if (close (fd) == -1) {
      perror ("close");
      return 1;
   }
   return 0;
}

void calculateProfile(const char* origin, const size_t length,
                      const char* bases, const int start, const int end,
                      const int window, const int step, const int period) {

   char *box, base;
   int offset =0 , j, baseidx=0, n=0;
   size_t *S;
   double normfactor = 100.0/(double)(window/period);

   fprintf(stderr,
           "Calculating [%s]-profile of bases %d-%d with window %d nt, step %d nt and period %d nt.\n",
           bases, start, end, window, step, period);

   box = (char *)calloc(window, sizeof(char));
   S = (size_t *)calloc(period, sizeof(size_t));


   /* skip up till start, but don't move offset passed start. */
   while (offset < length) {
      base = origin[offset];
      if(base >= 'A' && base <= 'Z') base += 'a' - 'A';
      if(base >= 'a' && base <= 'z') {
         if(baseidx + 1 == start)
            break;
         baseidx++;
      }
      offset++;
   }

   for(; offset < length && baseidx < end; offset++) {
      base = origin[offset];
      if(base >= 'A' && base <= 'Z') base += 'a' - 'A';
      if(base >= 'a' && base <= 'z') {
         ++baseidx;
         if(strchr(bases, base)) {
            if(! box[n % window]) {
               //if the flag wasn't set, then set it and increase the count.
               ++S[(baseidx-1) % period];
               box[n % window] = 1;
            }
         }
         else {
            if(box[n % window]) {
               //if the flag was set, clear it and decrease the count.
               --S[(baseidx-1) % period];
               box[n % window] = 0;
            }

            //unexpected base, print warning.
            if(! strchr("actgu", base))
               fprintf(stderr,"\nBase %c found at position %d\n",base,baseidx);
         }
         ++n;

         if(n >= window && ((n-window) % step) == 0 && baseidx < end) {
            fprintf(stdout, "%8d", baseidx-window/2);
            for(j=0; j < period; ++j)
               fprintf(stdout, "%8.1f", normfactor*S[j]);
            fprintf(stdout,"\n");
         }
      }
   }
   free(box);
   free(S);
   fprintf(stderr,"Bases %d-%d read (%d nt)\n", start, end, n);
}

int countBases(const char* origin, const size_t length) {
   /* how many bases from there */
   int i, tot=0;
   for(i=0; i < length; i++)
      if((origin[i] >= 'a' && origin[i] <= 'z') || (origin[i] >= 'A' && origin[i] <= 'Z'))
         ++tot;
   return tot;
}

int main(int argc, char *argv[]) {
   int start=1,end=0,window=201,step=51,period=3,tot=0;
   int i;
   char* mapped_file;
   char* origin;
   size_t length, olength;
   int argi = 1;
   char* filename;
   char* bases = "cg";


   if(strcmp(argv[argi], "-b") == 0) {
      bases = (char*) argv[++argi];
      for (i = 0; bases[i]; i++)
         if (bases[i] < 'a')
            bases[i] = bases[i] + 'a' - 'A';
      argi++;
   }
   filename = argv[argi++];


   if ( mapfile(filename, &mapped_file, &length) )
      exit(1);


   fprintf(stderr, "Searching for coding sequence in '%s'... ", filename);
   /* Skip until we see ORIGIN (the line that starts the seq)
    * If the file doesn't contain ORIGIN this may raise a segfault;
    * but at that point exiting with error is all we can do anyways.
    */
   origin = strstr(mapped_file, "\nORIGIN");
   /* then save a pointer at the start of the next line */
   origin = strchr(origin + strlen("\nORIGIN"), '\n') + 1;
{'fraction_non_alphanumeric': 0.1675340493854501, 'fraction_numerical': 0.011958808548333517, 'mean_word_length': 2.151430565247732, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 30, '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': '6307158', 'n_tokens_mistral': 3233, 'n_tokens_neox': 3125, 'n_words': 931}