/*                                                                                                                       */
/* collect_mgf - collects many MGF files in different EXPNO folders to one file with PEPMASS and CHARGE from dd_results  */
/*                                                                                                                       */
/* (c) Magnus Palmblad, The University of Reading, 2006                                                                  */ 
/*                                                                                                                       */
/* Usage: collect_mgf <EXPNO directory> <dd_results file> <start EXPNO index (even)> <end EXPNO index (even)> <output>   */
/*                                                                                                                       */
/* Example: collect_mgf some_data_directory dd_results 2 256 sum.mgf                                                     */
/*                                                                                                                       */
/* compile with gcc -o collect_mgf                                                                                       */
/*                                                                                                                       */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_SCANS 10000

int main (int argc, char *argv[]) 
{
  char *p, line[100], infile[100], outfile[100]; /* for reading input and writing output */
  int i, start_index, end_index, CHARGE_reached, CHARGE[MAX_SCANS];
  float PEPMASS[MAX_SCANS];
  FILE *inp, *outp;
   
  /* parse command line parameters */
  
  if (argc!=6)
    {
      printf("usage: collect_mgf <EXPNO directory> <dd_results file> <start EXPNO index (even)> <end EXPNO index (even)> <output file>\n");
      return 0;
    }

  
  /* read dd_results file */
  
  strcpy(infile,argv[2]);
  if ((inp = fopen(infile, "r"))==NULL) {printf("error opening dd_results file %s",infile); return -1;}
  printf("reading dd_results file...");fflush(stdout);    
  i=1;
  while (fgets(line, 100, inp) != NULL)
    {
      if (strcmp(line,"\n")==0) continue;
      p=strtok(line," \t");   
      if (p[0]!='#')
      	{
	  if (atoi(p)%2==0) /* take only even (MS/MS) scans */ 
	    {
	      p=strtok('\0'," \t");
	      p=strtok('\0'," \t");
	      PEPMASS[i]=atof(p);
	      p=strtok('\0'," \t"); 
	      CHARGE[i]=atoi(p);
	    }
	  i++;
       	}
    } 
  close(inp);
  printf("done\n");


  /* read MGF files */
  
  start_index=atoi(argv[3]);
  end_index=atoi(argv[4]);

  strcpy(outfile,argv[5]);
  if ((outp = fopen(outfile, "w"))==NULL) {printf("error opening output file %s",outfile); return -1;}  

  for (i=start_index;i<=end_index;i+=2) 
    if (CHARGE[i]>0) /* optional CHARGE filter - include only successfully deconvoluted precursors */
      {
	sprintf(infile,"%s/%i/pdata/1/%s_%i_1.mgf",argv[1],i,argv[1],i);
	if ((inp = fopen(infile, "r"))==NULL) {printf("error opening MGF file %s",infile); return -1;}
	printf("reading MGF file %s...",infile);fflush(stdout);    
	CHARGE_reached=0;
	while (fgets(line, 100, inp) != NULL)
	{
	  if (strcmp(line,"\n")==0) continue;
	  p=strtok(line," ");   
      	  if (CHARGE_reached==1) fprintf(outp,"%s",line);
	  if (strcmp(p,"CHARGE=1+\n")==0) {CHARGE_reached=1;fprintf(outp,"BEGIN IONS\nPEPMASS=%f\nCHARGE=%i+\n",PEPMASS[i],CHARGE[i]);}
	}
	fprintf(outp," IONS\n\n");
	close(inp);  
	printf("done\n");
      }
  
  close(outp);
  
  return 0;
}

