#!/usr/bin/env php
<?php # (jEdit options) :folding=explicit:collapseFolds=1:
/*****************************************************************************
    Processes a directory full of PDB files non-recursively and outputs
    a list of all the Ramachanadran scores

 -> We assume all files already have H's added! <-

INPUTS (via $_SERVER['argv']):
    the path to a directory; *.pdb will be processed

OUTPUTS:

*****************************************************************************/
// EVERY *top-level* page must start this way:
// 1. Define it's relationship to the root of the MolProbity installation.
// Pages in subdirectories of lib/ or public_html/ will need more "/.." 's.
    if(!defined('MP_BASE_DIR')) define('MP_BASE_DIR', realpath(dirname(__FILE__).'/..'));
// 2. Include core functionality - defines constants, etc.
    require_once(MP_BASE_DIR.'/lib/core.php');
    require_once(MP_BASE_DIR.'/lib/model.php');
    require_once(MP_BASE_DIR.'/lib/analyze.php');
    require_once(MP_BASE_DIR.'/lib/visualize.php');
// 3. Restore session data. If you don't want to access the session
// data for some reason, you must call mpInitEnvirons() instead.
    mpInitEnvirons();       // use std PATH, etc.
    //mpStartSession(true);   // create session dir
// 5. Set up reasonable values to emulate CLI behavior if we're CGI
    set_time_limit(0); // don't want to bail after 30 sec!

#{{{ a_function_definition - sumary_statement_goes_here
############################################################################
/**
* Documentation for this function.
*/
//function someFunctionName() {}
#}}}########################################################################

# MAIN - the beginning of execution for this page
############################################################################
// First argument is the name of this script...
if(is_array($_SERVER['argv'])) foreach(array_slice($_SERVER['argv'], 1) as $arg)
{
    if(!isset($inDir))
        $inDir = $arg;
    else
        die("Too many or unrecognized arguments: '$arg'\n");
}

if(! isset($inDir))
    die("No input directory specified.\n");
elseif(! is_dir($inDir))
    die("Input directory '$inDir' does not exist or is not a directory.\n");

echo "#filename:chain:resType:resNum+iCode:phi:psi:score%:eval:type\n";

// Loop through all PDBs in the provided directory
$h = opendir($inDir);
while(($infile = readdir($h)) !== false)
{
    $infile = "$inDir/$infile";
    if(is_file($infile) && endsWith($infile, ".pdb"))
    {
        mpStartSession(true);
        $id = addModelOrEnsemble(
            $infile,
            basename($infile),
            false,
            true,
            true,
            false);

        $filename = basename($infile);
        $model   =& $_SESSION['models'][$id];
        $pdbfile = $_SESSION['dataDir'].'/'.MP_DIR_MODELS."/$model[pdb]";
        $rawDir  = $_SESSION['dataDir'].'/'.MP_DIR_RAWDATA;
        if(!file_exists($rawDir)) mkdir($rawDir, 0777);
        $outfile = "$rawDir/$model[prefix]rama.data";

        // Run analysis; load data
        runRamachandran($pdbfile, $outfile);
        $rama = loadRamachandran($outfile);

        foreach($rama as $r)
        {
            //echo "$filename:$r[chainID]:$r[resType]:".trim($r['resNum'].$r['insCode']).":$r[phi]:$r[psi]:$r[scorePct]:$r[eval]:$r[type]\n";
            $format = "$filename:$r[chainID]:$r[resType]:"."%4d".$r['insCode'].":$r[phi]:$r[psi]:$r[scorePct]:$r[eval]:$r[type]\n";
            echo sprintf($format, $r['resNum']);
        }
        mpDestroySession();
    }
}
closedir($h);

############################################################################
// Clean up and go home
?>
