PageRenderTime 62ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/download_phenotype_class.php

https://github.com/Dave-Matthews/The-Triticeae-Toolbox
PHP | 283 lines | 234 code | 33 blank | 16 comment | 34 complexity | dc2a3570a161e0599e81a842be98d468 MD5 | raw file
  1. <?php
  2. /**
  3. * Download phenotype information for experiment
  4. *
  5. */
  6. namespace T3;
  7. class Downloads
  8. {
  9. public function __construct($function = null)
  10. {
  11. switch ($function) {
  12. case 'downloadMean':
  13. $this->downloadMean();
  14. break;
  15. case 'downloadPlot':
  16. $this->downloadPlot();
  17. break;
  18. default:
  19. echo "Error: bad option\n";
  20. break;
  21. }
  22. }
  23. private function downloadMean()
  24. {
  25. global $mysqli;
  26. $delimiter = ",";
  27. if (isset($_GET['pi'])) {
  28. $experiment_uid = $_GET['pi'];
  29. } else {
  30. echo "Error: experiment uid not set\n";
  31. return;
  32. }
  33. // get all line data for this experiment
  34. $sql="SELECT tht_base_uid, line_record_uid, check_line FROM tht_base WHERE experiment_uid='$experiment_uid'";
  35. $result_thtbase=mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
  36. while ($row_thtbase=mysqli_fetch_array($result_thtbase)) {
  37. $thtbase_uid[] = $row_thtbase['tht_base_uid'];
  38. $linerecord_uid[] = $row_thtbase['line_record_uid'];
  39. $check_line[] = $row_thtbase['check_line'];
  40. }
  41. $num_lines = count($linerecord_uid);
  42. $titles=array('Line Name'); //stores the titles for the display table with units
  43. $titles[]="GRIN Accession";//add CAP Code column to titles
  44. if (empty($thtbase_uid)) {
  45. echo "Error: experiment not found\n";
  46. return;
  47. }
  48. $thtbasestring = implode(",", $thtbase_uid);
  49. $sql1="SELECT DISTINCT p.phenotypes_name as name, p.phenotype_uid as uid, units.unit_name as unit,
  50. units.sigdigits_display as sigdig
  51. FROM phenotype_data as pd, phenotypes as p, units
  52. WHERE p.phenotype_uid = pd.phenotype_uid
  53. AND units.unit_uid = p.unit_uid
  54. AND pd.tht_base_uid IN ($thtbasestring)";
  55. //echo $sql1."<br>";
  56. $result1=mysqli_query($mysqli, $sql1) or die(mysqli_error($mysqli));
  57. $num_phenotypes = mysqli_num_rows($result1);
  58. while ($row1=mysqli_fetch_array($result1)) {
  59. $phenotype_data_name[]=$row1['name'];
  60. $phenotype_uid[]=$row1['uid'];
  61. $unit_sigdigits[]=$row1['sigdig'];
  62. $unit_name[]=$row1['unit'];
  63. $titles[]=ucwords($row1['name'])." (".strtolower($row1['unit']).")";
  64. }
  65. $titles[]="Check"; //add the check column to the display table
  66. $all_rows=array(); //2D array that will hold the values in table format to be displayed
  67. $all_rows_long=array(); // For the full unrounded values
  68. $single_row=array(); //1D array which will hold each row values in the table format to be displayed
  69. $single_row_long=array();
  70. $stringData = implode($delimiter, $titles);
  71. $stringData .= "\n";
  72. header('Content-type: application/vnd.ms-excel');
  73. header('Content-Disposition: attachment;filename="trial_data.csv"');
  74. echo "$stringData";
  75. //---------------------------------------------------------------------------------------------------------------
  76. //Go through lines to create a data table for display
  77. for ($lr_i=0; $lr_i<$num_lines; $lr_i++) {
  78. $thtbaseuid=$thtbase_uid[$lr_i];
  79. $linerecorduid=$linerecord_uid[$lr_i];
  80. //echo $linerecorduid." ".$thtbaseuid."<br>";
  81. $sql_lnruid="SELECT line_record_name FROM line_records WHERE line_record_uid='$linerecorduid'";
  82. $result_lnruid=mysqli_query($mysqli, $sql_lnruid) or die(mysqli_error($mysqli));
  83. $row_lnruid=mysqli_fetch_assoc($result_lnruid);
  84. $lnrname=$row_lnruid['line_record_name'];
  85. $single_row[0]=$lnrname;
  86. $single_row_long[0]=$lnrname;
  87. $sql_gr="select barley_ref_number
  88. from barley_pedigree_catalog bc, barley_pedigree_catalog_ref bcr
  89. where barley_pedigree_catalog_name = 'GRIN'
  90. and bc.barley_pedigree_catalog_uid = bcr.barley_pedigree_catalog_uid
  91. and bcr.line_record_uid = '$linerecorduid'";
  92. $result_gr=mysqli_query($mysqli, $sql_gr) or die(mysqli_error($mysqli));
  93. $row_gr=mysqli_fetch_assoc($result_gr);
  94. $single_row[1]=$row_gr['barley_ref_number'];
  95. $single_row_long[1]=$row_gr['barley_ref_number'];
  96. for ($i=0; $i<$num_phenotypes; $i++) {
  97. $puid=$phenotype_uid[$i];
  98. $sigdig=$unit_sigdigits[$i];
  99. $sql_val="SELECT value FROM phenotype_data
  100. WHERE tht_base_uid='$thtbaseuid'
  101. AND phenotype_uid = '$puid'";
  102. //echo $sql_val."<br>";
  103. $result_val=mysqli_query($mysqli, $sql_val);
  104. if (mysqli_num_rows($result_val) > 0) {
  105. $row_val=mysqli_fetch_assoc($result_val);
  106. $val=$row_val['value'];
  107. $val_long=$val;
  108. if ($sigdig >= 0) {
  109. $val = floatval($val);
  110. $val=number_format($val, $sigdig);
  111. }
  112. } else {
  113. $val = "--";
  114. $val_long = "--";
  115. }
  116. if (empty($val)) {
  117. $val = "--";
  118. $val_long = "--";
  119. }
  120. $single_row[$i+2]=$val;
  121. $single_row_long[$i+2]=$val_long;
  122. }
  123. //-----------------------------------------check line addition
  124. if ($check_line[$lr_i]=='yes') {
  125. $check=1;
  126. } else {
  127. $check=0;
  128. }
  129. $single_row[$num_phenotypes+2]=$check;
  130. $single_row_long[$num_phenotypes+2]=$check;
  131. //-----------------------------------------
  132. //var_dump($single_row_long);
  133. $stringData= implode($delimiter, $single_row_long);
  134. //echo $stringData."<br>";
  135. $stringData.="\n";
  136. echo "$stringData";
  137. $all_rows[]=$single_row;
  138. $all_rows_long[]=$single_row_long;
  139. }
  140. //-----------------------------------------get statistics
  141. $mean_arr=array('Mean','');
  142. $se_arr=array('Standard Error','');
  143. // Unformatted mean and SE
  144. $unformat_mean_arr=array('Mean','');
  145. $unformat_se_arr=array('Standard Error','');
  146. $nr_arr=array('Number Replicates','');
  147. $prob_arr=array('Prob > F','');
  148. $fmean="Mean,";
  149. $fse="SE,";
  150. $fnr="Number Replicates,";
  151. $fprob="Prob gt F,";
  152. for ($i=0;$i<$num_phenotypes;$i++)
  153. {
  154. $puid=$phenotype_uid[$i];
  155. $sigdig=$unit_sigdigits[$i];
  156. $sql_mdata="SELECT mean_value,standard_error,number_replicates,prob_gt_F
  157. FROM phenotype_mean_data
  158. WHERE phenotype_uid='$puid'
  159. AND experiment_uid='$experiment_uid'";
  160. $res_mdata=mysqli_query($mysqli, $sql_mdata) or die(mysqli_error($mysqli));
  161. $row_mdata=mysqli_fetch_array($res_mdata);
  162. $mean=$row_mdata['mean_value'];
  163. $se=$row_mdata['standard_error'];
  164. $nr=$row_mdata['number_replicates'];
  165. $prob=$row_mdata['prob_gt_F'];
  166. if($mean!=0) {
  167. $unformat_mean_arr[] = $mean;
  168. if ($sigdig>=0) $mean=number_format($mean,$sigdig);
  169. $mean_arr[] = $mean;
  170. } else {
  171. $unformat_mean_arr[] = "--";
  172. $mean_arr[]="--";
  173. }
  174. if($se!=0) {
  175. $unformat_se_arr[] = $se;
  176. if ($sigdig>=0) $se=number_format($se,$sigdig);
  177. $se_arr[] = $se;
  178. } else {
  179. $se_arr[]="--";
  180. $unformat_se_arr[] = "--";
  181. }
  182. if($nr==0) {
  183. $nr="--";
  184. }
  185. $nr_arr[]=$nr;
  186. if($prob!="" && $prob!="NULL") {
  187. $prob_arr[]=$prob;
  188. } else {
  189. $prob_arr[]="--";
  190. }
  191. }
  192. $fmean= implode($delimiter, $mean_arr)."\n";
  193. $fse= implode($delimiter, $se_arr)."\n";
  194. $fnr= implode($delimiter, $nr_arr)."\n";
  195. $fprob= implode($delimiter, $prob_arr)."\n";
  196. $ufmean= implode($delimiter, $unformat_mean_arr)."\n";
  197. $ufse= implode($delimiter, $unformat_se_arr)."\n";
  198. echo "$ufmean";
  199. echo "$ufse";
  200. echo "$fnr";
  201. echo "$fprob";
  202. }
  203. private function downloadPlot()
  204. {
  205. global $mysqli;
  206. if (isset($_GET['pi'])) {
  207. $puid = $_GET['pi'];
  208. } else {
  209. echo "Error: experiment uid not set\n";
  210. return;
  211. }
  212. $sql = "select trial_code from experiments where experiment_uid = ?";
  213. if ($stmt = mysqli_prepare($mysqli, $sql)) {
  214. mysqli_stmt_bind_param($stmt, "i", $puid);
  215. mysqli_stmt_execute($stmt);
  216. mysqli_stmt_bind_result($stmt, $trial_code);
  217. mysqli_stmt_fetch($stmt);
  218. mysqli_stmt_close($stmt);
  219. } else {
  220. echo "Error: bad query\n";
  221. }
  222. $sql = "select phenotype_uid, phenotypes_name from phenotypes";
  223. if ($stmt = mysqli_prepare($mysqli, $sql)) {
  224. mysqli_stmt_execute($stmt);
  225. mysqli_stmt_bind_result($stmt, $phenotype_uid, $name);
  226. while (mysqli_stmt_fetch($stmt)) {
  227. $trait_list[$phenotype_uid] = $name;
  228. }
  229. mysqli_stmt_close($stmt);
  230. }
  231. header('Content-type: application/vnd.ms-excel');
  232. header('Content-Disposition: attachment;filename="trial_data_plot.csv"');
  233. echo "Trial Code,Plot,Trait,Value\n";
  234. $sql = "select plot, phenotype_uid, value from phenotype_plot_data, fieldbook
  235. where phenotype_plot_data.plot_uid = fieldbook.plot_uid
  236. and phenotype_plot_data.experiment_uid = ? order by phenotype_uid, plot";
  237. if ($stmt = mysqli_prepare($mysqli, $sql)) {
  238. mysqli_stmt_bind_param($stmt, "i", $puid);
  239. mysqli_stmt_execute($stmt);
  240. mysqli_stmt_bind_result($stmt, $plot_uid, $phenotype_uid, $value);
  241. while (mysqli_stmt_fetch($stmt)) {
  242. $trait = $trait_list[$phenotype_uid];
  243. echo "$trial_code,$plot_uid,$trait,$value\n";
  244. }
  245. mysqli_stmt_close($stmt);
  246. } else {
  247. echo "Error: bad query\n";
  248. }
  249. }
  250. }