t('Sequences'), 'description' => t('A field for managing nucleotide and protein residues.'), 'default_widget' => 'chado_feature__residues_widget', 'default_formatter' => 'chado_feature__residues_formatter', 'settings' => array(), 'instance_settings' => array('text_processing' => 1, 'display_summary' => 0), 'storage' => array( 'type' => 'field_chado_storage', 'module' => 'tripal_chado', 'active' => TRUE ), ); } /** * Implements hook_attach_info(). * * This is a hook provided by the tripal_Chado module. It allows the field * to specify which bundles it will attach to and to specify thee settings. * * @param $entity_type * @param $entity * @param $term * * @return * A field array */ function attach_info($entity_type, $bundle, $settings) { $field_info = array(); $table_name = $settings['data_table']; $type_table = $settings['type_table']; $type_field = $settings['field']; $cv_id = $settings['cv_id']; $cvterm_id = $settings['cvterm_id']; // If this is not the feature table then we don't want to attach. if ($table_name != 'feature') { return $field_info; } $field_info = array( 'field_name' => 'feature__residues', 'field_type' => 'chado_feature__residues', 'widget_type' => 'chado_feature__residues_widget', 'description' => 'An IUPAC compatible residues for this feature.', 'label' => 'Sequences', 'is_required' => 0, 'storage' => 'field_chado_storage', 'widget_settings' => array( 'display_label' => 1 ), 'field_settings' => array( 'chado_table' => $table_name, 'chado_column' => 'residues', 'semantic_web' => array( 'name' => 'sequence_feature', 'accession' => '0000110', 'ns' => 'SO', 'nsurl' => 'http://www.sequenceontology.org', ), ), ); return $field_info; } /** * Implements hook_widget_info. * * This is a hook provided by the tripal_chado module for offloading * the hook_field_widget_info() hook for each field to specify. */ function widget_info() { return array( 'label' => t('Sequence'), 'field types' => array('chado_feature__residues'), ); } /** * Implements hook_formatter_info. * * This is a hook provided by the tripal_chado module for * offloading the hook_field_formatter_info() for each field * to specify. * */ function formatter_info() { return array( 'label' => t('Sequences'), 'field types' => array('chado_feature__residues'), ); } /** * * @param unknown $entity_type * @param unknown $entity * @param unknown $field * @param unknown $instance * @param unknown $langcode * @param unknown $items * @param unknown $display */ function formatter_view(&$element, $entity_type, $entity, $field, $instance, $langcode, $items, $display) { $num_bases = 50; $feature = $entity->chado_record; foreach ($items as $delta => $item) { // If there are no residues then skip this one. if (!array_key_exists('residues', $item['value'])) { continue; } $residues = $item['value']['residues']; $label = $item['value']['label']; $defline = $item['value']['defline']; $content = '
' . $label . '
'; $content .= '
';
$content .= '>' . $defline . "
";
$content .= wordwrap($residues, $num_bases, "
", TRUE);
$content .= '';
$element[$delta] = array(
// We create a render array to produce the desired markup,
'#type' => 'markup',
'#markup' => $content,
);
}
}
/**
* @see TripalField::widget_form()
*/
function widget_form(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
$settings = $field['settings'];
$field_name = $field['field_name'];
$field_type = $field['type'];
$field_table = $field['settings']['chado_table'];
$field_column = $field['settings']['chado_column'];
// This field provides several items if an alignment for the feature
// is present. We don't want widgets for those. Just for the first
// one which should always be the residues for the feature table.
if ($delta > 0) {
return;
}
$widget['value'] = array(
'#type' => 'value',
'#value' => $items[$delta]['value'],
);
$widget['feature__residues'] = array(
'#type' => 'textarea',
'#title' => $element['#title'],
'#description' => $element['#description'],
'#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
'#default_value' => $items[$delta]['feature__residues'],
'#delta' => $delta,
'#element_validate' => array('chado_feature__residues_widget_validate'),
'#cols' => 30,
);
}
/**
* @see TripalField::load()
*/
function load($field, $entity, $details) {
$field_name = $field['field_name'];
$feature = $details['record'];
$num_seqs = 0;
// We don't want to get the sequence for traditionally large types. They are
// too big, bog down the web browser, take longer to load and it's not
// reasonable to print them on a page.
if(strcmp($feature->type_id->name,'scaffold') == 0 or
strcmp($feature->type_id->name,'chromosome') == 0 or
strcmp($feature->type_id->name,'supercontig') == 0 or
strcmp($feature->type_id->name,'pseudomolecule') == 0) {
$entity->{$field_name}['und'][$num_seqs]['value'] = array(
'@type' => 'SO:0000110',
'type' => 'sequence_feature',
'label' => 'Residues',
'defline' => ">This sequence is too large for this display.",
'residues' => '',
);
$entity->{$field_name}['und'][$num_seqs]['feature__residues'] = '';
}
else {
$feature = chado_expand_var($feature,'field','feature.residues');
if ($feature->residues) {
$entity->{$field_name}['und'][$num_seqs]['value'] = array(
'@type' => 'SO:0000110',
'type' => 'sequence_feature',
'label' => 'Sequence',
'defline' => tripal_get_fasta_defline($feature, '', NULL, '', strlen($feature->residues)),
'residues' => $feature->residues,
);
$entity->{$field_name}['und'][$num_seqs]['feature__residues'] = $feature->residues;
}
else {
$entity->{$field_name}['und'][$num_seqs]['value'] = array();
$entity->{$field_name}['und'][$num_seqs]['feature__residues'] = '';
}
}
$num_seqs++;
// Add in the protein sequences
$values = array(
'object_id' => $feature->feature_id,
'subject_id' => array(
'type_id' => array(
'name' => 'polypeptide'
),
),
'type_id' => array(
'name' => 'derives_from',
),
);
$options = array(
'return_array' => 1,
'include_fk' => array(
'subject_id' => 1
),
);
$protein_rels = chado_generate_var('feature_relationship', $values, $options);
foreach ($protein_rels as $protein_rel) {
$protein_rel = chado_expand_var($protein_rel, 'field', 'feature.residues');
if ($protein_rel->subject_id->residues) {
$entity->{$field_name}['und'][$num_seqs++]['value'] = array(
'@type' => 'SO:0000104',
'type' => 'polypeptide',
'label' => 'Protein Sequence',
'defline' => tripal_get_fasta_defline($protein_rel->subject_id, '', NULL, '', strlen($protein_rel->subject_id->residues)),
'residues' => $protein_rel->subject_id->residues,
);
}
}
// Add in sequences from alignments.
$options = array(
'return_array' => 1,
'include_fk' => array(
'srcfeature_id' => array(
'type_id' => 1
),
'feature_id' => array(
'type_id' => 1
),
),
);
$feature = chado_expand_var($feature, 'table', 'featureloc', $options);
$featureloc_sequences = $this->get_featureloc_sequences($feature->feature_id, $feature->featureloc->feature_id);
// Add in the coding sequences.
$values = array(
'object_id' => $feature->feature_id,
'subject_id' => array(
'type_id' => array(
'name' => 'CDS'
),
),
'type_id' => array(
'name' => 'part_of',
),
);
$options = array(
'order_by' => array('rank' => 'ASC'),
'return_array' => 1,
'include_fk' => array(
'subject_id' => 1
),
);
$cds_rels = chado_generate_var('feature_relationship', $values, $options);
$coding_seq = '';
foreach ($cds_rels as $cds_rel) {
$cds_rel = chado_expand_var($cds_rel, 'field', 'feature.residues');
if ($cds_rel->subject_id->residues) {
$coding_seq .= $cds_rel->subject_id->residues;
}
}
if ($coding_seq) {
$entity->{$field_name}['und'][$num_seqs++]['value'] = array(
'@type' => 'SO:0000316',
'type' => 'coding_sequence',
'label' => 'Coding sequence (CDS)',
'defline' => tripal_get_fasta_defline($feature, '', $feature->featureloc->feature_id[0], '', strlen($coding_seq)),
'residues' => $coding_seq,
);
}
foreach($featureloc_sequences as $src => $attrs){
// the $attrs array has the following keys
// * id: a unique identifier combining the feature id with the cvterm id
// * type: the type of sequence (e.g. mRNA, etc)
// * location: the alignment location
// * defline: the definition line
// * formatted_seq: the formatted sequences
// * featureloc: the feature object aligned to
$entity->{$field_name}['und'][$num_seqs++]['value'] = array(
'residues' => $attrs['residues'],
'@type' => 'SO:0000110',
'type' => 'sequence_feature',
'defline' => tripal_get_fasta_defline($feature, '', $attrs['featureloc'], 'CDS', strlen($attrs['residues'])),
'label' => 'Sequence from alignment at ' . $attrs['location'],
);
// check to see if this alignment has any CDS. If so, generate a CDS sequence
$cds_sequence = tripal_get_feature_sequences(
array(
'feature_id' => $feature->feature_id,
'parent_id' => $attrs['featureloc']->srcfeature_id->feature_id,
'name' => $feature->name,
'featureloc_id' => $attrs['featureloc']->featureloc_id,
),
array(
'derive_from_parent' => 1, // CDS are in parent-child relationships so we want to use the sequence from the parent
'aggregate' => 1, // we want to combine all CDS for this feature into a single sequence
'sub_feature_types' => array('CDS'), // we're looking for CDS features
'is_html' => 0
)
);
if (count($cds_sequence) > 0) {
// the tripal_get_feature_sequences() function can return multiple sequences
// if a feature is aligned to multiple places. In the case of CDSs we expect
// that one mRNA is only aligned to a single location on the assembly so we
// can access the CDS sequence with index 0.
if ($cds_sequence[0]['residues']) {
$entity->{$field_name}['und'][$num_seqs++]['value'] = array(
'residues' => $cds_sequence[0]['residues'],
'@type' => 'SO:0000316',
'type' => 'coding_sequence',
'defline' => tripal_get_fasta_defline($feature, '', $attrs['featureloc'], 'CDS', $cds_sequence[0]['length']),
'label' => 'Coding sequence (CDS) from alignment at ' . $attrs['location'],
);
}
}
}
}
/**
*
* @param unknown $feature_id
* @param unknown $featurelocs
* @return multitype:|Ambigous