;new_proj
; 
; creates a new assignment project.
; 
; A project consists in a directory with several entries :
;     spectra/  contains the spectra for the assignment project
;             (usually links rather than real files)
;     db/   contains the data bases used for the assignment
;         spin_syst.dir spin_syst.pag    is the spin-system db
;                        (one entry per SS)
;         spin.dir spin.pag  is the spin db
;                    on entry for each spin  
;         *.dir *.pag : several peak db, associated to each files found 
;                     in spectra
;                  these are in dbm format (thus the dir / pag names )
;         primary      is the primary sequence of the molecule,
;                  one letter code, on a single line.
;                  in plain ascii
; each db files ar bound to a dbm array[]
; $sys[] for spin_syst
; $spin[] for spin
; $att[] for the peak data-base (one at a time)
; the syntax for each entry is :
;att :
;	$att<#att> = f1 f2 amp #spin1 #spin2 note
;
;spin
;		$spin<#spin> = delta  nom #ss note
;
;sys :
;		$sys<#ss> = index type list_of_spin

if (dbm("sys")) dbclose sys
if (dbm("spin")) dbclose spin
if (dbm("att")) dbclose att

dialogbox "New project" \
  "First, Create a directory which will hold all the information" message \
  "This directory will be called a project" message \
  separator \
  "Create project (directory)" file dd ($home//"/new_project") *
set proj = $dd

sh ("mkdir"; $proj; "; mkdir"; $proj // "/db; mkdir"; $proj // "/spectra")
sh ("mkdir"; $proj // "/PDB" ; "mkdir"; $proj // "/processing";$proj // "/constraints")
cd  $proj

set prim = " "
=redo
  if (dbm("code")) dbclose code
  dialogbox "Primary sequence" \
    "Then enter the primary sequence of the protein under study" message \
    "You can choose to get the sequence from a file, or to type it directly" message \
    "If you choose to read a file, then choose the file format (1 letter, 3 letter, PDB)" message \
    "If you choose to type it directly, use the 1 letter code" message \
    separator \
    "Choose your input method" enum "Direct input,3let code file,1let code file,PDB file" meth % \
    separator \
    "Depending on the input method, enter Primary sequence    OR    file name " message \
    "Primary sequence (1 letter code)" string prim $prim \
    noreturn \
    "/  File name" file fich " "  *
  set meth = (head($meth))
  if ($meth s= "Direct") then
    set prim = (toupper($prim))
    open db/primary
    dbopen /usr/local/gifa/macro/att/1let_3let code
    set failed = (1==0)       ; used to shorten when wrong aa is used
    for i = 1 to (len($prim))
      if (!$failed) then
        set aa = (subst($prim,$i,$i))
        if (!exist("code[" // $aa // "]")) then
          gm/al_yesno ($aa; " : Unknown residue, Continue ?")
        if ($returned s! "yes") set failed = (1==1)
        endif
        fprint db/primary $aa
      endif
    endfor
    close db/primary
    dbclose code
    if ($failed) goto redo
    gm/al_yesno (len($prim); "residues, Ok ?")
    if ($returned s! 'yes') goto redo
  elsif ($meth s= "3let" | $meth s= "1let") then
    open $fich
    open db/primary
    set i = 0
    if ($meth s= "3let") dbopen /usr/local/gifa/macro/att/3let_1let code
    set l = <$fich
    while (!eof($fich))
      while ($l s! " ")
        set t = (toupper(head($l)))  set l = (tail($l))
        if ($meth s= "3let") set t = (head($code[$t]))
        fprint db/primary $t       set i = (%+1)
      endwhile
      set l = <$fich
    endwhile
    close db/primary
    if ($meth s= "3let") dbclose code
    close $fich
    gm/al_yesno ($i; "residues, Ok ?")
    if ($returned s! 'yes') goto redo
  elsif ($meth s= "PDB") then
    initinprogress 3
    open $fich
    open db/primary
    set i = 0
    dbopen /usr/local/gifa/macro/att/3let_1let code
    set l = <$fich
    inprogress 1
    while (!eof($fich))
        set t = (head($l))  set atom = (head(tail(tail($l))))
        if ($t s= 'ATOM' & $atom s= 'CA') then
           set t = (head($code[head(tail(tail(tail($l))))]))
           fprint db/primary $t       set i = (%+1)
        endif
        set l = <$fich
    endwhile
    inprogress 2
    close db/primary
    close $fich
    dbclose code
    inprogress 3
    gm/al_yesno ($i; "residues, Ok ?")
    if ($returned s! 'yes') goto redo
  endif

; open db
dbopen db/spin spin
set spin["largest"] = 0
dbopen db/spin_syst sys
set sys["largest"] = 0

; set default param.
set tol_align := 0.03
set tol_click := 0.1
set box_f1 := 0.03
set box_f2 := 0.03

; ch_param

open parameters
fprint parameters ("set tol_align :="; $tol_align)
fprint parameters ("set tol_click :="; $tol_click)
fprint parameters ("sign"; $sign)
fprint parameters ("level"; $level)
fprint parameters ("scale"; $scale)
fprint parameters ("loga"; $loga)
close parameters

alert "Now, add spectra to the project, then start the assignment work"
