# Purpose: Domain server processing (ParExecuteIPC version)
# Author:  Chantal Korostensky
# Created: 10.04.1997
#

Title := 'No title specified';
if not assigned(domains) then ReadLibrary('SwissProt.m3.d30.data'): fi:
printlevel := 2;
#ReadProgram('~cbrg/GetMachine'):
 
init := proc ();
  if not assigned(DM) then CreateDayMatrices(); fi;
  Set(printgc = false);
  ReadLibrary (ProbModel);
  ReadLibrary (Parser);
  ReadLibrary (DomainHist);
  ReadLibrary (MAlignment);
#  db_path := GetMachine(): 
  db_path := '~darwin/DB/':
  ReadDb(db_path.'SwissProt');
end:

DoDomains := proc(wts)
global domains, dlist;
  Commands := sort(wts[1]);
  printf('The requested commands were: %a\n',Commands); 
  printf('You entered the following keywords: %a\n\n', wts[3]);
  DoMSA  := false; DoPAS  := false; DoTree := false; DoHist := false;
  for i to length(Commands) do
    if   SearchString('MSA',   Commands[i]) > -1 then DoMSA  := true; 
    elif SearchString('ances', Commands[i]) > -1 then DoPAS  := true; 
    elif SearchString('tree',  Commands[i]) > -1 then DoTree := true;
    elif SearchString('Hist',  Commands[i]) > -1 then DoHist := true; 
    else printf('\nI do not understand the command "%s"!\n\n',Commands[i]);
    fi;
  od;
  
  ent := wts[2];
  for i to length(ent) do
    e := Entry(ent[i]);
    if length(dlist[ent[i]]) > 0 then
      OpenWriting(terminal);
      for d in dlist[ent[i]] do
        if DoMSA or DoTree or DoPAS then
	  msa := domains[d, 'MA'];
	fi;
        if DoPAS then domains[d, 'MA']['PAS'] fi;
      od;
#      OpenAppending(tmpfile);
    fi;
    printent(ent[i]);
    if DoHist then 
      printlevel := 3;
      printf('\n\n********************************\n');
      printf('*** History of those domains ***\n');
      printf('********************************\n\n');
      dom := dlist[ent[i]];
      for d to length(dom) do
        Showhist(domains[dom[d]], domains);
      od;
      printlevel := 2;
    fi;
  od;
end:

ParseMsg := proc (msg: string)
global Title, domains, dlist;
  InputString := GetBody(msg);  
  if InputString[1] = '?' then return(InputString); fi;
  res := NextToken(InputString);
  comm := res[1]; InputString := res[2];
  Commands := []; 
  if SearchString('Domains',comm)=0 and SearchString('testnewfunction',comm)=0 then lprint('opcode not found');  return(0) fi;
  
  while length(InputString) > 0 do
    res := NextLine(InputString);
    InputLine := res[1]; InputString := res[2];
    res := NextToken(InputLine);
    tok := res[1]; InputLine := res[2];
    if tok='EOS' then break
    elif SearchString('Commands',tok)=0 then
      res := GetString(InputLine);
      while res[1] <>'' do
	Commands := append(Commands, res[1]);
	InputLine := res[2];
	res := GetString(InputLine);
      od;
    elif SearchString('Protein',tok)=0 then
      OldLine := InputLine;
      keys := []; res := GetString(InputLine);
      while res[1] <> '' do  
	InputLine := res[2];
	keys := append(keys, res[1]);
	res := GetString(InputLine);
      od;
      if length(keys) = 0 then
        return( '?You must specify a SwissProt protein. Found: ' .OldLine ); 
      fi;
      ent := SearchDb(op(keys));
      if length(ent) = 0 then
        return( '?Did not find this protein in SwissProt. You entered: ' .OldLine[2..length(OldLine)-1]);
      elif length(ent) > 50 then
        return( sprintf('?Too many proteins (%d) match "%s".\nPlease specify the protein better', length(ent),OldLine[2..length(OldLine)-1] ));
      fi;
    elif SearchString('Title',tok)=0 then
      Title := GetString(InputLine)[1];
    fi;	
  od;
[Commands, ent, keys];
end:

job := proc ()
  global Title;
  init(); 
  prefix := GetTmpDir().getpid ().'.';
  db := prefix.'db';
  CallSystem ('rm '.db.'*');
  OpenWriting (db);
  printlevel := 3;
  wts := ParseMsg (msg):
  OpenWriting (terminal);
  OpenWriting (tmpfile);	
  printf ('\nTitle of your job: %s\n', Title); 
  res := 'ERROR'; 
  if type (wts, string) then 
    printlong ('Sorry, your request could not be processed, because'. wts[2..-1]);
  else    
    DoDomains (wts);
  fi;
  OpenWriting (terminal);
  res := ReadRawFile (tmpfile);	
  res1 := ReadRawFile(db);
  CallSystem ('rm '.prefix.'*');
  res;
end:

# ReadProgram('~cbrg/TestNewFunction1/TestNewFunction1');
# init();
# ReadProgram('~cbrg/TestNewFunction1/lastmsg');
#  wts := ParseMsg (msg):
#  DoDomains (wts);
# wts := [['MSA'], Entry(424)];
