Location: PHPKode > projects > Generic Genetic Studies Database > ggsd-2.19/cgi-bin/define_affected_individuals.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	define_affected_individuals.php

#TYPE	PHP

#USES	sql_lib.inc(authenticate updateRecord tableQuery tableJoin compoundTableQuery)
#	html_lib.inc(printStatusSelectList printPhenotypeSelectList)

#PURPOSE To allow the user to select the criteria to designate individuals affected/unaffected
#	for defined affection statuses in the database, and then query the database and make the
#	assignments.

#USAGE	define_affected_individuals.php

#PARAMETERS
#	authorized user		auth_user	int	optional
#	affection status	status		string	optional
#	phenotype id		phenotype_id	int	optional
#	nationality		nationality	string	optional
#	affection value		affection_value	string	optional
#	comparison mode		comp_mode	string	optional

#RETURNS
#	affected individuals	individual_id/pedigree_id	list	if success
#	error message						string	if error

#DATASTRUCTURES

#FUNCTIONAL DESCRIPTION The script will first authenticate the user.  Then it will determine which affection
#		       status for which nationality the user wants to investigate.  Then it will determine which phenotype will be used
#		       to determine individuals affection status, the value to use for that phenotype, and the comparison
#		       mode to use for that value.  Then the script will use the search criteria to determine the affection state
#		       for every individual in that nationality for that phenotype value.

#ASSUMPTIONS

#LIMITATIONS

#COMMENTS

#PRELIMINARY DESIGN
#	    1	Get Args
#	    2	Authenticate User
#	    2	Get Status and Nationality
#	    3	Get Phenotype, Comp Value, and Comp Mode
#	    4	Determine Affected Individuals

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>Determine Affected Individuals</title>\n";
echo" </head>\n";
echo "<body bgcolor=\"white\">\n";
echo "<center><h3>Determine Affected Individuals</h3></center>\n";
echo "<hr>\n";
printHomeNavBar();
echo "<br>\n";


#1     Get Args
$access_type_required = 'administrative';
$status            = $_POST['status'];
$nationality       = $_POST['nationality'];
$phenotype_id      = $_POST['phenotype_id'];
$type_of_phenotype = $_POST['type_of_phenotype'];
$affection_value   = $_POST['affection_value'];
$comp_mode         = $_POST['comp_mode'];

$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 define_affected_individuals.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 define_affected_individuals.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 define_affected_individuals.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('define_affected_individuals.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 define_affected_individuals.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 define_affected_individuals.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 define_affected_individuals.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 define_affected_individuals.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('define_affected_individuals.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 define_affected_individuals.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('define_affected_individuals.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 define_affected_individuals.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 define_affected_individuals.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 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 Nationality
    if ((!preg_match('/\w+/',$status)) and
	(!preg_match('/\w+/',$nationality)) and
	(!preg_match('/\d+/',$phenotype_id)) and
	(!preg_match('/\w+/',$type_of_phenotype)) and
	(!preg_match('/\w+/',$affection_value)) and
	(!preg_match('/\w+/',$comp_mode))
       )
      {
	echo "<form action=\"define_affected_individuals.php\" name=\"form\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(this))\">\n";
	echo "This form will allow you to determine whether individuals are affected/unaffected for any of the defined affection stautus \n";
	echo "catergories in the database.  In order to do this, you must provide the affection status you want to determine, the nationality of \n";
	echo "the individuals you want to determine affection for, the phenotype that will be used to determine affection, the phenotype value used \n";
	echo "as the testing criteria, and the type of comparison that should be done using the phenotype value.<br><br>\n";
        echo "<b>Note:  For quantitative traits, there must be a defined phenotype named \"age\" in the database for this ";
        echo "functionality to work.</b><br><br>\n";

	echo "Please select the Affection Status you want to determine:<br>\n";
	$jscript_1 = printStatusSelectList($database_id);
	if ($jscript_1 == '0')
	  {
            $date = date('Y-m-d H:i:s');
	    echo "Error #11: $date<br>\n";
	    echo "Error in call to printStatusSelectList from define_affected_individuals.php<br>\n";
	    echo "See the above error message from printStatusSelectList 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 "</body></html>\n";
	    $ret_val = disconnect();
	    exit;
	  }#end error in call to printStatusSelect List
        if ($jscript_1 == 'no_defined_statuses')
          {
            echo "</body></html>\n";
            $ret_val = disconnect();
            exit;
          }
	echo "<br><br>\n";

	echo "Please select the Nationality of the individuals we are investigating:<br>\n";
	$jscript_2 = printNationalitySelectList($database_id);
	if ($jscript_2 == '0')
	  {
            $date = date('Y-m-d H:i:s');
	    echo "Error #12: $date<br>\n";
	    echo "Error in call to printNationalitySelectList from define_affected_individuals.php<br>\n";
	    echo "See the above error message from printNationalitySelectList 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 "</body></html>\n";
	    $ret_val = disconnect();
	    exit;
	  }#end error in call to printNationalitySelectList
        if ($jscript_2 == 'no_defined_nationalities')
          {
            echo "</body></html>\n";
            $ret_val = disconnect();
            exit;
          }
	echo "<br><br>\n";
        echo "<b>Note:  All affection status data in the database will be overwritten with what is determined in this analysis.<br>\n";
        echo "If an individual is designated as affected in the database now but is determined to be unaffected during this analysis, their<br>\n";
        echo "affection status will be changed.</b><br><br>\n";

	$jscript_1 = preg_replace('/\n else {return true;}\n}$/','',$jscript_1);
	$jscript_2 = preg_replace('/^{\n/','',$jscript_2);
	$jscript = "$jscript_1\n $jscript_2";

	echo "<center>\n";
	echo "<input type=\"submit\" name=\"submit\" value=\"Select Phenotype\">\n";
	echo "<input type=\"reset\" name=\"reset\" value=\"Reset Status and Nationality\">\n";
        echo "</center>\n";
        echo "</form>\n";
        echo "<br><br>\n\n";

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

	echo "<script>\n";
	echo "function Validate(form)\n";
	echo "$jscript\n";
	echo "</script>\n";
      }#end if don't have any parameters

    #4     Get Phenotype, Phenotype Value, and Comp Mode
    if ((preg_match('/\w+/',$status)) and
	(preg_match('/\w+/',$nationality)) and
	(!preg_match('/\d+/',$phenotype_id)) and
	(!preg_match('/\w+/',$type_of_phenotype)) and
	(!preg_match('/\w+/',$affection_value)) and
	(!preg_match('/\w+/',$comp_mode))
       )
      {
        $query_field_list = array('affection_status_id');
        $affection_status_name_array = distinctTableQuery('affection_status_name','affection_status_table',$database_id,$query_field_list,$status,'equal','all');
        if (!is_array($affection_status_name_array))
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #13: $date<br>\n";
            echo "Error in call to distinctTableQuery from define_affected_individuals.php<br>\n";
            echo "$affection_status_name_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 "</body></html>\n";
	    $ret_val = disconnect();
	    exit;
          }
        $print_status = $affection_status_name[0];
        $print_nationality = strtoupper($nationality);
	echo "<form action=\"define_affected_individuals.php\" name=\"form\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(this))\">\n";
	echo "Please select the Phenotype to be used for determining the affection state of $print_nationality individuals ";
        echo "for affection status $print_status.<br><br>\n";

        echo "<center>\n";
	echo "Select Phenotype:<br>\n";
	$jscript = printPhenotypeSelectList($database_id,'0','all');
	if ($jscript == '0')
          {
            $date = date('Y-m-d H:i:s');
	    echo "Error #14: $date<br>\n";
	    echo "Error in call to printPhenotypeSelectList from define_affected_individuals.php<br>\n";
	    echo "See above error message from printPhenotypeSelectList 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 "</body></html>\n";
	    $ret_val = disconnect();
	    exit;
	  }#end error in call to printPhenotypeSelectList
	if ($jscript == 'no_defined_phenotypes')
          {
            $date = date('Y-m-d H:i:s');
            echo "This message was generated at $date.<br>\n";
            echo "</form>\n";
            echo "</body></html>\n";
            $ret_val = disconnect();
            exit;
          }
        echo "<br><br>\n";

	echo "<input type=\"hidden\" name=\"status\" value=\"$status\">\n";
	echo "<input type=\"hidden\" name=\"nationality\" value=\"$nationality\">\n";
	echo "<center>\n";
	echo "<input type=\"submit\" name=\"submit\" value=\"Enter Phenotype Criteria\">\n";
	echo "<input type=\"reset\" name=\"reset\" value=\"Reset Phenotype\">\n";
        echo "</form>\n";
        echo "</center>\n";
        echo "<br><br>\n\n";

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

	echo "<script>\n";
	echo "function Validate(form)\n";
	echo "$jscript\n";
	echo "</script>\n";
      }#end have status and nationality, but nothing else

    #5     Get Phenotype Value and Comp Mode
    if ((preg_match('/\w+/',$status)) and
	(preg_match('/\w+/',$nationality)) and
	(preg_match('/\d+/',$phenotype_id)) and
	(!preg_match('/\w+/',$type_of_phenotype)) and
	(!preg_match('/\w+/',$affection_value)) and
	(!preg_match('/\w+/',$comp_mode))
       )
      {
	echo "<form action=\"define_affected_individuals.php\" name=\"form\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(this))\">\n";
	$field_list_array = array('phenotype_id');
	$phenotype_rec_list = tableQuery('phenotype_table',$database_id,$field_list_array,$phenotype_id,'equal','all');
	if (!is_array($phenotype_rec_list))
          {
            $date = date('Y-m-d H:i:s');
	    echo "Error #15: $date<br>\n";
	    echo "Error in call to tableQuery from define_affected_individuals.php<br>\n";
	    echo "$phenotype_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 "</body></html>\n";
	    $ret_val = disconnect();
	    exit;
	  }#end error in call to tableQuery
        if (sizeof($phenotype_rec_list) == '0')
          {
            $date = date('Y-m-d H:i:s');
            echo "The Phenotype you selected with ID $phenotype_id is no longer in the database.<br>\n";
            echo "Therefore no one's affection status can be determined using values for this phenotype.<br>\n";
            echo "This message was generated at $date.<br>\n";
            echo "</body></html>\n";
            $ret_val = disconnect();
            exit;
          }#end if no longer a phenotype
        elseif (sizeof($phenotype_rec_list) == '1')
	  {
            $phenotype_rec     = $phenotype_rec_list[0];
	    $type_of_phenotype = $phenotype_rec['type_of_phenotype'];
          }
        else #error in data integrity
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #16: $date<br>\n";
            echo "!!!Error in Database Data Integrity!!!!<br>\n";
            echo "Stop using the data in the database until Database Administrator tells you all data is okay.<br>\n";
            echo "User wanted to determine which individuals were affected for affection status id $status \n";
            echo "based upon values for phenotype id $phenotype_id in database id $database_id, but when query the \n";
            echo "phenotype table with that id multiple 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 "</body></html>\n";
	    $ret_val = disconnect();
	    exit;
          }#end error in data integrity

	if ($type_of_phenotype == 'qualitative')
          {
	    echo "Please enter the phenotype value for phenotype $phenotype_rec[phenotype_name] that will be used to determine the affection staus, \n";
	    echo "and whether individuals should have a value Greater Than, Less Than, or Equal to that value in order to be considered affected.<br><br>\n";
	    echo "<table>\n";
	    echo "<tr><td>Phenotype Value:</td><td><input type=\"text\" name=\"affection_value\" size=\"15\" maxlength=\"52\"></td></tr>\n";
	    echo "<tr><td>&nbsp:</td></tr>\n";
	    echo "<tr><td>&nbsp;</td></tr>\n";
	    echo "<tr><td>Select what values affected individuals should have in relation to the value entered above:</td></tr>\n";
	    echo "<tr><td><input type=\"radio\" name=\"comp_mode\" value=\"greater_than\"></td><td>Strictly Greater Than</td></tr>\n";
            echo "<tr><td><input type=\"radio\" name=\"comp_mode\" value=\"greater_than_equal\"></td><td>Greater Than or Equal To</td></tr>\n";
	    echo "<tr><td><input type=\"radio\" name=\"comp_mode\" value=\"less_than\"></td><td>Strictly Less Than</td></tr>\n";
            echo "<tr><td><input type=\"radio\" name=\"comp_mode\" value=\"less_than_equal\"></td><td>Less Than or Equal To</td></tr>\n";
	    echo "<tr><td><input type=\"radio\" name=\"comp_mode\" value=\"equal\"></td><td>Strictly Equal To</td></tr>\n";
	    echo "</table>\n";

	    echo "<input type=\"hidden\" name=\"status\" value=\"$status\">\n";
	    echo "<input type=\"hidden\" name=\"nationality\" value=\"$nationality\">\n";
	    echo "<input type=\"hidden\" name=\"phenotype_id\" value=\"$phenotype_id\">\n";
	    echo "<input type=\"hidden\" name=\"type_of_phenotype\" value=\"$type_of_phenotype\">\n";
	    echo "<center>\n";
	    echo "<input type=\"submit\" name=\"submit\" value=\"Determine Affected Individuals\">\n";
	    echo "<input type=\"reset\" name=\"reset\" value=\"Reset Phenotype Value and Comparison Mode\">\n";
            echo "</form>\n";
            echo "</center>\n";
            echo "<br><br>\n";

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

	    echo "<script>\n";
	    echo "function Validate(form)\n";
            echo "{\n";
            echo " if (form.affection_value.value == \"\"){\n";
            echo "   alert(\"You must enter a Phenotype Value\");\n";
            echo "   return(false);}\n";
            echo "\n";
            echo " var rv = false;\n";
            echo " for (var j = 0; j < form.comp_mode.length; j++){\n";
            echo "   if (form.comp_mode[j].checked) {rv = true;}}\n";
            echo "\n";
            echo " if (!rv) {return(false);}\n";
            echo "\n";
            echo " return(true);\n";
            echo "}\n";
	    echo "</script>\n";
	  }#end if type_of_phenotype == qualitative

	if ($type_of_phenotype == 'quantitative')
          {
	    echo "Please select whether to use the 10th or 90th percentile values for phenotype $phenotype_rec[phenotype_name], and whether \n";
	    echo "individuals should have values Greater Than, Less Than, or Equal to the selected value.<br><br>\n";

	    echo "<table>\n";
	    echo "<tr><td>&nbsp;</td><td>Select Which Phenotype Value to use:</td><tr>\n";
	    echo "<tr><td><input type=\"radio\" name=\"affection_value\" value=\"10th_percentile\"></td><td>10th Percentile</td></tr>\n";
	    echo "<tr><td><input type=\"radio\" name=\"affection_value\" value=\"90th_percentile\"></td><td>90th Percentile</td></tr>\n";
	    echo "<tr><td>&nbsp;</td></tr>\n";
	    echo "<tr><td>&nbsp;</td></tr>\n";
	    echo "<tr><td>&nbsp;</td><td>Select what values affected individuals should have in relation to the value entered above:</td></tr>\n";
	    echo "<tr><td><input type=\"radio\" name=\"comp_mode\" value=\"greater_than\"></td><td>Strictly Greater Than</td></tr>\n";
            echo "<tr><td><input type=\"radio\" name=\"comp_mode\" value=\"greater_than_equal\"></td><td>Greater Than or Equal To</td></tr>\n";
	    echo "<tr><td><input type=\"radio\" name=\"comp_mode\" value=\"less_than\"></td><td>Strictly Less Than</td></tr>\n";
            echo "<tr><td><input type=\"radio\" name=\"comp_mode\" value=\"less_than_equal\"></td><td>Less Than or Equal To</td></tr>\n";
	    echo "<tr><td><input type=\"radio\" name=\"comp_mode\" value=\"equal\"></td><td>Strictly Equal To</td></tr>\n";
	    echo "</table>\n";

	    echo "<input type=\"hidden\" name=\"status\" value=\"$status\">\n";
	    echo "<input type=\"hidden\" name=\"nationality\" value=\"$nationality\">\n";
	    echo "<input type=\"hidden\" name=\"phenotype_id\" value=\"$phenotype_id\">\n";
	    echo "<input type=\"hidden\" name=\"type_of_phenotype\" value=\"$type_of_phenotype\">\n";
	    echo "<center>\n";
	    echo "<input type=\"submit\" name=\"submit\" value=\"Determine Affected Individuals\">\n";
	    echo "<input type=\"reset\" name=\"reset\" value=\"Reset Phenotype, Value, and Comparison Mode\">\n";
            echo "</form>\n";
            echo "</center>\n";
            echo "<br><br>\n";

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

	    echo "<script>\n";
	    echo "function Validate(form)\n";
	    echo "{\n";
	    echo " var rv = false;\n";
            echo " for (var i = 0; i < form.affection_value.length; i++){\n";
            echo " if (form.affection_value[i].checked) {rv = true;}}\n";
            echo "\n";
            echo " if (!rv) {return(false);}\n";
            echo "\n";
	    echo " rv = false;\n";
            echo " for (var j = 0; j < form.comp_mode.length; j++){\n";
            echo "   if (form.comp_mode[j].checked) {rv = true;}}\n";
            echo "\n";
            echo " if (!rv) {return(false);}\n";
            echo "\n";
            echo " return(true);\n";
	    echo "}\n";
	    echo "</script>\n";
	  }#end if type_of_phenotype == 'quantitative'
      }#end if have status and nationality and phenotype_id

    #6     Determine Affected Individuals
    if ((preg_match('/\w+/',$status)) and
	(preg_match('/\w+/',$nationality)) and
	(preg_match('/\d+/',$phenotype_id)) and
	(preg_match('/\w+/',$type_of_phenotype)) and
	(preg_match('/\w+/',$affection_value)) and
	(preg_match('/\w+/',$comp_mode))
       )
      {
        $rand = rand();
        $tmp_log_file = "log_file_"."$rand".".txt";
        $log_file_handle = fopen("../data/$tmp_log_file","w");
        if (!$log_file_handle)
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #17: $date<br>\n";
            echo "Error in call to fopen from insert_markers.php<br>\n";
            echo "Unable to open log file to keep track of when, what, and by whom data is altered in the database<br><br>\n";
            echo "Please email the above error using the Comments/Help/Support link \n";
            echo "on the left navigation bar.\n";
            echo "</body></html>\n";
            $ret_val = disconnect();
            exit;
          }#end error in call to fopen

        $field_list = array('affection_status_id');
        $affection_status_rec_list = tableQuery('affection_status_table',$database_id,$field_list,$status,'equal','all');
        if (!is_array($affection_status_rec_list))
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #18: $date<br>\n";
            echo "Error in call to tableQuery from define_affected_individuals.php<br>\n";
            echo "$affection_status_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 "</body></html>\n";
            fclose($log_file_handle);
            chmod("../data/$tmp_log_file",0777);
            $command = "rm ../data/$tmp_log_file";
            exec($command,$output_array,$return_code);
	    $ret_val = disconnect();
	    exit;
          }#end error in call to tableQuery
	if (sizeof($affection_status_rec_list) == '0')
          {
            $date = date('Y-m-d H:i:s');
            echo "There is no affection status in the database with affection status id $status.  ";
            echo "It appears that the affection status corresponding to that \n";
            echo "id was deleted from the database before you submitted your request to determine ";
            echo "which individuals were affected for it.  This message was generated at \n"; 
            echo "$date.\n";
            echo "</body></html>\n";
            fclose($log_file_handle);
            chmod("../data/$tmp_log_file",0777);
            $command = "rm ../data/$tmp_log_file";
            exec($command,$output_array,$return_code);
            $ret_val = disconnect();
            exit;
          }#end if no status
        elseif (sizeof($affection_status_rec_list) == '1') {$affection_status_rec = $affection_status_rec_list[0];}
        else
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #19: $date<br>\n";
            echo "!!!Error in Database Data Integrity!!!!<br>\n";
            echo "Stop using the data in the database until Database Administrator tells you all data is okay.<br>\n";
            echo "User wanted to determine which individuals were affected for affection status id $status \n";
            echo "in database id $database_id, but when query the \n";
            echo "affection status table with that id multiple 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 "</body></html>\n";
            fclose($log_file_handle);
            chmod("../data/$tmp_log_file",0777);
            $command = "rm ../data/$tmp_log_file";
            exec($command,$output_array,$return_code);
	    $ret_val = disconnect();
	    exit;
          }#end error in data integrity
	$affection_status_name = $affection_status_rec['affection_status_name'];

	if ($type_of_phenotype == 'qualitative')
          {
	    $field_list_array = array('phenotype_id');
	    $phenotype_rec_list = tableQuery('phenotype_table',$database_id,$field_list_array,$phenotype_id,'equal','all');
	    if (!is_array($phenotype_rec_list))
              {
                $date = date('Y-m-d H:i:s');
		echo "Error #20: $date<br>\n";
		echo "Error in call to tableQuery from define_affected_individuals.php<br>\n";
		echo "$phenotype_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 "</body></html>\n";
                fclose($log_file_handle);
                chmod("../data/$tmp_log_file",0777);
                $command = "rm ../data/$tmp_log_file";
                exec($command,$output_array,$return_code);
		$ret_val = disconnect();
		exit;
	      }#end error in call to tableQuery
            if (sizeof($phenotype_rec_list) == '0')
              {
                $date = date('Y-m-d H:i:s');
                echo "The Phenotype you selected with ID $phenotype_id is no longer in the database.<br>\n";
                echo "Therefore no one's affection status can be determined using values for this phenotype.<br>\n";
                echo "This message was generated at $date.<br>\n";
                echo "</body></html>\n";
                fclose($log_file_handle);
                chmod("../data/$tmp_log_file",0777);
                $command = "rm ../data/$tmp_log_file";
                exec($command,$output_array,$return_code);
                $ret_val = disconnect();
                exit;
              }
            elseif (sizeof($phenotype_rec_list) == '1')
              {
	        $phenotype_rec  = $phenotype_rec_list[0];
	        $phenotype_name = $phenotype_rec['phenotype_name'];
              }
            else #error in data integrity
              {
                $date = date('Y-m-d H:i:s');
                echo "Error #21: $date<br>\n";
                echo "!!!Error in Database Data Integrity!!!!<br>\n";
                echo "Stop using the data in the database until Database Administrator tells you all data is okay.<br>\n";
                echo "User wanted to determine which individuals were affected for affection status id $status \n";
                echo "based upon values for phenotype id $phenotype_id in database id $database_id, but when query the \n";
                echo "phenotype table with that id multiple 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 "</body></html>\n";
                fclose($log_file_handle);
                chmod("../data/$tmp_log_file",0777);
                $command = "rm ../data/$tmp_log_file";
                exec($command,$output_array,$return_code);
	        $ret_val = disconnect();
	        exit;
              }#end else error in data integrity

	    $field_list_array = array('nationality');
            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";}
	    $individual_rec_list = tableJoin('pedigree_table','individual_table',$database_id,$field_list_array,$nationality,'equal','all',$order_by);
	    if (!is_array($individual_rec_list))
              {
                $date = date('Y-m-d H:i:s');
		echo "Error #22: $date<br>\n";
		echo "Error in call to tableJoin from define_affected_individuals.php<br>\n";
		echo "$individual_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 "</body></html>\n";
                fclose($log_file_handle);
                chmod("../data/$tmp_log_file",0777);
                $command = "rm ../data/$tmp_log_file";
                exec($command,$output_array,$return_code);
		$ret_val = disconnect();
		exit;
	      }#end error in call to tableJoin

	    $affected_individual_array          = array();
            $affected_individual_table_id_array = array();
            $altered_pedigree_array             = array();
	    foreach ($individual_rec_list as $individual_rec)
              {
	        $field_list_array = array('individual_table_id','phenotype_id');
		$term_array       = array($individual_rec['individual_table_id'],$phenotype_id);
		$mode_array       = array('equal','equal');
		$phenotype_value_rec_list = compoundTableQuery('qualitative_phenotype_value_table',$database_id,$field_list_array,$term_array,$mode_array,'and','all');
		if (!is_array($phenotype_value_rec_list))
                  {
                    $date = date('Y-m-d H:i:s');
		    echo "Error #23: $date<br>\n";
		    echo "Error in call to compoundTableQuery from define_affected_individiduals.php<br>\n";
		    echo "$phenotype_value_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 "</body></html>\n";
                    fclose($log_file_handle);
                    chmod("../data/$tmp_log_file",0777);
                    $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                    exec($command,$output_array,$return_code);
                    $command = "rm ../data/$tmp_log_file";
                    exec($command,$output_array,$return_code);
		    $ret_val = disconnect();
		    exit;
		  }#end error in call to compoundTableQuery
                if (sizeof($phenotype_value_rec_list) == '0'){continue;}
                elseif (sizeof($phenotype_value_rec_list) == '1')
                  {
		    $phenotype_value_rec = $phenotype_value_rec_list[0];
		    $phenotype_value     = $phenotype_value_rec['phenotype_value'];
                  }
                else #error in data integrity
                  {
                    $date = date('Y-m-d H:i:s');
                    echo "Error #24: $date<br>\n";
                    echo "!!!Error in Database Data Integrity!!!!<br>\n";
                    echo "Stop using the data in the database until Database Administrator tells you all data is okay.<br>\n";
                    echo "User wanted to determine Individual $individual_rec[individual_id] in Pedigree $individual_rec[pedigree_id] \n";
                    echo "affection status for status $affection_status_name \n";
                    echo "based upon values for Qualitative Phenotype $phenotype_name in database id $database_id, but when query the \n";
                    echo "qualitative phenotype table for that individual and phenotype multiple 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 "</body></html>\n";
                    fclose($log_file_handle);
                    chmod("../data/$tmp_log_file",0777);
                    $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                    exec($command,$output_array,$return_code);
                    $command = "rm ../data/$tmp_log_file";
                    exec($command,$output_array,$return_code);
	            $ret_val = disconnect();
	            exit;
                  }#end else error in data integrity

		if ($comp_mode == 'greater_than')
                  {
		    if ($phenotype_value > $affection_value)
                      {
		       $insert_array = array($individual_rec['individual_table_id'],$individual_rec['individual_id'],$individual_rec['pedigree_id']);
		       $affected_individual_array[] = $insert_array;
                       $affected_individual_table_id_array[] = $individual_rec['individual_table_id'];
                       if (!in_array($individual_rec['pedigree_table_id'],$altered_pedigree_array)) {$altered_pedigree_array[] = $individual_rec['pedigree_table_id'];}
		      }#end if phenotype_value > affection_value
		  }#end if comp_mode == 'greater_than'

                 if ($comp_mode == 'greater_than_equal')
                  {
		    if ($phenotype_value >= $affection_value)
                      {
		       $insert_array = array($individual_rec['individual_table_id'],$individual_rec['individual_id'],$individual_rec['pedigree_id']);
		       $affected_individual_array[] = $insert_array;
                       $affected_individual_table_id_array[] = $individual_rec['individual_table_id'];
                       if (!in_array($individual_rec['pedigree_table_id'],$altered_pedigree_array)) {$altered_pedigree_array[] = $individual_rec['pedigree_table_id'];}
		      }#end if phenotype_value >= affection_value
		  }#end if comp_mode == 'greater_than_equal'

		 if ($comp_mode == 'less_than')
                   {
		    if ($phenotype_value < $affection_value)
                      {
		       $insert_array = array($individual_rec['individual_table_id'],$individual_rec['individual_id'],$individual_rec['pedigree_id']);
		       $affected_individual_array[] = $insert_array;
                       $affected_individual_table_id_array[] = $individual_rec['individual_table_id'];
                       if (!in_array($individual_rec['pedigree_table_id'],$altered_pedigree_array)) {$altered_pedigree_array[] = $individual_rec['pedigree_table_id'];}
		      }#end if phenotype_value < affection_value
		   }#end if comp_mode == 'less_than'

                 if ($comp_mode == 'less_than_equal')
                   {
		    if ($phenotype_value <= $affection_value)
                      {
		       $insert_array = array($individual_rec['individual_table_id'],$individual_rec['individual_id'],$individual_rec['pedigree_id']);
		       $affected_individual_array[] = $insert_array;
                       $affected_individual_table_id_array[] = $individual_rec['individual_table_id'];
                       if (!in_array($individual_rec['pedigree_table_id'],$altered_pedigree_array)) {$altered_pedigree_array[] = $individual_rec['pedigree_table_id'];}
		      }#end if phenotype_value <= affection_value
		   }#end if comp_mode == 'less_than_equal'

		 if ($comp_mode == 'equal')
                   {
		    if ($phenotype_value == $affection_value)
                      {
		       $insert_array = array($individual_rec['individual_table_id'],$individual_rec['individual_id'],$individual_rec['pedigree_id']);
		       $affected_individual_array[] = $insert_array;
                       $affected_individual_table_id_array[] = $individual_rec['individual_table_id'];
                       if (!in_array($individual_rec['pedigree_table_id'],$altered_pedigree_array)) {$altered_pedigree_array[] = $individual_rec['pedigree_table_id'];}
		      }#end if phenotype_value == affection_value
		   }#end if comp_mode == 'equal'
	       }#end foreach individual

	      if (sizeof($affected_individual_array) > '0')
                {
                  echo "The Below individuals were determined to be affected for affection status $affection_status_name based upon being "; 
	          if ($comp_mode == 'greater_than')       {echo "Strictly Greater Than ";}
                  if ($comp_mode == 'greater_than_equal') {echo "Greater Than or Equal To ";}
	          if ($comp_mode == 'less_than')          {echo "Strictly Less Than ";}
                  if ($comp_mode == 'less_than_equal')    {echo "Less Than or Equal To ";}
	          if ($comp_mode == 'equal')              {echo "Strictly Equal To ";}
	          echo "value $affection_value for phenotype $phenotype_name.<br>\n";

	          foreach ($affected_individual_array as $individual_info_array)
                    {
                      $field_list_array = array('affection_status_id','individual_table_id');
                      $term_array = array($status,$individual_info_array[0]);
                      $mode_array = array('equal','equal');
                      $indiv_affect_link_rec_list = compoundTableQuery('individual_affection_status_link_table',$database_id,$field_list_array,$term_array,$mode_array,'and','all');
                      if (!is_array($indiv_affect_link_rec_list))
                        {
                          $date = date('Y-m-d H:i:s');
                          echo "Error #25: $date<br>\n";
                          echo "Error in call to compoundTableQuery from define_affected_individuals.php<br>\n";
                          echo "$indiv_affect_link_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 "</body></html>\n";
                          fclose($log_file_handle);
                          chmod("../data/$tmp_log_file",0777);
                          $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                          exec($command,$output_array,$return_code);
                          $command = "rm ../data/$tmp_log_file";
                          exec($command,$output_array,$return_code);
		          $ret_val = disconnect();
		          exit;
                        }
                      if (sizeof($indiv_affect_link_rec_list) == '0')
                        {
                          $ret_val = insertIndividualAffectionStatusLink($database_id,$status,$individual_info_array[0],'2');
                          if ($ret_val != '1')
                            {
                              $date = date('Y-m-d H:i:s');
                              echo "Error #26: $date<br>\n";
                              echo "Error in call to insertIndividualAffectionStatusLink from define_affected_individuals.php<br>\n";
                              echo "$ret_val<br><br>\n";
                              echo "Please email the above error using the Comments/Help/Support link on the \n";
                              echo "left navigation bar.\n";
                              echo "</body></html>\n";
                              fclose($log_file_handle);
                              chmod("../data/$tmp_log_file",0777);
                              $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                              exec($command,$output_array,$return_code);
                              $command = "rm ../data/$tmp_log_file";
                              exec($command,$output_array,$return_code);
                              $ret_val = disconnect();
                              exit;
                            }
                          echo "Individual $individual_info_array[1] in Pedigree $individual_info_array[2] was determined to be affected.<br>\n";
                          $date = date('Y-m-d H:i:s');
                          $log_file_statement = "$date:\t$_SESSION[user_name] Inserted New Individual-Affection Status Link into Database ID $database_id ";
                          $log_file_statement.= "for Affection Status ID $status and Individual Table ID $individual_info_array[0], ";
                          $log_file_statement.= "set Individual Affection Status to 2.\n";
                          fwrite($log_file_handle,$log_file_statement);
                        }#end if individual affection status link doesn't exist
                      elseif(sizeof($indiv_affect_link_rec_list) == '1')
                        {
                          $indiv_affect_link_rec = $indiv_affect_link_rec_list[0];
                          $indiv_affect_link_id  = $indiv_affect_link_rec['individual_affection_status_link_id'];
	                  $update_field_array = array('individual_affection_status');
		          $update_value_array = array('2');
		          $ret_val = updateRecord('individual_affection_status_link_table',$indiv_affect_link_id,$update_field_array,$update_value_array);
		          if ($ret_val != '1')
                            {
                              $date = date('Y-m-d H:i:s');
		              echo "Error #27: $date<br>\n";
		              echo "Error in call to updateRecord from define_affected_individuals.php<br>\n";
		              echo "$ret_val<br><br>\n";
		              echo "Please email the above error using the Comments/Help/Support link on the \n";
		              echo "left navigation bar.\n";
		              echo "</body></html>\n";
                              fclose($log_file_handle);
                              chmod("../data/$tmp_log_file",0777);
                              $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                              exec($command,$output_array,$return_code);
                              $command = "rm ../data/$tmp_log_file";
                              exec($command,$output_array,$return_code);
		              $ret_val = disconnect();
		              exit;
		            }#end error in call to updateRecord
		          echo "Individual $individual_info_array[1] in Pedigree $individual_info_array[2] was determined to be affected.<br>\n";
                          $date = date('Y-m-d H:i:s');
                          $log_file_statement = "$date:\t$_SESSION[user_name] Updated Individual-Affection Status ID $indiv_affect_link_id in ";
                          $log_file_statement.= "Database ID $database_id, set Individual Affection Status to 2.\n";
                          fwrite($log_file_handle,$log_file_statement);
                        }#end if individual affection status link exists
                      else #error in the data in the database
                        {
                          $date = date('Y-m-d H:i:s');
                          echo "Error #28: $date<br>\n";
                          echo "!!!Error in Database Data Integrity!!!!<br>\n";
                          echo "Stop using the data in the database until Database Administrator tells you all data is okay.<br>\n";
                          echo "When queried the Individual Affection Status Link Table with affection_status_id $status and \n";
                          echo "individual_table_id $individual_info_array[0] in database id $database_id there was more than one record returned!<br>\n";
                          echo "The individual_affection_status_link_id's returned were:<br>\n";
                          foreach ($indiv_affect_link_rec_list as $indiv_affect_link_rec) {echo "$indiv_affect_link_rec[individual_affection_status_link_id]<br>\n";}
                          echo "<br>\n";
                          echo "Please email the above error using the Comments/Help/Support link on the \n";
		          echo "left navigation bar.\n";
		          echo "</body></html>\n";
                          fclose($log_file_handle);
                          chmod("../data/$tmp_log_file",0777);
                          $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                          exec($command,$output_array,$return_code);
                          $command = "rm ../data/$tmp_log_file";
                          exec($command,$output_array,$return_code);
		          $ret_val = disconnect();
		          exit;
                        }#end else there is an error in the data in the database
	            }#end foreach affected_individual

                  foreach ($individual_rec_list as $individual_rec) #determine which individuals were not affected and set status to 0
                    {
                      if (!in_array($individual_rec['individual_table_id'],$affected_individual_table_id_array))
                        {
                          $field_list_array = array('affection_status_id','individual_table_id');
                          $term_array       = array($status,$individual_rec['individual_table_id']);
                          $mode_array       = array('equal','equal');
                          $indiv_affect_link_rec_list = compoundTableQuery('individual_affection_status_link_table',$database_id,$field_list_array,$term_array,$mode_array,'and','all');
                          if (!is_array($indiv_affect_link_rec_list))
                            {
                              $date = date('Y-m-d H:i:s');
                              echo "Error #29: $date<br>\n";
                              echo "Error in call to compoundTableQuery from define_affected_individuals.php<br>\n";
                              echo "$indiv_affect_link_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 "</body></html>\n";
                              fclose($log_file_statement);
                              $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                              exec($command,$output_array,$return_code);
                              $command = "rm ../data/$tmp_log_file";
                              exec($command,$output_array,$return_code);
		              $ret_val = disconnect();
		              exit;
                            }#end error in call to compoundTableQuery
                          if (sizeof($indiv_affect_link_rec_list) == '0')
                            {
                              $ret_val = insertIndividualAffectionStatusLink($database_id,$status,$individual_rec['individual_table_id'],'0');
                              if ($ret_val != '1')
                                {
                                  $date = date('Y-m-d H:i:s');
                                  echo "Error #30: $date<br>\n";
                                  echo "Error in call to insertIndividualAffectionStatusLink from define_affected_individuals.php<br>\n";
                                  echo "$ret_val<br><br>\n";
                                  echo "Please email the above error using the Comments/Help/Support link on the \n";
		                  echo "left navigation bar.\n";
		                  echo "</body></html>\n";
                                  fclose($log_file_handle);
                                  chmod("../data/$tmp_log_file",0777);
                                  $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                                  exec($command,$output_array,$return_code);
                                  $command = "rm ../data/$tmp_log_file";
                                  exec($command,$output_array,$return_code);
		                  $ret_val = disconnect();
		                  exit;
                                }#end error in call to insertIndividualAffectionStatusLink
                              $date = date('Y-m-d H:i:s');
                              $log_file_statement = "$date:\t$_SESSION[user_name] Inserted new Individual-Affection Status Link into ";
                              $log_file_statement.= "Database ID $database_id for Affection Status ID $status and Individual Table ID ";
                              $log_file_statement.= "$individual_rec[individual_table_id], set Individual Affection Status to 0.\n";
                              fwrite($log_file_handle,$log_file_statement);
                            }#end if individual affection status link does not exist
                          elseif (sizeof($indiv_affect_link_rec_list) == '1')
                            {
                              $indiv_affect_link_rec = $indiv_affect_link_rec_list[0];
                              $indiv_affect_link_id  = $indiv_affect_link_rec['individual_affection_status_link_id'];
                              $update_field_array    = array('individual_affection_status');
                              $update_value_array    = array('0');
                              $ret_val               = updateRecord('individual_affection_status_link_table',$indiv_affect_link_rec,$update_field_array,$update_value_array);
                              if ($ret_val != '1')
                                {
                                  $date = date('Y-m-d H:i:s');
                                  echo "Error #31: $date<br>\n";
                                  echo "Error in call to updateRecord from define_affected_individuals.php<br>\n";
                                  echo "$ret_val<br><br>\n";
                                  echo "Please email the above error using the Comments/Help/Support link on the \n";
		                  echo "left navigation bar.\n";
		                  echo "</body></html>\n";
                                  fclose($log_file_handle);
                                  chmod("../data/$tmp_log_file",0777);
                                  $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                                  exec($command,$output_array,$return_code);
                                  $command = "rm ../data/$tmp_log_file";
                                  exec($command,$output_array,$return_code);
		                  $ret_val = disconnect();
		                  exit;
                                }#end error in call to updateRecord
                              $date = date('Y-m-d H:i:s');
                              $log_file_statement = "$date:\t$_SESSION[user_name] Updated Individual-Affection Status Link ID $indiv_affect_link_rec ";
                              $log_file_statement.= "in Database ID $database_id, set Individual Affection Status to 0.\n";
                              fwrite($log_file_handle,$log_file_statement);
                            }#end if have link rec
                          else #error in the data in the database
                            {
                              $date = date('Y-m-d H:i:s');
                              echo "Error #32: $date<br>\n";
                              echo "!!!Error in Database Data Integrity!!!!<br>\n";
                              echo "Stop using the data in the database until Database Administrator tells you all data is okay.<br>\n";
                              echo "When queried the Individual Affection Status Link Table with affection_status_id $status and \n";
                              echo "individual_table_id $individual_info_array[0] in database id $database_id \n";
                              echo "there was more than one record returned!<br>\n";
                              echo "The individual_affection_status_link_id's returned were:<br>\n";
                              foreach ($indiv_affect_link_rec_list as $indiv_affect_link_rec) {echo "$indiv_affect_link_rec[individual_affection_status_link_id]<br>\n";}
                              echo "<br>\n";
                              echo "Please email the above error using the Comments/Help/Support link on the \n";
		              echo "left navigation bar.\n";
		              echo "</body></html>\n";
                              fclose($log_file_handle);
                              chmod("../data/$tmp_log_file",0777);
                              $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                              exec($command,$output_array,$return_code);
                              $command = "rm ../data/$tmp_log_file";
                              exec($command,$output_array,$return_code);
		              $ret_val = disconnect();
		              exit;
                            }#end else there is an error in the data in the database
                        }#end if individual was not determined to be affected
                      else {continue;}#individual was determined to be affected
                    }#end determine which individuals were not determined to be affected and set affection status to 0

                  foreach ($altered_pedigree_array as $altered_pedigree)
                    {
                      $field_list_array = array('pedigree_table_id');
                      $rec_list = distinctTableQuery('individual_table_id','individual_table',$database_id,$field_list_array,$altered_pedigree,'equal','all');
                      if (!is_array($rec_list))
                        {
                          $date = date('Y-m-d H:i:s');
                          echo "Error #33: $date<br>\n";
                          echo "Error in call to distinctTableQuery from define_affected_individuals.php<br>\n";
                          echo "$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 "</body></html>\n";
                          fclose($log_file_handle);
                          chmod("../data/$tmp_log_file",0777);
                          $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                          exec($command,$output_array,$return_code);
                          $command = "rm ../data/$tmp_log_file";
                          exec($command,$output_array,$return_code);
		          $ret_val = disconnect();
		          exit;
                        }#end error in call to tableQuery
                      $indiv_table_id_list = join($rec_list,',');
                      unset($rec_list);

                      $field_list_array = array('affection_status_id','individual_table_id','individual_affection_status');
                      $term_array       = array($status,$indiv_table_id_list,'2');
                      $mode_array       = array('equal','in','equal');
                      $rec_list         = compoundTableQuery('individual_affection_status_link_table',$database_id,$field_list_array,$term_array,$mode_array,'and','all');
                      if (!is_array($rec_list))
                        {
                          echo "Error 13a:<br>\n";
                          echo "Error in call to compoundTableQuery from define_affected_individuals.php<br>\n";
                          echo "$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 "</body></html>\n";
                          fclose($log_file_handle);
                          chmod("../data/$tmp_log_file",0777);
                          $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                          exec($command,$output_array,$return_code);
                          $command = "rm ../data/$tmp_log_file";
                          exec($command,$output_array,$return_code);
		          $ret_val = disconnect();
		          exit;
                        }
                      $num_affected = sizeof($rec_list);

                      $field_list_array = array('affection_status_id','pedigree_table_id');
                      $term_array       = array($status,$altered_pedigree);
                      $mode_array       = array('equal','equal');
                      $ped_affect_link_rec_list = compoundTableQuery('pedigree_affection_status_link_table',$database_id,$field_list_array,$term_array,$mode_array,'and','all');
                      if (!is_array($ped_affect_link_rec_list))
                        {
                          $date = date('Y-m-d H:i:s');
                          echo "Error #34: $date<br>\n";
                          echo "Error in call to compoundTableQuery from define_affected_individuals.php<br>\n";
                          echo "$ped_affect_link_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 "</body></html>\n";
                          fclose($log_file_handle);
                          chmod("../data/$tmp_log_file",0777);
                          $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                          exec($command,$output_array,$return_code);
                          $command = "rm ../data/$tmp_log_file";
                          exec($command,$output_array,$return_code);
		          $ret_val = disconnect();
		          exit;
                        }
                      if (sizeof($ped_affect_link_rec_list) == '0')
                        {
                          $ret_val = insertPedigreeAffectionStatusLink($database_id,$status,$altered_pedigree,$num_affected);
                          if ($ret_val != '1')
                            {
                              $date = date('Y-m-d H:i:s');
                              echo "Error #35: $date<br>\n";
                              echo "Error in call to insertPedigreeAffectionStatusLink from define_affected_individuals.php<br>\n";
                              echo "$ret_val<br><br>\n";
                              echo "Please email the above error using the Comments/Help/Support link on the \n";
		              echo "left navigation bar.\n";
		              echo "</body></html>\n";
                              fclose($log_file_handle);
                              chmod("../data/$tmp_log_file",0777);
                              $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                              exec($command,$output_array,$return_code);
                              $command = "rm ../data/$tmp_log_file";
                              exec($command,$output_array,$return_code);
		              $ret_val = disconnect();
		              exit;
                            }#end error in call to insertPedigreeAffectionStatusLink
                          $date = date('Y-m-d H:i:s');
                          $log_file_statement = "$date:\t$_SESSION[user_name] Inserted new Pedigree-Affection Status Link into ";
                          $log_file_statement.= "Database ID $database_id for Affection Status ID $status and Pedigree Table ID $altered_pedigree, ";
                          $log_file_statement.= "Number Affected in Pedigree to $num_affected.\n";
                          fwrite($log_file_handle,$log_file_statement);
                        }#end if no pedigree affection status link exists
                      elseif ($sizeof($ped_affect_link_rec_list) == '1')
                        {
                          $ped_affect_link_rec = $ped_affect_link_rec_list[0];
                          $ped_affect_link_id  = $ped_affect_link_rec['pedigree_affection_status_link_id'];
                          $update_field_array = array('number_affected_in_pedigree');
                          $update_value_array = array ($num_affected);
                          $ret_val = updateRecord('pedigree_affection_status_link_table',$ped_affect_link_id,$update_field_array,$update_value_array);
                          if ($ret_val != '1')
                            {
                              $date = date('Y-m-d H:i:s');
                              echo "Error #36: $date<br>\n";
                              echo "Error in call to updateRecord from define_affected_individuals.php<br>\n";
                              echo "$ret_val<br><br>\n";
                              echo "Please email the above error using the Comments/Help/Support link on the \n";
		              echo "left navigation bar.\n";
		              echo "</body></html>\n";
                              fclose($log_file_handle);
                              chmod("../data/$tmp_log_file",0777);
                              $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                              exec($command,$output_array,$return_code);
                              $command = "rm ../data/$tmp_log_file";
                              exec($command,$output_array,$return_code);
		              $ret_val = disconnect();
		              exit;
                            }#end error in call to updateRecord
                          $date = date('Y-m-d H:i:s');
                          $log_file_statement = "$date:\t$_SESSION[user_name] Updated Pedigree-Affection Status Link ID $ped_affect_link_id ";
                          $log_file_statement.= "in Database ID $database_id, set Number Affected in Pedigree to $num_affected.\n";
                          fwrite($log_file_handle,$log_file_statement);
                        }#end if pedigree affection status link exits
                      else #data integrity error
                        {
                          $date = date('Y-m-d H:i:s');
                          echo "Error #37: $date<br>\n";
                          echo "!!!Error in Database Data Integrity!!!!<br>\n";
                          echo "Stop using the data in the database until Database Administrator tells you all data is okay.<br>\n";
                          echo "When queried the Pedigree Affection Status Link Table with affection_status_id $status and \n";
                          echo "pedigree_table_id $altered_pedigree in database id $database_id there was more than one record returned!<br>\n";
                          echo "The pedigree_affection_status_link_id's returned were:<br>\n";
                          foreach ($ped_affect_link_rec_list as $ped_affect_link_rec) {echo "$ped_affect_link_rec[pedigree_affection_status_link_id]<br>\n";}
                          echo "<br>\n";
                          echo "Please email the above error using the Comments/Help/Support link on the \n";
		          echo "left navigation bar.\n";
		          echo "</body></html>\n";
                          fclose($log_file_handle);
                          chmod("../data/$tmp_log_file",0777);
                          $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                          exec($command,$output_array,$return_code);
                          $command = "rm ../data/$tmp_log_file";
                          exec($command,$output_array,$return_code);
		          $ret_val = disconnect();
		          exit;
                        }#end else there is an error in the data in the database
                    }#end foreach altered_pedigree
                }#end if have affected individuals
              else #no affected individuals
                {
                  echo "There were no individuals determined to be affected for affection status $affection_status_name by having phenotype values "; 
	          if ($comp_mode == 'greater_than')       {echo "Strictly Greater Than ";}
                  if ($comp_mode == 'greater_than_equal') {echo "Greater Than or Equal To ";}
	          if ($comp_mode == 'less_than')          {echo "Strictly Less Than ";}
                  if ($comp_mode == 'less_than_equal')    {echo "Less Than or Equal To ";}
	          if ($comp_mode == 'equal')              {echo "Equal To ";}
	          echo "value $affection_value for phenotype $phenotype_name.<br>\n";

                  foreach ($individual_rec_list as $individual_rec) #set status to 0 for all individuals
                    {
                      $field_list_array = array('affection_status_id','individual_table_id');
                      $term_array       = array($status,$individual_rec['individual_table_id']);
                      $mode_array       = array('equal','equal');
                      $indiv_affect_link_rec_list = compoundTableQuery('individual_affection_status_link_table',$database_id,$field_list_array,$term_array,$mode_array,'and','all');
                      if (!is_array($indiv_affect_link_rec_list))
                        {
                          $date = date('Y-m-d H:i:s');
                          echo "Error #38: $date<br>\n";
                          echo "Error in call to compoundTableQuery from define_affected_individuals.php<br>\n";
                          echo "$indiv_affect_link_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 "</body></html>\n";
                          fclose($log_file_handle);
                          chmod("../data/$tmp_log_file",0777);
                          $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                          exec($command,$output_array,$return_code);
                          $command = "rm ../data/$tmp_log_file";
                          exec($command,$output_array,$return_code);
		          $ret_val = disconnect();
		          exit;
                        }#end error in call to compoundTableQuery
                      if (sizeof($indiv_affect_link_rec_list) == '0')
                        {
                          $ret_val = insertIndividualAffectionStatusLink($database_id,$status,$individual_rec['individual_table_id'],'0');
                          if ($ret_val != '1')
                            {
                              $date = date('Y-m-d H:i:s');
                              echo "Error #39: $date<br>\n";
                              echo "Error in call to insertIndividualAffectionStatusLink from define_affected_individuals.php<br>\n";
                              echo "$ret_val<br><br>\n";
                              echo "Please email the above error using the Comments/Help/Support link on the \n";
		              echo "left navigation bar.\n";
		              echo "</body></html>\n";
                              fclose($log_file_handle);
                              chmod("../data/$tmp_log_file",0777);
                              $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                              exec($command,$output_array,$return_code);
                              $command = "rm ../data/$tmp_log_file";
                              exec($command,$output_array,$return_code);
		              $ret_val = disconnect();
		              exit;
                            }#end error in call to insertIndividualAffectionStatusLink
                          $date = date('Y-m-d H:i:s');
                          $log_file_statement = "$date:\t$_SESSION[user_name] Inserted a new Individual-Affection Status Link into ";
                          $log_file_statement.= "Database ID $database_id for Affection Status ID $status and Individual Table ID ";
                          $log_file_statement.= "$individual_rec[individual_table_id], set Individual Affection Status to 0.\n";
                          fwrite($log_file_handle,$log_file_statement);
                        }#end if individual affection status link does not exist
                      elseif (sizeof($indiv_affect_link_rec_list) == '1')
                        {
                          $indiv_affect_link_rec = $indiv_affect_link_rec_list[0];
                          $indiv_affect_link_id  = $indiv_affect_link_rec['individual_affection_status_link_id'];
                          $update_field_array    = array('individual_affection_status');
                          $update_value_array    = array('0');
                          $ret_val               = updateRecord('individual_affection_status_link_table',$indiv_affect_link_id,$update_field_array,$update_value_array);
                          if ($ret_val != '1')
                            {
                              $date = date('Y-m-d H:i:s');
                              echo "Error #40: $date<br>\n";
                              echo "Error in call to updateRecord from define_affected_individuals.php<br>\n";
                              echo "$ret_val<br><br>\n";
                              echo "Please email the above error using the Comments/Help/Support link on the \n";
		              echo "left navigation bar.\n";
		              echo "</body></html>\n";
                              fclose($log_file_handle);
                              chmod("../data/$tmp_log_file",0777);
                              $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                              exec($command,$output_array,$return_code);
                              $command = "rm ../data/$tmp_log_file";
                              exec($command,$output_array,$return_code);
		              $ret_val = disconnect();
		              exit;
                            }#end error in call to updateRecord
                          $date = date('Y-m-d H:i:s');
                          $log_file_statement = "$date:\t$_SESSION[user_name] Updated Individual-Affection Status ID $indiv_affect_link_id in ";
                          $log_file_statement.= "Database ID $database_id, set Individual Affection Status to 0.\n";
                          fwrite($log_file_handle,$log_file_statement);
                        }#end else individual affection status link does exist
                      else #error in the data in the database
                        {
                          $date = date('Y-m-d H:i:s');
                          echo "Error #41: $date<br>\n";
                          echo "!!!Error in Database Data Integrity!!!!<br>\n";
                          echo "Stop using the data in the database until Database Administrator tells you all data is okay.<br>\n";
                          echo "When queried the Individual Affection Status Link Table with affection_status_id $status and \n";
                          echo "individual_table_id $individual_info_array[0] in database id $database_id there was more than one record returned!<br>\n";
                          echo "The individual_affection_status_link_id's returned were:<br>\n";
                          foreach ($indiv_affect_link_rec_list as $indiv_affect_link_rec) {echo "$indiv_affect_link_rec[individual_affection_status_link_id]<br>\n";}
                          echo "<br>\n";
                          echo "Please email the above error using the Comments/Help/Support link on the \n";
		          echo "left navigation bar.\n";
		          echo "</body></html>\n";
                          fclose($log_file_handle);
                          chmod("../data/$tmp_log_file",0777);
                          $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                          exec($command,$output_array,$return_code);
                          $command = "rm ../data/$tmp_log_file";
                          exec($command,$output_array,$return_code);
		          $ret_val = disconnect();
		          exit;
                        }#end else there is an error in the data in the database
                    }#end set affection status to 0 for all individuals
                }#end else no affected individuals
	     }#end if (type_of_phenotype == 'qualitative')

	   if ($type_of_phenotype == 'quantitative')
             {
	      $field_list_array = array('phenotype_id');
	      $phenotype_rec_list = tableQuery('phenotype_table',$database_id,$field_list_array,$phenotype_id,'equal','all');
	      if (!is_array($phenotype_rec_list))
                {
                  $date = date('Y-m-d H:i:s');
		  echo "Error #42: $date<br>\n";
		  echo "Error in call to tableQuery from define_affected_individuals.php<br>\n";
		  echo "$phenotype_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 "</body></html>\n";
                  fclose($log_file_handle);
                  chmod("../data/$tmp_log_file",0777);
                  $command = "rm ../data/$tmp_log_file";
                  exec($command,$output_array,$return_code);
		  $ret_val = disconnect();
		  exit;
	        }#end error in call to tableQuery
              if (sizeof($phenotype_rec_list) == '0')
                {
                  $date = date('Y-m-d H:i:s');
                  echo "The Phenotype you selected with ID $phenotype_id is no longer in the database.<br>\n";
                  echo "Therefore no one's affection status can be determined using values for this phenotype.<br>\n";
                  echo "This message was generated at $date.<br>\n";
                  echo "</body></html>\n";
                  fclose($log_file_handle);
                  chmod("../data/$tmp_log_file",0777);
                  $command = "rm ../data/$tmp_log_file";
                  exec($command,$output_array,$return_code);
                  $ret_val = disconnect();
                  exit;
                }#end if no phenotype
              elseif (sizeof($phenotype_rec_list) == '1')
                {
	          $phenotype_rec  = $phenotype_rec_list[0];
	          $phenotype_name = $phenotype_rec['phenotype_name'];
                }#end if have phenotype
              else #error in data integrity
                {
                  $date = date('Y-m-d H:i:s');
                  echo "Error #43: $date<br>\n";
                  echo "!!!Error in Database Data Integrity!!!!<br>\n";
                  echo "Stop using the data in the database until Database Administrator tells you all data is okay.<br>\n";
                  echo "User wanted to determine which individuals were affected for affection status id $status \n";
                  echo "based upon values for quantitative phenotype id $phenotype_id in database_id $database_id, but when query the \n";
                  echo "phenotype table with that id multiple 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 "</body></html>\n";
                  fclose($log_file_handle);
                  chmod("../data/$tmp_log_file",0777);
                  $command = "rm ../data/$tmp_log_file";
                  exec($command,$output_array,$return_code);
	          $ret_val = disconnect();
	          exit;
                }#end else error in data integrity

	      $field_list_array = array('phenotype_name');
	      $age_phenotype_rec_list = tableQuery('phenotype_table',$database_id,$field_list_array,'age','equal','all');
	      if (!is_array($age_phenotype_rec_list))
                {
                  $date = date('Y-m-d H:i:s');
		  echo "Error #44: $date<br>\n";
		  echo "Error in call to tableQuery from define_affected_individuals.php<br>\n";
		  echo "$age_phenotype_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 "</body></html>\n";
                  fclose($log_file_handle);
                  chmod("../data/$tmp_log_file",0777);
                  $command = "rm ../data/$tmp_log_file";
                  exec($command,$output_array,$return_code);
		  $ret_val = disconnect();
		  exit;
	        }#end error in call to tableQuery
              if (sizeof($age_phenotype_rec_list) == '0')
                {
                  $date = date('Y-m-d H:i:s');
                  echo "In order to determine affected individuals for quantitative traits, all individuals must have an age associated with them.  \n";
                  echo "This means that there needs to be a phenotype defined in the database named \"age\", but there is no such defined phenotype.  \n";
                  echo "Please use the \"Insert Phenotype\" tool on the left navigation bar to define this phenotype, \n";
                  echo "then use the \"Insert Phenotype Values\"  tool \n";
                  echo "to insert the age of all the individuals.<br>This message was generated at $date.\n";
                  fclose($log_file_handle);
                  chmod("../data/$tmp_log_file",0777);
                  $command = "rm ../data/$tmp_log_file";
                  exec($command,$output_array,$return_code);
                  $ret_val = disconnect();
                  exit;
                }
              elseif (sizeof($age_phenotype_rec_list) == '1')
                {
	          $age_phenotype_rec = $age_phenotype_rec_list[0];
	          $age_phenotype_id  = $age_phenotype_rec['phenotype_id'];
                }#end if have age phenotype
              else #error in data integrity
                {
                  $date = date('Y-m-d H:i:s');
                  echo "Error #45: $date<br>\n";
                  echo "!!!Error in Database Data Integrity!!!!<br>\n";
                  echo "Stop using the data in the database until Database Administrator tells you all data is okay.<br>\n";
                  echo "User wanted to determine which individuals were affected for affection status id $status \n";
                  echo "based upon values for quantitative phenotype id $phenotype_id in database id $database_id. \n";
                  echo "In order to due this need individual's age \n";
                  echo "but when query the phenotype table for 'age' id multiple 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 "</body></html>\n";
                  fclose($log_file_handle);
                  chmod("../data/$tmp_log_file",0777);
                  $command = "rm ../data/$tmp_log_file";
                  exec($command,$output_array,$return_code);
	          $ret_val = disconnect();
	          exit;
                }#end else error in data integrity

	      $field_list_array = array('nationality');
              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";}
	      $nat_indiv_table_id_array = distinctTableJoin('individual_table_id','pedigree_table','individual_table',$database_id,$field_list_array,$nationality,'equal','all',$order_by);
	      if (!is_array($nat_indiv_table_id_array))
                {
                  $date = date('Y-m-d H:i:s');
		  echo "Error #46: $date<br>\n";
		  echo "Error in call to distinctTableQuery from define_affected_individuals.php<br>\n";
		  echo "$nat_indiv_table_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 "</body></html>\n";
                  fclose($log_file_handle);
                  chmod("../data/$tmp_log_file",0777);
                  $command = "rm ../data/$tmp_log_file";
                  exec($command,$output_array,$return_code);
		  $ret_val = disconnect();
		  exit;
	        }#end error in call to distinctTableJoin
              if (sizeof($nat_indiv_table_id_array) == '0')
                {
                  $date = date('Y-m-d H:i:s');
                  echo "There are no pedigrees, and therefore no individuals, that belong to nationality $nationality.<br>\n";
                  echo "So no individuals can be determined to be affected for Affection Status $affection_status_name based upon \n";
                  echo "Phenotype $phenotype_name.<br>\n";
                  echo "This message was generated at $date<br>\n";
                  echo "</body></html>\n";
                  fclose($log_file_handle);
                  chmod("../data/$tmp_log_file",0777);
                  $command = "rm ../data/$tmp_log_file";
                  exec($command,$output_array,$return_code);
                  $ret_val = disconnect();
                  exit;
                }#end if no individuals
	      $nat_indiv_table_id_term = join($nat_indiv_table_id_array,',');
              unset($nat_indiv_table_id_array);

	      $field_list_array = array('age_group_nationality');
	      $age_group_rec_list = tableQuery('age_group_table',$database_id,$field_list_array,$nationality,'equal','all');
	      if (!is_array($age_group_rec_list))
                {
                  $date = date('Y-m-d H:i:s');
		  echo "Error #47:$date<br>\n";
		  echo "Error in call to tableQuery from define_affected_individuals.php<br>\n";
		  echo "$age_group_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 "</body></html>\n";
                  fclose($log_file_handle);
                  chmod("../data/$tmp_log_file",0777);
                  $command = "rm ../data/$tmp_log_file";
                  exec($command,$output_array,$return_code);
		  $ret_val = disconnect();
		  exit;
	        }#end error in call to tableQuery
              if (sizeof($age_group_rec_list) == '0')
                {
                  $date = date('Y-m-d H:i:s');
                  echo "There are no defined age groups in the database for Nationality $nationality.<br>\n";
                  echo "Therefore Individuals belonging to that nationality can be determined to be affected \n";
                  echo "for Affection Status $affection_status_name based on the Quantitative Phenotype $phenotype_name.<br>\n";
                  echo "This message was generated at $date.<br>\n";
                  echo "</body></html>\n";
                  fclose($log_file_handle);
                  chmod("../data/$tmp_log_file",0777);
                  $command = "rm ../data/$tmp_log_file";
                  exec($command,$output_array,$return_code);
                  $ret_val = disconnect();
                  exit;
                }#end if no age groups for nationality

	      $affected_individual_array = array();
              $affected_individual_table_id_array = array();
              $altered_pedigree_array    = array();
	      foreach ($age_group_rec_list as $age_group_rec)
                {
	          $min_age = $age_group_rec['age_group_min_age'];
		  $max_age = $age_group_rec['age_group_max_age'];
		  $query_min_age = $min_age - 1;
		  $query_max_age = $max_age + 1;

		  $field_list_array = array('age_group_id','phenotype_id');
		  $term_array       = array($age_group_rec['age_group_id'],$phenotype_id);
		  $mode_array       = array('equal','equal');
		  $age_group_phenotype_percentile_link_rec_list = compoundTableQuery('age_group_phenotype_percentile_link_table',$database_id,$field_list_array,$term_array,$mode_array,'and','all');
		  if (!is_array($age_group_phenotype_percentile_link_rec_list))
                    {
                      $date = date('Y-m-d H:i:s');
		      echo "Error #48: $date<br>\n";
		      echo "Error in call to compoundTableQuery from define_affected_individuals.php<br>\n";
		      echo "$age_group_phenotype_percentile_link_rec_list<br><br>\n";
		      echo "Please email the above the error using the Comments/Help/Support link on the \n";
		      echo "left navigation bar.\n";
		      echo "</body></html>\n";
                      fclose($log_file_handle);
                      chmod("../data/$tmp_log_file",0777);
                      $command = "rm ../data/$tmp_log_file";
                      exec($command,$output_array,$return_code);
		      $ret_val = disconnect();
		      exit;
		    }#end error in call to compoundTableQuery
                  if (sizeof($age_group_phenotype_percentile_link_rec_list) == '0')
                    {
                      $date = date('Y-m-d H:i:s');
                      echo "There are no Phenotype Percentiles for Phenotype $phenotype_name associated with the Age Group \n";
                      echo "$min_age - $max_age years old for Nationality $nationality in the database.<br>\n";
                      echo "Therefore nothing will be changed for individuals in this Age Group for this Nationality.<br>\n";
                      echo "This message was generated at $date.<br><br>\n";
                      continue;
                    }#end if no percentiles for this age group
                 if (sizeof($age_group_phenotype_percentile_link_rec_list) == '1')
		   {$age_group_phenotype_percentile_link_rec = $age_group_phenotype_percentile_link_rec_list[0];}
                 else #error in data integrity
                   {
                     $date = date('Y-m-d H:i:s');
                     echo "Error #49: $date<br>\n";
                     echo "!!!Error in Database Data Integrity!!!!<br>\n";
                     echo "Stop using the data in the database until Database Administrator tells you all data is okay.<br>\n";
                     echo "User wanted to determine which individuals were affected for Affection Status $affection_status_name \n";
                     echo "based upon values for Quantitative Phenotype $phenotype_name.<br>\n";
                     echo "But when query the Age Group-Phenotype Percentile Link Table with age_group_id $age_group_rec[age_group_id] \n";
                     echo "and phenotype_id $phenotype_id  in database id $database_id multiple records are 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 "</body></html>\n";
                     fclose($log_file_handle);
                     chmod("../data/$tmp_log_file",0777);
                     $command = "rm ../data/$tmp_log_file";
                     exec($command,$output_array,$return_code);
	             $ret_val = disconnect();
	             exit;
                   }#end else error in data integrity

		 $field_list_array = array('phenotype_id','phenotype_value','phenotype_value','individual_table_id');
		 $term_array       = array($age_phenotype_id,$query_min_age,$query_max_age,$nat_indiv_table_id_term);
		 $mode_array       = array('equal','greater_than','less_than','in');
		 $age_indiv_table_id_array = distinctCompoundTableQuery('individual_table_id','quantitative_phenotype_value_table',$database_id,$field_list_array,$term_array,$mode_array,'and','all');
		 if (!is_array($age_indiv_table_id_array))
                   {
                     $date = date('Y-m-d H:i:s');
		     echo "Error #50:$date<br>\n";
		     echo "Error in call distinctCompoundTableQuery from define_affected_individuals.php<br>\n";
		     echo "$age_indiv_table_id_array<br><br>\n";
		     echo "Please email the above the error using the Comments/Help/Support link on the \n";
		     echo "left navigation bar.\n";
		     echo "</body></html>\n";
                     fclose($log_file_handle);
                     chmod("../data/$tmp_log_file",0777);
                     $command = "rm ../data/$tmp_log_file";
                     exec($command,$output_array,$return_code);
		     $ret_val = disconnect();
		     exit;
		   }#end error in call to distinctCompoundTableQuery
		 if (sizeof($age_indiv_table_id_array) == '0') {continue;}
                 $age_indiv_table_id_term = join($age_indiv_table_id_array,',');

		 $field_list_array = array('individual_table_id');
		 $term_array       = array($age_indiv_table_id_term);
		 $mode_array       = array('in');
                 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";}
		 $individual_rec_list = compoundTableQuery('individual_table',$database_id,$field_list_array,$term_array,$mode_array,'and','all',$order_by);
		 if (!is_array($individual_rec_list))
                   {
                     $date = date('Y-m-d H:i:s');
		     echo "Error #51: $date<br>\n";
		     echo "Error in call compoundTableQuery from define_affected_individuals.php<br>\n";
		     echo "$individual_rec_list<br><br>\n";
		     echo "Please email the above the error using the Comments/Help/Support link on the \n";
		     echo "left navigation bar.\n";
		     echo "</body></html>\n";
                     fclose($log_file_handle);
                     chmod("../data/$tmp_log_file",0777);
                     $command = "rm ../data/$tmp_log_file";
                     exec($command,$output_array,$return_code);
		     $ret_val = disconnect();
		     exit;
		   }#end error in call to compoundTableQuery

		 foreach ($individual_rec_list as $individual_rec)
                   {
		    $field_list_array = array('individual_table_id','phenotype_id');
		    $term_array       = array($individual_rec['individual_table_id'],$phenotype_id);
		    $mode_array       = array('equal','equal');
		    $phenotype_value_rec_list = compoundTableQuery('quantitative_phenotype_value_table',$database_id,$field_list_array,$term_array,$mode_array,'and','all');
		    if (!is_array($phenotype_value_rec_list))
                      {
                        $date = date('Y-m-d H:i:s');
		        echo "Error #52:$date<br>\n";
		        echo "Error in call to compoundTableQuery from define_affected_individiduals.php<br>\n";
		        echo "$phenotype_value_rec_list<br><br>\n";
		        echo "Please the above error using the Comments/Help/Support link on the \n";
		        echo "left navigation bar.\n";
		        echo "</body></html>\n";
                        fclose($log_file_handle);
                        chmod("../data/$tmp_log_file",0777);
                        $command = "rm ../data/$tmp_log_file";
                        exec($command,$output_array,$return_code);
		        $ret_val = disconnect();
		        exit;
		      }#end error in call to compoundTableQuery
                    if (sizeof($phenotype_value_rec_list) == '0'){continue;}
                    elseif (sizeof($phenotype_value_rec_list) == '1')
                      {
		        $phenotype_value_rec = $phenotype_value_rec_list[0];
		        $phenotype_value     = $phenotype_value_rec['phenotype_value'];
                      }#end if have phenotype value
                    else #error in data integrity
                      {
                        $date = date('Y-m-d H:i:s');
                        echo "Error #53: $date<br>\n";
                        echo "!!!Error in Database Data Integrity!!!!<br>\n";
                        echo "Stop using the data in the database until Database Administrator tells you all data is okay.<br>\n";
                        echo "User wanted to determine which individuals were affected for Affection Status $affection_status_name \n";
                        echo "based upon values for Quantitative Phenotype $phenotype_name in database id $database_id.<br>\n";
                        echo "But when query the Quantitative Phenotype Value table for \n";
                        echo "Individual $individual_rec[individual_id] in Pedigree $individual_rec[pedigree_id] \n";
                        echo "for that phenotype multiple 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 "</body></html>\n";
                        fclose($log_file_handle);
                        chmod("../data/$tmp_log_file",0777);
                        $command = "rm ../data/$tmp_log_file";
                        exec($command,$output_array,$return_code);
	                $ret_val = disconnect();
	                exit;
                      }#end else error in data integrity

		    if ($affection_value == '10th_percentile')
                      {
		       if ($individual_rec['sex'] == '1')
                         {
		          if ($comp_mode == 'greater_than')
                            {
			     if ($phenotype_value > $age_group_phenotype_percentile_link_rec['age_group_phenotype_male_10th_percentile_value'])
                               {
				$insert_array = array($individual_rec['individual_table_id'],$individual_rec['individual_id'],$individual_rec['pedigree_id']);
				$affected_individual_array[] = $insert_array;
                                $affected_individual_table_id_array[] = $individual_rec['individual_table_id'];
                                if (!in_array($individual_rec['pedigree_table_id'],$altered_pedigree_array)) {$altered_pedigree_array[] = $individual_rec['pedigree_table_id'];}
		               }#end if phenotype_value > affection_value
		            }#end if comp_mode == 'greater_than'

                          if ($comp_mode == 'greater_than_equal')
                            {
			     if ($phenotype_value >= $age_group_phenotype_percentile_link_rec['age_group_phenotype_male_10th_percentile_value'])
                               {
				$insert_array = array($individual_rec['individual_table_id'],$individual_rec['individual_id'],$individual_rec['pedigree_id']);
				$affected_individual_array[] = $insert_array;
                                $affected_individual_table_id_array[] = $individual_rec['individual_table_id'];
                                if (!in_array($individual_rec['pedigree_table_id'],$altered_pedigree_array)) {$altered_pedigree_array[] = $individual_rec['pedigree_table_id'];}
		               }#end if phenotype_value >= affection_value
		            }#end if comp_mode == 'greater_than_equal'

			  if ($comp_mode == 'less_than')
                            {
			     if ($phenotype_value < $age_group_phenotype_percentile_link_rec['age_group_phenotype_male_10th_percentile_value'])
                               {
				$insert_array = array($individual_rec['individual_table_id'],$individual_rec['individual_id'],$individual_rec['pedigree_id']);
				$affected_individual_array[] = $insert_array;
                                $affected_individual_table_id_array[] = $individual_rec['individual_table_id'];
                                if (!in_array($individual_rec['pedigree_table_id'],$altered_pedigree_array)) {$altered_pedigree_array[] = $individual_rec['pedigree_table_id'];}
		               }#end if phenotype_value < affection_value
		            }#end if comp_mode == 'less_than'

                          if ($comp_mode == 'less_than_equal')
                            {
			     if ($phenotype_value <= $age_group_phenotype_percentile_link_rec['age_group_phenotype_male_10th_percentile_value'])
                               {
				$insert_array = array($individual_rec['individual_table_id'],$individual_rec['individual_id'],$individual_rec['pedigree_id']);
				$affected_individual_array[] = $insert_array;
                                $affected_individual_table_id_array[] = $individual_rec['individual_table_id'];
                                if (!in_array($individual_rec['pedigree_table_id'],$altered_pedigree_array)) {$altered_pedigree_array[] = $individual_rec['pedigree_table_id'];}
		               }#end if phenotype_value <= affection_value
		            }#end if comp_mode == 'less_than_equal'

			  if ($comp_mode == 'equal')
                            {
			     if ($phenotype_value == $age_group_phenotype_percentile_link_rec['age_group_phenotype_male_10th_percentile_value'])
                               {
				$insert_array = array($individual_rec['individual_table_id'],$individual_rec['individual_id'],$individual_rec['pedigree_id']);
				$affected_individual_array[] = $insert_array;
                                $affected_individual_table_id_array[] = $individual_rec['individual_table_id'];
                                if (!in_array($individual_rec['pedigree_table_id'],$altered_pedigree_array)) {$altered_pedigree_array[] = $individual_rec['pedigree_table_id'];}
			       }#end if phenotype_value == affection_value
		            }#end if comp_mode == 'equal'
		         }#end if individual is a male

		       if ($individual_rec['sex'] == '2')
                         {
			  if ($comp_mode == 'greater_than')
                            {
			     if ($phenotype_value > $age_group_phenotype_percentile_link_rec['age_group_phenotype_female_10th_percentile_value'])
                               {
				$insert_array = array($individual_rec['individual_table_id'],$individual_rec['individual_id'],$individual_rec['pedigree_id']);
				$affected_individual_array[] = $insert_array;
                                $affected_individual_table_id_array[] = $individual_rec['individual_table_id'];
                                if (!in_array($individual_rec['pedigree_table_id'],$altered_pedigree_array)) {$altered_pedigree_array[] = $individual_rec['pedigree_table_id'];}
		               }#end if phenotype_value > affection_value
		            }#end if comp_mode == 'greater_than'

                          if ($comp_mode == 'greater_than_equal')
                            {
			     if ($phenotype_value >= $age_group_phenotype_percentile_link_rec['age_group_phenotype_female_10th_percentile_value'])
                               {
				$insert_array = array($individual_rec['individual_table_id'],$individual_rec['individual_id'],$individual_rec['pedigree_id']);
				$affected_individual_array[] = $insert_array;
                                $affected_individual_table_id_array[] = $individual_rec['individual_table_id'];
                                if (!in_array($individual_rec['pedigree_table_id'],$altered_pedigree_array)) {$altered_pedigree_array[] = $individual_rec['pedigree_table_id'];}
		               }#end if phenotype_value >= affection_value
		            }#end if comp_mode == 'greater_than_equal'

			  if ($comp_mode == 'less_than')
                            {
			     if ($phenotype_value < $age_group_phenotype_percentile_link_rec['age_group_phenotype_female_10th_percentile_value'])
                               {
				$insert_array = array($individual_rec['individual_table_id'],$individual_rec['individual_id'],$individual_rec['pedigree_id']);
				$affected_individual_array[] = $insert_array;
                                $affected_individual_table_id_array[] = $individual_rec['individual_table_id'];
                                if (!in_array($individual_rec['pedigree_table_id'],$altered_pedigree_array)) {$altered_pedigree_array[] = $individual_rec['pedigree_table_id'];}
		               }#end if phenotype_value < affection_value
		            }#end if comp_mode == 'less_than'

                          if ($comp_mode == 'less_than_equal')
                            {
			     if ($phenotype_value <= $age_group_phenotype_percentile_link_rec['age_group_phenotype_female_10th_percentile_value'])
                               {
				$insert_array = array($individual_rec['individual_table_id'],$individual_rec['individual_id'],$individual_rec['pedigree_id']);
				$affected_individual_array[] = $insert_array;
                                $affected_individual_table_id_array[] = $individual_rec['individual_table_id'];
                                if (!in_array($individual_rec['pedigree_table_id'],$altered_pedigree_array)) {$altered_pedigree_array[] = $individual_rec['pedigree_table_id'];}
		               }#end if phenotype_value <= affection_value
		            }#end if comp_mode == 'less_than_equal'

			  if ($comp_mode == 'equal')
                            {
			     if ($phenotype_value == $age_group_phenotype_percentile_link_rec['age_group_phenotype_female_10th_percentile_value'])
                               {
				$insert_array = array($individual_rec['individual_table_id'],$individual_rec['individual_id'],$individual_rec['pedigree_id']);
				$affected_individual_array[] = $insert_array;
                                $affected_individual_table_id_array[] = $individual_rec['individual_table_id'];
                                if (!in_array($individual_rec['pedigree_table_id'],$altered_pedigree_array)) {$altered_pedigree_array[] = $individual_rec['pedigree_table_id'];}
			       }#end if phenotype_value == affection_value
		            }#end if comp_mode == 'equal'
		         }#end if individual is a female
		      }#end if (affection_value == '10th_percentile')

		    if ($affection_value == '90th_percentile')
                      {
		       if ($individual_rec['sex'] == '1')
                         {
		          if ($comp_mode == 'greater_than')
                            {
			     if ($phenotype_value > $age_group_phenotype_percentile_link_rec['age_group_phenotype_male_90th_percentile_value'])
                               {
				$insert_array = array($individual_rec['individual_table_id'],$individual_rec['individual_id'],$individual_rec['pedigree_id']);
				$affected_individual_array[] = $insert_array;
                                $affected_individual_table_id_array[] = $individual_rec['individual_table_id'];
                                if (!in_array($individual_rec['pedigree_table_id'],$altered_pedigree_array)) {$altered_pedigree_array[] = $individual_rec['pedigree_table_id'];}
		               }#end if phenotype_value > affection_value
		            }#end if comp_mode == 'greater_than'

                          if ($comp_mode == 'greater_than_equal')
                            {
			     if ($phenotype_value >= $age_group_phenotype_percentile_link_rec['age_group_phenotype_male_90th_percentile_value'])
                               {
				$insert_array = array($individual_rec['individual_table_id'],$individual_rec['individual_id'],$individual_rec['pedigree_id']);
				$affected_individual_array[] = $insert_array;
                                $affected_individual_table_id_array[] = $individual_rec['individual_table_id'];
                                if (!in_array($individual_rec['pedigree_table_id'],$altered_pedigree_array)) {$altered_pedigree_array[] = $individual_rec['pedigree_table_id'];}
		               }#end if phenotype_value >= affection_value
		            }#end if comp_mode == 'greater_than_equal'

			  if ($comp_mode == 'less_than')
                            {
			     if ($phenotype_value < $age_group_phenotype_percentile_link_rec['age_group_phenotype_male_90th_percentile_value'])
                               {
				$insert_array = array($individual_rec['individual_id'],$individual_rec['individual_id'],$individual_rec['pedigree_id']);
				$affected_individual_array[] = $insert_array;
                                $affected_individual_table_id_array[] = $individual_rec['individual_table_id'];
                                if (!in_array($individual_rec['pedigree_table_id'],$altered_pedigree_array)) {$altered_pedigree_array[] = $individual_rec['pedigree_table_id'];}
		               }#end if phenotype_value < affection_value
		            }#end if comp_mode == 'less_than'

                          if ($comp_mode == 'less_than_equal')
                            {
			     if ($phenotype_value <= $age_group_phenotype_percentile_link_rec['age_group_phenotype_male_90th_percentile_value'])
                               {
				$insert_array = array($individual_rec['individual_id'],$individual_rec['individual_id'],$individual_rec['pedigree_id']);
				$affected_individual_array[] = $insert_array;
                                $affected_individual_table_id_array[] = $individual_rec['individual_table_id'];
                                if (!in_array($individual_rec['pedigree_table_id'],$altered_pedigree_array)) {$altered_pedigree_array[] = $individual_rec['pedigree_table_id'];}
		               }#end if phenotype_value <= affection_value
		            }#end if comp_mode == 'less_than_equal'

			  if ($comp_mode == 'equal')
                            {
			     if ($phenotype_value == $age_group_phenotype_percentile_link_rec['age_group_phenotype_male_90th_percentile_value'])
                               {
				$insert_array = array($individual_rec['individual_table_id'],$individual_rec['individual_id'],$individual_rec['pedigree_id']);
				$affected_individual_array[] = $insert_array;
                                $affected_individual_table_id_array[] = $individual_rec['individual_table_id'];
                                if (!in_array($individual_rec['pedigree_table_id'],$altered_pedigree_array)) {$altered_pedigree_array[] = $individual_rec['pedigree_table_id'];}
			       }#end if phenotype_value == affection_value
		            }#end if comp_mode == 'equal'
		         }#end if individual is a male

		       if ($individual_rec['sex'] == '2')
                         {
			  if ($comp_mode == 'greater_than')
                            {
			     if ($phenotype_value > $age_group_phenotype_percentile_link_rec['age_group_phenotype_female_90th_percentile_value'])
                               {
				$insert_array = array($individual_rec['individual_table_id'],$individual_rec['individual_id'],$individual_rec['pedigree_id']);
				$affected_individual_array[] = $insert_array;
                                $affected_individual_table_id_array[] = $individual_rec['individual_table_id'];
                                if (!in_array($individual_rec['pedigree_table_id'],$altered_pedigree_array)) {$altered_pedigree_array[] = $individual_rec['pedigree_table_id'];}
		               }#end if phenotype_value > affection_value
		            }#end if comp_mode == 'greater_than'

                          if ($comp_mode == 'greater_than_equal')
                            {
			     if ($phenotype_value >= $age_group_phenotype_percentile_link_rec['age_group_phenotype_female_90th_percentile_value'])
                               {
				$insert_array = array($individual_rec['individual_table_id'],$individual_rec['individual_id'],$individual_rec['pedigree_id']);
				$affected_individual_array[] = $insert_array;
                                $affected_individual_table_id_array[] = $individual_rec['individual_table_id'];
                                if (!in_array($individual_rec['pedigree_table_id'],$altered_pedigree_array)) {$altered_pedigree_array[] = $individual_rec['pedigree_table_id'];}
		               }#end if phenotype_value >= affection_value
		            }#end if comp_mode == 'greater_than_equal'

			  if ($comp_mode == 'less_than')
                            {
			     if ($phenotype_value < $age_group_phenotype_percentile_link_rec['age_group_phenotype_female_90th_percentile_value'])
                               {
				$insert_array = array($individual_rec['individual_table_id'],$individual_rec['individual_id'],$individual_rec['pedigree_id']);
				$affected_individual_array[] = $insert_array;
                                $affected_individual_table_id_array[] = $individual_rec['individual_table_id'];
                                if (!in_array($individual_rec['pedigree_table_id'],$altered_pedigree_array)) {$altered_pedigree_array[] = $individual_rec['pedigree_table_id'];}
		               }#end if phenotype_value < affection_value
		            }#end if comp_mode == 'less_than'

                          if ($comp_mode == 'less_than_equal')
                            {
			     if ($phenotype_value <= $age_group_phenotype_percentile_link_rec['age_group_phenotype_female_90th_percentile_value'])
                               {
				$insert_array = array($individual_rec['individual_table_id'],$individual_rec['individual_id'],$individual_rec['pedigree_id']);
				$affected_individual_array[] = $insert_array;
                                $affected_individual_table_id_array[] = $individual_rec['individual_table_id'];
                                if (!in_array($individual_rec['pedigree_table_id'],$altered_pedigree_array)) {$altered_pedigree_array[] = $individual_rec['pedigree_table_id'];}
		               }#end if phenotype_value <= affection_value
		            }#end if comp_mode == 'less_than_equal'

			  if ($comp_mode == 'equal')
                            {
			     if ($phenotype_value == $age_group_phenotype_percentile_link_rec['age_group_phenotype_female_90th_percentile_value'])
                               {
				$insert_array = array($individual_rec['individual_table_id'],$individual_rec['individual_id'],$individual_rec['pedigree_id']);
				$affected_individual_array[] = $insert_array;
                                $affected_individual_table_id_array[] = $individual_rec['individual_table_id'];
                                if (!in_array($individual_rec['pedigree_table_id'],$altered_pedigree_array)) {$altered_pedigree_array[] = $individual_rec['pedigree_table_id'];}
			       }#end if phenotype_value == affection_value
		            }#end if comp_mode == 'equal'
		         }#end if individual is a female
		      }#end if (affection_value == '90th_percentile')
		   }#end foreach individual
	        }#end foreach age_group

	      if (sizeof($affected_individual_array) > '0')
                {
                  echo "The Below individuals were determined to be affected for affection status $affection_status_name based upon being "; 
	          if ($comp_mode == 'greater_than')       {echo "Strictly Greater Than ";}
                  if ($comp_mode == 'greater_than_equal') {echo "Greater Than or Equal To";}
	          if ($comp_mode == 'less_than')          {echo "Strictly Less Than ";}
                  if ($comp_mode == 'less_than_equal')    {echo "Less Than or Equal To";}
	          if ($comp_mode == 'equal')              {echo "Strictly Equal To ";}
	          echo "value $affection_value for phenotype $phenotype_name.<br>\n";

	          foreach ($affected_individual_array as $individual_info_array)
                    {
                      $field_list_array = array('individual_table_id','affection_status_id');
                      $term_array       = array($individual_info_array[0],$status);
                      $mode_array       = array('equal','equal');
                      $indiv_affect_link_rec_list = compoundTableQuery('individual_affection_status_link_table',$database_id,$field_list_array,$term_array,$mode_array,'and','all');
                      if (!is_array($indiv_affect_link_rec_list))
                        {
                          $date = date('Y-m-d H:i:s');
                          echo "Error #54: $date<br>\n";
                          echo "Error in call to compoundTableQuery from define_affected_individuals.php<br>\n";
                          echo "$indiv_affect_link_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 "</body></html>\n";
                          fclose($log_file_handle);
                          chmod("../data/$tmp_log_file",0777);
                          $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                          exec($command,$output_array,$return_code);
                          $command = "rm ../data/$tmp_log_file";
                          exec($command,$output_array,$return_code);
		          $ret_val = disconnect();
		          exit;
                        }#end error in call to compoundTableQuery
                      if (sizeof($indiv_affect_link_rec_list) == '0')
                        {
                          $ret_val = insertIndividualAffectionStatusLink($database_id,$status,$individual_info_array[0],'2');
                          if ($ret_val != '1')
                            {
                              $date = date('Y-m-d H:i:s');
                              echo "Error #55: $date<br>\n";
                              echo "Error in call to insertIndividualAffectionStatusLink from define_affected_individuals.php<br>\n";
                              echo "$ret_val<br><br>\n";
                              echo "Please email the above error using the Comments/Help/Support link on the \n";
		              echo "left navigation bar.\n";
		              echo "</body></html>\n";
                              fclose($log_file_handle);
                              chmod("../data/$tmp_log_file",0777);
                              $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                              exec($command,$output_array,$return_code);
                              $command = "rm ../data/$tmp_log_file";
                              exec($command,$output_array,$return_code);
		              $ret_val = disconnect();
		              exit;
                            }#end error in call to insertIndividualAffectionStatusLink
                          echo "Individual $individual_info_array[1] in Pedigree $individual_info_array[2] was determined to be affected.<br>\n";
                          $date = date('Y-m-d H:i:s');
                          $log_file_statement = "$date:\t$_SESSION[user_name] Inserted new Individual-Affection Status Link into ";
                          $log_file_statement.= "Database ID $database_id for Affection Status ID $status and Individual Table ID ";
                          $log_file_statement.= "$individual_infor_array[0], set Individual Affection Status to 2.\n";
                          fwrite($log_file_handle,$log_file_statement);
                        }#end if individual affection status link doesn't exist
		      elseif (sizeof($indiv_affect_link_rec_list) == '1')
                        {
                          $indiv_affect_link_rec = $indiv_affect_link_rec_list[0];
                          $indiv_affect_link_id  = $indiv_affect_link_rec['individual_affection_status_link_id'];

	                  $update_field_array = array('individual_affection_status');
		          $update_value_array = array('2');
		          $ret_val = updateRecord('individual_affection_status_link_table',$indiv_affect_link_id,$update_field_array,$update_value_array);
		          if ($ret_val != '1')
                            {
                              $date = date('Y-m-d H:i:s');
		              echo "Error #56: $date<br>\n";
		              echo "Error in call to updateRecord from define_affected_individuals.php<br>\n";
		              echo "$ret_val<br><br>\n";
		              echo "Please email the above error using the Comments/Help/Support link on the \n";
		              echo "left navigation bar.\n";
		              echo "</body></html>\n";
                              fclose($log_file_handle);
                              chmod("../data/$tmp_log_file",0777);
                              $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                              exec($command,$output_array,$return_code);
                              $command = "rm ../data/$tmp_log_file";
                              exec($command,$output_array,$return_code);
		              $ret_val = disconnect();
		              exit;
		            }#end error in call to updateRecord
		          echo "Individual $individual_info_array[1] in Pedigree $individual_info_array[2] was determined to be affected.<br>\n";
                          $date = date('Y-m-d H:i:s');
                          $log_file_statement = "$date:\t$_SESSION[user_name] Updated Individual-Affection Status ID $indiv_affect_link_id in ";
                          $log_file_statement.= "Database ID $database_id, set Individual Affection Status to 2.\n";
                          fwrite($log_file_handle,$log_file_statement);
                        }#end if individual affection status link does exist
		      else
                        {
                          $date = date('Y-m-d H:i:s');
                          echo "Error #57:$date<br>\n";
                          echo "!!!Error in Database Data Integrity!!!!<br>\n";
                          echo "Stop using the data in the database until Database Administrator tells you all data is okay.<br>\n";
                          echo "When queried the Individual Affection Status Link Table with affection_status_id $status and \n";
                          echo "individual_table_id $individual_info_array[0] in database id $database_id there was more than one record returned!<br>\n";
                          echo "The individual_affection_status_link_id's returned were:<br>\n";
                          foreach ($indiv_affect_link_rec_list as $indiv_affect_link_rec) {echo "$indiv_affect_link_rec[individual_affection_status_link_id]<br>\n";}
                          echo "<br>\n";
                          echo "Please email the above error using the Comments/Help/Support link on the \n";
		          echo "left navigation bar.\n";
		          echo "</body></html>\n";
                          fclose($log_file_handle);
                          chmod("../data/$tmp_log_file",0777);
                          $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                          exec($command,$output_array,$return_code);
                          $command = "rm ../data/$tmp_log_file";
                          exec($command,$output_array,$return_code);
		          $ret_val = disconnect();
		          exit;
                        }#end else there is an error in the data in the database
	            }#end foreach affected_individual

                  foreach ($individual_rec_list as $individual_rec) #determine which individuals were not affected and set status to 0
                    {
                      if (!in_array($individual_rec['individual_table_id'],$affected_individual_table_id_array))
                        {
                          $field_list_array = array('affection_status_id','individual_table_id');
                          $term_array       = array($status,$individual_rec['individual_table_id']);
                          $mode_array       = array('equal','equal');
                          $indiv_affect_link_rec_list = compoundTableQuery('individual_affection_status_link_table',$database_id,$field_list_array,$term_array,$mode_array,'and','all');
                          if (!is_array($indiv_affect_link_rec_list))
                            {
                              $date = date('Y-m-d H:i:s');
                              echo "Error #58: $date<br>\n";
                              echo "Error in call to compoundTableQuery from define_affected_individuals.php<br>\n";
                              echo "$indiv_affect_link_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 "</body></html>\n";
                              fclose($log_file_handle);
                              chmod("../data/$tmp_log_file",0777);
                              $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                              exec($command,$output_array,$return_code);
                              $command = "rm ../data/$tmp_log_file";
                              exec($command,$output_array,$return_code);
		              $ret_val = disconnect();
		              exit;
                            }#end error in call to compoundTableQuery
                          if (sizeof($indiv_affect_link_rec_list) == '0')
                            {
                              $ret_val = insertIndividualAffectionStatusLink($database_id,$status,$individual_rec['individual_table_id'],'0');
                              if ($ret_val != '1')
                                {
                                  $date = date('Y-m-d H:i:s');
                                  echo "Error #59: $date<br>\n";
                                  echo "Error in call to insertIndividualAffectionStatusLink from define_affected_individuals.php<br>\n";
                                  echo "$ret_val<br><br>\n";
                                  echo "Please email the above error using the Comments/Help/Support link on the \n";
		                  echo "left navigation bar.\n";
		                  echo "</body></html>\n";
                                  fclose($log_file_handle);
                                  chmod("../data/$tmp_log_file",0777);
                                  $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                                  exec($command,$output_array,$return_code);
                                  $command = "rm ../data/$tmp_log_file";
                                  exec($command,$output_array,$return_code);
		                  $ret_val = disconnect();
		                  exit;
                                }#end error in call to insertIndividualAffectionStatusLink
                              $date = date('Y-m-d H:i:s');
                              $log_file_statement = "$date:\t$_SESSION[user_name] Inserted a new Individual-Affection Status Link into ";
                              $log_file_statement.= "Database ID $database_id for Affection Status ID $status and Individual Table ID ";
                              $log_file_statement.= "$individual_rec[individual_table_id], set Individual Affection Status to 0.\n";
                              fwrite($log_file_handle,$log_file_statement);
                            }#end if individual affection status link does not exist
                          elseif (sizeof($indiv_affect_link_rec_list) == '1')
                            {
                              $indiv_affect_link_rec = $indiv_affect_link_rec_list[0];
                              $indiv_affect_link_id  = $indiv_affect_link_rec['individual_affection_status_link_id'];
                              $update_field_array    = array('individual_affection_status');
                              $update_value_array    = array('0');
                              $ret_val               = updateRecord('individual_affection_status_link_table',$indiv_affect_link_id,$update_field_array,$update_value_array);
                              if ($ret_val != '1')
                                {
                                  $date = date('Y-m-d H:i:s'); 
                                  echo "Error #60: $date<br>\n";
                                  echo "Error in call to updateRecord from define_affected_individuals.php<br>\n";
                                  echo "$ret_val<br><br>\n";
                                  echo "Please email the above error using the Comments/Help/Support link on the \n";
		                  echo "left navigation bar.\n";
		                  echo "</body></html>\n";
                                  fclose($log_file_handle);
                                  chmod("../data/$tmp_log_file",0777);
                                  $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                                  exec($command,$output_array,$return_code);
                                  $command = "rm ../data/$tmp_log_file";
                                  exec($command,$output_array,$return_code);
		                  $ret_val = disconnect();
		                  exit;
                                }#end error in call to updateRecord
                              $date = date('Y-m-d H:i:s');
                              $log_file_statement = "$date:\t$_SESSION[user_name] Updated Individual-Affection Status Link ID $indiv_affect_link_id ";
                              $log_file_statement.= "in Database ID $database_id, set Individual Affection Status to 0.\n";
                              fwrite($log_file_handle,$log_file_statement);
                            }#end if have link
                          else #error in the data in the database
                            {
                              $date = date('Y-m-d H:i:s');
                              echo "Error #61: $date<br>\n";
                              echo "!!!Error in Database Data Integrity!!!!<br>\n";
                              echo "Stop using the data in the database until Database Administrator tells you all data is okay.<br>\n";
                              echo "When queried the Individual Affection Status Link Table with affection_status_id $status and \n";
                              echo "individual_table_id $individual_info_array[0] in database_id $database_id \n";
                              echo "there was more than one record returned!<br>\n";
                              echo "The individual_affection_status_link_id's returned were:<br>\n";
                              foreach ($indiv_affect_link_rec_list as $indiv_affect_link_rec) {echo "$indiv_affect_link_rec[individual_affection_status_link_id]<br>\n";}
                              echo "<br>\n";
                              echo "Please email the above error using the Comments/Help/Support link on the \n";
		              echo "left navigation bar.\n";
		              echo "</body></html>\n";
                              fclose($log_file_handle);
                              chmod("../data/$tmp_log_file",0777);
                              $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                              exec($command,$output_array,$return_code);
                              $command = "rm ../data/$tmp_log_file";
                              exec($command,$output_array,$return_code);
		              $ret_val = disconnect();
		              exit;
                            }#end else there is an error in the data in the database
                        }#end if individual was not determined to be affected
                      else {continue;}#individual was determined to be affected
                    }#end determine which individuals were not determined to be affected and set affection status to 0

                  foreach ($altered_pedigree_array as $altered_pedigree)
                    {
                      $field_list_array = array('pedigree_table_id');
                      $rec_list = distinctTableQuery('individual_table_id','individual_table',$database_id,$field_list_array,$altered_pedigree,'equal','all');
                      if (!is_array($rec_list))
                        {
                          $date = date('Y-m-d H:i:s');
                          echo "Error #62: $date<br>\n";
                          echo "Error in call to distinctTableQuery from define_affected_individuals.php<br>\n";
                          echo "$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 "</body></html>\n";
                          fclose($log_file_handle);
                          chmod("../data/$tmp_log_file",0777);
                          $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                          exec($command,$output_array,$return_code);
                          $command = "rm ../data/$tmp_log_file";
                          exec($command,$output_array,$return_code);
		          $ret_val = disconnect();
		          exit;
                        }#end error in call to distinctTableQuery
                      $indiv_table_id_list = join($rec_list,',');
                      unset($rec_list);

                      $field_list_array = array('affection_status_id','individual_table_id','individual_affection_status');
                      $term_array       = array($status,$indiv_table_id_list,'2');
                      $mode_array       = array('equal','in','equal');
                      $rec_list         = compoundTableQuery('individual_affection_status_link_table',$database_id,$field_list_array,$term_array,$mode_array,'and','all');
                      if (!is_array($rec_list))
                        {
                          echo "Error 13a:<br>\n";
                          echo "Error in call to compoundTableQuery from define_affected_individuals.php<br>\n";
                          echo "$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 "</body></html>\n";
                          fclose($log_file_handle);
                          chmod("../data/$tmp_log_file",0777);
                          $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                          exec($command,$output_array,$return_code);
                          $command = "rm ../data/$tmp_log_file";
                          exec($command,$output_array,$return_code);
		          $ret_val = disconnect();
		          exit;
                        }
                      $num_affected = sizeof($rec_list);

                      $field_list_array = array('affection_status_id','pedigree_table_id');
                      $term_array       = array($status,$altered_pedigree);
                      $mode_array       = array('equal','equal');
                      $ped_affect_link_rec_list = compoundTableQuery('pedigree_affection_status_link_table',$database_id,$field_list_array,$term_array,$mode_array,'and','all');
                      if (!is_array($ped_affect_link_rec_list))
                        {
                          $date = date('Y-m-d H:i:s');
                          echo "Error #63: $date<br>\n";
                          echo "Error in call to compoundTableQuery from define_affected_individuals.php<br>\n";
                          echo "$ped_affect_link_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 "</body></html>\n";
                          fclose($log_file_handle);
                          chmod("../data/$tmp_log_file",0777);
                          $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                          exec($command,$output_array,$return_code);
                          $command = "rm ../data/$tmp_log_file";
                          exec($command,$output_array,$return_code);
		          $ret_val = disconnect();
		          exit;
                        }
                      if (sizeof($ped_affect_link_rec_list) == '0')
                        {
                          $ret_val = insertPedigreeAffectionStatusLink($database_id,$status,$altered_pedigree,$num_affected);
                          if ($ret_val != '1')
                            {
                              $date = date('Y-m-d H:i:s');
                              echo "Error #64: $date<br>\n";
                              echo "Error in call to insertPedigreeAffectionStatusLink from define_affected_individuals.php<br>\n";
                              echo "$ret_val<br><br>\n";
                              echo "Please email the above error using the Comments/Help/Support link on the \n";
		              echo "left navigation bar.\n";
		              echo "</body></html>\n";
                              fclose($log_file_handle);
                              chmod("../data/$tmp_log_file",0777);
                              $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                              exec($command,$output_array,$return_code);
                              $command = "rm ../data/$tmp_log_file";
                              exec($command,$output_array,$return_code);
		              $ret_val = disconnect();
		              exit;
                            }#end error in call to insertPedigreeAffectionStatusLink
                          $date = date('Y-m-d H:i:s');
                          $log_file_statement = "$date:\t$_SESSION[user_name] Inserted a new Pedigree-Affection Status Link into ";
                          $log_file_statement.= "Database ID $database_id for Affection Status ID $status and Pedigree Table ID ";
                          $log_file_statement.= "$altered_pedigree, set Number Affected in Pedigree to $num_affected.\n";
                          fwrite($log_file_handle,$log_file_statement);
                        }#end if no pedigree affection status link exists
                      elseif ($sizeof($ped_affect_link_rec_list) == '1')
                        {
                          $ped_affect_link_rec = $ped_affect_link_rec_list[0];
                          $ped_affect_link_id  = $ped_affect_link_rec['pedigree_affection_status_link_id'];
                          $update_field = "number_affected_in_pedigree";
                          $update_field_array = array($update_field);
                          $update_value_array = array ($num_affected);
                          $ret_val = updateRecord('pedigree_affection_status_link_table',$ped_affect_link_id,$update_field_array,$update_value_array);
                          if ($ret_val != '1')
                            {
                              $date = date('Y-m-d H:i:s');
                              echo "Error #65: $date<br>\n";
                              echo "Error in call to updateRecord from define_affected_individuals.php<br>\n";
                              echo "$ret_val<br><br>\n";
                              echo "Please the above error using the Comments/Help/Support link on the \n";
		              echo "left navigation bar.\n";
		              echo "</body></html>\n";
                              fclose($log_file_handle);
                              chmod("../data/$tmp_log_file",0777);
                              $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                              exec($command,$output_array,$return_code);
                              $command = "rm ../data/$tmp_log_file";
                              exec($command,$output_array,$return_code);
		              $ret_val = disconnect();
		              exit;
                            }#end error in call to updateRecord
                          $date = date('Y-m-d H:i:s');
                          $log_file_statement = "$date:\t$_SESSION[user_name] Updated Pedigree-Affection Status Link ID $ped_affect_link_id in ";
                          $log_file_statement.= "Database ID $database_id, set Number Affected in Pedigree to $num_affected.\n";
                          fwrite($log_file_handle,$log_file_statement);
			}#end if pedigree affection status link does exist
		      else
                        {
                          $date = date('Y-m-d H:i:s');
                          echo "Error #66: $date<br>\n";
                          echo "!!!Error in Database Data Integrity!!!!<br>\n";
                          echo "Stop using the data in the database until Database Administrator tells you all data is okay.<br>\n";
                          echo "When queried the Pedigree Affection Status Link Table with affection_status_id $status and \n";
                          echo "pedigree_table_id $altered_pedigree in database id $database_id there was more than one record returned!<br>\n";
                          echo "The pedigree_affection_status_link_id's returned were:<br>\n";
                          foreach ($ped_affect_link_rec_list as $ped_affect_link_rec) {echo "$ped_affect_link_rec[pedigree_affection_status_link_id]<br>\n";}
                          echo "<br>\n";
                          echo "Please email the above error using the Comments/Help/Support link on the \n";
		          echo "left navigation bar.\n";
		          echo "</body></html>\n";
                          fclose($log_file_handle);
                          chmod("../data/$tmp_log_file",0777);
                          $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                          exec($command,$output_array,$return_code);
                          $command = "rm ../data/$tmp_log_file";
                          exec($command,$output_array,$return_code);
		          $ret_val = disconnect();
		          exit;
                        }#end else there is an error in the data in the database
                    }#end foreach altered_pedigree
                }#end if have affected individuals
              else #no affected individuals
                {
                  echo "There were no individuals determined to be affected for affection status $affection_status_name by having phenotype values "; 
	          if ($comp_mode == 'greater_than')       {echo "Strictly Greater Than ";}
                  if ($comp_mode == 'greater_than_equal') {echo "Greater Than or Equal To";}
	          if ($comp_mode == 'less_than')          {echo "Strictly Less Than ";}
                  if ($comp_mode == 'less_than_equal')    {echo "Less Than or Equal To";}
	          if ($comp_mode == 'equal')              {echo "Strictly Equal To ";}
	          echo "value $affection_value for phenotype $phenotype_name.<br>\n";

                  foreach ($individual_rec_list as $individual_rec) #set status to 0 for all individuals
                    {
                      $field_list_array = array('affection_status_id','individual_table_id');
                      $term_array       = array($status,$individual_rec['individual_table_id']);
                      $mode_array       = array('equal','equal');
                      $indiv_affect_link_rec_list = compoundTableQuery('individual_affection_status_link_table',$database_id,$field_list_array,$term_array,$mode_array,'and','all');
                      if (!is_array($indiv_affect_link_rec_list))
                        {
                          $date = date('Y-m-d H:i:s');
                          echo "Error #67: $date<br>\n";
                          echo "Error in call to compoundTableQuery from define_affected_individuals.php<br>\n";
                          echo "$indiv_affect_link_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 "</body></html>\n";
                          fclose($log_file_handle);
                          chmod("../data/$tmp_log_file",0777);
                          $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                          exec($command,$output_array,$return_code);
                          $command = "rm ../data/$tmp_log_file";
                          exec($command,$output_array,$return_code);
		          $ret_val = disconnect();
		          exit;
                        }#end error in call to compoundTableQuery
                      if (sizeof($indiv_affect_link_rec_list) == '0')
                        {
                          $ret_val = insertIndividualAffectionStatusLink($database_id,$status,$individual_rec['individual_table_id'],'0');
                          if ($ret_val != '1')
                            {
                              $date = date('Y-m-d H:i:s');
                              echo "Error #68: $date<br>\n";
                              echo "Error in call to insertIndividualAffectionStatusLink from define_affected_individuals.php<br>\n";
                              echo "$ret_val<br><br>\n";
                              echo "Please email the above error using the Comments/Help/Support link on the \n";
		              echo "left navigation bar.\n";
		              echo "</body></html>\n";
                              fclose($log_file_handle);
                              chmod("../data/$tmp_log_file",0777);
                              $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                              exec($command,$output_array,$return_code);
                              $command = "rm ../data/$tmp_log_file";
                              exec($command,$output_array,$return_code);
		              $ret_val = disconnect();
		              exit;
                            }#end error in call to insertIndividualAffectionStatusLink
                          $date = date('Y-m-d H:i:s');
                          $log_file_statement = "$date:\t$_SESSION[user_name] Inserted a new Individual-Affection Status Link into ";
                          $log_file_statement.= "Database ID $database_id for Affection Status ID $status and Individual Table Id ";
                          $log_file_statement.= "$individual_rec[individual_table_id], set Individual Affection Status to 0.\n";
                          fwrite($log_file_handle,$log_file_statement);
                        }#end if individual affection status link does not exist
                      elseif (sizeof($indiv_affect_link_rec_list) == '1')
                        {
                          $indiv_affect_link_rec = $indiv_affect_link_rec_list[0];
                          $indiv_affect_link_id  = $indiv_affect_link_rec['individual_affection_status_link_id'];
                          $update_field_array    = array('individual_affection_status');
                          $update_value_array    = array('0');
                          $ret_val               = updateRecord('individual_affection_status_link_table',$indiv_affect_link_id,$update_field_array,$update_value_array);
                          if ($ret_val != '1')
                            {
                              $date = date('Y-m-d H:i:s');
                              echo "Error #69: $date<br>\n";
                              echo "Error in call to updateRecord from define_affected_individuals.php<br>\n";
                              echo "$ret_val<br><br>\n";
                              echo "Please email the above error using the Comments/Help/Support link on the \n";
		              echo "left navigation bar.\n";
		              echo "</body></html>\n";
                              fclose($log_file_handle);
                              chmod("../data/$tmp_log_file",0777);
                              $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                              exec($command,$output_array,$return_code);
                              $command = "rm ../data/$tmp_log_file";
                              exec($command,$output_array,$return_code);
		              $ret_val = disconnect();
		              exit;
                            }#end error in call to updateRecord
                          $date = date('Y-m-d H:i:s');
                          $log_file_statement = "$date:\t$_SESSION[user_name] Updated Individual-Affection Status Link ID $indiv_affect_link_id in ";
                          $log_file_statement.= "Database ID $database_id, set Individual Affection Status to 0.\n";
                          fwrite($log_file_handle,$log_file_statement);
                        }#end else individual affection status link does exist
                      else #error in the data in the database
                        {
                          $date = date('Y-m-d H:i:s');
                          echo "Error #70: $date<br>\n";
                          echo "!!!Error in Database Data Integrity!!!!<br>\n";
                          echo "Stop using the data in the database until Database Administrator tells you all data is okay.<br>\n";
                          echo "When queried the Individual Affection Status Link Table with affection_status_id $status and \n";
                          echo "individual_table_id $individual_info_array[0] in database id $database_id there was more than one record returned!<br>\n";
                          echo "The individual_affection_status_link_id's returned were:<br>\n";
                          foreach ($indiv_affect_link_rec_list as $indiv_affect_link_rec) {echo "$indiv_affect_link_rec[individual_affection_status_link_id]<br>\n";}
                          echo "<br>\n";
                          echo "Please email the above error using the Comments/Help/Support link on the \n";
		          echo "left navigation bar.\n";
		          echo "</body></html>\n";
                          fclose($log_file_handle);
                          chmod("../data/$tmp_log_file",0777);
                          $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
                          exec($command,$output_array,$return_code);
                          $command = "rm ../data/$tmp_log_file";
                          exec($command,$output_array,$return_code);
		          $ret_val = disconnect();
		          exit;
                        }#end else there is an error in the data in the database
                    }#end set affection status to 0 for all individuals
                }#end else no affected individuals
	     }#end if (type_of_phenotype == 'quantitative')
           fclose($log_file_handle);
           chmod("../data/$tmp_log_file",0777);
           $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
           exec($command,$output_array,$return_code);
           $command = "rm ../data/$tmp_log_file";
           exec($command,$output_array,$return_code);

           echo "Now emailing all the users of the database to inform them of the new information in the database.<br>\n";
           $num_affected = sizeof($affected_individual_array);
           $num_of_individuals = sizeof($individual_rec_list);
           $num_unaffected = $num_of_individuals - $num_affected;

           $database_rec_list = getDatabase($database_id);
           if (!is_array($database_rec_list))
             {
               $date = date('Y-m-d H:i:s');
               echo "Error #71: $date<br>\n";
               echo "Error in call to getDatabase from define_affected_individuals.php<br>\n";
               echo "$database_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 "</body></html>\n";
               $ret_val = disconnect();
               exit;
             }#end error in call to getDatabase
           $database_rec = $database_rec_list[0];

           $user_rec_list = getAllUsers();
           if (!is_array($user_rec_list))
             {
               $date = date('Y-m-d H:i:s');
               echo "Error #72: $date<br>\n";
               echo "Error in call to getAllUsers() from define_affected_individuals.php<br>\n";
               echo "$user_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 "</body></html>\n";
               $ret_val = disconnect();
               exit;
             }#end error in call to getAllUsers
	   foreach ($user_rec_list as $user_rec)
             {
               if ($user_rec['user_name'] == 'root') {$root_email = $user_rec['user_email'];}
             }#end foreach user

           $subject = "New Affection Status Information for Individuals in your Genetic Studies Database";
           $message = "The affection status of individuals from nationality $nationality for affection status $affection_status_name ";
           $message.= " in database $database_rec[database_name] was determined.  ";
           $message.= "Individuals were determined to be affected if their phenotype value for phenotype $phenotype_name ";
           $message.= "was $comp_mode $affection_value.  This resulted in $num_affected individuals being designated affected and ";
           $message.= "$num_unaffected individuals being designated unknown '0'.";
           $header  = "From: $root_email";


           $field_list = array('user_database_link_id');
           $user_database_link_rec_list = tableQuery('user_database_link_table',$database_id,$field_list,'_','wildcard','all');
           if (!is_array($user_database_link_rec_list))
             {
               $date = date('Y-m-d H:i:s');
               echo"Error #73: $date<br>\n";
               echo "Error in call to tableQuery from define_affected_individuals.php<br>\n";
               echo "$user_database_link_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 "</body></html>\n";
               $ret_val = disconnect();
               exit;
             }#end error in call to tableQuery
	   if (sizeof($user_database_link_rec_list) > '0')
             {
               foreach ($user_database_link_rec_list as $user_database_link_rec)
                 {
                   $user_id = $user_database_link_rec['user_id'];
                   $field_list = array('user_id');
                   $user_rec_list = getUser($user_id);
                   if (!is_array($user_rec_list))
                     {
                       $date = date('Y-m-d H:i:s');
                       echo "Error #74: $date<br>\n";
                       echo "Error in call to getUser from define_affected_individuals.php<br>\n";
                       echo "$user_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 "</body></html>\n";
                       $ret_val = disconnect();
                       exit;
                     }#end error in call to getUser
		   if (sizeof($user_rec_list) == '0') {continue;}
                   elseif (sizeof($user_rec_list) == '1') {$user_rec = $user_rec_list[0];}
                   else
                     {
                       $date = date('Y-m-d H:i:s');
                       echo "Error #75: $date<br>\n";
                       echo "!!!Error in Database Data Integrity!!!!<br>\n";
                       echo "Stop using the data in the database until Database Administrator tells you all data is okay.<br>\n";
                       echo "Tried to send an update email to user id $user_id, but when queried the user table \n";
                       echo "with that id multiple 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 "</body></html>\n";
                       $ret_val = disconnect();
                       exit;
                     }#end error in data integrity
		   $user_email_address = $user_rec['user_email'];

                   $ret_val = mail($user_email_address,$subject,$message,$header);
                   #if (!$ret_val) {echo "Unable to send an update email to user $user_rec[user_name]<br>\n";}
                   #else           {echo "Sent an update email to user $user_name<br>\n";}
                 }#end foreach user associated with database
	     }#end if have users associated with database
	   echo "Finished sending update emails to all the users<br>\n";
         }#end if have all the parameters
  }#end if (auth_user == '1')
$ret_val = disconnect();
echo "</body></html>\n";
?>
Return current item: Generic Genetic Studies Database