Data processing task for bioinformatics I threw together this C program today to handle a bioinformatics data processing task. The program seems to work correctly, but I wanted to know if anyone has suggestions regarding how the input data are parsed and how I've used control structures in the main processing loop.

#include &lt;assert.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

#define BUFFERSIZE 1024

FILE *fileopen(const char *filename, const char *mode)
{
  FILE *fh = fopen(filename, mode);
  if(fh == NULL)
  {
    fprintf(stderr, "error: unable to open file '%s'\n", filename);
    exit(1);
  }
  return fh;
}

void *memalloc(size_t size)
{
  void *memory = malloc(size);
  if(memory == NULL)
  {
    fprintf(stderr, "error: unable to allocate memory\n");
    exit(1);
  }
  return memory;
}

int main(int argc, const char **argv)
{
  // Parse command line arguments
  char seqbuffer1[BUFFERSIZE];
  char seqbuffer2[BUFFERSIZE];
  char qualbuffer1[BUFFERSIZE];
  char qualbuffer2[BUFFERSIZE];
  char *seqbufferout;
  FILE *seqinfile;
  FILE *seqoutfile;
  FILE *qualinfile;
  FILE *qualoutfile;
  int seqlength;
  if(argc != 6)
  {
    fprintf(stderr, "error: 5 arguments required, %d provided\n", argc - 1);
    exit(1);
  }
  seqinfile   = fileopen(argv[1], "r");
  qualinfile  = fileopen(argv[2], "r");
  seqoutfile  = fileopen(argv[3], "w");
  qualoutfile = fileopen(argv[4], "w");
  seqlength   = atoi(argv[5]);
  assert(seqlength &gt; 0);

  // Process reads
  seqbufferout = (char *)memalloc( sizeof(char) * (seqlength + 1) );
  seqbufferout[seqlength] = '\0';
  while(fgets(seqbuffer1, BUFFERSIZE, seqinfile))
  {
    if(strlen(seqbuffer1) &gt; 0)
    {
      int ambiguous;
      int i;

      assert(strncmp(seqbuffer1, "&gt;", 1) == 0);
      if(fgets(seqbuffer2, BUFFERSIZE, seqinfile) == NULL)
      {
        fprintf(stderr, "error: file ends with a fasta header\n");
        exit(1);
      }

      if( fgets(qualbuffer1, BUFFERSIZE, qualinfile) == NULL || 
          fgets(qualbuffer2, BUFFERSIZE, qualinfile) == NULL )
      {
        fprintf(stderr, "error: no quality entry corresponding to sequence '%s'\n", seqbuffer1);
        exit(1);
      }

      ambiguous = 0;
      for(i = 0; i &lt; seqlength; i++)
      {
        if(seqbuffer2[i] == '.')
        {
          ambiguous = 1;
          break;
        }
        seqbufferout[i] = seqbuffer2[i];
      }

      if(!ambiguous)
      {
        fprintf(seqoutfile,  "%s%s\n", seqbuffer1, seqbufferout);
        fputs(qualbuffer1, qualoutfile);

        i = 0;
        char *tok = strtok(qualbuffer2, " ");
        while(tok != NULL &amp;&amp; i &lt; seqlength - 1)
        {
          if(i &gt; 0)
            fputs(" ", qualoutfile);
          fputs(tok, qualoutfile);
          tok = strtok(NULL, " ");
          i++;
        }
        fputs("\n", qualoutfile);
      }
    }
  }

  fclose(seqinfile);
  fclose(qualinfile);
  fclose(seqoutfile);
  fclose(qualoutfile);
  free(seqbufferout);

  return 0;
}
Instead of the big indent block you make by doing if(strlen(seqbuffer1) &gt; 0), do: if (strlen(seqbuffer1) &lt;1) continue;, so the entire rest of the loop can be outdented a level, making it more readable. 

Similarly, you can turn the if(!ambiguous) into if (ambiguous) continue; and outdent that block as well.

Depending on what version of C you're using, ambiguous could be a boolean instead of an int.

You could be a little more consistent with your NULL comparisons to the result of fgets() - the one in the while() condition is implicitly != NULL but the ones in the if() statements are explicitly == NULL. IMO you should be either explicit everywhere or implicit everywhere - either choice is fine, but mixing styles makes your code a bit more unclear.

assert(strncmp(seqbuffer1, "&gt;", 1) == 0); is the same as the more-easily-understood assert(seqbuffer1[0] == '&gt;'), right?

Also, you've got at least one potential bug: What if BUFFERSIZE is smaller than input line length and then seqlength > BUFFERSIZE?