TripalProteinFASTADownloader.inc 4.0 KB

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