Ejemplo n.º 1
0
<?php

/**
 * @author:wusuopubupt
 * @date:2013-10-16
 * @from:http://ac.jobdu.com/problem.php?pid=1391
 * 
 * Print matrix in clockwise
 * */
$matrix = array(array(1, 2, 3, 4), array(5, 6, 7, 8), array(9, 10, 11, 12), array(13, 14, 15, 16), array(17, 18, 19, 20));
print_matrix($matrix);
function print_matrix($arr)
{
    $top = 0;
    $left = 0;
    $right = count($arr[0]) - 1;
    $bottom = count($arr) - 1;
    while ($left != $right && $top != $bottom) {
        //top
        for ($j = $left; $j <= $right; $j++) {
            echo $arr[$top][$j] . " ";
        }
        $top++;
        //right
        for ($i = $top; $i <= $bottom; $i++) {
            echo $arr[$i][$right] . " ";
        }
        $right--;
        //bottom
        for ($j = $right; $j >= $left; $j--) {
            echo $arr[$bottom][$j] . " ";
Ejemplo n.º 2
0
function matchrxn($qrxn, $crxn, $cmaps, $options)
{
    global $use_cmmmsrv;
    global $ostype;
    global $MATCHMOL;
    global $n_labels;
    global $n_maps;
    global $map_list;
    global $debug;
    $result = TRUE;
    $rxndescr = analyze_rxnfile($qrxn);
    $nqrmol = get_nrmol($rxndescr);
    $nqpmol = get_npmol($rxndescr);
    $rxndescr = analyze_rxnfile($crxn);
    $ncrmol = get_nrmol($rxndescr);
    $ncpmol = get_npmol($rxndescr);
    $optstr = $options;
    if ($nqrmol == 0 && $ncpmol == 0) {
        $result = FALSE;
    }
    // some initial check
    if ($nqpmol == 0 && $ncrmol == 0) {
        $result = FALSE;
    }
    if ($result == TRUE) {
        if ($use_cmmmsrv == "y") {
            $separator = "\$\$\$\$";
            $lf = "\n";
        } else {
            $separator = "\\\$\\\$\\\$\\\$";
            if ($ostype == 1) {
                $lf = "\n";
            }
            // Linux
            if ($ostype == 2) {
                $lf = "\r\n";
            }
            // Windows
        }
        $allmol = array();
        $qrmol = array();
        $qpmol = array();
        $crmol = array();
        $cpmol = array();
        $allmol = explode("\$MOL\r\n", $qrxn);
        $header = $allmol[0];
        $label_list = array();
        $map_list = array();
        $n_labels = 0;
        $n_maps = 0;
        $n_qmaps = 0;
        $n_cmaps = 0;
        $qmap_list = array();
        $cmap_list = array();
        if ($nqrmol > 0) {
            for ($i = 0; $i < $nqrmol; $i++) {
                $qrmol[$i] = $allmol[$i + 1];
                $mnum = $i + 1;
                if ($debug > 2) {
                    debug_output("query reactant no. {$mnum}:\n{$qrmol[$i]}\n");
                }
            }
        }
        if ($nqpmol > 0) {
            for ($i = 0; $i < $nqpmol; $i++) {
                $qpmol[$i] = $allmol[$i + 1 + $nqrmol];
                $mnum = $i + 1;
                if ($debug > 2) {
                    debug_output("query product no. {$mnum}:\n{$qpmol[$i]}\n");
                }
            }
        }
        $qmaps = get_maps($qrxn);
        $qmap_list = explode(",", $qmaps);
        $n_qmaps = count($qmap_list);
        if (strlen($qmaps) < 8) {
            $n_qmaps = 0;
        }
        $allmol = "";
        $allmol = explode("\$MOL\r\n", $crxn);
        $header = $allmol[0];
        // reset labels and maps
        $n_maps = 0;
        $map_list = "";
        $n_labels = 0;
        $label_list = "";
        if ($ncrmol > 0) {
            for ($i = 0; $i < $ncrmol; $i++) {
                $crmol[$i] = $allmol[$i + 1];
                $mnum = $i + 1;
                if ($debug > 2) {
                    debug_output("candidate reactant no. {$mnum}:\n{$crmol[$i]}\n");
                }
            }
        }
        if ($ncpmol > 0) {
            for ($i = 0; $i < $ncpmol; $i++) {
                $cpmol[$i] = $allmol[$i + 1 + $ncrmol];
                $mnum = $i + 1;
                if ($debug > 2) {
                    debug_output("candidate product no. {$mnum}:\n{$cpmol[$i]}\n)");
                }
            }
        }
        //echo "<pre>$qrmol[0]\n$qpmol[0]\n\n$crmol[0]\n$cpmol[0]</pre>";
        //$cmaps = get_maps($crxn);    // this is now retrieved from rxnstructable
        $cmap_list = explode(",", $cmaps);
        $n_cmaps = count($cmap_list);
        if (strlen($cmaps) < 8) {
            $n_cmaps = 0;
        }
        // set up and initialize the two match matrices (reactant and product)
        $rmm = array();
        for ($iq = 0; $iq < $nqrmol; $iq++) {
            for ($ic = 0; $ic < $ncrmol; $ic++) {
                $rmm[$iq][$ic] = "0";
            }
        }
        $pmm = array();
        for ($iq = 0; $iq < $nqpmol; $iq++) {
            for ($ic = 0; $ic < $ncpmol; $ic++) {
                $pmm[$iq][$ic] = "0";
            }
        }
        if ($n_qmaps > 0 && $n_cmaps > 0) {
            $optstr .= "n";
        }
        $mmcmd = "{$MATCHMOL} {$optstr} -";
        //============ first match the reactants, if there are any
        if ($nqrmol > 0 && $ncrmol > 0) {
            // assemble the SDF files to be passed to matchmol
            for ($iq = 0; $iq < $nqrmol; $iq++) {
                $qmol = rtrim($qrmol[$iq]);
                $sdf = $qmol . $lf . $separator . $lf;
                for ($ic = 0; $ic < $ncrmol; $ic++) {
                    $cmol = rtrim($crmol[$ic]);
                    $sdf .= $cmol . $lf . $separator . $lf;
                }
                // end   for ($ic ...
                //echo "<pre>reactant query for query reactant no. $iq:\n$sdf\n</pre>";
                // now do the match
                if ($use_cmmmsrv == "y") {
                    $matchresult = filterthroughcmmm("{$sdf}", "#### matchmol:{$optstr}");
                } else {
                    if ($ostype == 1) {
                        $matchresult = filterthroughcmd("{$sdf} ", "{$mmcmd}");
                    }
                    if ($ostype == 2) {
                        $sdf = str_replace("\r", "", $sdf);
                        // for Windows
                        $sdf = str_replace("\n", "\r\n", $sdf);
                        // for Windows
                        $matchresult = filterthroughcmd2("{$sdf} ", "{$mmcmd}");
                    }
                }
                //echo "<pre>match result for for query reactant no. $iq:\n$matchresult\n</pre>";
                $br = explode("\n", $matchresult);
                $nr = count($br);
                for ($ir = 0; $ir < $nr; $ir++) {
                    if (strpos($br[$ir], ":T") !== FALSE) {
                        $rmm[$iq][$ir] = $br[$ir];
                    }
                }
            }
            // end   for ($iq ....
            // we have to clean up the matrix to remove multiple hits of one query mol
            if ($nqrmol > 1) {
                $rmm = cleanup_matrix($rmm);
            }
        }
        // end    if ($nqrmol > 0)
        //============ now match the products, if there are any
        if ($nqpmol > 0 && $ncpmol > 0) {
            // assemble the SDF files to be passed to matchmol
            for ($iq = 0; $iq < $nqpmol; $iq++) {
                $qmol = rtrim($qpmol[$iq]);
                $sdf = $qmol . $lf . $separator . $lf;
                for ($ic = 0; $ic < $ncpmol; $ic++) {
                    $cmol = rtrim($cpmol[$ic]);
                    $sdf .= $cmol . $lf . $separator . $lf;
                }
                // end   for ($ic ...
                //echo "<pre>product query for query product no. $iq:\n$sdf\n</pre>";
                // now do the match
                //$sdf = str_replace("\r","",$sdf);
                if ($use_cmmmsrv == "y") {
                    $matchresult = filterthroughcmmm("{$sdf}", "#### matchmol:{$optstr}");
                } else {
                    if ($ostype == 1) {
                        $matchresult = filterthroughcmd("{$sdf} ", "{$mmcmd}");
                    }
                    if ($ostype == 2) {
                        $sdf = str_replace("\r", "", $sdf);
                        // for Windows
                        $sdf = str_replace("\n", "\r\n", $sdf);
                        // for Windows
                        $matchresult = filterthroughcmd2("{$sdf} ", "{$mmcmd}");
                    }
                }
                //echo "<pre>match result for for query product no. $iq:\n$matchresult\n</pre>";
                $br = explode("\n", $matchresult);
                $nr = count($br);
                for ($ir = 0; $ir < $nr; $ir++) {
                    if (strpos($br[$ir], ":T") !== FALSE) {
                        $pmm[$iq][$ir] = $br[$ir];
                    }
                }
            }
            // end   for ($iq ....
            // we have to clean up the matrix to remove multiple hits of one query mol
            if ($nqpmol > 1) {
                $pmm = cleanup_matrix($pmm);
            }
        }
        // end    if ($nqpmol > 0)
        //echo "<pre>\n";
        if ($debug == 4 || $debug == 6) {
            echo "<pre>now the reactant match matrix looks like this:\n";
            print_matrix($rmm);
            echo "and the product match matrix looks like this:\n";
            print_matrix($pmm);
            echo "</pre>";
        }
        // and finally we check if every query structure matches a candidate structure
        // first check: reactants
        $foundallreactants = TRUE;
        for ($iq = 0; $iq < $nqrmol; $iq++) {
            $foundthisreactant = FALSE;
            for ($ic = 0; $ic < $ncrmol; $ic++) {
                if (strpos($rmm[$iq][$ic], ":T") !== FALSE) {
                    $foundthisreactant = TRUE;
                }
            }
            if ($foundthisreactant == FALSE) {
                $foundallreactants = FALSE;
            }
        }
        // second check: products
        $foundallproducts = TRUE;
        for ($iq = 0; $iq < $nqpmol; $iq++) {
            $foundthisproduct = FALSE;
            for ($ic = 0; $ic < $ncpmol; $ic++) {
                if (strpos($pmm[$iq][$ic], ":T") !== FALSE) {
                    $foundthisproduct = TRUE;
                }
            }
            if ($foundthisproduct == FALSE) {
                $foundallproducts = FALSE;
            }
        }
        // now let's draw the conclusion:
        if ($foundallreactants == FALSE || $foundallproducts == FALSE) {
            $result = FALSE;
        } else {
            // now we should consider atom mappings, if present
            if ($n_qmaps > 0 && $n_cmaps > 0) {
                $allmapsconfirmed = TRUE;
                for ($iqm = 0; $iqm < $n_qmaps; $iqm++) {
                    $thismapconfirmed = FALSE;
                    if ($debug > 2) {
                        debug_output("now checking query map no. {$iqm}: {$qmap_list[$iqm]}\n");
                    }
                    // split map into reactant and product part
                    $qmarr = explode("=", $qmap_list[$iqm]);
                    $qrpart = $qmarr[0];
                    $qppart = $qmarr[1];
                    if ($debug > 2) {
                        debug_output("query reactant: {$qrpart}     query product: {$qppart}\n");
                    }
                    // extract mol id and atom id
                    $qrarr = explode(":", $qrpart);
                    $qrmid = $qrarr[0];
                    $qrmidn = str_replace("r", "", $qrmid);
                    $qrmidn = intval($qrmidn) - 1;
                    $qraid = $qrarr[1];
                    $qparr = explode(":", $qppart);
                    $qpmid = $qparr[0];
                    $qpmidn = str_replace("p", "", $qpmid);
                    $qpmidn = intval($qpmidn) - 1;
                    $qpaid = $qparr[1];
                    // now look up in the reactant match matrix which candidate reactant(s)
                    // match with this query reactant
                    if ($debug > 2) {
                        debug_output("   checking reactant match matrix for query reactant {$qrmid} ({$qrmidn})\n");
                    }
                    for ($irmm = 0; $irmm < $ncrmol; $irmm++) {
                        if ($rmm[$qrmidn][$irmm] !== "0") {
                            // assemble the id string
                            $crmidn = $irmm;
                            $crmid = $crmidn + 1;
                            $crmid = "r" . $crmid . ":";
                            if ($debug > 2) {
                                debug_output("   ---- there is a matching candidate reactant: {$crmid} ({$crmidn})\n");
                            }
                            // now check if there is a suitable counterpart on the product side
                            if ($debug > 2) {
                                debug_output("   checking product match matrix for query product {$qpmid} ({$qpmidn})\n");
                            }
                            for ($ipmm = 0; $ipmm < $ncpmol; $ipmm++) {
                                if ($pmm[$qpmidn][$ipmm] !== "0") {
                                    // assemble the id string
                                    $cpmidn = $ipmm;
                                    $cpmid = $cpmidn + 1;
                                    $cpmid = "p" . $cpmid . ":";
                                    if ($debug > 2) {
                                        debug_output("   ---- there is a matching candidate product: {$cpmid} ({$cpmidn})\n");
                                    }
                                    // now we can check the atom mappings....
                                    // first, go through the list of candidate maps and look for a map
                                    for ($icm = 0; $icm < $n_cmaps; $icm++) {
                                        $cmarr = explode("=", $cmap_list[$icm]);
                                        $crpart = $cmarr[0];
                                        $cppart = $cmarr[1];
                                        if ($debug > 2) {
                                            debug_output("  checking query map {$iqm} against candidate map {$icm}\n");
                                        }
                                        if ($debug > 2) {
                                            debug_output("    ==== candidate reactant: {$crpart}     candidate product: {$cppart}\n");
                                        }
                                        // extract mol id and atom id
                                        $crarr = explode(":", $crpart);
                                        $crmid2 = $crarr[0];
                                        $crmid2n = str_replace("r", "", $crmid2);
                                        $crmid2n = intval($crmid2n) - 1;
                                        $craid2 = trim($crarr[1]);
                                        $cparr = explode(":", $cppart);
                                        $cpmid2 = $cparr[0];
                                        $cpmid2n = str_replace("p", "", $cpmid2);
                                        $cpmid2n = intval($cpmid2n) - 1;
                                        $cpaid2 = trim($cparr[1]);
                                        // check for presence of both mol identifiers
                                        if (strpos($crpart, $crmid) !== FALSE && strpos($cppart, $cpmid) !== FALSE) {
                                            if ($debug > 2) {
                                                debug_output("    **** found a map: {$cmap_list[$icm]}\n");
                                            }
                                            // now do some atom-number translation...
                                            // first, get the atom match matrix for this pair of reactants
                                            $rmmitem = $rmm[$qrmidn][$irmm];
                                            $rmmitemarr = explode(":T ", $rmmitem);
                                            $rmmitem = $rmmitemarr[1];
                                            // next, get the atom match matrix for this pair of products
                                            $pmmitem = $pmm[$qpmidn][$ipmm];
                                            $pmmitemarr = explode(":T ", $pmmitem);
                                            $pmmitem = $pmmitemarr[1];
                                            if ($debug > 2) {
                                                debug_output("    reactant atom map: {$rmmitem}\n");
                                                debug_output("    product atom map:  {$pmmitem}\n");
                                            }
                                            // now extract all possible orientations from the atom maps
                                            $ror = explode(".", $rmmitem);
                                            $n_ror = count($ror);
                                            $por = explode(".", $pmmitem);
                                            $n_por = count($por);
                                            for ($i_ror = 0; $i_ror < $n_ror; $i_ror++) {
                                                if ($debug > 4) {
                                                    debug_output("    trying reactant orientation no. {$i_ror}: {$ror[$i_ror]}\n");
                                                }
                                                $ratom = $ror[$i_ror];
                                                $ratomarr = explode(";", $ratom);
                                                foreach ($ratomarr as $ra) {
                                                    //echo "    reactant atom pair: $ra\n";
                                                    $ra2arr = explode("=", $ra);
                                                    $rqa = $ra2arr[0];
                                                    $rcalist = "";
                                                    $rcalist = $ra2arr[1];
                                                    if ($debug > 4) {
                                                        debug_output("    query reactant atom {$rqa} corresponds to candidate reactant atom(s): {$rcalist}\n");
                                                    }
                                                    if ($qraid == $rqa) {
                                                        if ($debug > 4) {
                                                            debug_output("    .... this is interesting! (rqa = {$rqa})\n");
                                                        }
                                                        // get all sub-orientations
                                                        $rca2arr = explode(",", $rcalist);
                                                        foreach ($rca2arr as $rca) {
                                                            $rca = trim($rca);
                                                            // this is important!
                                                            if ($debug > 4) {
                                                                debug_output("    labelled query reactant atom {$rqa} corresponds to candidate reactant atom: {$rca}\n");
                                                                debug_output("      craid2 = {$craid2} rca = {$rca} rqa = {$rqa}\n");
                                                            }
                                                            if ($rca == $craid2) {
                                                                if ($debug > 4) {
                                                                    debug_output("    found a label counterpart for {$qraid}: {$craid2}!!\n");
                                                                    debug_output("    who is the counterpart of query product atom {$qpaid}? is it {$cpaid2}?\n");
                                                                }
                                                                // now look up the product end...
                                                                // check all possible reactant orientations....
                                                                for ($i_por = 0; $i_por < $n_por; $i_por++) {
                                                                    if ($debug > 4) {
                                                                        debug_output("    trying product orientation no. {$i_por}: {$por[$i_por]}\n");
                                                                    }
                                                                    $patom = $por[$i_por];
                                                                    $patomarr = explode(";", $patom);
                                                                    foreach ($patomarr as $pa) {
                                                                        //echo "    product atom pair: $pa\n";
                                                                        $pa2arr = explode("=", $pa);
                                                                        $pqa = trim($pa2arr[0]);
                                                                        $pcalist = "";
                                                                        $pcalist = $pa2arr[1];
                                                                        if ($debug > 6) {
                                                                            debug_output("        query product atom {$pqa} corresponds to candidate product atom(s): {$pcalist}\n");
                                                                        }
                                                                        if ($qpaid == $pqa) {
                                                                            if ($debug > 6) {
                                                                                debug_output("        NOW IT BECOMES INTERESTING!\n");
                                                                            }
                                                                            // again, get all sub-orientations
                                                                            $pca2arr = explode(",", $pcalist);
                                                                            foreach ($pca2arr as $pca) {
                                                                                $pca = trim($pca);
                                                                                // this is important!
                                                                                if ($debug > 6) {
                                                                                    debug_output("            query product atom {$qpaid} corresponds to candidate product atom: {$pca}\n");
                                                                                }
                                                                                if ($cpaid2 == $pca) {
                                                                                    $thismapconfirmed = TRUE;
                                                                                    if ($debug > 2) {
                                                                                        debug_output("BINGO!!!! confirmed query map no. {$iqm}\n");
                                                                                    }
                                                                                    break 7;
                                                                                    // immediately stop looping (7 depth levels!)
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if ($thismapconfirmed == FALSE) {
                        $allmapsconfirmed = FALSE;
                        if ($debug > 2) {
                            debug_output("==== could not confirm query map no. {$iqm}\n");
                        }
                    }
                }
                if ($allmapsconfirmed == FALSE) {
                    $result = FALSE;
                }
            }
            // end of "if we have both maps..."
        }
    }
    // end if ($result == TRUE) from initial check
    return $result;
}