Location: PHPKode > projects > Generic Genetic Studies Database > ggsd-2.19/cgi-bin/genotype_dump.php
<?php

#Copyright 2008 Aaron Day-Williams <hide@address.com>
#
#This file is part of Generic Genetic Studies Database (GGSD).
#
#GGSD is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#GGSD is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with GGSD.  If not, see <http://www.gnu.org/licenses/>.


header('Content-type: text/html');
session_cache_limiter('private');
session_start();
require 'sql_lib.inc';
require 'html_lib.inc';
require 'file_system_lib.inc';

#NAME	genotype_dump.php

#TYPE	PHP

#USES	sql_lib.inc(authenticate tableQuery)
#	html_lib.inc(printGenotypeCheckList printPhenotypeCheckList printStatusSelectList)

#PURPOSE To present the user of a list of all the genotypes in the database for download.

#USAGE	genotype_dump.php

#PARAMETERS
#	authorized user	auth_user	string		optional
#	status		status		string		optional
#       want phenotypes want_phenotypes int             optional
#	phenotype ids	pheno_ids	checkbox list	optional

#RETURNS
#	genotype records	genotype_record	checkbox list	if success
#	error message				string		if error

#DATASTRUCTURES	The parameters described as checkbox list is a list of checkboxes
#		that all have the same name but different values.  The result if
#		multiple of the checkboxes are checked is an array of values in the
#		name of the checkbox.

#FUNCTIONAL DESCRIPTION The script will authorize the user.  Then ask which affection
#		       status the user wants to download and whether they want phenotype
#		       data.  If they want phenotype data they will get a checkbox of available
#		       phenotypes.  Then they will get button to download the data they requested.

#ASSUMPTIONS That the user wants to see all genotypes for every person in the database.

#LIMITATIONS No restrictions to which genotypes are downloaded.  

#COMMENTS

#PRELIMINARY DESIGN
#	    1	Get Args
#	    2	Authorize User
#	    3	Get Status and Whether want Phenotypes
#	    4	Print Phenotype CheckList
#	    5	Print Genotype Records

echo "<html>\n";
echo " <head>\n";
if (isset($_POST['end_session'])){echo "  <META HTTP-EQUIV=Refresh CONTENT=\"$logout_refresh_time;URL=../right.html\">\n";}
else                             {echo "  <META HTTP-EQUIV=Refresh CONTENT=\"$normal_refresh_time; URL=$_SERVER[SCRIPT_NAME]\">\n";}
echo "  <title>Genotype Dump</title>\n";
echo " </head>\n";
 
echo "<body bgcolor = \"white\">\n";
echo "<center><h3>Genotype Dump</h3></center>\n";
echo "<hr>\n";
printHomeNavBar();
echo "<br>\n";


#1	Get Args
$access_type_required  = 'search';
$status                = $_POST['status'];
$want_phenotypes       = $_POST['want_phenotypes'];
$pheno_multiple_pages  = $_POST['pheno_multiple_pages'];
$pheno_ids             = $_POST['pheno_id'];
$table_name            = $_POST['table_name'];
$field_list            = $_POST['field_list'];
$term                  = $_POST['term'];
$mode                  = $_POST['mode'];
$join                  = $_POST['join'];
$order_by              = $_POST['order_by'];
$function_to_call      = $_POST['function_to_call'];
$last_record_retrieved = $_POST['last_record_retrieved'];
$create_analysis_files = $_POST['create_analysis_files'];
$analysis_option       = $_POST['analysis_option'];
$analysis_sub_option   = $_POST['analysis_sub_option'];
$r_plot_statistics     = $_POST['r_plot_statistics'];

$ret_val = connect();
if ($ret_val != '1')
  {
    $date = date('Y-m-d H:i:s');
    echo "Error #1: $date<br>\n";
    echo "Error in call to connect from genotype_dump.php<br>\n";
    echo "$ret_val<br><br>\n";
    echo "Please email the above error using the Comments/Help/Support link \n";
    echo "in the left navigation bar.<br>\n";
    echo "</body></html>\n";
    exit;
  }


#2	Authenticate User
if (isset($_POST['end_session']))#if want to end session
  {
    #if (key_exists('files_on_server',$_SESSION))
    #  {
    #    $files_to_delete_array = $_SESSION['files_on_server'];
    #    $ret_val = deleteFiles($files_to_delete_array);
    #    if ($ret_val != '1')
    #      {
    #        $date = date('Y-m-d H:i:s');
    #        echo "Error #2: $date<br>\n";
    #        echo "Error in call to deleteFiles from genotype_dump.php<br>\n";
    #        echo "$ret_val<br><br>\n";
    #        echo "Please email the above error using Comments/Help/Support link \n";
#	    echo "in the left navigation bar.<br>\n";
#	    echo "</body></html>\n";
#	    session_unset();
#	    session_destroy();
#	    $ret_val = disconnect();
#	    exit;
#	  }#end error in call to deleteFiles
#      }#end if files that need to be deleted

    if (is_dir("../data/download_data/$_SESSION[user_name]"))
      {
        $ret_val = deleteExpiredFiles($_SESSION['user_name']);
        if ($ret_val != '1')
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #3: $date<br>\n";
            echo "Error in call to deleteExpiredFiles from genotype_dump.php<br>\n";
            echo "$ret_val<br><br>\n";
            echo "Please email the above error using Comments/Help/Support link \n";
	    echo "in the left navigation bar.<br>\n";
	    echo "</body></html>\n";
	    session_unset();
	    session_destroy();
	    $ret_val = disconnect();
	    exit;
	  }#end error in call to deleteExpiredFiles
      }#end if user has own directory in download_data directory
    session_unset();
    session_destroy();
    echo "You have successfully logged out of the system.<br>\n";
    #echo "To log back in click on any of the links on the left navigation bar.<br>\n";
    $ret_val = disconnect();
    exit;
  }#end if want to end session
else#don't want to end session
  {
    if ((!session_is_registered('user_name')) and (!session_is_registered('user_passwd')))
      {
	if ((!preg_match('/\w+/',$_POST['user_name'])) and (!preg_match('/\w+/',$_POST['user_passwd'])))
	  {
	    $ret_val = printLoginPage('genotype_dump.php');
            if ($ret_val != '1')
              {
                $date = date('Y-m-d H:i:s');
		echo "Error #4: $date<br>\n";
		echo "Error in call to printLoginPage from genotype_dump.php<br>\n";
		echo "$ret_val<br><br>\n";
		echo "Please email the above error using Comments/Help/Support link \n";
		echo "in the left navigation bar.<br>\n";
		echo "</body></html>\n";
		session_unset();
		session_destroy();
		$ret_val = disconnect();
		exit;
	      }#end if error in call to printLoginPage
	  }#end if haven't started a session yet
        else #have started a session
	  {
	    $auth_user = authenticate($_POST['user_name'],$_POST['user_passwd'],$_POST['rand'],$access_type_required,$_POST['database_id']);
	    if (($auth_user != '1') and ($auth_user != '0'))
	      {
		$date = date('Y-m-d H:i:s');
		echo "Error #5: $date<br>\n";
		echo "Error in call to authenticate from genotype_dump.php<br>\n";
		echo "$auth_user<br>\n";
		echo "Please email the above error using Comments/Help/Support link \n";
		echo "in the left navigation bar.<br>\n";
		echo "</body></html>\n";
		session_unset();
		session_destroy();
		$ret_val = disconnect();
		exit;
	      }
            if ($auth_user == '0')
	      {
		echo "You are not an authorized user of this website!!<br><br>\n";
		echo "You must be an authorized user to access the data contained in this site.\n";
		echo "If you feel like you should be an authorized user, or want to see if you can\n";
		echo "become an authorized user please contact $contact_person at \n";
                echo "<a href=\"mailto:$contact_email\">$contact_email</a>\n";
		echo "</body></html>\n";
		session_unset();
		session_destroy();
		$ret_val = disconnect();
		exit;
	      }
            if ($auth_user == '1')
	      {
		$_SESSION['user_name']   = $_POST['user_name'];
		$_SESSION['user_passwd'] = $_POST['user_passwd'];
                $_SESSION['database_id'] = $_POST['database_id'];
		$_SESSION['rand']        = $_POST['rand'];
                $_SESSION['entry_time']  = time();
		session_write_close();
	      }
	  }#end else have user name an passwd
      }#end if session variables not registered
    else #session variable are registered
      {
        if ((time() - $_SESSION['entry_time']) > $session_expiration_time)#if session has expired
          {
            #if (key_exists('files_on_server',$_SESSION))
            #  {
            #    $files_to_delete_array = $_SESSION['files_on_server'];
            #    $ret_val = deleteFiles($files_to_delete_array);
            #    if ($ret_val != 1)
            #      {
            #        $date = date('Y-m-d H:i:s');
            #        echo "Error #6: $date<br>\n";
            #        echo "Error in call to deleteFiles from genotype_dump.php<br>\n";
            #        echo "$ret_val<br><br>\n";
            #        echo "Please email the above error using Comments/Help/Support link \n";
	#	    echo "in the left navigation bar.<br>\n";
        #            session_unset();
        #            session_destroy();
        #            $ret_val = disconnect();
        #            exit;
        #          }#end if error in call to deleteFiles
        #      }#end if files that need to be deleted

            if (is_dir("../data/download_data/$_SESSION[user_name]"))
              {
                $ret_val = deleteExpiredFiles($_SESSION['user_name']);
                if ($ret_val != 1)
                  {
                    $date = date('Y-m-d H:i:s');
                    echo "Error #7: $date<br>\n";
                    echo "Error in call to deleteExpiredFiles from genotype_dump.php<br>\n";
                    echo "$ret_val<br><br>\n";
                    echo "Please email the above error using Comments/Help/Support link \n";
		    echo "in the left navigation bar.<br>\n";
                    session_unset();
                    session_destroy();
                    $ret_val = disconnect();
                    exit;
                  }#end if error in call to deleteExpiredFiles
              }#end if user has a data directory
            session_unset();
            session_destroy();

            echo "<b>Your Session has expired.  You must log back into the system.</b><br><br><br>\n";
            $ret_val = printLoginPage('genotype_dump.php');
            if ($ret_val != '1')
              {
                $date = date('Y-m-d H:i:s');
		echo "Error #8: $date<br>\n";
		echo "Error in call to printLoginPage from genotype_dump.php<br>\n";
		echo "$ret_val<br><br>\n";
		echo "Please email the above error using Comments/Help/Support link \n";
		echo "in the left navigation bar.<br>\n";
		echo "</body></html>\n";
		session_unset();
		session_destroy();
		$ret_val = disconnect();
		exit;
	      }#end if error in call to printLoginPage
          }#end if session has expired 
        else #session hasn't expired
          {
	    if ((!session_is_registered('database_id')) and (!isset($_POST['database_id'])))
	      {
		$ret_val = printDatabaseSelectPage('genotype_dump.php');
		if ($ret_val != '1')
		  {
		    $date = date('Y-m-d H:i:s');
		    echo "Error #9: $date<br>\n";
		    echo "Error in call to printDatabaseSelectPage from genotype_dump.php<br>\n";
		    echo "$ret_val<br><br>\n";
		    echo "Please email the above error using Comments/Help/Support link \n";
		    echo "in the left navigation bar.<br>\n";
		    echo "</body></html>\n";
		    session_unset();
		    session_destroy();
		    $ret_val = disconnect();
		    exit;
		  }#end if error in call to printDatabaseSelectPage
	      }#end if the database id session variable isn't set and haven't selected
	    else #all the session variables are set
	      {
		$user_name   = $_SESSION['user_name'];
		$user_passwd = $_SESSION['user_passwd'];
		if (session_is_registered('database_id')){$database_id = $_SESSION['database_id'];}
		else #set database id session variable
		  {
		    $database_id = $_POST['database_id']; 
		    $_SESSION['database_id'] = $_POST['database_id'];
		  }#end else set database id session variable
		$rand        = $_SESSION['rand'];
		$_SESSION['entry_time'] = time();
		$auth_user = authenticate($user_name,$user_passwd,$rand,$access_type_required,$database_id);
		if (($auth_user != '1') and ($auth_user != '0'))
		  {
		    $date = date('Y-m-d H:i:s');
		    echo "Error #10: $date<br>\n";
		    echo "Error in call to authenticate from genotype_dump.php<br>\n";
		    echo "$auth_user<br>\n";
		    echo "Please email the above error using Comments/Help/Support link \n";
		    echo "in the left navigation bar.<br>\n";
		    echo "</body></html>\n";
		    session_unset();
		    session_destroy();
		    $ret_val = disconnect();
		    exit;
		  }
		if ($auth_user == '0')
		  {
		    echo "You are not an authorized user of this website!!<br><br>\n";
		    echo "You must be an authorized user to access the data contained in this site.\n";
		    echo "If you feel like you should be an authorized user, or want to see if you can\n";
		    echo "become an authorized user please contact $contact_person \n";
		    echo "at <a href=\"mailto:$contact_email\">$contact_email</a>\n";
		    echo "</body></html>\n";
		    session_unset();
		    session_destroy();
		    $ret_val = disconnect();
		    exit;
		  }
	      }#end else all the session variables are set
          }#end else session hasn't expired
      }#end else registered values
  }#end else not ending session
  
if ($auth_user == '1')
  {
    $database_id = $_SESSION['database_id'];
    #3   Get Status and Whether want Phenotypes
    if (!preg_match('/\w+/',$status))
      {
	echo "<form action=\"genotype_dump.php\" name=\"form\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(this))\">\n";
	echo "This page allows you to download all the genotype information for every individual for every marker in the database. \n";
	echo "You must select a status you want to investigate for these genotypes.  You may also select whether you want \n";
	echo "phenotype information downloaded as well.<br><br>\n";
	
        echo "<b>Affection Status:</b> &nbsp;\n";
	$jscript = printStatusSelectList($database_id);
        if ($jscript == '0')
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #11: $date<br>\n";
            echo "Error in call to printStatusSelectList from genotype_dump.php<br>\n";
            echo "See error message from printStatusSelectList above for an explanation.<br><br>\n";
            echo "Please email the above error using Comments/Help/Support link \n";
	    echo "in the left navigation bar.<br>\n";
            echo "</form>\n";
	    echo "</body></html>\n";
            $ret_val = disconnect();
            exit;
          }#end error in call to printStatusSelctList
        if ($jscript == 'no_defined_statuses')
          {
            $date = date('Y-m-d H:i:s');
            echo "This message was generated at $date<br><br>\n";
            echo "Continue on to next page without a status.<br>\n";
            echo "<input type=\"hidden\" name=\"status\" value=\"no_defined_statuses\">\n";
            $jscript = "{return true;}";
          }

	echo "<br><br>\n";
	echo "<input type=\"radio\" name=\"want_phenotypes\" value=\"0\" checked>I Do Not Want Phenotype Information<br>\n";
	echo "<input type=\"radio\" name=\"want_phenotypes\" value=\"1\">I Do Want Phenotype Information<br>\n";
		
	echo "<center>\n";
	echo "<input type=\"submit\" name=\"submit\" value=\"Get Genotypes\">\n";
	echo "<input type=\"reset\" name=\"reset\" value=\"Reset Selections\">\n";
        echo "</form>\n";
        echo "</center>\n";
        echo "<br><br>\n";

        $ret_val = printLogOutButton('genotype_dump.php');
        echo "\n\n";

	echo "<script>\n";
	echo "function Validate(form)\n";
	echo "$jscript\n";
	echo "</script>\n";
      }#end if not status

    #4 Print Phenotype CheckList
    if ((preg_match('/\w+/',$status)) and
	($want_phenotypes == '1') and
        (!preg_match('/\d/',$pheno_multiple_pages))
	)
      {
        echo "Please select which Phenotypes to Investigate:<br><br>\n";

	$field_list_array = array('phenotype_id');
        $num_phenotypes = countTableQuery('phenotype_table',$database_id,$field_list_array,'_','wildcard');
        if (!preg_match('/^\d+$/',$num_phenotypes))
          {
            $date = date('Y-m-d H:i:s');
	    echo "Error #12: $date<br>\n";
	    echo "Error in call to countTableQuery from genotype_dump.php<br>\n";
	    echo "$num_phenotypes<br><br>\n";
	    echo "Please email the above error using the Comments/Help/Support link on the \n";
	    echo "left navigation bar.\n";
	    echo "</form>\n";
	    echo "</body></html>\n";
	    $ret_val = disconnect();
	    exit;
	  }#end if error in call to countTableQuery
        if ($num_phenotypes == '0')
          {
            $date = date('Y-m-d H:i:s');
            echo "Currently there are no phenotypes in the database.<br>\n";
            echo "This message was generated at $date.<br><br>\n";
            echo "Click the button below to download all the genotypes in the database.<br><br>\n";

            echo "<form action=\"genotype_dump.php\" name=\"form\" method=\"POST\" target=\"main\">\n";
            echo "<input type=\"hidden\" name=\"status\" value=\"$status\">\n";
            echo "<input type=\"hidden\ name=\"want_phenotypes\" value=\"0\">\n";
            echo "<center>\n";
            echo "<input type=\"submit\" name=\"submit\" value=\"Download Genotypes\">\n";
            echo "</form>\n";
            echo "</center>\n";
	    echo "<br><br>\n";

	    $ret_val = printLogOutButton('genotype_dump.php');
            echo "\n\n";
            $ret_val = disconnect();
            exit;
          }#end if there aren't any phenotypes

        if ($num_phenotypes > ($max_records_to_print_per_page * 5))
          {
            echo "<br>\n";
            echo "<hr>\n";
            echo "<br>\n";
            echo "There are $num_phenotypes Phenotypes.  If you would like information on all the phenotypes, without \n";
            echo "selecting each one, click the button below:<br><br>\n";

            $pheno_id_array = distinctTableQuery('phenotype_id','phenotype_table',$database_id,$field_list_array,'_','wildcard','all','type_of_phenotype,phenotype_name');
	    if (!is_array($pheno_id_array))
	      {
		$date = date('Y-m-d H:i:s');
		echo "Error #13: $date<br>\n";
		echo "Error in call to distinctTableQuery from genotype_dump.php<br>\n";
		echo "$pheno_id_array<br><br>\n";
		echo "Please email the above error using the Comments/Help/Support link on the \n";
		echo "left navigation bar.\n";
		echo "</form>\n";
		echo "</body></html>\n";
	        $ret_val = disconnect();
	        exit;
	      }#end if error in call to distinctTableQuery
            $all_pheno_id_term = join($pheno_id_array,',');
            unset ($pheno_id_array);

            echo "<table align=\"center\">\n";
            echo "<form action=\"genotype_dump.php\" name=\"skip_form\" method=\"POST\" target=\"main\">\n";
            echo "<input type=\"hidden\" name=\"pheno_id\" value=\"$all_pheno_id_term\">\n";
            echo "<input type=\"hidden\" name=\"want_phenotypes\" value=\"$want_phenotypes\">\n";
            echo "<input type=\"hidden\" name=\"pheno_multiple_pages\" value=\"0\">\n";
            echo "<input type=\"hidden\" name=\"status\" value=\"$status\">\n";
            echo "<tr>\n";
            echo "<td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Download Genotypes and Phenotypes\"></td>\n";
            echo "</tr>\n";
            echo "</form>\n";
            echo "</table>\n";
            echo "<br>\n";
            echo "<hr>\n";
            echo "<br>\n";
            echo "Otherwise, select the Phenotypes you would like information for below.<br><br><br>\n";
          }#end if more than 5 pages of records

        if ($num_phenotypes > $max_records_to_print_per_page)
          {
            $pheno_multiple_pages = 1;
            $from                 = 0;
            $to                   = $max_records_to_print_per_page - 1;
            $range                = "$from"."-"."$to";

 	    $pheno_rec_list = tableQuery('phenotype_table',$database_id,$field_list_array,'_','wildcard',$range,'type_of_phenotype,phenotype_name');
	    if (!is_array($pheno_rec_list))
	      {
		$date = date('Y-m-d H:i:s');
		echo "Error #14: $date<br>\n";
		echo "Error in call to tableQuery from genotype_dump.php<br>\n";
		echo "$pheno_rec_list<br><br>\n";
		echo "Please email the above error using the Comments/Help/Support link on the \n";
		echo "left navigation bar.\n";
		echo "</form>\n";
		echo "</body></html>\n";
	        $ret_val = disconnect();
	        exit;
	      }#end if (!is_array(pheno_rec_list))
            $final_function_call   = 'tableQuery';
            $final_table_name      = 'phenotype_table';
            $final_field_list      = $field_list_array;
            $final_term            = '_';
            $final_mode            = 'wildcard';
            $final_order_by        = 'type_of_phenotype,phenotype_name';
            $last_record_retrieved = $max_records_to_print_per_page;
          }#end if multiple pages
        else #only one page
          {
            $pheno_multiple_pages = 0;

            $pheno_rec_list = tableQuery('phenotype_table',$database_id,$field_list_array,'_','wildcard','all','type_of_phenotype,phenotype_name');
	    if (!is_array($pheno_rec_list))
	      {
		$date = date('Y-m-d H:i:s');
		echo "Error #15: $date<br>\n";
		echo "Error in call to tableQuery from genotype_dump.php<br>\n";
		echo "$pheno_rec_list<br><br>\n";
		echo "Please email the above error using the Comments/Help/Support link on the \n";
		echo "left navigation bar.\n";
		echo "</form>\n";
		echo "</body></html>\n";
	        $ret_val = disconnect();
	        exit;
	      }#end if (!is_array(pheno_rec_list))
          }#end else only one page

        $ret_val = printLogOutButton('genotype_dump.php');
        echo "<br><br>\n";

        echo "<table align=\"center\">\n";
        if ($pheno_multiple_pages)
          {
            echo "<form action=\"genotype_dump.php\" name=\"skip_form1\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(form))\">\n";
            echo "<input type=\"hidden\" name=\"pheno_id\" value=\"\">\n";
            echo "<input type=\"hidden\" name=\"want_phenotypes\" value=\"$want_phenotypes\">\n";
            echo "<input type=\"hidden\" name=\"pheno_multiple_pages\" value=\"0\">\n";
            echo "<input type=\"hidden\" name=\"status\" value=\"$status\">\n";
            echo "<tr>\n";
            echo "<td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Download Genotypes and Phenotypes\" onClick=\"copyData('skip_form1')\"></td>\n";
            echo "</form>\n\n";

            echo "<form action=\"genotype_dump.php\" name=\"form\" method=\"POST\" target=\"main\">\n";
            $records_left = $num_phenotypes - $max_records_to_print_per_page;
            if ($records_left > $max_records_to_print_per_page)
              {echo "<td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Get Next $max_records_to_print_per_page Phenotypes\"></td>\n";}
            else
              {echo "<td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Get Next $records_left Phenotypes\"></td>\n";}
            echo "</tr>\n";
            echo "</table>\n";
            echo "<br>\n";
            echo "<table align=\"center\">\n";
            echo "<tr><td>&nbsp;</td><td align=\"center\"><input type=\"reset\" name=\"reset\" value=\"Reset Selections\"></td></tr>\n";
            echo "</table>\n";
          }#end if multiple pages of phenotypes
        else #only a single page of phenotype
          {
            echo "<form action=\"genotype_dump.php\" name=\"form\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(this))\">\n";
            echo "<tr>\n";
            echo "<td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Download Genotypes and Phenotypes\"></td>\n";
            echo "<td align=\"center\"><input type=\"reset\" name=\"reset\" value=\"Reset Selections\"></td>\n";
            echo "</tr>\n";
            echo "</table>\n";
          }
        echo "<br><br>\n";

	$jscript = printPhenotypeCheckList($pheno_rec_list);
	if ($jscript == '0')
	  {
	    $date = date('Y-m-d H:i:s');
	    echo "Error #16: $date<br>\n";
	    echo "Error in call to printPhenotypeCheckList from genotype_dump.php<br>\n";
	    echo "See above error message from printPhenotypeCheckList for an explanation.<br><br>\n";
	    echo "Please email the above error using the Comments/Help/Support link on the \n";
	    echo "left navigation bar.\n";
	    echo "</form>\n";
	    echo "</body></html>\n";
	    $ret_val = disconnect();
	    exit;
	  }#end if (jscript == '0')

	echo "<input type=\"hidden\" name=\"status\" value=\"$status\">\n";
        echo "<input type=\"hidden\" name=\"want_phenotypes\" value=\"$want_phenotypes\">\n";
        echo "<input type=\"hidden\" name=\"pheno_multiple_pages\" value=\"$pheno_multiple_pages\">\n";
        if ($pheno_multiple_pages)
          {
            echo "<input type=\"hidden\" name=\"function_to_call\" value=\"$final_function_call\">\n";
            if ($final_function_call == 'tableJoin') 
              {
                foreach ($final_table_name as $name) {echo "<input type=\"hidden\" name=\"table_name[]\" value=\"$name\">\n";}
              }
            else{echo "<input type=\"hidden\" name=\"table_name\" value=\"$final_table_name\">\n";}

	    foreach ($final_field_list as $field){echo "<input type=\"hidden\" name=\"field_list[]\" value=\"$field\">\n";}

            if (is_array($final_term)){foreach ($final_term as $term){echo "<input type=\"hidden\" name=\"term[]\" value=\"$term\">\n";}}
            else {echo "<input type=\"hidden\" name=\"term\" value=\"$final_term\">\n";}

            if (is_array($final_mode)){foreach ($final_mode as $mode){echo "<input type=\"hidden\" name=\"mode[]\" value=\"$mode\">\n";}}
            else {echo "<input type=\"hidden\" name=\"mode\" value=\"$final_mode\">\n";}

            if ($final_function_call == 'compoundTableQuery') {echo "<input type=\"hidden\" name=\"join\" value=\"$final_join\">\n";}

            echo "<input type=\"hidden\" name=\"order_by\" value=\"$final_order_by\">\n";
            echo "<input type=\"hidden\" name=\"last_record_retrieved\" value=\"$last_record_retrieved\">\n";
          }#end if multiple pages of phenotypes

	echo "<br>\n";
	echo "<table align=\"center\">\n";
        if ($pheno_multiple_pages)
          {
            echo "<tr><td>&nbsp;</td><td align=\"center\"><input type=\"reset\" name=\"reset\" value=\"Reset Selections\"></td></tr>\n";
            echo "</table>\n";
            echo "<br>\n";
            echo "<table align=\"center\">\n";
            if ($records_left > $max_records_to_print_per_page)
              {echo "<td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Get Next $max_records_to_print_per_page Phenotypes\"></td>\n";}
            else
              {echo "<td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Get Next $records_left Phenotypes\"></td>\n";}
          }#end if multiple pages of phenotypes
        else #only a single page of phenotype
          {
            echo "<tr>\n";
            echo "<td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Download Genotypes and Phenotypes\"></td>\n";
            echo "<td align=\"center\"><input type=\"reset\" name=\"reset\" value=\"Reset Selections\"></td>\n";
            echo "</tr>\n";
          }
	echo "</form>\n\n";

        if ($pheno_multiple_pages)
          {
            echo "<form action=\"genotype_dump.php\" name=\"skip_form2\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(form))\">\n";
            echo "<input type=\"hidden\" name=\"pheno_id\" value=\"\">\n";
            echo "<input type=\"hidden\" name=\"want_phenotypes\" value=\"$want_phenotypes\">\n";
            echo "<input type=\"hidden\" name=\"pheno_multiple_pages\" value=\"0\">\n";
            echo "<input type=\"hidden\" name=\"marker_flag\" value=\"$marker_flag\">\n";
            echo "<input type=\"hidden\" name=\"marker_multiple_pages\" value=\"$marker_multiple_pages\">\n";
            echo "<input type=\"hidden\" name=\"status\" value=\"$status\">\n";
            echo "<td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Download Genotypes and Phenotypes\" onClick=\"copyData('skip_form2')\"></td>\n";
            echo "</tr>\n";
            echo "</form>\n\n";
          }#end if multiple pages of phenotypes
        echo "</table>\n";
        echo "<br>\n";

        $ret_val = printLogOutButton('genotype_dump.php');
        echo "\n\n";

	echo "<script>\n";
	echo "function Validate(form)\n";
	echo "$jscript\n";
        if ($pheno_multiple_pages)
          {
            echo "\n";
            echo "function copyData(formname)\n";
            echo "{\n";
            echo " var pheno_string = new Array();\n";
            echo " var length = 0;\n";
            echo " for (var i=0; i<document.form.elements['pheno_id[]'].length; i++){\n";
            echo "   if (document.form.elements['pheno_id[]'][i].checked){\n";
            echo "     length = pheno_string.length;\n";
            echo "     pheno_string[length] = document.form.elements['pheno_id[]'][i].value;}}\n";
            echo " if (formname == \"skip_form1\"){\n";
            echo "   document.skip_form1.pheno_id.value = pheno_string;}\n";
            echo " if (formname == \"skip_form2\"){\n";
            echo "   document.skip_form2.pheno_id.value = pheno_string;}\n";
            echo "}\n";
          }#end if multiple pages of phenotype
	echo "</script>\n";
        unset($pheno_multiple_pages);
      }#end if want phenotypes

    #If want phenotypes but haven't seen all the phenotypes yet
    if ((preg_match('/\w+/',$status)) and
	($want_phenotypes == '1') and
        ($pheno_multiple_pages == '1')
	)
      {
        echo "Select the Phenotypes you want data for:<br><br>\n";

        if ($function_to_call == 'tableQuery')
          {
            $num_of_phenotypes = countTableQuery($table_name,$database_id,$field_list,$term,$mode);
	    if (!preg_match('/^\d+$/',$num_of_phenotypes))
	      {
		$date = date('Y-m-d H:i:s');
		echo "Error #17: $date<br>\n";
		echo "Error in call to countTableQuery from genotype_dump.php<br>\n";
		echo "$num_of_phenotypes<br><br>\n";
		echo "Please email the above error using The Comments/Help/Support link on the \n";
		echo "left navigation bar.\n";
		echo "</form>\n";
		echo "</body></html>\n";
		$ret_val = disconnect();
		exit;
	      }#end error in call to countTableQuery
	    $records_yet_to_be_retrieved = $num_of_phenotypes - $last_record_retrieved;

	    if ($records_yet_to_be_retrieved > $max_records_to_print_per_page)
	      {
                $pheno_multiple_pages = 1;
		$from = $last_record_retrieved;
		$to   = ($last_record_retrieved-1) + $max_records_to_print_per_page;
		$range = "$from"."-"."$to";
		$pheno_rec_list = tableQuery($table_name,$database_id,$field_list,$term,$mode,$range,$order_by);
		if (!is_array($pheno_rec_list))
		  {
		    $date = date('Y-m-d H:i:s');
		    echo "Error #18: $date<br>\n";
		    echo "Error in call to tableQuery from genotype_dump.php<br>\n";
		    echo "$pheno_rec_list<br><br>\n";
		    echo "Please email the above error using The Comments/Help/Support link on the \n";
		    echo "left navigation bar.\n";
		    echo "</form>\n";
		    echo "</body></html>\n";
		    $ret_val = disconnect();
		    exit;
		  }#end if (!is_array(pheno_rec_list))
		if (sizeof($pheno_rec_list) == 0)
		  {
		    $date = date('Y-m-d H:i:s');
		    echo "Error #19: $date<br>\n";
		    echo "Error in call to genotype_dump.php<br>\n";
		    echo "There should be records returned for tableQuery search with parameters:<br>\n";
		    echo "table_name = $table_name<br>\n";
		    echo "field_list = ".join(',',$field_list)."<br>\n";
		    echo "term       = $term<br>\n";
		    echo "mode       = $mode<br>\n";
		    echo "range      = $range<br>\n";
		    echo "order_by   = $order_by<br>\n";
		    echo "But no records were returned<br><br>\n";
		    echo "Please email the above error using The Comments/Help/Support link on the \n";
		    echo "left navigation bar.\n";
		    echo "</form>\n";
		    echo "</body></html>\n";
		    $ret_val = disconnect();
		    exit;
		  }#end if no records returned
                $last_record_retrieved = $to + 1;
	      }#end if multiple pages
	    else #only one page
	      {
                $pheno_multiple_pages = 0;
		$from = $last_record_retrieved;
		$to   = $num_of_phenotypes - 1;
		$range = "$from"."-"."$to";
		$pheno_rec_list = tableQuery($table_name,$database_id,$field_list,$term,$mode,$range,$order_by);
		if (!is_array($pheno_rec_list))
		  {
		    $date = date('Y-m-d H:i:s');
		    echo "Error #20: $date<br>\n";
		    echo "Error in call to tableQuery from genotype_dump.php<br>\n";
		    echo "$pheno_rec_list<br><br>\n";
		    echo "Please email the above error using The Comments/Help/Support link on the \n";
		    echo "left navigation bar.\n";
		    echo "</form>\n";
		    echo "</body></html>\n";
		    $ret_val = disconnect();
		    exit;
		  }#end if (!is_array(pheno_rec_list))
		if (sizeof($pheno_rec_list) == 0)
		  {
		    $date = date('Y-m-d H:i:s');
		    echo "Error #21: $date<br>\n";
		    echo "Error in call to genotype_dump.php<br>\n";
		    echo "There should be records returned for tableQuery search with parameters:<br>\n";
		    echo "table_name = $table_name<br>\n";
		    echo "field_list = ".join(',',$field_list)."<br>\n";
		    echo "term       = $term<br>\n";
		    echo "mode       = $mode<br>\n";
		    echo "range      = $range<br>\n";
		    echo "order_by   = $order_by<br>\n";
		    echo "But no records were returned<br><br>\n";
		    echo "Please email the above error using The Comments/Help/Support link on the \n";
		    echo "left navigation bar.\n";
		    echo "</form>\n";
		    echo "</body></html>\n";
		    $ret_val = disconnect();
		    exit;
	          }#end if no records returned
	      }#end else only one page
	  }#end if tableQuery

        $ret_val = printLogOutButton('genotype_dump.php');
        echo "<br><br>\n";

        echo "<table align=\"center\">\n";
	if ($pheno_multiple_pages)
	  {
            echo "<form action=\"genotype_dump.php\" name=\"skip_form1\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(form))\">\n";
            echo "<input type=\"hidden\" name=\"status\" value=\"$status\">\n";
            echo "<input type=\"hidden\" name=\"want_phenotypes\" value=\"$want_phenotypes\">\n";
            echo "<input type=\"hidden\" name=\"pheno_multiple_pages\" value=\"0\">\n";
            echo "<input type=\"hidden\" name=\"pheno_id\" value=\"\">\n";
            echo "<tr>\n";
            echo "<td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Download Genotypes and Phenotypes\" onClick=\"copyData('skip_form1')\"></td>\n";
            echo "</form>\n\n";

	    echo "<form action=\"genotype_dump.php\" name=\"form\" method=\"POST\" target=\"main\">\n";
	    $records_left = ($num_of_phenotypes - $last_record_retrieved );
	    if ($records_left > $max_records_to_print_per_page) 
              {echo "<td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Get Next $max_records_to_print_per_page Phenotypes\"></td>\n";}
	    else
              {echo "<td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Get Next $records_left Phenotypes\"></td>\n";}
            echo "</tr>\n";
            echo "</table>\n";
            echo "<br>\n";
            echo "<table align=\"center\">\n";
            echo "<tr><td>&nbsp;</td><td align=\"center\"><input type=\"reset\" name=\"reset\" value=\"Deselect Selections\"></td></tr>\n";
            echo "</table>\n";
	  }#end multiple pages
	else #only one page
	  {
	    echo "<form action=\"genotype_dump.php\" name=\"form\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(this))\">\n";
            echo "<tr>\n";
	    echo "<td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Download Genotypes and Phenotypes\"></td>\n";
            echo "<td align=\"center\"><input type=\"reset\" name=\"reset\" value=\"Deselect Selections\"></td>\n";
            echo "</tr>\n";
            echo "</table>\n";
	  }#end only one page of markers
        echo "<br><br>\n";

	$jscript = printPhenotypeCheckList($pheno_rec_list);
	if ($jscript == '0')
          {
	    $date = date('Y-m-d H:i:s');
	    echo "Error #22: $date<br>\n";
	    echo "Error in call to printPhenotypeCheckList from genotype_dump.php<br>\n";
	    echo "See above error message from printPhenotypeCheckList for an explanation.<br><br>\n";
	    echo "Please email the above error using The Comments/Help/Support link on the \n";
	    echo "left navigation bar. \n";
	    echo "</form>\n";
	    echo "</body></html>\n";
	    $ret_val = disconnect();
	    exit;
	  }#end if (jscript == '0')

        echo "<input type=\"hidden\" name=\"status\" value=\"$status\">\n";
        echo "<input type=\"hidden\" name=\"want_phenotypes\" value=\"$want_phenotypes\">\n";
        echo "<input type=\"hidden\" name=\"pheno_multiple_pages\" value=\"$pheno_multiple_pages\">\n";
        if (sizeof($pheno_ids) > '0')#if have phenotype ids from previous page
          {
            if (!is_array($pheno_ids)){echo "<input type=\"hidden\" name=\"pheno_id[]\" value=\"$pheno_ids\">\n";}
            else
              {
                $pheno_id_string = join($pheno_ids,',');
                echo "<input type=\"hidden\" name=\"pheno_id[]\" value=\"$pheno_id_string\">\n";
                unset($pheno_id_string);
              }
          }#end if have phenotype ids from previous page

	echo "<br><br>\n";
	echo "<table align=\"center\">\n";
	if ($pheno_multiple_pages)
	  {
	    echo "<input type=\"hidden\" name=\"function_to_call\" value=\"$function_to_call\">\n";
	    if ($function_to_call == 'tableJoin') 
	      {
	        foreach ($table_name as $name) {echo "<input type=\"hidden\" name=\"table_name[]\" value=\"$name\">\n";}
	      }
	    else{echo "<input type=\"hidden\" name=\"table_name\" value=\"$table_name\">\n";}

	    foreach ($field_list as $field){echo "<input type=\"hidden\" name=\"field_list[]\" value=\"$field\">\n";}

	    if (is_array($term)){foreach ($term as $sub_term){echo "<input type=\"hidden\" name=\"term[]\" value=\"$sub_term\">\n";}}
	    else {echo "<input type=\"hidden\" name=\"term\" value=\"$term\">\n";}

	    if (is_array($mode)){foreach ($mode as $sub_mode){echo "<input type=\"hidden\" name=\"mode[]\" value=\"$sub_mode\">\n";}}
	    else {echo "<input type=\"hidden\" name=\"mode\" value=\"$mode\">\n";}

	    if ($function_to_call == 'compoundTableQuery') {echo "<input type=\"hidden\" name=\"join\" value=\"$join\">\n";}

	    echo "<input type=\"hidden\" name=\"order_by\" value=\"$order_by\">\n";
            echo "<input type=\"hidden\" name=\"last_record_retrieved\" value=\"$last_record_retrieved\">\n";
		     
            echo "<tr><td>&nbsp;</td><td align=\"center\"><input type=\"reset\" name=\"reset\" value=\"Deselect Selections\"></td></tr>\n";
            echo "</table>\n";
            echo "<br>\n";
            echo "<table align=\"center\">\n";
	    if ($records_left > $max_records_to_print_per_page) 
              {echo "<td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Get Next $max_records_to_print_per_page Phenotypes\"></td>\n";}
	    else
              {echo "<td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Get Next $records_left Phenotypes\"></td>\n";}
	  }#end have multiple pages to display
	else #only have one page of markers to display
	  {
            echo "<tr>\n";
	    echo "<td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Download Genotypes and Phenotypes\"></td>\n";
            echo "<td align=\"center\"><input type=\"reset\" name=\"reset\" value=\"Deselect Selections\"></td>\n";
            echo "</tr>\n";
	  }
	echo "</form>\n\n";

        if ($pheno_multiple_pages)
          {
            echo "<form action=\"genotype_dump.php\" name=\"skip_form2\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(form))\">\n";
            echo "<input type=\"hidden\" name=\"status\" value=\"$status\">\n";
            echo "<input type=\"hidden\" name=\"want_phenotypes\" value=\"$want_phenotypes\">\n";
            echo "<input type=\"hidden\" name=\"pheno_multiple_pages\" value=\"0\">\n";
            echo "<input type=\"hidden\" name=\"pheno_id\" value=\"\">\n";
            echo "<td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Download Genotypes and Phenotypes\" onClick=\"copyData('skip_form2')\"></td>\n";
            echo "</tr>\n";
            echo "</form>\n\n";
          }#end if multiple pages of phenotypes
        echo "</table>\n";
	echo "<br><br>\n";

        $ret_val = printLogOutButton('genotype_dump.php');
        echo "\n\n";
		 
	echo "<script>\n";
	echo "function Validate(form)\n";
	echo "$jscript\n";
	echo "\n";
        if ($pheno_multiple_pages)
          {
            echo "\n";
            echo "function copyData(formname)\n";
            echo "{\n";
            echo " var pheno_string = new Array();\n";
            echo " var length = 0;\n";
            echo " for (var i=0; i<document.form.elements['pheno_id[]'].length; i++){\n";
            echo "   if (document.form.elements['pheno_id[]'][i].type == \"hidden\"){\n";
            echo "     if (document.form.elements['pheno_id[]'][i].value != \"\"){\n";
            echo "       length = pheno_string.length;\n";
            echo "       pheno_string[length] = document.form.elements['pheno_id[]'][i].value;}}\n";
            echo "   else{\n";
            echo "     if (document.form.elements['pheno_id[]'][i].checked){\n";
            echo "       length = pheno_string.length;\n";
            echo "       pheno_string[length] = document.form.elements['pheno_id[]'][i].value;}}}\n";
            echo " if (formname == \"skip_form1\"){\n";
            echo "   document.skip_form1.pheno_id.value = pheno_string;}\n";
            echo " if (formname == \"skip_form2\"){\n";
            echo "   document.skip_form2.pheno_id.value = pheno_string;}\n";
            echo "}\n";
          }#end if multiple pages of phenotype
	echo "</script>\n";
        unset($pheno_multiple_pages);
      }#end if want phenotypes, but haven't seen all of them yet

    #Have information, but haven't selected if want analysis files
    if (
        ((preg_match('/\w+/',$status)) and 
         ($want_phenotypes == '0') and
         (!preg_match('/\d/',$create_analysis_files))
        ) or
        ((preg_match('/\w+/',$status)) and
         ($want_phenotypes == '1')     and
         ($pheno_multiple_pages == '0')and 
         (sizeof($pheno_ids) > '0') and
         (!preg_match('/\d/',$create_analysis_files))
        )
       )
      {
        echo "<form action=\"genotype_dump.php\" name=\"form\" method=\"POST\" target=\"main\">\n";
        echo "Please select whether you would like to generate input files for an analysis program using Mega2:<br><br>\n";

        echo "<table align=\"center\">\n";
        echo "<tr><td>Do NOT Generate Analysis Files:</td><td><input type=\"radio\" name=\"create_analysis_files\" value=\"0\" checked></td><td>(Downloaded in preMakeped Format)</td></tr>\n";
        echo "<tr><td>Generate Analysis Files:</td><td><input type=\"radio\" name=\"create_analysis_files\" value=\"1\"></td></tr>\n";
        echo "</table>\n";
        echo "<br><br>\n";

        echo "<input type=\"hidden\" name=\"status\" value=\"$status\">\n";
        echo "<input type=\"hidden\" name=\"want_phenotypes\" value=\"$want_phenotypes\">\n";
        if ($want_phenotypes == 1)
          {
            echo "<input type=\"hidden\" name=\"pheno_multiple_pages\" value=\"$pheno_multiple_pages\">\n";
            if (!is_array($pheno_ids)){echo "<input type=\"hidden\" name=\"pheno_id[]\" value=\"$pheno_ids\">\n";}
            else
              {
                $pheno_id_string = join($pheno_ids,',');
                echo "<input type=\"hidden\" name=\"pheno_id[]\" value=\"$pheno_id_string\">\n";
                unset($pheno_id_string);
              }
          }#end if have phenotype ids

        echo "<table align=\"center\">\n";
        echo "<tr>\n";
        echo "<td><input type=\"submit\" name=\"submit\" value=\"Get Data\"></td>\n";
        echo "<td><input type=\"reset\" name=\"reset\" value=\"Reset Selection\"></td>\n";
        echo "</table>\n";
        echo "</form>\n";
        echo "<br><br>\n\n";

        printLogOutButton('genotype_dump.php');
      }#end if Have information, but haven't selected if want analysis files

    #Want analysis files, but haven't selected which analyses
    if (
        ((preg_match('/\w+/',$status)) and 
         ($want_phenotypes == '0') and
         ($create_analysis_files == '1') and
         (!preg_match('/\d+/',$analysis_option))
        ) or
        ((preg_match('/\w+/',$status)) and
         ($want_phenotypes == '1')     and
         ($pheno_multiple_pages == '0')and 
         (sizeof($pheno_ids) > '0') and
         ($create_analysis_files == '1') and
         (!preg_match('/\d+/',$analysis_option))
        )
       )
      {
        echo "<form action=\"genotype_dump.php\" name=\"form\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(this))\" onReset=\"resetSubMenu()\">\n";
        $jscript = printMega2Form();
        if ($jscript == '0')
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #23: $date<br><br>\n";
            echo "Error in call to printMega2Form from genotype_dump.php<br>\n";
            echo "Please see above error message from printMega2Form for an explanation.<br><br>\n";
            echo "Please email the above error using Comments/Help/Support link \n";
            echo "in the left navigation bar.<br>\n";
            echo "</body></html>\n";
            $ret_val = disconnect();
            exit;
          }#end if error in call to printMega2Form
        echo "<br><br>\n";

        echo "<input type=\"hidden\" name=\"create_analysis_files\" value=\"$create_analysis_files\">\n";
        echo "<input type=\"hidden\" name=\"status\" value=\"$status\">\n";
        echo "<input type=\"hidden\" name=\"want_phenotypes\" value=\"$want_phenotypes\">\n";
        if ($want_phenotypes == 1)
          {
            echo "<input type=\"hidden\" name=\"pheno_multiple_pages\" value=\"$pheno_multiple_pages\">\n";
            if (!is_array($pheno_ids)){echo "<input type=\"hidden\" name=\"pheno_id[]\" value=\"$pheno_ids\">\n";}
            else
              {
                $pheno_id_string = join($pheno_ids,',');
                echo "<input type=\"hidden\" name=\"pheno_id[]\" value=\"$pheno_id_string\">\n";
                unset($pheno_id_string);
              }
          }#end if have phenotype ids

        echo "<table align=\"center\">\n";
        echo "<tr>\n";
        echo "<td><input type=\"submit\" name=\"submit\" value=\"Get Data\"></td>\n";
        echo "<td><input type=\"reset\" name=\"reset\" value=\"Reset Selection\"></td>\n";
        echo "</table>\n";
        echo "</form>\n";
        echo "<br><br>\n\n";

        printLogOutButton('genotype_dump.php');
        echo "\n\n";

        echo "<script>\n";
        echo "function Validate(form)\n";
        echo "{\n";
        echo "$jscript\n";
        echo "</script>\n";
      }#end if Want analysis files, but haven't selected which analyses

    #5   Print Genotype Records
    if (
        ((preg_match('/\w+/',$status)) and 
         ($want_phenotypes == '0') and
         ($create_analysis_files == '0')
        ) or
        ((preg_match('/\w+/',$status)) and 
         ($want_phenotypes == '0') and
         ($create_analysis_files == '1') and
         (preg_match('/\d+/',$analysis_option))
        ) or
	((preg_match('/\w+/',$status)) and
         ($want_phenotypes == '1')     and
         ($pheno_multiple_pages == '0')and 
         (sizeof($pheno_ids) > '0') and
         ($create_analysis_files == '0')
        ) or
        ((preg_match('/\w+/',$status)) and
         ($want_phenotypes == '1')     and
         ($pheno_multiple_pages == '0')and 
         (sizeof($pheno_ids) > '0') and
         ($create_analysis_files == '1') and
         (preg_match('/\d+/',$analysis_option))
        )
       )
      {
        echo "The data you requested is being prepared to be downloaded.  When the page finishes loading, click the \"Download Data\" button \n";
        echo "to download the data to your computer:<br><br>\n";

        $rand = rand();
        $file_name = "download_genotype_record_".$rand.".txt";
        $parameter_file_name = "parameter_string_".$rand.".txt";

        $parameter_file_handle = fopen("../bin/database_query_scripts/$parameter_file_name","w");
        if (!$parameter_file_handle)
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #24: $date<br>\n";
            echo "Error in call to fopen from genotype_dump.php<br>\n";
            echo "Unable to print file $file_name in data/download_data/<br><br>\n";
            echo "Please email the above error using Comments/Help/Support link \n";
            echo "in the left navigation bar.<br>\n";
            echo "</body></html>\n";
            $ret_val = disconnect();
            exit;
          }#end if can't open parameter file

	$field_list_array = array('marker_id');
        if ($db_type == 'mysql'){$order_by = 'chromosome+0,chromosome_position ASC';}
        if ($db_type == 'pgsql'){$order_by = "chromosome::text::integer,chromosome_position ASC";}
	$marker_rec_list = distinctTableQuery('marker_id','marker_table',$database_id,$field_list_array,'_','wildcard','all',$order_by);
	if (!is_array($marker_rec_list))
	  {
            $date = date('Y-m-d H:i:s');
	    echo "Error #25: $date<br>\n";
	    echo "Error in call to distinctTableQuery from genotype_dump.php<br>\n";
	    echo "$marker_rec_list<br><br>\n";
	    echo "Please email the above error using the Comments/Help/Support link on the \n";
	    echo "left navigation bar.\n";
	    echo "</form>\n";
	    echo "</body></html>\n";
	    $ret_val = disconnect();
	    exit;
	  }#end if error in call to distinctTableQuery
        $marker_term = join($marker_rec_list,',');
        $num_of_markers = sizeof($marker_rec_list);
        unset($marker_rec_list);

        if (sizeof($pheno_ids) > 0)
          {
            if (is_array($pheno_ids)) {$pheno_term = implode(',',$pheno_ids);}
	    else                      {$pheno_term = $pheno_ids;}
            $pheno_array   = explode(',',$pheno_term);
            $num_of_phenos = sizeof($pheno_array);
            unset($pheno_array);
          }#end if want phenotypes

	$field_list_array = array('individual_table_id');
        if ($db_type == 'mysql'){$order_by = 'pedigree_id+0,individual_id+0';}
        if ($db_type == 'pgsql'){$order_by = "pedigree_id::text::integer,individual_id::text::integer";}
	$indiv_rec_list = distinctTableQuery('individual_table_id','individual_table',$database_id,$field_list_array,'_','wildcard','all',$order_by);
	if (!is_array($indiv_rec_list))
	  {
            $date = date('Y-m-d H:i:s');
	    echo "Error #26: $date<br>\n";
	    echo "Error in call to distinctTableQuery from genotype_dump.php<br>\n";
	    echo "$indiv_rec_list<br><br>\n";
	    echo "Please email the above error using the Comments/Help/Support link on the \n";
	    echo "left navigation bar.\n";
	    echo "</form>\n";
	    echo "</body></html>\n";
	    $ret_val = disconnect();
	    exit;
	  }#end if (!is_array(indiv_rec_list))
	$indiv_term    = join($indiv_rec_list,',');
        $num_of_indivs = sizeof($indiv_rec_list);
        unset($indiv_rec_list);

        $type_of_query = 'genotype_record';
        $print_string = "indiv_list:$indiv_term\n";
        $print_string.= "marker_list:$marker_term\n";
        $print_string.= "status:$status\n";
        $print_string.= "file_name:$file_name\n";
        if (sizeof($pheno_ids) > 0){$print_string.= "pheno_list:$pheno_term";}
        if ($create_analysis_files)
          {
            $print_string.= "analysis_option:$analysis_option\n";
            if (!is_array($analysis_sub_option)){$print_string.= "analysis_sub_option:$analysis_sub_option\n";}
            else #is an array
              {
                $sub_string = "";
                foreach ($analysis_sub_option as $sub_option){$sub_string.= "$sub_option ";}
                $sub_string = preg_replace('/ $/','',$sub_string);
                $print_string.= "analysis_sub_option:$sub_string\n";
              }#end if analysis_sub_option is an array
            if (sizeof($r_plot_statistics) > 0)
              {
                $sub_string = "";
                foreach ($r_plot_statistics as $stat){$sub_string.= "$stat ";}
                $sub_string = preg_replace('/ $/','',$sub_string);
                $print_string.= "r_plot_statistics:$sub_string";
                $print_string.= " e\n";
              }#end if plot statistics
          }#end if want to generate analysis files

        $ret_val = fwrite($parameter_file_handle,$print_string);
        if (!$ret_val)
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #27: $date<br>\n";
            echo "Error in call to fwrite from genotype_dump.php<br>\n";
            echo "Unable to write to parameter file handle for file $parameterfile_name in /bin/database-query_scripts/<br><br>\n";
            echo "Please email the above error using the Comments/Help/Support link on the \n";
            echo "left navigation bar.\n";
            echo "</form>\n";
            echo "</body></html>\n";
            $ret_val = disconnect();
            exit;
          }#end error in call to fwrite

        $ret_val = fclose($parameter_file_handle);
        if (!$ret_val)
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #28: $date<br>\n";
            echo "Error in call to fclose from genotype_dump.php<br>\n";
            echo "Unable to close parameter file handle for file $parameterfile_name in /bin/database_query_scripts/<br><br>\n";
            echo "Please email the above error using the Comments/Help/Support link on the \n";
            echo "left navigation bar.\n";
            echo "</form>\n";
            echo "</body></html>\n";
            $ret_val = disconnect();
            exit;
          }#end error in call to fclose

        $ret_val = chdir("../bin/database_query_scripts/");
        if (!$ret_val)
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #29: $date<br>\n";
            echo "Error in call to chdir from genotype_dump.php<br>\n";
            echo "Unable to change directory to database query scripts directory<br><br>\n";
            echo "Please email the above error using Comments/Help/Support link \n";
	    echo "in the left navigation bar.<br>\n";
	    echo "</body></html>\n";
            $ret_val = disconnect();
            exit;
          }#end error in call to chdir

        if (sizeof($pheno_ids) > 0)
          {
	    if (($num_of_indivs*($num_of_markers+$num_of_phenos)) > $max_records_to_wait_to_run){$background_flag = 1;}
            else                                                                                {$background_flag = 0;}
          }#end if want phenotypes
        else #don't want phenotypes
	  {
	    if (($num_of_indivs*$num_of_markers) > $max_records_to_wait_to_run){$background_flag = 1;}
            else                                                               {$background_flag = 0;}
          }#end else don't want phenotypes
        $command = "./query_and_write_results.php '$parameter_file_name'  '$database_id' '$_SESSION[user_name]'  'Download All Genotypes' '$background_flag' '$type_of_query' '$create_analysis_files'";
        if ($background_flag)
          {
            $command.= " > dummy_query_and_write_results_background_output.txt &";
            $last_line = system($command,$sys_ret_val);
          }#end if running in background
        else #running in foreground
          {
            passthru($command,$sys_ret_val);
          }#end else running in foreground

        $ret_val = chdir("../../cgi-bin/");
        if (!$ret_val)
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #30: $date<br>\n";
            echo "Error in call to chdir from genotype_dump.php<br>\n";
            echo "Unable to change directory to cgi-bin directory<br><br>\n";
            echo "Please email the above error using Comments/Help/Support link \n";
	    echo "in the left navigation bar.<br>\n";
	    echo "</body></html>\n";
            $ret_val = disconnect();
            exit;
          }#end error in call to chdir

        if ($sys_ret_val != '0')
	  {
	    $date = date('Y-m-d H:i:s');
	    echo "Error #31: $date<br>\n";
	    echo "Error in system call to query_and_write_results.pl<br>\n";
	    echo "The return value of the sytem call was $sys_ret_val<br>\n";
            if ($background_flag){echo "The last line of output was $last_line<br><br>\n";}
            echo "Please email the above error using the Comments/Help/Support link on the \n";
	    echo "left navigation bar.\n";
	    echo "</form>\n";
	    echo "</body></html>\n";
	    $ret_val = disconnect();
	    exit;
	  }#end if error in system call

        if ($background_flag)
	  {
            echo "<center>\n";
	    echo "<b>The data you requested to download is being prepared.<br>\n";
	    echo "You will receive an email when the data is ready to be downloaded.<br>\n";
	    echo "Follow the instructions in the email to retrieve your data.</b><br><br>\n";
	    echo "</center>\n";
	  }#end if queries run in the background
	else #queries not run in the background
	  {
	    if ($create_analysis_files)
              {
                echo "<form action=\"download.php\" name=\"form\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(this))\">\n"; 
                echo "<br><br>\n";
                echo "Below are the data files generated by the application and Mega2 based upon your selections.  Please select them \n";
                echo "one at a time and click 'Download File' to download the file to your computer:<br><br>\n";
                $dir_handle = opendir("../data/download_data/$_SESSION[user_name]/");
                echo "<table align=\"center\">\n";
                echo "<tr>\n";
                echo "<td>Data Files:</td><td>\n";
                echo "<select name=\"file_name\">\n";
                echo "<option>Select File to Download\n";
                while ($file = readdir($dir_handle))
                  {
                    if (preg_match("/$rand/",$file)){echo "<option value=\"../data/download_data/$_SESSION[user_name]/$file\">$file\n";}
                  }#end while files in download data directory
                echo "</select>\n";
                echo "</td>\n";
                echo "</tr>\n";
                echo "</table>\n";
                echo "<br><br>\n\n";

                echo "<input type=\"hidden\" name=\"type_of_download\" value=\"\">\n";
                echo "<center>\n";
                echo "<input type=\"submit\" name=\"submit\" value=\"Download Selected File\">\n";
                echo "</center>\n";
              }#end if created_analysis files
            else #did not create analysis files
              {
                echo "<form action=\"download.php\" name=\"form\" method=\"POST\" target=\"main\">\n";# onSubmit=\"return(Validate(this))\">\n"; 
                echo "<input type=\"hidden\" name=\"file_name\" value=\"../data/download_data/$_SESSION[user_name]/$file_name\">\n";
                echo "<input type=\"hidden\" name=\"type_of_download\" value=\"genotype_record\">\n";
	        echo "<center>\n";
	        echo "<input type=\"submit\" name=\"submit\" value=\"Download Selected Records\">\n";
                echo "</center>\n";
              }#end else did not create analysis files
            echo "</form>\n";
            echo "<br><br>\n";

            $ret_val = printLogOutButton('genotype_dump.php');
	    echo "\n\n";

            if ($create_analysis_files)
              {
                echo "<script>\n";
                echo "function Validate(form)\n";
                echo "{\n";
                echo "  if (form.file_name.selectedIndex == 0){\n";
                echo "    alert(\"You must select a File to Download\");\n";
                echo "    form.file_name.focus();\n";
                echo "    return(false);}\n";
                echo "  else{return(true);}\n";
                echo "}\n";
                echo "</script>\n";
              }#end if created analysis files
	  }#end else queries not run in the background
      }#end if have all info to print
  }#end if auth_user == '1'
$ret_val = disconnect();
echo "</body></html>\n";
?>


Return current item: Generic Genetic Studies Database