php的execute_sql没有将数据插入数据库

时间:2018-04-02 07:55:56

标签: php mysql docker-compose

我提交了这两个文件:

由于sample_synteny.txt 304420020185300000016_synteny``仅包含列名mgsvfunction syn($filename)数据库似乎未在SELECT * FROM数据库的帮助下加载org1, org1_start, org1_end, org2, org2_start, org2_end, length, SYNcolor没有任何数据。在处理此文件期间,我得到以下输出:

insert into 304530020182800000020_synteny (org1, org1_start, org1_end, org2, org2_start, org2_end, length, SYNcolor) VALUES('Organism_A','25787','22159','Organism_B','18060','25102','20','3.0E-3','3629','#8953BA')
...

另一方面,sample_annotation.txt已在mgsv的帮助下加载function annotation($annfilename),因为SELECT * FROM 304420020185300000016_annotation``为我提供了一系列值({{1} }})。在处理此文件期间,我得到以下输出:

org_id, start, end, strand, feature_name, feature_value, track_name, track_shape, track_color

insert into 304500120185700000019_annotation (org_id, start, end, strand, feature_name, feature_value, track_name, track_shape, track_color) VALUES('Organism_C','42','3972','+','XG001','','gene','arrow','brown') ... 如下所示,导致上述错误:

mgsv/index.php

我创建了一个doker-compose.yml,可以通过以下方式运行:

<?php
$link = mysql_connect($database_host, $database_user, $database_pass);
if($link == FALSE){
    echo 'Cannot connect to database';
}

@mysql_select_db($database_name) or die("Unable to connect to database");

function execute_sql($sql){
    $result = mysql_query($sql);
    return $result;
}

function synteny_table($newsession_id, $synfilename) {
    ## initialize bool
    $bool = 'true';

    ## Get the first line of the file
    $header_line = exec("head -n 1 tmp/" . $newsession_id . $synfilename);
    ## split the line into items
    $header_pieces = explode("\t",$header_line);

    ## the file header line is good
    if( $bool == 'true'){
        ## Initialize a string to create fields for additional columns
        $userdef = "";
        ## Iterate from the 7th column till the end
        for( $i = 6; $i < count( $header_pieces); $i++){
            $userdef .= "`".$header_pieces[$i]."`"."varchar(100) DEFAULT NULL,";
        }
        ## Check if there is a length function
        $check_length = array_search('length', $header_pieces);
        ## if not create a column
        if($check_length == '') {
            $userdef .= "`length` varchar(100) DEFAULT NULL,";
        }
        ## get the create statement
        $createTable = "CREATE TABLE `{$newsession_id}_synteny` (
            `id` int(10) NOT NULL AUTO_INCREMENT,
            `org1` varchar(250) NOT NULL,
            `org1_start` int(10) unsigned NOT NULL,
            `org1_end` bigint(20) unsigned NOT NULL,
            `org2` varchar(250) NOT NULL,
            `org2_start` int(10) unsigned NOT NULL,
            `org2_end` bigint(20) unsigned NOT NULL,
            $userdef
            `SYNcolor` varchar(8) DEFAULT NULL,
            PRIMARY KEY (`id`),
            KEY `org1_start` (`org1_start`),
            KEY `org1_end` (`org1_end`),
            KEY `org2_start` (`org2_start`),
            KEY `org2_end` (`org2_end`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;";
        ## execute the 
        $result = mysql_query($createTable);
        if(! $result){
            $bool = "Unable to create the synteny table.<br>" . mysql_error();
        }
    }
    ## return the boolean information
    return $bool;
}

function desc_synteny_table($newsession_id) {
    $cnames = array();
    $sql = "DESCRIBE `{$newsession_id}_synteny`";
    $result = mysql_query($sql);
    if( ! $result){
        return mysql_error();
    }
    while( $row = mysql_fetch_assoc( $result)){
        array_push($cnames, $row['Field']);
    }
    return $cnames;
}

function getRandomColorHex($max_r = 255, $max_g = 255, $max_b = 255) {
    return sprintf( '#%02X%02X%02X', rand(0,$max_r), rand(0,$max_g), rand(0,$max_b) );
}

function syn($filename) {
    $bool_page = 'true';
    #echo "($filename[0])<br>";
    ## Get the global values
    global $upload_dir, $newsession_id, $email;
    #echo "($upload_dir)<br>";
    ## Get the first line
    $header_line = exec( "head -n 1 mgsv/tmp/".$newsession_id . $filename);
    if($header_line == ''){
        $bool_page = 'Synteny file: Unable to store in /tmp folder.';
        return $bool_page;
    }
    ## convert into array
    $header_pieces = explode( "\t", $header_line);

    ## check if the first item starts with #
    if( ! preg_match("/^#/i", $header_pieces[0])) {
        $bool_page = 'Synteny file: The # symbol at line 1 is not present.';
        return $bool_page;
    }

    ## Replace the # for further analysis
    $header_pieces[0] = str_replace('#', '', $header_pieces[0]);

    ## Get the fixed column names
    $fixed_org = array('org1', 'org1_start', 'org1_end', 'org2', 'org2_start', 'org2_end');

    ## Check if the header matches our column names
    ## for each fixed name
    for($i = 0; $i < sizeof($fixed_org); $i++){
        ## if the fixed name doesnt match the file column name
        if( $header_pieces[$i] != $fixed_org[$i]) {
            ## raise error
            $bool_page = "Synteny file: Column No." . ($i + 1) . " header should be '" . $fixed_org[$i] . "'.";
            return $bool_page;
        }
    }

    ## Check if the number of columns is the same as column headers
    $second_line = exec( "head -n 2 mgsv/tmp/" . $newsession_id . $filename . "| tail -n 1");
    $second_pieces = explode( "\t", $second_line);
    if( sizeof($header_pieces) != sizeof($second_pieces)){
        $bool_page = "Synteny file: There are " . sizeof($header_pieces) . " column names but " . sizeof($second_pieces) . " columns.";
        return $bool_page;
    }

    ## For the non-fixed column names
    for($i = 6; $i < sizeof($header_pieces); $i++){
        ## make sure they are not empty
        if( preg_match("/^\s*$/i", $header_pieces[$i])) {
            $bool_page = "Synteny file: Column No." . ($i + 1) . " header is blank.";
            return $bool_page;
        }
    }

    ## Check if the column names are unique
    if( count( array_unique( $header_pieces)) < count( $header_pieces)) {
        $bool_page = 'Synteny file: There are more than one column with the same name';
        return $bool_page;
    }

    ## create the table in the database
    $bool_page = synteny_table( $newsession_id, $filename);
    if($bool_page != 'true'){
        return $bool_page;
    }

    ## Get the field names
    $cnames = desc_synteny_table($newsession_id);
    if(is_string($cnames)){
        return $cnames;
    }

    ## Remove the id field name
    array_shift($cnames);
    ## get a string from the array
    $insert_into = implode(', ', $cnames);


    ## look for column name 'length'
    $check_length = array_search('length', $header_pieces);

    ## define pattern
    $pattern = '/^.*\t[0-9]+\t[0-9]+\t.*\t[0-9]+\t[0-9]+';

    ## Get the starting index of the new items
    $newitems = count($header_pieces) - 7;
    ## If no addition items are provided
    for ($i = 0; $i <= $newitems; $i++) {
        ## generate the regex for each additional column
        $pattern .= '\t[0-9\.Ee\-\+]*';
    }
    ## close the $pattern here.
    $pattern .= '$/';

    ## create a read file handle to the uplaoded file
    $fh = fopen($upload_dir . $newsession_id . $filename, "r");
    ## Remove the first line
    fgets($fh);

    ## initialize a line_number to know
    $line_number = 1;
    while( ! feof( $fh)){
        ## increment it
        $line_number++;
        ## get each line
        $line = fgets($fh);
        ## continue if it is an empty line
        if($line == ''){ continue; }
        ## trim for end of line characters
        $line = trim($line);
        ## compare with the pattern
        $correct_format = preg_match($pattern, $line);
        if($correct_format == false){
            $bool_page = "Synteny File: line number $line_number, is not in the correct format";
            break;
        }
        #echo "($line)<br>";
        $values = "VALUES(";
        $num_of_items = explode("\t", $line);
        for ($jj = 0; $jj < count($num_of_items); $jj++) {
            $values .= "'" . $num_of_items[$jj] . "',";
        }
        ## check for the first 250 characters of org1 and org2
        if(substr($num_of_items[0], 0, 250) == substr($num_of_items[3], 0, 250)){
            ## If they are similar they throw an error and quit
            $bool_page = "Synteny File: Org1 and Org2 are similar at line $line_number.";
            break;
        }
        ## If there is no column called length in the input synteny file
        if($check_length == false){
            ## get the difference between start and end of org2
            $org2 = abs($num_of_items[4] - $num_of_items[5]) + 1;
            ## get the difference between start and end of org1
            $org1 = abs($num_of_items[1] - $num_of_items[2]) + 1;
            ## get the minimum of the above two lengths
            $minlength = min($org1, $org2);
            ## concatenate this minimum value to the input command
            $values .= "'" . $minlength . "',";
        }
        ## generate a random color to store each synteny
        $values .= "'" .  getRandomColorHex() . "')";

        ## If everything is good
        if( $bool_page == 'true'){
            ## Insert the values into the table
            $sql = "insert into {$newsession_id}_synteny ($insert_into) $values";
            ## show the statement
            #echo "insert command: $sql<br>";
            ## execute the statement
            $result = execute_sql($sql);
        }
    }
    ## Delete the files from server
    if(file_exists($upload_dir . $newsession_id . $filename)){
        unlink($upload_dir . $newsession_id . $filename);
    }
    ## return the boolean parameter
    return $bool_page;
}

可以通过git clone https://github.com/mictadlo/mGSV-docker.git docker-compose up --build 访问该服务,可以通过localhost访问PhpMyAdmin

我错过了什么?

提前谢谢

0 个答案:

没有答案