(define *reference-data-dir* ".")
(define (quick-save)
   (save-state)
   (map (lambda (imol) (write-pdb-file imol (molecule-name imol)))
      (model-molecule-list)
))
(define cs-file-name (append-dir-file *reference-data-dir* "cs.tab"))
(if(file-exists? cs-file-name)
 (call-with-input-file cs-file-name
  (lambda(port)
   (let loop ((voce (read port))
                    (all '()))
   (if (eof-object? voce)
    ((let (( pdb-1 (append-dir-file *reference-data-dir* (car all))))
      (let (( mtz-1 (append-dir-file *reference-data-dir* (cadr all))))
        (let ((pdb-mol (read-pdb pdb-1)))
         (sleep 1)
         (set-density-size 12.00)
;MTZ     (let ((map-mol (make-and-draw-map mtz-1 "F" "PHIMR" "WTMR" 0 0)))
;MAP     (let ((map-mol (handle-read-ccp4-map mtz-1 0)))
           (let ((lunghezza(length all)))
            (let loop2 (( n lunghezza))
             (if (= n 2)
               (  (quick-save)
                  (close-molecule pdb-mol)
                  (close-molecule map-mol)
                  ;(format #t "ho finito")
                  (coot-real-exit 0)
               )
               (
                (let (( chain-id (list-ref all (- n 8))))
                (let (( sequence (list-ref all (- n 7))))
                (let (( res1 (list-ref all (- n 6))))
                (let (( res2 (list-ref all (- n 5))))
                (let (( res3 (list-ref all (- n 4))))
                (let (( res4 (list-ref all (- n 3))))
                (let (( res5 (list-ref all (- n 2))))
                (let (( res6 (list-ref all (- n 1))))
                     (format #t "protein-db-loops( ~s ~s ~s ~s ~s ~s%)"
                           res1 res2 res3 res4 res5 res6)

                     (protein-db-loops pdb-mol (list(list chain-id res1 "")(list chain-id res2 "")(list chain-id res3 "")(list chain-id res4 "")(list chain-id res5 "")(list chain-id res6 ""))map-mol 1)
                     (let((iloop(+ pdb-mol 2)))
                     (format #t "mutate-and-autofit-residue-range ~s ~s ~s ~s ~s%"
                           iloop chain-id res1 res6 sequence)

                      ;(format #t "   Best score: ~s~%" iloop)
                     (turn-off-backup iloop)  
;                     (mutate-and-autofit-residue-range iloop chain-id (+ res4 1) (- res3 1) sequence))
                     ( mutate-residue-range iloop chain-id (+ res4 1) (- res3 1) sequence))
                     ))))))))
                  
                (loop2 (- n 8))))))))))
   )
   (loop (read port)(cons voce all) )
   )))))



