手记

php 转化smiles为分子式

近来在使用PHP进行结构式搜索的时候,发现获取到smiles无法进行查询操作,然后想到了转化为分子式的操作。主要是用在有机物。


困难一:我在smiles 上利用正则匹配C,O。遇到了问题就是其他元素也有C字母,所以无法正确去掉C


解决:我只用原来的smiles来分析有机物的组成,然后我按照有机物的结构,单独找出来C,O的数量,其余元素,简单统计一下放在后面就好了。主要分为三部分,C数目,O数目,其他元素


实现;1.前台获取的smiles,符合相应的规则

    2.php处理

    


                                        $Cnum='';                    $Onum='';                    //print($smiles."原来的");                    $find=array("=","#",".","1","[","]","(",")");                    $replace = array("");                    $smiles=str_replace($find,$replace,$smiles,$j);                    //print($j);                    //ECHO '/n';                    $ChemElement= array("Li","Be","Na","Mg","Al","Si","Cl","Br","Ca","Cr","Mn","Fe","Co","Ni","Cu","Zn","Ga","Gc","Ag","Au");                    foreach($ChemElement as $value){                        //print($value);                        $k_x=substr_count($smiles,$value);                        if($k_x>0){                            $k_x=$k_x==1?'':$k_x;                            //str_replace($value,'',$smiles);                            $smiles_new.=$value.$k_x;                        }                    }                    $k_c=substr_count($smiles,'C');                    //print('C个数'.$k_c);                    $i_c=preg_match_all('/C[a-z]/m',$smiles);                    //print('非碳个数'.$i_c);                    $j_c=$k_c-$i_c;                    //print_r('碳个数'.$j_c);                    //$smiles=preg_replace('/C[0-9A-Z/.]/m','',$smiles,-1,$count);                    //print($smiles);                    //print('替换次数'.$count);                    $k_o=substr_count($smiles,'O');                    //print('O个数'.$k_o);                    $i_o=preg_match_all('/O[a-z]/m',$smiles);                    //print('非氧个数'.$i_o);                    $j_o=$k_o-$i_o;                    //$smiles=preg_replace('/C[0-9A-Z/.]/m','',$smiles,-1,$count);                    //print($smiles);                    //print('替换次数'.$count);                    if($j_c>0){$j_c=$j_c==1?'':$j_c;$Cnum='C'.$j_c;}                    if($j_o>0){$j_o=$j_o==1?'':$j_o;$Onum='O'.$j_o;}                    $smilesPara = $Cnum.$Onum.$smiles_new;                                           结果:基本上可以解决一般意义上的分子式,当然元素我没有写全,我认为常用的写一下就好,本来就是为了搜索,不常见的物质,化学品库里也没有。 这里推荐一下 斯芬克斯的PHP搜索引擎,很不错。


0人推荐
随时随地看视频
慕课网APP