NewickImporter.inc

File

tripal_chado/includes/TripalImporter/NewickImporter.inc
View source
  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. 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. $type_cv = tripal_get_default_cv('phylotree', 'type_id');
  98. $so_cv = chado_get_cv(['name' => 'sequence']);
  99. $cv_id = $so_cv->cv_id;
  100. if (!$so_cv) {
  101. drupal_set_message('The Sequence Ontolgoy does not appear to be imported.
  102. Please import the Sequence Ontology before adding a tree.', 'error');
  103. }
  104. $form['leaf_type'] = [
  105. '#title' => t('Tree Type'),
  106. '#type' => 'textfield',
  107. '#description' => t("Choose the tree type. The type is
  108. a valid Sequence Ontology (SO) term. For example, trees derived
  109. from protein sequences should use the SO term 'polypeptide'.
  110. Alternatively, a phylotree can be used for representing a taxonomic
  111. tree. In this case, the word 'taxonomy' should be used."),
  112. '#required' => TRUE,
  113. '#default_value' => $leaf_type,
  114. '#autocomplete_path' => "admin/tripal/legacy/tripal_cv/cvterm/auto_name/$cv_id",
  115. ];
  116. $form['dbxref'] = [
  117. '#title' => t('Database Cross-Reference'),
  118. '#type' => 'textfield',
  119. '#description' => t("Enter a database cross-reference of the form
  120. [DB name]:[accession]. The database name must already exist in the
  121. database. If the accession does not exist it is automatically added."),
  122. '#required' => FALSE,
  123. '#default_value' => $dbxref,
  124. ];
  125. $form['description'] = [
  126. '#type' => 'textarea',
  127. '#title' => t('Description'),
  128. '#required' => TRUE,
  129. '#default_value' => $comment,
  130. '#description' => t('Enter a description for this tree.'),
  131. ];
  132. $form['name_re'] = [
  133. '#title' => t('Feature Name Regular Expression'),
  134. '#type' => 'textfield',
  135. '#description' => t('If this is a phylogenetic (non taxonomic) tree, then
  136. the tree nodes will be automatically associated with features. However,
  137. if the nodes in the tree file are not exactly as the names of features
  138. but have enough information to uniquely identify the feature then you
  139. may provide a regular expression that the importer will use to extract
  140. the feature names from the node names.'),
  141. '#default_value' => $name_re,
  142. ];
  143. $form['match'] = [
  144. '#title' => t('Use Unique Feature Name'),
  145. '#type' => 'checkbox',
  146. '#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.'),
  147. '#default_value' => $match,
  148. ];
  149. return $form;
  150. }
  151. /**
  152. * @see TripalImporter::formValidate()
  153. */
  154. public function formValidate($form, &$form_state) {
  155. $values = $form_state['values'];
  156. $options = [
  157. 'name' => trim($values["tree_name"]),
  158. 'description' => trim($values["description"]),
  159. 'analysis_id' => $values["analysis_id"],
  160. 'leaf_type' => $values["leaf_type"],
  161. 'tree_file' => $this->arguments['files'][0]['file_path'],
  162. 'format' => 'newick',
  163. 'dbxref' => trim($values["dbxref"]),
  164. 'match' => $values["match"],
  165. 'name_re' => $values["name_re"],
  166. ];
  167. $errors = [];
  168. $warnings = [];
  169. chado_validate_phylotree('insert', $options, $errors, $warnings);
  170. // Now set form errors if any errors were detected.
  171. if (count($errors) > 0) {
  172. foreach ($errors as $field => $message) {
  173. if ($field == 'name') {
  174. $field = 'tree_name';
  175. }
  176. form_set_error($field, $message);
  177. }
  178. }
  179. // Add any warnings if any were detected
  180. if (count($warnings) > 0) {
  181. foreach ($warnings as $field => $message) {
  182. drupal_set_message($message, 'warning');
  183. }
  184. }
  185. }
  186. /**
  187. * @see TripalImporter::run()
  188. */
  189. public function run() {
  190. $arguments = $this->arguments['run_args'];
  191. $options = array(
  192. 'name' => $this->arguments["tree_name"],
  193. 'description' => $this->arguments["description"],
  194. 'analysis_id' => $this->arguments["analysis_id"],
  195. 'leaf_type' => $this->arguments["leaf_type"],
  196. 'tree_file' => $this->arguments['files'][0]['file_path'],
  197. 'format' => 'newick',
  198. 'dbxref' => $this->arguments["dbxref"],
  199. 'match' => $this->arguments["match"],
  200. 'name_re' => $this->arguments["name_re"],
  201. );
  202. $errors = array();
  203. $warnings = array();
  204. chado_insert_phylotree($options, $errors, $warnings);
  205. }
  206. }