function chado_phylotree_form

2.x tripal_phylogeny.chado_node.inc chado_phylotree_form($node, &$form_state)
3.x tripal_phylogeny.chado_node.inc chado_phylotree_form($node, &$form_state)

Implementation of hook_form().

Related topics

File

tripal_phylogeny/includes/tripal_phylogeny.chado_node.inc, line 112
Implements the phylotree node content type

Code

function chado_phylotree_form($node, &$form_state) {

  $form = array();

  // Default values can come in the following ways:
  //
  // 1) as elements of the $node object.  This occurs when editing an existing phylotree
  // 2) in the $form_state['values'] array which occurs on a failed validation or
  //    ajax callbacks from non submit form elements
  // 3) in the $form_state['input'[ array which occurs on ajax callbacks from submit
  //    form elements and the form is being rebuilt
  //
  // set form field defaults
  $phylotree = null;
  $phylotree_id = null;
  $tree_name = '';
  $leaf_type = '';
  $analysis_id = '';
  $dbxref = '';
  $comment = '';
  $tree_required = TRUE;
  $tree_file = '';
  $name_re = '';
  $match = '';

  // If we are editing an existing node then the phylotree is already part of the node.
  if (property_exists($node, 'phylotree')) {
    $phylotree = $node->phylotree;
    $phylotree = chado_expand_var($phylotree, 'field', 'phylotree.comment');
    $phylotree_id = $phylotree->phylotree_id;
    $tree_name = $phylotree->name;
    $leaf_type = $phylotree->type_id ? $phylotree->type_id->name : '';
    $comment = $phylotree->comment;
    $analysis_id = $phylotree->analysis_id ? $phylotree->analysis_id->analysis_id : '';
    $dbxref = $phylotree->dbxref_id->db_id->name . ":" . $phylotree->dbxref_id->accession;
    $name_re = $phylotree->tripal_variables->phylotree_name_re;
    $match = $phylotree->tripal_variables->phylotree_use_uniquename;

    // If the dbxref is the null db then hide it.
    if ($phylotree->dbxref_id->db_id->name == 'null') {
      $dbxref = '';
    }

    // Get the tree file name. If the file was added via the Drupal interface
    // then a numeric file_id will be present in the phylotree_tree_file
    // variable. If not then the tree was loaded on the command-line and
    // the actual filename is in this variable.
    $file_id = $phylotree->tripal_variables->phylotree_tree_file;
    if (is_numeric($file_id)) {
      $file = file_load($file_id);
      if ($file) {
        $tree_file = $file->filename;
      }
    }
    else {
      $tree_file = $file_id;
    }

    // The tree file is not a required input field when editing the node.
    $tree_required = FALSE;

    // Keep track of the phylotree id.
    $form['phylotree_id'] = array(
      '#type' => 'value',
      '#value' => $phylotree_id,
    );
  }
  // If we are re constructing the form from a failed validation or ajax callback
  // then use the $form_state['values'] values.
  if (array_key_exists('values', $form_state) and isset($form_state['values']['tree_name'])) {
    $tree_name = $form_state['values']['tree_name'];
    $leaf_type = $form_state['values']['leaf_type'];
    $analysis_id = $form_state['values']['analysis_id'];
    $dbxref = $form_state['values']['dbxref'];
    $comment = $form_state['values']['description'];
  }
  // If we are re building the form from after submission (from ajax call) then
  // the values are in the $form_state['input'] array.
  if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
    $tree_name = $form_state['input']['tree_name'];
    $leaf_type = $form_state['input']['leaf_type'];
    $analysis_id = $form_state['input']['analysis_id'];
    $comment = $form_state['input']['description'];
    $dbxref = $form_state['input']['dbxref'];
  }

  $form['tree_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Tree Name'),
    '#required' => TRUE,
    '#default_value' => $tree_name,
    '#description' => t('Enter the name used to refer to this phylogenetic tree.'),
    '#maxlength' => 255
  );

  $type_cv = tripal_get_default_cv('phylotree', 'type_id');
  $so_cv = tripal_get_cv(array('name' => 'sequence'));
  $cv_id = $so_cv->cv_id;
  if (!$so_cv) {
    drupal_set_message('The Sequence Ontolgoy does not appear to be imported.
        Please import the Sequence Ontology before adding a tree.', 'error');
  }

  $form['leaf_type'] = array(
    '#title' => t('Tree Type'),
    '#type' => 'textfield',
    '#description' => t("Choose the tree type. The type is
        a valid Sequence Ontology (SO) term. For example, trees derived
        from protein sequences should use the SO term 'polypeptide'.
        Alternatively, a phylotree can be used for representing a taxonomic
        tree. In this case, the word 'taxonomy' should be used."),
    '#required' => TRUE,
    '#default_value' => $leaf_type,
    '#autocomplete_path' => "admin/tripal/chado/tripal_cv/cvterm/auto_name/$cv_id",
  );

  // Get the list of analyses.
  $sql = "SELECT * FROM {analysis} ORDER BY name";
  $arset = chado_query($sql);
  $analyses = array();
  $analyses[''] = '';
  while ($analysis = $arset->fetchObject()) {
    $analyses[$analysis->analysis_id] = $analysis->name;
  }
  $form['analysis_id'] = array(
    '#title' => t('Analysis'),
    '#type' => 'select',
    '#description' => t("Choose the analysis from which this phylogenetic tree was derived"),
    '#required' => TRUE,
    '#default_value' => $analysis_id,
    '#options' => $analyses,
  );

  $form['dbxref'] = array(
    '#title' => t('Database Cross-Reference'),
    '#type' => 'textfield',
    '#description' => t("Enter a database cross-reference of the form
        [DB name]:[accession]. The database name must already exist in the
        database. If the accession does not exist it is automatically added."),
    '#required' => FALSE,
    '#default_value' => $dbxref,
  );

  $form['description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#required' => TRUE,
    '#default_value' => $comment,
    '#description' => t('Enter a description for this tree.'),
  );

  $upload_location = tripal_get_files_stream('tripal_phylogeny');
  $form['tree_file'] = array(
    '#type' => 'fieldset',
    '#title' => t('Tree File Import'),
    '#collapsible' => FALSE,
  );

  $description = t('Please provide a file in the Newick format that contains
      the nodes of this tree.');
  if ($tree_file) {
    $form['tree_file']['curr_file'] = array(
      '#type' => 'item',
      '#title' => 'Current Tree File',
      '#markup' => $tree_file,
    );
    $description = t('Please provide a file in the Newick format that
        contains the nodes of this tree.  Please note that uploading a new
        file will overwrite the current tree.');
  }
  $form['tree_file']['tree_file'] = array(
    '#type' => 'managed_file',
    '#title' => t('New Tree File'),
    '#description' => $description,
    '#upload_location' => $upload_location,
    '#upload_validators' => array(
      // We don't want to require a specific file extension so leave the array empty.
      'file_validate_extensions' => array(),
      // The following is for checking the Newick file format.
      'chado_phylotree_validate_newick_format' => array(),
    ),
    '#required' => $tree_required,
  );

  $form['tree_file']['name_re'] = array(
    '#title' => t('Feature Name Regular Expression'),
    '#type' => 'textfield',
    '#description' => t('If this is a phylogenetic (non taxonomic) tree, then
          the tree nodes will be automatically associated with features. However,
          if the nodes in the tree file are not exactly as the names of features
          but have enough information to uniquely identify the feature then you
          may provide a regular expression that the importer will use to extract
          the feature names from the node names.'),
    '#default_value' => $name_re,
  );
  $form['tree_file']['match'] = array(
    '#title' => t('Use Unique Feature Name'),
    '#type' => 'checkbox',
    '#description' => t('If this is a phylogenetic (non taonomic tree) and the nodes ' .
      'should match the unique name of the feature rather than the name of the feautre ' .
      'then select this box. If unselected the loader will try to match the feature ' .
      'using the feature name.'),
    '#default_value' => $match,
  );

  return $form;
}