robifasta.c 8.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
#include <R.h>
#include <Rinternals.h>
#include <Rdefines.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>


static size_t count_fasta(const char * data, size_t size) {
  size_t i;
  size_t  j;

  for(i=0,j = 0; j < size; j++, data++)
        if (*data == '>' && (data[-1] == '\n' || j == 0) ) i++;

  return i;
}


SEXP R_parse_fasta(SEXP raw) {
  char* pointer;
  char* seqbuffer;
  char* iseqbuffer;
  size_t seqbuffer_size;
  size_t size;
  size_t ssize;
  size_t nfasta;

  SEXP   seqids;
  SEXP   headers;
  SEXP   sequences;
  SEXP   fastas;

  size_t i;
  size_t j;
  char* index;
  char* index_e;
  char  save;

  seqbuffer = NULL;
  seqbuffer_size = 0;

  if (! IS_RAW(raw))
      error("the raw parameter must be of raw type");

	pointer = (char *) RAW(raw);
	size = LENGTH(raw);
	nfasta = count_fasta((const char*)pointer,size);

	seqids = PROTECT(NEW_STRING(nfasta));
	headers = PROTECT(NEW_STRING(nfasta));
	sequences = PROTECT(NEW_STRING(nfasta));

	fastas = PROTECT(NEW_LIST(3));

	i = 0;
	j = 0;
	index = pointer;




	while (i < size) {

	  while (!(*index == '>'
              && (index[-1] == '\n' || i == 0))
            && i < size)
	    i++,index++;

	  if (i >= size) break;

    /* pointer pointes on the > char */
    index++;
    i++;
    index_e = index;

    /* Look for the next white char to delimite Sequence id */

    while(*index_e != ' ' && *index_e!= '\r' && *index_e != '\n' && i < size)
      index_e++,i++;

	  if (i >= size) break;

    save = *index_e;
    *index_e = 0;
    SET_STRING_ELT(seqids, j, mkChar(index));
    *index_e = save;

    while(*index_e == ' ' && i < size)
      index_e++,i++;

	  if (i >= size) break;

    index = index_e;

    /* Look for the next end of line to delimite Sequence header */

    while(*index_e!= '\r' && *index_e != '\n' && i < size)
      index_e++,i++;

	  if (i >= size) break;

    save = *index_e;
    *index_e = 0;
    SET_STRING_ELT(headers, j, mkChar(index));
    *index_e = save;

    while ((*index_e== '\r' || *index_e == '\n') && i < size)
      index_e++,i++;

	  if (i >= size) break;

    index = index_e;

    while (!(*index_e== '>' && index_e[-1] == '\n') && i < size)
      index_e++,i++;

    ssize = index_e - index;

    if (ssize > seqbuffer_size) {
      seqbuffer_size = ssize;
      seqbuffer = R_alloc(seqbuffer_size,sizeof(char));
      if (seqbuffer == NULL)
        error("cannot assign requested memory");
    }

    iseqbuffer = seqbuffer;

    for (; index < index_e; index++) {
      if ((*index >= 'A' && *index <= 'Z') ||
          (*index >= 'a' && *index <= 'z')) {
        *iseqbuffer = *index;
        iseqbuffer++;

      }
    }
    *iseqbuffer=0;


    SET_STRING_ELT(sequences, j, mkChar(seqbuffer));

    j++;
  }

  SET_VECTOR_ELT(fastas,0,seqids);
  SET_VECTOR_ELT(fastas,1,headers);
  SET_VECTOR_ELT(fastas,2,sequences);

	UNPROTECT(4);

	return fastas;
}

static size_t preparse_elements(char* from, char* to) {
  char *start;             /* Used to store the beginning of the dict */
  char previous;           /* memory for the previous separator */

  char stacks[10000];      /* Static array on the heap to store the heap of nested structures */
  char *pstack;            /* Pointer into the heap */
  int32_t level;           /* Nesting level of the brackets */
  int32_t count;           /* Count of elements in the dict */

  previous = ',';

  start = from;
  *to=0;

  level = 0;
  count = 0;
  pstack = stacks;

  for(; from < to; from++) {

    if (level < 0) error("Parsing error in python dict too many closing bracket");

    /**
     * Strip white char at level 0 of the structure
     */

    if (level==0 && *from == ' ') {
      *from = 0;
      continue;
    }

    /**
     * Skip the strings. Then backoted char are not considered
     */

    if (*from == '"' || *from == '\'') {
      *pstack = *from;
      from++;
      while(*from != *pstack && from < to) from++;

      if (from == to) error("Parsing error in python dict");

      continue;
   }

    /**
     * Considere the nested brackets
     */

        /*   Openning Bracket      */

    if (*from == '(') {*pstack = ')', level++, pstack++; continue;}
    if (*from == '[') {*pstack = ']', level++, pstack++; continue;}
    if (*from == '{') {*pstack = '}', level++, pstack++; continue;}

        /*   Closing Bracket      */

211
    if ((*from == ')' || *from == ']' || *from == '}')) {
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
      if (*from == pstack[-1]) level--,pstack--;
      else error("Parsing error in python dict in bracket nesting");

      continue;
    }

    if (*from == ',' && level == 0) {
      if (previous != ':')
        error("Parsing error in python dict elements");

      previous=',';
      *from=0;
      count++;
      continue;
    }

    if (*from == ':' && level == 0) {
      if (previous != ',')
        error("Parsing error in python dict elements separator");

      previous=':';
      *from=0;
      continue;
    }

  }
  return count + 1;
}

#define STRDUP(str,lit) strcpy(str = R_alloc(strlen(lit)+1,sizeof(char)), lit)

243
SEXP R_parse_python_dict(char* source) {
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
  char * isource;
  char * esource;
  char *last;
  char stacks[10000];
  char *pstack;

  int32_t level;
  int32_t count;
  int32_t i;

  SEXP keys;
  SEXP values;
  SEXP dict;


  level = 0;
  pstack = stacks;

  while(*source == ' ') source++;

  if (*source != '{')
265
    return NEW_LIST(0);
266 267 268 269 270 271 272 273 274 275 276 277

  source++;

  isource = source;
  last = source;

  while(*isource > 0) {
    if (*isource != ' ') last=isource;
    isource++;
  }

  if (*last != '}')
278
    return NEW_LIST(0);
279 280 281 282 283 284 285 286 287 288 289 290 291


  count = preparse_elements(source,last);

  while(last[-1]==0) last--;

  keys = PROTECT(NEW_STRING(count));
  values = PROTECT(NEW_STRING(count));
  dict = PROTECT(NEW_LIST(2));

  for (isource = source,i=0;
       isource < last;
       isource++) {
292
    while(*isource == 0 && isource < last) isource++;
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
    isource++;
    for(esource=isource; *esource > 0; ) esource++;
    esource--;
    *esource=0;

    SET_STRING_ELT(keys, i, mkChar(isource));

    for(isource=esource; *isource == 0; ) isource++;
    SET_STRING_ELT(values, i, mkChar(isource));
    isource+=strlen(isource);

    i++;
  }

  if (i != count)
    error("Error during the parsing of the dictionary");


  UNPROTECT(3);

  SET_VECTOR_ELT(dict,0,keys);
  SET_VECTOR_ELT(dict,1,values);

  return dict;
}

#undef STRDUP
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420


SEXP R_as_named_list(SEXP string, SEXP exit_at_first_error) {
  const char* csource;
  size_t      lsource;
  size_t      lbuffer;

  char*       source;
  size_t      nstring;

  size_t      i;

  SEXP        value;
  SEXP        svalue;
  SEXP        converted;

  int32_t     stop_ifna;

  if (! isString(string))
      error("the string parameter must be a charactere");

  if (! isLogical(exit_at_first_error))
      error("the exit_at_first_error parameter must be a logical");

  stop_ifna = *LOGICAL(exit_at_first_error);

  nstring = LENGTH(string);

  converted = PROTECT(NEW_LIST(nstring));

  lbuffer = 0;

  for (i = 0; i < nstring; i++) {
    svalue = STRING_ELT(string,i);

    if (svalue != NA_STRING) {
      csource = CHAR(svalue);
      lsource = strlen(csource);

      if (lsource >= lbuffer) {
        source  = R_alloc(lsource+1,sizeof(char));
        lbuffer = lsource+1;
      }

      strncpy(source,csource,lsource);
      source[lsource]=0;

      value = PROTECT(R_parse_python_dict(source));

      if (stop_ifna && LENGTH(value) == 0) {
        UNPROTECT(1);
        break;
      }
    }
    else value = PROTECT(NEW_LIST(0));

    SET_VECTOR_ELT(converted,i,value);
    UNPROTECT(1);
  }

  for (; i < nstring; i++) {
    SET_VECTOR_ELT(converted,i,NEW_LIST(0));
  }

  UNPROTECT(1);
  return converted;
}


SEXP R_trim_and_unquote_strings(SEXP string) {
  size_t nstring;
  SEXP converted;
  size_t lbuffer;
  SEXP svalue;
  const char* csource;
  size_t lsource;
  size_t i;
  const char* last;
  char* source;

  if (! isString(string))
      error("the string parameter must be a charactere");

  nstring = LENGTH(string);

  converted = PROTECT(NEW_STRING(nstring));

  lbuffer = 0;

  for (i = 0; i < nstring; i++) {
    svalue = STRING_ELT(string,i);

    if (svalue != NA_STRING) {
      csource = CHAR(svalue);
      lsource = strlen(csource);
      last = csource + lsource - 1;

      while(*csource == ' ') csource++;

      if (*csource == '"' || *csource == '\'') {

Eric Coissac's avatar
Eric Coissac committed
421
        while(*last == ' ' && last > csource) last --;
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447

        if (*csource == *last) {
          csource++;
          lsource = last - csource;

          if (lsource >= lbuffer) {
            source  = R_alloc(lsource+1,sizeof(char));
            lbuffer = lsource+1;
          }

          strncpy(source,csource,lsource);
          source[lsource]=0;

          SET_STRING_ELT(converted, i, mkChar(source));
          continue;
        }
      }
    }

    SET_STRING_ELT(converted,i,svalue);
  }

  UNPROTECT(1);
  return converted;

}