NewickImporter.inc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. class NewickImporter extends TripalImporter {
  3. /**
  4. * The name of this loader. This name will be presented to the site
  5. * user.
  6. */
  7. public static $name = 'Newick Tree Loader';
  8. /**
  9. * The machine name for this loader. This name will be used to construct
  10. * the URL for the loader.
  11. */
  12. public static $machine_name = 'chado_newick_loader';
  13. /**
  14. * A brief description for this loader. This description will be
  15. * presented to the site user.
  16. */
  17. public static $description = 'Load Newick formatted phylogenetic trees.';
  18. /**
  19. * An array containing the extensions of allowed file types.
  20. */
  21. public static $file_types = ['tree', 'txt'];
  22. /**
  23. * Provides information to the user about the file upload. Typically this
  24. * may include a description of the file types allowed.
  25. */
  26. public static $upload_description = 'Please provide the Newick formatted tree file (one tree per file only). The file must have a .txt or .tree extension.';
  27. /**
  28. * The title that should appear above the file upload section.
  29. */
  30. public static $upload_title = 'Newick Upload';
  31. /**
  32. * Text that should appear on the button at the bottom of the importer
  33. * form.
  34. */
  35. public static $button_text = 'Import Newick file';
  36. /**
  37. * Indicates the methods that the file uploader will support.
  38. */
  39. public static $methods = [
  40. // Allow the user to upload a file to the server.
  41. 'file_upload' => TRUE,
  42. // Allow the user to provide the path on the Tripal server for the file.
  43. 'file_local' => TRUE,
  44. // Allow the user to provide a remote URL for the file.
  45. 'file_remote' => TRUE,
  46. ];
  47. /**
  48. * @see TripalImporter::form()
  49. */
  50. public function form($form, &$form_state) {
  51. // Default values can come in the following ways:
  52. //
  53. // 1) as elements of the $node object. This occurs when editing an existing phylotree
  54. // 2) in the $form_state['values'] array which occurs on a failed validation or
  55. // ajax callbacks from non submit form elements
  56. // 3) in the $form_state['input'[ array which occurs on ajax callbacks from submit
  57. // form elements and the form is being rebuilt
  58. //
  59. // set form field defaults
  60. $phylotree = NULL;
  61. $phylotree_id = NULL;
  62. $tree_name = '';
  63. $leaf_type = '';
  64. $analysis_id = '';
  65. $dbxref = '';
  66. $comment = '';
  67. $tree_required = TRUE;
  68. $tree_file = '';
  69. $name_re = '';
  70. $match = '';
  71. // If we are re constructing the form from a failed validation or ajax callback
  72. // then use the $form_state['values'] values.
  73. if (array_key_exists('values', $form_state) and isset($form_state['values']['tree_name'])) {
  74. $tree_name = $form_state['values']['tree_name'];
  75. $leaf_type = $form_state['values']['leaf_type'];
  76. $analysis_id = $form_state['values']['analysis_id'];
  77. $dbxref = $form_state['values']['dbxref'];
  78. $comment = $form_state['values']['description'];
  79. }
  80. // If we are re building the form from after submission (from ajax call) then
  81. // the values are in the $form_state['input'] array.
  82. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  83. $tree_name = $form_state['input']['tree_name'];
  84. $leaf_type = $form_state['input']['leaf_type'];
  85. $analysis_id = $form_state['input']['analysis_id'];
  86. $comment = $form_state['input']['description'];
  87. $dbxref = $form_state['input']['dbxref'];
  88. }
  89. $form['tree_name'] = [
  90. '#type' => 'textfield',
  91. '#title' => t('Tree Name'),
  92. '#required' => TRUE,
  93. '#default_value' => $tree_name,
  94. '#description' => t('Enter the name used to refer to this phylogenetic tree.'),
  95. '#maxlength' => 255,
  96. ];
  97. $so_cv = chado_get_cv(['name' => 'sequence']);
  98. $cv_id = $so_cv->cv_id;
  99. if (!$so_cv) {
  100. drupal_set_message('The Sequence Ontolgoy does not appear to be imported.
  101. Please import the Sequence Ontology before adding a tree.', 'error');
  102. }
  103. $form['leaf_type'] = [
  104. '#title' => t('Tree Type'),
  105. '#type' => 'textfield',
  106. '#description' => t("Choose the tree type. The type is
  107. a valid Sequence Ontology (SO) term. For example, trees derived
  108. from protein sequences should use the SO term 'polypeptide'.
  109. Alternatively, a phylotree can be used for representing a taxonomic
  110. tree. In this case, the word 'taxonomy' should be used."),
  111. '#required' => TRUE,
  112. '#default_value' => $leaf_type,
  113. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/cvterm/$cv_id",
  114. ];
  115. $form['dbxref'] = [
  116. '#title' => t('Database Cross-Reference'),
  117. '#type' => 'textfield',
  118. '#description' => t("Enter a database cross-reference of the form
  119. [DB name]:[accession]. The database name must already exist in the
  120. database. If the accession does not exist it is automatically added."),
  121. '#required' => FALSE,
  122. '#default_value' => $dbxref,
  123. ];
  124. $form['description'] = [
  125. '#type' => 'textarea',
  126. '#title' => t('Description'),
  127. '#required' => TRUE,
  128. '#default_value' => $comment,
  129. '#description' => t('Enter a description for this tree.'),
  130. ];
  131. $form['name_re'] = [
  132. '#title' => t('Feature Name Regular Expression'),
  133. '#type' => 'textfield',
  134. '#description' => t('If this is a phylogenetic (non taxonomic) tree, then
  135. the tree nodes will be automatically associated with features. However,
  136. if the nodes in the tree file are not exactly as the names of features
  137. but have enough information to uniquely identify the feature then you
  138. may provide a regular expression that the importer will use to extract
  139. the feature names from the node names.'),
  140. '#default_value' => $name_re,
  141. ];
  142. $form['match'] = [
  143. '#title' => t('Use Unique Feature Name'),
  144. '#type' => 'checkbox',
  145. '#description' => t('If this is a phylogenetic (non taxonomic tree) and the nodes ' . 'should match the unique name of the feature rather than the name of the feature ' . 'then select this box. If unselected the loader will try to match the feature ' . 'using the feature name.'),
  146. '#default_value' => $match,
  147. ];
  148. return $form;
  149. }
  150. /**
  151. * @see TripalImporter::formValidate()
  152. */
  153. public function formValidate($form, &$form_state) {
  154. $values = $form_state['values'];
  155. $options = [
  156. 'name' => trim($values["tree_name"]),
  157. 'description' => trim($values["description"]),
  158. 'analysis_id' => $values["analysis_id"],
  159. 'leaf_type' => $values["leaf_type"],
  160. 'format' => 'newick',
  161. 'dbxref' => trim($values["dbxref"]),
  162. 'match' => $values["match"],
  163. 'name_re' => $values["name_re"],
  164. ];
  165. $errors = [];
  166. $warnings = [];
  167. chado_validate_phylotree('insert', $options, $errors, $warnings);
  168. // Now set form errors if any errors were detected.
  169. if (count($errors) > 0) {
  170. foreach ($errors as $field => $message) {
  171. if ($field == 'name') {
  172. $field = 'tree_name';
  173. }
  174. form_set_error($field, $message);
  175. }
  176. }
  177. // Add any warnings if any were detected
  178. if (count($warnings) > 0) {
  179. foreach ($warnings as $field => $message) {
  180. drupal_set_message($message, 'warning');
  181. }
  182. }
  183. }
  184. /**
  185. * @see TripalImporter::run()
  186. */
  187. public function run() {
  188. $arguments = $this->arguments['run_args'];
  189. $options = array(
  190. 'name' => $arguments["tree_name"],
  191. 'description' => $arguments["description"],
  192. 'analysis_id' => $arguments["analysis_id"],
  193. 'leaf_type' => $arguments["leaf_type"],
  194. 'tree_file' => $this->arguments['files'][0]['file_path'],
  195. 'format' => 'newick',
  196. 'dbxref' => $arguments["dbxref"],
  197. 'match' => $arguments["match"],
  198. 'name_re' => $arguments["name_re"],
  199. );
  200. $errors = array();
  201. $warnings = array();
  202. chado_insert_phylotree($options, $errors, $warnings);
  203. }
  204. }