meme-server.c

来自「EM算法的改进」· C语言 代码 · 共 698 行 · 第 1/2 页

C
698
字号
  char *dfmt = "-u '+%d/%m/%y %H:%M:%S'";	/* date format */  char *type, *text, *npstr;  int nprocs = 0;			/* assume non-parallel */  char *p0;    #include <errno.h>  /* create temporary file names */  resfilename = newfilename("results", meme_logs);  datefilename = newfilename("date", meme_logs);  /* get the type of output: html or ascii */  type = strstr(a_header->description, "(Use web browser to view results)") ?    "-html" : "";  /* send MAST as text file if MEME is */  text = strstr(a_header->description, "(Use web browser to view results)") ?     "" : "-text";  /* get number of processes to use if parallel MEME */  if ((npstr = strstr(MEME, "-p ")) != NULL) { 		/* nprocs given */    sscanf(npstr, "-p %d", &nprocs);  }  /* create a script to:	1) execute MEME	2) mail the result of MEME	3) execute and mail results of MAST  */  /* Open the script file. */  if ((qfile = fopen(qfilename, "w")) == NULL) {    fprintf(stderr, "Error opening file %s.\n", qfilename);    fflush(stderr);    exit(10);  }  /* script header */  fprintf(qfile, "#!/bin/csh\n");  fprintf(qfile, "# This script was automatically generated ");  fprintf(qfile, "by the MEME server (meme-server.c).\n\n");  /* change to LOGS directory (needed by qsub) */  fprintf(qfile, "set dir = %s\n", meme_logs);  fprintf(qfile, "set bin = %s\n", meme_bin);  /* save the submission time */  sprintf(dcmd, "date %s > %s\n", dfmt, datefilename);  system(dcmd);					/* get submission time */  /* save the start time */  fprintf(qfile, "set t1 = `date %s`\n", dfmt);  /* run MEME and save the results in a file */  if ((p0 = strstr(MEME, "-p ")) != NULL) {	/* nprocs given */    int flen;    p0 += 3;    flen = (int)(p0 - MEME);    fprintf(qfile, "%*.*s\"%s\"\\\n", flen, flen, MEME, p0);  } else if (strstr(MEME, "-p") != NULL) {     fprintf(qfile, "%s %d\\\n", MEME, nprocs);  } else {    fprintf(qfile, "%s\\\n", MEME);  }  fprintf(qfile, "  %s \\\n", meme_input);  fprintf(qfile, "  %s \\\n", a_header->switches);      /* overridable */  fprintf(qfile, "  -nostatus -maxiter %d \\\n", MAXITER);  fprintf(qfile, "  -sf '%s' \\\n", a_header->remotename);  fprintf(qfile, "    >& %s\n\n", resfilename);  /* mail the MEME results */  fprintf(qfile, "%s/mailer\\\n", meme_bin);  fprintf(qfile, "  %s\\\n", a_header->address);  fprintf(qfile, "  \'MEME job %ld results: %s\' %s %s\n\n",    job, a_header->description, resfilename, type);  /*   run MAST on the MEME output and mail results back  */  fprintf(qfile, "$bin/make_logodds %s /dev/null > /dev/null\n",     resfilename);  fprintf(qfile, "if ($status == 0) then\n");  fprintf(qfile, "  set mast_out = $dir/mast.%ld.results\n", job);  fprintf(qfile, "  $bin/mast %s %s -mf \'<motifs found in %s>\'\\\n",    resfilename, meme_input, a_header->remotename);  fprintf(qfile, "    -df \'%s\' -stdout %s > $mast_out\n",    a_header->remotename, text);  fprintf(qfile, "  %s/mailer\\\n", meme_bin);  fprintf(qfile, "    %s\\\n", a_header->address);  fprintf(qfile, "    \'MAST job %ld results: %s\' $mast_out %s\n",    job, a_header->description, type);  fprintf(qfile, "  /bin/rm $mast_out\n");  fprintf(qfile, "endif\n\n");  /* save the final time */  fprintf(qfile, "set t2 = `date %s`\n", dfmt);  /* log this job */  fprintf(qfile, "touch $dir/meme-log\n");		/* make sure log-file exists */  fprintf(qfile,     "echo `hostname` %9ld submit: `cat %s` start: $t1 end: $t2 %s %s %s >> $dir/meme-log\n\n",     job, datefilename, a_header->switches, a_header->loginfo, a_header->address  );  /* fprintf(qfile, "exit 0 \n" );	*/  /* delete the files we have created */  fprintf(qfile, "/bin/rm -f %s\n", meme_input);	/* sequence file */  fprintf(qfile, "/bin/rm -f %s\n", datefilename);	/* submission date file */  fprintf(qfile, "/bin/rm -f %s\n", resfilename);	/* output of MEME */  fprintf(qfile, "/bin/rm -f %s\n", qfilename);	/* this script */  /* Close the script file. */  fclose(qfile);  /* free the file names created here */  myfree(resfilename);  myfree(datefilename);}/*********************************************************************** * * void confirm * * Send a confirmation message to the user * ***********************************************************************/void confirm( HEADER *a_header, char * meme_bin ){  char command[2024];                   /* confirmation message command */  int job = getpid();                   /* get the job number */  int result;   /* make the message */  sprintf(command, "%s/mailer %s \'MEME job %d confirmation: \' -stdin << END\n""Your MEME search request %d is being processed.\n""You should receive two subsequent messages containing:\n""	1) MEME search results\n""	2) MAST analysis of your sequences using the MEME results\n""END\n",    meme_bin, a_header->address, job, job);   /* send it */  if ((result = system(command)) != SYSTEM_OK) {    fprintf(stderr, "Error level %d from \'%s\'.\n", result, command);    fflush(stderr);    exit(30);  }} /*********************************************************************** * * void submit_job * * Submit the given job file to queueing system. * ***********************************************************************/void submit_job(char *qfilename){  int result;  char command[2000];   sprintf(command, QCMD, qfilename);  if ((result = system(command)) != SYSTEM_OK) {    fprintf(stderr, "Error level %d from \'%s\'.\n", result, command);    fflush(stderr);    exit(30);  }}/*********************************************************************** * * doit * * Do whatever the server is supposed to do with incoming data. * ***********************************************************************/void doit (int sock){  char *meme_input;  char *qfilename;  HEADER a_header;  static char *meme_logs = NULL;     /* meme logs directory */  static char *meme_bin = NULL;     /* meme bin directory */  meme_logs = getenv("MEME_LOGS");  if (meme_logs == NULL) {    fprintf(stderr, "You must define environment variable MEME_LOGS\n");    exit(1);  }   meme_bin = getenv("MEME_BIN");  if (meme_bin == NULL) {    fprintf(stderr, "You must define environment variable MEME_BIN\n");    exit(1);  }  /* get unique filenames */  meme_input = newfilename("data", meme_logs);  qfilename = newfilename("script", meme_logs);  /* Get the file from the client. */  receive_file(sock, meme_input, &a_header);  send_ack(sock);  /* Write out an NQS job file. */  make_q_script(&a_header, meme_input, qfilename, meme_bin, meme_logs);    /* Submit the job file to the queue. */  submit_job(qfilename);  /* Send confirmation message */  confirm(&a_header, meme_bin);   /* free filenames */  myfree(meme_input);  myfree(qfilename);}/***********************************************************************  void reapchild  SIGCHLD handler. Supplied by Mike Wan at SDSC. (11/9/95)***********************************************************************/#include <sys/wait.h>#include <signal.h>static void reapchild (int sig){  int status;  pid_t waitval;   while ( (waitval=waitpid (-1, &status, WNOHANG)) > 0) {    if (sig == SIGCHLD) {       /*printf("Child process %d exited.\n", (int) waitval);*/    } else {      printf( "Received signal %d from process %d\n", sig, (int) waitval );    }    fflush(stdout);    signal (SIGCHLD, reapchild);  }}/*********************************************************************** * * main * ***********************************************************************/extern int main(  int argc,  char *argv[]){  #if defined(ibmrs6000)  size_t len;  #else  int len;  #endif  int sock, new_socket;  struct sockaddr_in acceptor;  struct sockaddr_in connector;  if (argc < 4) {    /*    printf("Usage: meme-server <socket> <meme> <qcmd>\n");    printf("	<socket>  		socket number to listen on\n");    */    printf("	<command>		command for server to execute\n");    printf("	<qcmd>			queueing command including %%s\n");    fflush(stdout);    exit(1);  }  /* get root directory for meme system */  SOCKET_NUMBER = atoi(argv[1]);  MEME = argv[2];  QCMD = argv[3];  printf("MEME server initialized.\n");  printf("command is: %s\n", MEME);  printf("qcmd is: %s\n", QCMD);  fflush(stdout);  /* Parent needs to handle the SIGCHLD signal before the     child can exit properly. */  signal (SIGCHLD, reapchild);  len = sizeof(acceptor);  memset(&acceptor, 0, len);  acceptor.sin_family = AF_INET;  acceptor.sin_port = 0xffff & (htons(SOCKET_NUMBER));    connector.sin_addr.s_addr = INADDR_ANY;  /* Create a new socket. */  sock = socket(AF_INET, SOCK_STREAM, 0);  /* Bind the socket. */  if (bind(sock, (struct sockaddr *)&acceptor, len) < 0) {    perror("Node bind error");    fprintf(stderr, "Node errno is %d\n", errno);    fflush(stderr);    exit(14);  }  /* Listen on the socket. */  if (listen(sock, 5) < 0 ) {    perror("Node listen");    fflush(stderr);    exit(15);  }    /* Loop forever, forking for each incoming request. */  for(;;)    {      /* Block until a request comes in, then create a new socket. */      new_socket = accept(sock, (struct sockaddr *)&acceptor, &len);      if (new_socket < 0) {        /* allow interrupt caused by child process terminating */        if (errno == EINTR) {           continue;            /* go to top of loop */        } else {          perror("Node accept");          fflush(stderr);          exit(16);        }      }       /* On a fork, the parent gets the child's process ID back. */      if (fork()==0) {     /* This means I am child. */	close(sock);       /* Ignore the old socket. */	doit(new_socket);  /* <- Primary server activity here. */	exit(0);           /* After execution, the child exits. */      }      /* The parent closes the new socket. */      close(new_socket);    }  /* NOTREACHED */  return(0);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?