|
@@ -1,17 +1,13 @@
|
|
Manual Field Creation
|
|
Manual Field Creation
|
|
======================
|
|
======================
|
|
-
|
|
|
|
To show how a TripalField works we will break down a class implementation section by section. Here we will use the **obi__organism** field that comes with Tripal and which extends the ChadoField class. The ChadoField class is almost identical to the TripalField class except that it provides a few extra settings for working with Chado tables. To create your own class you need to create a new class that implements the necessary functions.
|
|
To show how a TripalField works we will break down a class implementation section by section. Here we will use the **obi__organism** field that comes with Tripal and which extends the ChadoField class. The ChadoField class is almost identical to the TripalField class except that it provides a few extra settings for working with Chado tables. To create your own class you need to create a new class that implements the necessary functions.
|
|
|
|
|
|
.. note::
|
|
.. note::
|
|
Creation of your first field may not seem easy! The following document is a lot to think about and consider. Therefore, when you write your first field, don't try to do everything at once. Take it one piece at a time. The variables and functions described here are in order with the most critical components described first. Take it at an even pace.
|
|
Creation of your first field may not seem easy! The following document is a lot to think about and consider. Therefore, when you write your first field, don't try to do everything at once. Take it one piece at a time. The variables and functions described here are in order with the most critical components described first. Take it at an even pace.
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
Directory Structure for Fields
|
|
Directory Structure for Fields
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
-
|
|
|
|
-
|
|
|
|
Before we create our class we must first create a proper directory structure. Tripal expects that all new Tripal field classes are located inside of a custom module in the following directory structure:
|
|
Before we create our class we must first create a proper directory structure. Tripal expects that all new Tripal field classes are located inside of a custom module in the following directory structure:
|
|
|
|
|
|
.. code-block:: bash
|
|
.. code-block:: bash
|
|
@@ -31,7 +27,6 @@ In the directories above the token [your_module] can be substituted with the nam
|
|
|
|
|
|
Anatomy of the ChadoField Class
|
|
Anatomy of the ChadoField Class
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
-
|
|
|
|
The following describes a ChadoField class from top to bottom. The code for the obi__organism field is shown in order that it appears in the class with descriptions provided for the meaning of each piece of code. To write your own class, duplicate the variables and function and customize accordingly. First, let's look at the definition of the class. The following line defines the class and indicates that it extends the ChadoField class:
|
|
The following describes a ChadoField class from top to bottom. The code for the obi__organism field is shown in order that it appears in the class with descriptions provided for the meaning of each piece of code. To write your own class, duplicate the variables and function and customize accordingly. First, let's look at the definition of the class. The following line defines the class and indicates that it extends the ChadoField class:
|
|
|
|
|
|
.. code-block:: php
|
|
.. code-block:: php
|
|
@@ -133,8 +128,6 @@ Finally, the last item in our Class variables is the **download_formatters**. T
|
|
|
|
|
|
.. .. code-block::
|
|
.. .. code-block::
|
|
|
|
|
|
-
|
|
|
|
-
|
|
|
|
// Indicates the download formats for this field. The list must be the
|
|
// Indicates the download formats for this field. The list must be the
|
|
// name of a child class of the TripalFieldDownloader.
|
|
// name of a child class of the TripalFieldDownloader.
|
|
public static $download_formatters = array(
|
|
public static $download_formatters = array(
|
|
@@ -154,7 +147,6 @@ To get started, the load() function receives a single argument. The entity objec
|
|
|
|
|
|
.. code-block:: php
|
|
.. code-block:: php
|
|
|
|
|
|
-
|
|
|
|
public function load($entity) {
|
|
public function load($entity) {
|
|
|
|
|
|
|
|
|
|
@@ -162,7 +154,6 @@ Because this is a ChadoField and the TripalChado module supports this field and
|
|
|
|
|
|
.. code-block:: php
|
|
.. code-block:: php
|
|
|
|
|
|
-
|
|
|
|
$record = $entity->chado_record;
|
|
$record = $entity->chado_record;
|
|
|
|
|
|
Having the record helps tremendously. Our **obi__organism** field is meant to be attached to genomic feature content types (e.g. genes, mRNA, etc.), germplasm, etc. Therefore, the entity will be a record of one of those types. In the case of a genomic feature, these come from the **feature** table of Chado. In the case of a germplam, these records come from the **stock** table of Chado. Both of these records have an **organism_id** field which is a foreign key to the organism table where we find out details about the organism.
|
|
Having the record helps tremendously. Our **obi__organism** field is meant to be attached to genomic feature content types (e.g. genes, mRNA, etc.), germplasm, etc. Therefore, the entity will be a record of one of those types. In the case of a genomic feature, these come from the **feature** table of Chado. In the case of a germplam, these records come from the **stock** table of Chado. Both of these records have an **organism_id** field which is a foreign key to the organism table where we find out details about the organism.
|
|
@@ -171,7 +162,6 @@ Before we set the values for our field, we need a little bit more information.
|
|
|
|
|
|
.. code-block:: php
|
|
.. code-block:: php
|
|
|
|
|
|
-
|
|
|
|
$settings = $this->instance['settings'];
|
|
$settings = $this->instance['settings'];
|
|
$field_table = $this->instance['settings']['chado_table'];
|
|
$field_table = $this->instance['settings']['chado_table'];
|
|
$field_column = $this->instance['settings']['chado_column'];
|
|
$field_column = $this->instance['settings']['chado_column'];
|
|
@@ -180,7 +170,6 @@ Next, we want to get this field name and its type. We obviously know our field
|
|
|
|
|
|
.. code-block:: php
|
|
.. code-block:: php
|
|
|
|
|
|
-
|
|
|
|
$field_name = $this->field['field_name'];
|
|
$field_name = $this->field['field_name'];
|
|
$field_type = $this->field['type'];
|
|
$field_type = $this->field['type'];
|
|
|
|
|
|
@@ -189,7 +178,6 @@ Now, let's plan how we want our values to appear in our field. The organism rec
|
|
|
|
|
|
.. code-block:: php
|
|
.. code-block:: php
|
|
|
|
|
|
-
|
|
|
|
// Get the terms for each of the keys for the 'values' property.
|
|
// Get the terms for each of the keys for the 'values' property.
|
|
$label_term = 'rdfs:label';
|
|
$label_term = 'rdfs:label';
|
|
$genus_term = tripal_get_chado_semweb_term('organism', 'genus');
|
|
$genus_term = tripal_get_chado_semweb_term('organism', 'genus');
|
|
@@ -204,7 +192,6 @@ Next, let's initialize our field's value to be empty. When setting a field valu
|
|
|
|
|
|
.. code-block:: php
|
|
.. code-block:: php
|
|
|
|
|
|
-
|
|
|
|
// Set some defaults for the empty record.
|
|
// Set some defaults for the empty record.
|
|
$entity->{$field_name}['und'][0] = array(
|
|
$entity->{$field_name}['und'][0] = array(
|
|
'value' => array(),
|
|
'value' => array(),
|
|
@@ -217,7 +204,6 @@ Now that we've got some preliminary values and we've initialized our value array
|
|
|
|
|
|
.. code-block:: php
|
|
.. code-block:: php
|
|
|
|
|
|
-
|
|
|
|
if ($record) {
|
|
if ($record) {
|
|
|
|
|
|
|
|
|
|
@@ -225,7 +211,6 @@ Now if we do have a record we need to get the value The first step is to actual
|
|
|
|
|
|
.. code-block:: php
|
|
.. code-block:: php
|
|
|
|
|
|
-
|
|
|
|
if ($field_table == 'biomaterial') {
|
|
if ($field_table == 'biomaterial') {
|
|
$organism = $record->taxon_id;
|
|
$organism = $record->taxon_id;
|
|
}
|
|
}
|
|
@@ -235,9 +220,7 @@ Now if we do have a record we need to get the value The first step is to actual
|
|
|
|
|
|
We can easily get all of the values we need from this organism object. We can now access the values for this organism using the Chado organism table column names (e.g. $organism->genus, $organism->species).
|
|
We can easily get all of the values we need from this organism object. We can now access the values for this organism using the Chado organism table column names (e.g. $organism->genus, $organism->species).
|
|
|
|
|
|
-.. code-block:: php
|
|
|
|
-
|
|
|
|
-
|
|
|
|
|
|
+.. code-block:: php
|
|
|
|
|
|
$label = tripal_replace_chado_tokens($string, $organism);
|
|
$label = tripal_replace_chado_tokens($string, $organism);
|
|
$entity->{$field_name}['und'][0]['value'] = array(
|
|
$entity->{$field_name}['und'][0]['value'] = array(
|
|
@@ -260,7 +243,6 @@ Okay, so, we have our values set. However, remember, our fields must support tw
|
|
|
|
|
|
.. code-block:: php
|
|
.. code-block:: php
|
|
|
|
|
|
-
|
|
|
|
// Set the linker field appropriately.
|
|
// Set the linker field appropriately.
|
|
if ($field_table == 'biomaterial') {
|
|
if ($field_table == 'biomaterial') {
|
|
$linker_field = 'chado-biomaterial__taxon_id';
|
|
$linker_field = 'chado-biomaterial__taxon_id';
|