TripalNucFASTADownloader.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. class TripalNucFASTADownloader extends TripalFieldDownloader {
  3. /**
  4. * Sets the label shown to the user describing this formatter. It
  5. * should be a short identifier. Use the $full_label for a more
  6. * descriptive label.
  7. */
  8. static public $label = 'FASTA';
  9. /**
  10. * A more verbose label that better describes the formatter.
  11. */
  12. static public $full_label = 'Nucleotide FASTA';
  13. /**
  14. * Indicates the default extension for the outputfile.
  15. */
  16. static public $default_extension = 'fna';
  17. /**
  18. * @see TripalFieldDownloader::format()
  19. */
  20. protected function formatEntity($entity) {
  21. $lines = array();
  22. // Get the list of all fields that have been attached to the entity
  23. $instances = field_info_instances('TripalEntity', $entity->bundle);
  24. $available_fields = array();
  25. foreach ($instances as $field_name => $instance) {
  26. if ($instance['field_name'] == 'entity_id') {
  27. continue;
  28. }
  29. $available_fields[$instance['field_name']] = $instance;
  30. }
  31. foreach ($this->fields as $field_id) {
  32. $field = field_info_field_by_id($field_id);
  33. $field_name = $field['field_name'];
  34. if (!property_exists($entity, $field_name)) {
  35. continue;
  36. }
  37. // If we only have one element then this is good.
  38. if (count($entity->{$field_name}['und']) == 1) {
  39. $value = $entity->{$field_name}['und'][0]['value'];
  40. // If the single element is not an array then this is good.
  41. if (!is_array($value)) {
  42. // We need to make sure we have some fields for the definition line.
  43. // those may or may not have been included, so we should add them.
  44. $defline = '>';
  45. $found_identifier = FALSE;
  46. if (property_exists($entity, 'data__identifier')) {
  47. $found_identifier = TRUE;
  48. $defline .= $entity->{'data__identifier'}['und'][0]['value'] . ' ';
  49. }
  50. if (property_exists($entity, 'schema__name')) {
  51. $found_identifier = TRUE;
  52. $defline .= $entity->{'schema__name'}['und'][0]['value'] . ' ';
  53. }
  54. if (property_exists($entity, 'data__accession')) {
  55. $found_identifier = TRUE;
  56. $defline .= $entity->{'data__accession'}['und'][0]['value'] . ' ';
  57. }
  58. if (!$found_identifier) {
  59. $defline .= "Unknown feature identifier. Please add a name field to the data collection";
  60. }
  61. if (property_exists($entity, 'data__sequence_coordinates')) {
  62. $location = strip_tags(drupal_render(field_view_field('TripalEntity', $entity, 'data__sequence_coordinates'))) . '; ';
  63. $location = preg_replace('/\&nbsp\;/', ' ', $location);
  64. $defline .= $location;
  65. }
  66. // Add to the defnition line values from any single valued fields.
  67. foreach ($available_fields as $fname => $instance) {
  68. if (count($entity->{$fname}['und']) == 1) {
  69. if (!is_array($entity->{$fname}['und'][0]['value'])) {
  70. // Skip the identifier fields and the residues fields.
  71. if (!in_array($fname, array('data__identifier',
  72. 'schema__name', 'data__protein_sequence', $field_name))) {
  73. $fvalue = $entity->{$fname}['und'][0]['value'];
  74. if ($fvalue) {
  75. $defline .= $instance['label'] . ': ' . $fvalue . '; ';
  76. }
  77. }
  78. }
  79. else {
  80. if (array_key_exists('rdfs:label', $entity->{$fname}['und'][0]['value'])) {
  81. $defline .= $instance['label'] . ': ' . strip_tags($entity->{$fname}['und'][0]['value']['rdfs:label']) . '; ';
  82. }
  83. }
  84. }
  85. }
  86. $defline = rtrim($defline, '; ');
  87. // Now add the residues.
  88. $lines[] = $defline;
  89. $residues = explode('|', wordwrap($value, 50, "|", TRUE));
  90. foreach ($residues as $line) {
  91. $lines[] = $line;
  92. }
  93. }
  94. else {
  95. // TODO: What to do with fields that are arrays?
  96. }
  97. }
  98. else {
  99. // TODO: What to do with fields that have multiple values?
  100. }
  101. }
  102. return $lines;
  103. }
  104. /**
  105. * @see TripalFieldDownloader::getHeader()
  106. */
  107. protected function getHeader() {
  108. }
  109. }