tripal_chado.fields.inc 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141
  1. <?php
  2. /**
  3. * Implements hook_bundle_fields_info().
  4. *
  5. * This is a Tripal defined hook that supports integration with the
  6. * TripalEntity field.
  7. */
  8. function tripal_chado_bundle_fields_info($entity_type, $bundle) {
  9. $chado_bundle = db_select('chado_bundle', 'cb')
  10. ->fields('cb')
  11. ->condition('bundle_id', $bundle->id)
  12. ->execute()
  13. ->fetchObject();
  14. // Get the details about the mapping of this bundle to the Chado table:
  15. $details = array(
  16. 'chado_cvterm_id' => $chado_bundle->type_id,
  17. 'chado_table' => $chado_bundle->data_table,
  18. 'chado_type_table' => $chado_bundle->type_linker_table,
  19. 'chado_type_column' => $chado_bundle->type_column,
  20. );
  21. $info = array();
  22. // Create the fields for each column in the table.
  23. tripal_chado_bundle_fields_info_base($info, $details, $entity_type, $bundle);
  24. // Create custom fields.
  25. tripal_chado_bundle_fields_info_custom($info, $details, $entity_type, $bundle);
  26. // Create fields for linking tables.
  27. tripal_chado_bundle_fields_info_linker($info, $details, $entity_type, $bundle);
  28. return $info;
  29. }
  30. /**
  31. *
  32. * @param unknown $details
  33. */
  34. function tripal_chado_bundle_fields_info_base(&$info, $details, $entity_type, $bundle) {
  35. $table_name = $details['chado_table'];
  36. $type_table = $details['chado_type_table'];
  37. $type_field = $details['chado_type_column'];
  38. $cvterm_id = $details['chado_cvterm_id'];
  39. // Iterate through the columns of the table and see if fields have been
  40. // created for each one. If not, then create them.
  41. $schema = chado_get_schema($table_name);
  42. if (!$schema) {
  43. return;
  44. }
  45. $pkey = $schema['primary key'][0];
  46. // Get the list of columns for this table and create a new field for each one.
  47. $columns = $schema['fields'];
  48. foreach ($columns as $column_name => $details) {
  49. // Skip the source columns in the analysis table. We have a custom
  50. // field for those columns
  51. if ($table_name == 'analysis' and ($column_name == 'sourceuri' or
  52. $column_name == 'sourceversion' or $column_name == 'sourcename')) {
  53. continue;
  54. }
  55. // Skip the cvterm.is_relationshptype.
  56. if ($table_name == 'cvterm' and $column_name == 'is_relationshiptype') {
  57. continue;
  58. }
  59. // Don't create base fields for the primary key and the type_id field.
  60. if ($column_name == $pkey or $column_name == $type_field) {
  61. continue;
  62. }
  63. $cvterm = tripal_get_chado_semweb_term($table_name, $column_name, array('return_object' => TRUE));
  64. if (!$cvterm) {
  65. tripal_report_error('tripal', TRIPAL_ERROR,
  66. 'Cannot create field for "%table_name.%column_name". Missing an appropriate vocabulary term',
  67. array('%table_name' => $table_name, '%column_name' => $column_name));
  68. drupal_set_message(t('Cannot create field for "%table_name.%column_name". Missing an appropriate vocabulary term',
  69. array('%table_name' => $table_name, '%column_name' => $column_name)), 'error');
  70. continue;
  71. }
  72. $field_name = strtolower($cvterm->dbxref_id->db_id->name . '__' . preg_replace('/ /', '_', $cvterm->name));
  73. $field_name = substr($field_name, 0, 32);
  74. // Skip the primary key field.
  75. if ($column_name == $schema['primary key'][0]) {
  76. continue;
  77. }
  78. // Skip the type field that will always be custom
  79. if (($table_name == $type_table and $column_name == $type_field) or
  80. $column_name == 'type_id') {
  81. continue;
  82. }
  83. // Set some defaults for the field.
  84. $base_info = array(
  85. 'field_name' => $field_name,
  86. 'type' => '',
  87. 'cardinality' => 1,
  88. 'locked' => FALSE,
  89. 'storage' => array(
  90. 'type' => 'field_chado_storage',
  91. 'sql' => array(),
  92. ),
  93. );
  94. // Alter the field info array depending on the column details.
  95. switch($details['type']) {
  96. case 'char':
  97. $base_info['type'] = 'text';
  98. $base_info['settings']['max_length'] = $details['length'];
  99. break;
  100. case 'varchar':
  101. $base_info['type'] = 'text';
  102. $base_info['settings']['max_length'] = $details['length'];
  103. break;
  104. case 'text':
  105. $base_info['type'] = 'text';
  106. $base_info['settings']['max_length'] = 17179869184;
  107. $base_info['settings']['text_processing'] = 1;
  108. break;
  109. case 'blob':
  110. // not sure how to support a blob field.
  111. continue;
  112. break;
  113. case 'int':
  114. $base_info['type'] = 'number_integer';
  115. break;
  116. case 'float':
  117. $base_info['type'] = 'number_float';
  118. $base_info['settings']['precision'] = 10;
  119. $base_info['settings']['scale'] = 2;
  120. $base_info['settings']['decimal_separator'] = '.';
  121. break;
  122. case 'numeric':
  123. $base_info['type'] = 'number_decimal';
  124. break;
  125. case 'serial':
  126. // Serial fields are most likely not needed as a field.
  127. break;
  128. case 'boolean':
  129. $base_info['type'] = 'list_boolean';
  130. $base_info['settings']['allowed_values'] = array(0 => "No", 1 => "Yes");
  131. break;
  132. case 'datetime':
  133. // Use the Drupal Date and Date API to create the field/widget
  134. $base_info['type'] = 'datetime';
  135. break;
  136. }
  137. // Set some default semantic web information
  138. if ($column_name == 'uniquename') {
  139. $base_info['settings']['text_processing'] = 0;
  140. }
  141. //
  142. // PUB TABLE
  143. //
  144. elseif ($table_name == 'pub' and $column_name == 'uniquename') {
  145. $base_info['type'] = 'text';
  146. $base_info['settings']['text_processing'] = 0;
  147. }
  148. $info[$field_name] = $base_info;
  149. }
  150. }
  151. /**
  152. *
  153. * @param unknown $details
  154. */
  155. function tripal_chado_bundle_fields_info_custom(&$info, $details, $entity_type, $bundle) {
  156. $table_name = $details['chado_table'];
  157. $type_table = $details['chado_type_table'];
  158. $type_field = $details['chado_type_column'];
  159. $cvterm_id = $details['chado_cvterm_id'];
  160. $schema = chado_get_schema($table_name);
  161. // BASE TYPE_ID
  162. // Exclude the type_id field on base tables where content types are mapped
  163. // by the type_id field or on the organism table where the type_id is mean
  164. // as an infraspecific name type.
  165. if (array_key_exists('type_id', $schema['fields']) and
  166. $table_name != 'organism' and $type_field != 'type_id') {
  167. $field_name = 'schema__additional_type';
  168. $field_type = 'schema__additional_type';
  169. $info[$field_name] = array(
  170. 'field_name' => $field_name,
  171. 'type' => $field_type,
  172. 'cardinality' => 1,
  173. 'locked' => FALSE,
  174. 'storage' => array(
  175. 'type' => 'field_chado_storage',
  176. ),
  177. );
  178. }
  179. // BASE ORGANISM_ID
  180. if ($table_name != 'organism' and
  181. array_key_exists('organism_id', $schema['fields'])) {
  182. $field_name = 'obi__organism';
  183. $field_type = 'obi__organism';
  184. $info[$field_name] = array(
  185. 'field_name' => $field_name,
  186. 'type' => $field_type,
  187. 'cardinality' => 1,
  188. 'locked' => FALSE,
  189. 'storage' => array(
  190. 'type' => 'field_chado_storage',
  191. ),
  192. );
  193. }
  194. // BASE ORGANISM_ID
  195. if ($table_name == 'cvterm') {
  196. $field_name = 'sio__vocabulary';
  197. $field_type = 'sio__vocabulary';
  198. $info[$field_name] = array(
  199. 'field_name' => $field_name,
  200. 'type' => $field_type,
  201. 'cardinality' => 1,
  202. 'locked' => FALSE,
  203. 'storage' => array(
  204. 'type' => 'field_chado_storage',
  205. ),
  206. );
  207. }
  208. // BASE DBXREF
  209. if (array_key_exists('dbxref_id', $schema['fields'])) {
  210. $field_name = 'data__accession';
  211. $field_type = 'data__accession';
  212. $info[$field_name] = array(
  213. 'field_name' => $field_name,
  214. 'type' => $field_type,
  215. 'cardinality' => 1,
  216. 'locked' => FALSE,
  217. 'storage' => array(
  218. 'type' => 'field_chado_storage',
  219. ),
  220. );
  221. }
  222. // FEATURE MD5CHECKSUM
  223. if ($table_name == 'feature') {
  224. $field_name = 'data__sequence_checksum';
  225. $field_type = 'data__sequence_checksum';
  226. $info[$field_name] = array(
  227. 'field_name' => $field_name,
  228. 'type' => $field_type,
  229. 'cardinality' => 1,
  230. 'locked' => FALSE,
  231. 'storage' => array(
  232. 'type' => 'field_chado_storage',
  233. ),
  234. );
  235. }
  236. // FEATURE RESIDUES
  237. if ($table_name == 'feature') {
  238. $field_name = 'data__sequence';
  239. $field_type = 'data__sequence';
  240. $info[$field_name] = array(
  241. 'field_name' => $field_name,
  242. 'type' => $field_type,
  243. 'cardinality' => 1,
  244. 'locked' => FALSE,
  245. 'storage' => array(
  246. 'type' => 'field_chado_storage',
  247. ),
  248. );
  249. }
  250. // FEATURE SEQLEN
  251. if ($table_name == 'feature') {
  252. $field_name = 'data__sequence_length';
  253. $field_type = 'data__sequence_length';
  254. $info[$field_name] = array(
  255. 'field_name' => $field_name,
  256. 'type' => $field_type,
  257. 'cardinality' => 1,
  258. 'locked' => FALSE,
  259. 'storage' => array(
  260. 'type' => 'field_chado_storage',
  261. ),
  262. );
  263. }
  264. // PROTEIN & CDS SEQUENCE
  265. if ($table_name == 'feature' and
  266. ($bundle->label == 'mRNA' or $bundle->label == 'transcript')) {
  267. $field_name = 'data__protein_sequence';
  268. $field_type = 'data__protein_sequence';
  269. $info[$field_name] = array(
  270. 'field_name' => $field_name,
  271. 'type' => $field_type,
  272. 'cardinality' => 1,
  273. 'locked' => FALSE,
  274. 'storage' => array(
  275. 'type' => 'field_chado_storage',
  276. ),
  277. );
  278. $field_name = 'so__cds';
  279. $field_type = 'so__cds';
  280. $info[$field_name] = array(
  281. 'field_name' => $field_name,
  282. 'type' => $field_type,
  283. 'cardinality' => 1,
  284. 'locked' => FALSE,
  285. 'storage' => array(
  286. 'type' => 'field_chado_storage',
  287. ),
  288. );
  289. }
  290. // GENE TRANSCRIPTS
  291. $rel_table = $table_name . '_relationship';
  292. if (chado_table_exists($rel_table) and $bundle->label == 'gene') {
  293. $field_name = 'so__transcript';
  294. $field_type = 'so__transcript';
  295. $info[$field_name] = array(
  296. 'field_name' => $field_name,
  297. 'type' => $field_type,
  298. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  299. 'locked' => FALSE,
  300. 'storage' => array(
  301. 'type' => 'field_chado_storage',
  302. ),
  303. );
  304. }
  305. // ORGANISM TYPE_ID
  306. // if ($table_name == 'organism' and array_key_exists('type_id', $schema['fields'])) {
  307. // $field_name = 'taxarank__infraspecific_taxon';
  308. // $field_type = 'taxarank__infraspecific_taxon';
  309. // $info[$field_name] = array(
  310. // 'field_name' => $field_name,
  311. // 'type' => $field_type,
  312. // 'cardinality' => 1,
  313. // 'locked' => FALSE,
  314. // 'storage' => array(
  315. // 'type' => 'field_chado_storage',
  316. // ),
  317. // 'settings' => array(
  318. // ),
  319. // );
  320. // }
  321. // FEATUREMAP UNITTYPE_ID
  322. if ($table_name == 'featuremap') {
  323. $field_name = 'uo__unit';
  324. $field_type = 'uo__unit';
  325. $info[$field_name] = array(
  326. 'field_name' => $field_name,
  327. 'type' => $field_type,
  328. 'cardinality' => 1,
  329. 'locked' => FALSE,
  330. 'storage' => array(
  331. 'type' => 'field_chado_storage',
  332. ),
  333. );
  334. }
  335. // Analysis source
  336. if ($table_name == 'analysis') {
  337. $field_name = 'local__source_data';
  338. $field_type = 'local__source_data';
  339. $info[$field_name] = array(
  340. 'field_name' => $field_name,
  341. 'type' => $field_type,
  342. 'cardinality' => 1,
  343. 'locked' => FALSE,
  344. 'storage' => array(
  345. 'type' => 'field_chado_storage',
  346. ),
  347. );
  348. }
  349. // Add an image field to the Organism type. This is a Drupal field and
  350. // not stored in Chado, but is used for backwards compatibility.
  351. if ($table_name == 'organism') {
  352. $field_name = 'data__image';
  353. $info[$field_name] = array(
  354. 'field_name' => $field_name,
  355. 'type' => 'image',
  356. 'cardinality' => 1,
  357. 'locked' => FALSE,
  358. 'storage' => array(
  359. 'type' => 'field_sql_storage',
  360. ),
  361. );
  362. }
  363. }
  364. /**
  365. *
  366. * @param unknown $details
  367. */
  368. function tripal_chado_bundle_fields_info_linker(&$info, $details, $entity_type, $bundle) {
  369. $table_name = $details['chado_table'];
  370. $type_table = $details['chado_type_table'];
  371. $type_field = $details['chado_type_column'];
  372. $cvterm_id = $details['chado_cvterm_id'];
  373. // CONTACTS
  374. $contact_table = $table_name . '_contact';
  375. if (chado_table_exists($contact_table)) {
  376. $schema = chado_get_schema($contact_table);
  377. $pkey = $schema['primary key'][0];
  378. $field_name = $table_name . '_contact';
  379. $field_type = 'chado_linker__contact';
  380. $info[$field_name] = array(
  381. 'field_name' => $field_name,
  382. 'type' => $field_type,
  383. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  384. 'locked' => FALSE,
  385. 'storage' => array(
  386. 'type' => 'field_chado_storage',
  387. ),
  388. );
  389. }
  390. // DBXREF
  391. $dbxref_table = $table_name . '_dbxref';
  392. if (chado_table_exists($dbxref_table)) {
  393. $field_name = 'sbo__database_cross_reference';
  394. $field_type = 'sbo__database_cross_reference';
  395. $info[$field_name] = array(
  396. 'field_name' => $field_name,
  397. 'type' => $field_type,
  398. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  399. 'locked' => FALSE,
  400. 'storage' => array(
  401. 'type' => 'field_chado_storage',
  402. ),
  403. );
  404. }
  405. // EXPRESSION
  406. // TODO: this should only show up on gene or mRNA bunldes, not every feature.
  407. // $expression_table = $table_name . '_expression';
  408. // if (chado_table_exists($expression_table)) {
  409. // $field_name = 'go__gene_expression';
  410. // $field_type = 'go__gene_expression';
  411. // $info[$field_name] = array(
  412. // 'field_name' => $field_name,
  413. // 'type' => $field_type,
  414. // 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  415. // 'locked' => FALSE,
  416. // 'storage' => array(
  417. // 'type' => 'field_chado_storage',
  418. // ),
  419. // );
  420. // }
  421. // FEATURELOC
  422. if ($table_name == 'feature') {
  423. $field_name = 'data__sequence_coordinates';
  424. $field_type = 'data__sequence_coordinates';
  425. $info[$field_name] = array(
  426. 'field_name' => $field_name,
  427. 'type' => $field_type,
  428. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  429. 'locked' => FALSE,
  430. 'storage' => array(
  431. 'type' => 'field_chado_storage',
  432. ),
  433. );
  434. }
  435. // FEATUREPOS
  436. if ($table_name == 'feature') {
  437. $field_name = 'ogi__location_on_map';
  438. $field_type = 'ogi__location_on_map';
  439. $info[$field_name] = array(
  440. 'field_name' => $field_name,
  441. 'type' => $field_type,
  442. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  443. 'locked' => FALSE,
  444. 'storage' => array(
  445. 'type' => 'field_chado_storage',
  446. ),
  447. );
  448. }
  449. // GENOTYPE
  450. $genotype_table = $table_name . '_genotype';
  451. if (chado_table_exists($genotype_table)) {
  452. $field_name = 'so__genotype';
  453. $field_type = 'so__genotype';
  454. $info[$field_name] = array(
  455. 'field_name' => $field_name,
  456. 'type' => $field_type,
  457. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  458. 'locked' => FALSE,
  459. 'storage' => array(
  460. 'type' => 'field_chado_storage',
  461. ),
  462. );
  463. }
  464. // PHENOTYPE
  465. $phenotype_table = $table_name . '_phenotype';
  466. if (chado_table_exists($phenotype_table)) {
  467. $field_name = 'sbo__phenotype';
  468. $field_type = 'sbo__phenotype';
  469. $info[$field_name] = array(
  470. 'field_name' => $field_name,
  471. 'type' => $field_type,
  472. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  473. 'locked' => FALSE,
  474. 'storage' => array(
  475. 'type' => 'field_chado_storage',
  476. ),
  477. );
  478. }
  479. // PROPERTIES
  480. $prop_table = $table_name . 'prop';
  481. if (chado_table_exists($prop_table)) {
  482. // Get the list of existing property types for this table.
  483. $sql = 'SELECT DISTINCT type_id FROM {' . $prop_table . '}';
  484. $props = chado_query($sql);
  485. while ($prop = $props->fetchObject()) {
  486. $term = chado_generate_var('cvterm', array('cvterm_id' => $prop->type_id));
  487. // The tripal_analysis_KEGG, tripal_analysis_blast, and
  488. // tripal_analysis_interpro modules store results in the analysisprop
  489. // table which is probably not the best place, but we don't want to
  490. // create a ton of fields for this.
  491. if ($prop_table == 'analysisprop' and
  492. ($term->dbxref_id->db_id->name == 'KEGG_BRITE' or
  493. $term->dbxref_id->db_id->name == 'tripal')) {
  494. continue;
  495. }
  496. $field_name = strtolower(preg_replace('/[^\w]/','_', $term->dbxref_id->db_id->name . '__' . $term->name));
  497. $field_name = substr($field_name, 0, 32);
  498. $field_type = 'chado_linker__prop';
  499. $info[$field_name] = array(
  500. 'field_name' => $field_name,
  501. 'type' => $field_type,
  502. 'cardinality' => 1,
  503. 'locked' => FALSE,
  504. 'storage' => array(
  505. 'type' => 'field_chado_storage',
  506. ),
  507. );
  508. }
  509. }
  510. // PUBLICATIONS
  511. $pub_table = $table_name . '_pub';
  512. if (chado_table_exists($pub_table)) {
  513. $field_name = 'schema__publication';
  514. $field_type = 'schema__publication';
  515. $info[$field_name] = array(
  516. 'field_name' => $field_name,
  517. 'type' => $field_type,
  518. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  519. 'locked' => FALSE,
  520. 'storage' => array(
  521. 'type' => 'field_chado_storage',
  522. ),
  523. );
  524. }
  525. // PUBLICATIONS (in reverse)
  526. // We want to be able to show all of the content that a publication links
  527. // to. The sio__references field does that.
  528. if ($table_name == 'pub') {
  529. $field_name = 'sio__references';
  530. $field_type = 'sio__references';
  531. $info[$field_name] = array(
  532. 'field_name' => $field_name,
  533. 'type' => $field_type,
  534. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  535. 'locked' => FALSE,
  536. 'storage' => array(
  537. 'type' => 'field_chado_storage',
  538. ),
  539. );
  540. }
  541. // RELATIONSHIPS
  542. // If the linker table does not exists then we don't want to add attach.
  543. $rel_table = $table_name . '_relationship';
  544. if (chado_table_exists($rel_table)) {
  545. $field_name = 'sbo__relationship';
  546. $field_type = 'sbo__relationship';
  547. $info[$field_name] = array(
  548. 'field_name' => $field_name,
  549. 'type' => $field_type,
  550. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  551. 'locked' => FALSE,
  552. 'storage' => array(
  553. 'type' => 'field_chado_storage',
  554. ),
  555. );
  556. }
  557. // SYNONYMS
  558. $syn_table = $table_name . '_synonym';
  559. if (chado_table_exists($syn_table)) {
  560. $field_name = 'schema__alternate_name';
  561. $field_type = 'schema__alternate_name';
  562. $info[$field_name] = array(
  563. 'field_name' => $field_name,
  564. 'type' => $field_type,
  565. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  566. 'locked' => FALSE,
  567. 'storage' => array(
  568. 'type' => 'field_chado_storage',
  569. ),
  570. 'settings' => array(
  571. ),
  572. );
  573. }
  574. }
  575. /**
  576. * Impelments hook_create_tripalfield_instance().
  577. *
  578. * This is a Tripal defined hook that supports integration with the
  579. * TripalEntity field.
  580. */
  581. function tripal_chado_bundle_instances_info($entity_type, $bundle) {
  582. $chado_bundle = db_select('chado_bundle', 'cb')
  583. ->fields('cb')
  584. ->condition('bundle_id', $bundle->id)
  585. ->execute()
  586. ->fetchObject();
  587. $details = array(
  588. 'chado_cvterm_id' => $chado_bundle->type_id,
  589. 'chado_table' => $chado_bundle->data_table,
  590. 'chado_type_table' => $chado_bundle->type_linker_table,
  591. 'chado_type_column' => $chado_bundle->type_column,
  592. );
  593. $info = array();
  594. tripal_chado_bundle_instances_info_base($info, $entity_type, $bundle, $details);
  595. tripal_chado_bundle_instances_info_custom($info, $entity_type, $bundle, $details);
  596. tripal_chado_bundle_instances_info_linker($info, $entity_type, $bundle, $details);
  597. return $info;
  598. }
  599. /**
  600. * Helper function for the hook_create_tripalfield_instance().
  601. *
  602. * Add the field instances that corresond to the columns of the base table.
  603. *
  604. * @param $entity_type
  605. * @param $bundle
  606. * @param $details
  607. */
  608. function tripal_chado_bundle_instances_info_base(&$info, $entity_type, $bundle, $details) {
  609. $fields = array();
  610. // Get Chado information
  611. $table_name = $details['chado_table'];
  612. $type_table = $details['chado_type_table'];
  613. $type_field = $details['chado_type_column'];
  614. $cvterm_id = $details['chado_cvterm_id'];
  615. // Iterate through the columns of the table and see if fields have been
  616. // created for each one. If not, then create them.
  617. $schema = chado_get_schema($table_name);
  618. if (!$schema) {
  619. return;
  620. }
  621. $pkey = $schema['primary key'][0];
  622. $columns = $schema['fields'];
  623. foreach ($columns as $column_name => $details) {
  624. // Skip the source columns in the analysis table. We have a custom
  625. // field for those columns
  626. if ($table_name == 'analysis' and ($column_name == 'sourceuri' or
  627. $column_name == 'sourceversion' or $column_name == 'sourcename')) {
  628. continue;
  629. }
  630. // Don't create base fields for the primary key and the type_id field.
  631. if ($column_name == $pkey or $column_name == $type_field) {
  632. continue;
  633. }
  634. // Skip the type field that will always be custom
  635. if (($table_name == $type_table and $column_name == $type_field) or
  636. $column_name == 'type_id') {
  637. continue;
  638. }
  639. $cvterm = tripal_get_chado_semweb_term($table_name, $column_name, array('return_object' => TRUE));
  640. if (!$cvterm) {
  641. // We already provided an error when creating the base field. So
  642. // don't create another one here.
  643. continue;
  644. }
  645. $field_name = strtolower($cvterm->dbxref_id->db_id->name . '__' . preg_replace('/ /', '_', $cvterm->name));
  646. $field_name = substr($field_name, 0, 32);
  647. // Skip the primary key field.
  648. if ($column_name == $schema['primary key'][0]) {
  649. continue;
  650. }
  651. // Skip the type field.
  652. if ($table_name == $type_table and $column_name == $type_field) {
  653. continue;
  654. }
  655. $base_info = array(
  656. 'field_name' => $field_name,
  657. 'entity_type' => 'TripalEntity',
  658. 'bundle' => $bundle->name,
  659. 'label' => ucwords(preg_replace('/_/', ' ', $column_name)),
  660. 'description' => '',
  661. 'required' => FALSE,
  662. 'settings' => array(
  663. 'auto_attach' => TRUE,
  664. 'term_vocabulary' => $cvterm->dbxref_id->db_id->name,
  665. 'term_name' => $cvterm->name,
  666. 'term_accession' => $cvterm->dbxref_id->accession,
  667. 'chado_table' => $table_name,
  668. 'chado_column' => $column_name,
  669. 'base_table' => $table_name,
  670. ),
  671. 'widget' => array(
  672. 'settings' => array(
  673. 'display_label' => 1,
  674. ),
  675. ),
  676. 'display' => array(
  677. 'default' => array(
  678. 'label' => 'inline',
  679. 'region' =>'Left',
  680. 'settings' => array(),
  681. ),
  682. ),
  683. );
  684. // Determine if the field is required.
  685. if (array_key_exists('not null', $details) and $details['not null'] === TRUE) {
  686. $base_info['required'] = TRUE;
  687. }
  688. // Alter the field info array depending on the column details.
  689. switch($details['type']) {
  690. case 'char':
  691. $base_info['widget']['type'] = 'text_textfield';
  692. break;
  693. case 'varchar':
  694. $base_info['widget']['type'] = 'text_textfield';
  695. break;
  696. case 'text':
  697. $base_info['display']['default']['label'] = 'above';
  698. $base_info['widget']['type'] = 'text_textarea';
  699. $base_info['settings']['text_processing'] = '1';
  700. $base_info['settings']['format'] = 'full_html';
  701. break;
  702. case 'blob':
  703. // not sure how to support a blob field.
  704. continue;
  705. break;
  706. case 'int':
  707. $base_info['widget']['type'] = 'number';
  708. break;
  709. case 'float':
  710. $base_info['widget']['type'] = 'number';
  711. break;
  712. case 'numeric':
  713. $base_info['widget']['type'] = 'number';
  714. break;
  715. case 'serial':
  716. // Serial fields are most likely not needed as a field.
  717. break;
  718. case 'boolean':
  719. $base_info['widget']['type'] = 'options_onoff';
  720. $base_info['required'] = FALSE;
  721. break;
  722. case 'datetime':
  723. $base_info['widget']['type'] = 'date_select';
  724. $base_info['widget']['settings']['increment'] = 1;
  725. $base_info['widget']['settings']['tz_handling'] = 'none';
  726. $base_info['widget']['settings']['collapsible'] = TRUE;
  727. // TODO: Add settings so that the minutes increment by 1.
  728. // And turn off the timezone, as the Chado field doesn't support it.
  729. break;
  730. }
  731. // Set some default semantic web information
  732. if ($column_name == 'uniquename') {
  733. $base_info['label'] = 'Identifier';
  734. $base_info['widget_type'] = 'text_textfield';
  735. }
  736. elseif ($base_info['label'] == 'Timeaccessioned') {
  737. $base_info['label'] = 'Time Accessioned';
  738. $base_info['description'] = 'Please enter the time that this record was first added to the database.';
  739. }
  740. elseif ($base_info['label'] == 'Timelastmodified') {
  741. $base_info['label'] = 'Time Last Modified';
  742. $base_info['description'] = 'Please enter the time that this record was last modified. The default is the current time.';
  743. }
  744. //
  745. // ORGANISM TABLE
  746. //
  747. elseif ($table_name == 'organism' and $column_name == 'comment') {
  748. $base_info['label'] = 'Description';
  749. }
  750. //
  751. // PUB TABLE
  752. //
  753. elseif ($table_name == 'pub' and $column_name == 'uniquename') {
  754. $base_info['widget_type'] = 'text_textfield';
  755. }
  756. //
  757. // ANALYSIS TABLE
  758. //
  759. elseif ($table_name == 'analysis' and $column_name == 'program') {
  760. $base_info['description'] = 'The program name (e.g. blastx, blastp, sim4, genscan. If the analysis was not derived from a software package then provide a very brief description of the pipeline, workflow or method.';
  761. $base_info['label'] = 'Program, Pipeline, Workflow or Method Name';
  762. }
  763. elseif ($table_name == 'analysis' and $column_name == 'algorithm') {
  764. $base_info['label'] = 'Source Version';
  765. $base_info['description'] = 'The name of the algorithm used to produce the dataset if different from the program.';
  766. }
  767. elseif ($table_name == 'analysis' and $column_name == 'programversion') {
  768. $base_info['label'] = 'Program Version';
  769. $base_info['description'] = 'The version of the program used to perform this analysis. (e.g. TBLASTX 2.0MP-WashU [09-Nov-2000]. Enter "n/a" if no version is available or applicable.';
  770. }
  771. if ($table_name == 'analysis' and ($column_name == 'sourceuri' or
  772. $column_name == 'sourceversion' or $column_name == 'sourcename')) {
  773. // Skip the source columns in the analysis table. We have a custom
  774. // field for those columns
  775. continue;
  776. }
  777. //
  778. // PROJECT TABLE
  779. //
  780. elseif ($table_name == 'project' and $column_name == 'description') {
  781. $base_info['label'] = 'Short Description';
  782. }
  783. $info[$field_name] = $base_info;
  784. }
  785. }
  786. /**
  787. * Helper function for the hook_create_tripalfield_instance().
  788. *
  789. * Adds custom fields for base fields. These override the settings provided
  790. * in the tripal_chado_create_tripalfield_instance_base() function.
  791. *
  792. * @param $entity_type
  793. * @param $bundle
  794. * @param $details
  795. */
  796. function tripal_chado_bundle_instances_info_custom(&$info, $entity_type, $bundle, $details) {
  797. $table_name = $details['chado_table'];
  798. $type_table = $details['chado_type_table'];
  799. $type_field = $details['chado_type_column'];
  800. $cvterm_id = $details['chado_cvterm_id'];
  801. $schema = chado_get_schema($table_name);
  802. // BASE TYPE_ID
  803. // Exclude the type_id field on base tables where content types are mapped
  804. // by the type_id field or on the organism table where the type_id is mean
  805. // as an infraspecific name type.
  806. if (array_key_exists('type_id', $schema['fields']) and
  807. $table_name != 'organism' and $type_field != 'type_id') {
  808. $field_name = 'schema__additional_type';
  809. $is_required = FALSE;
  810. if (array_key_exists('not null', $schema['fields']['type_id']) and
  811. $schema['fields']['type_id']['not null']) {
  812. $is_required = TRUE;
  813. }
  814. $info[$field_name] = array(
  815. 'field_name' => $field_name,
  816. 'entity_type' => $entity_type,
  817. 'bundle' => $bundle->name,
  818. 'label' => 'Type',
  819. 'description' => 'Select a type for this record.',
  820. 'required' => $is_required,
  821. 'settings' => array(
  822. 'auto_attach' => TRUE,
  823. 'chado_table' => $table_name,
  824. 'chado_column' => 'type_id',
  825. 'base_table' => $table_name,
  826. ),
  827. 'widget' => array(
  828. 'type' => 'schema__additional_type_widget',
  829. 'settings' => array(
  830. 'display_label' => 1,
  831. ),
  832. ),
  833. 'display' => array(
  834. 'default' => array(
  835. 'label' => 'inline',
  836. 'type' => 'schema__additional_type_formatter',
  837. 'settings' => array(),
  838. ),
  839. ),
  840. );
  841. }
  842. // BASE ORGANISM_ID
  843. if ($table_name != 'organism' and array_key_exists('organism_id', $schema['fields'])) {
  844. $field_name = 'obi__organism';
  845. $is_required = FALSE;
  846. if (array_key_exists('not null', $schema['fields']['organism_id']) and
  847. $schema['fields']['organism_id']['not null']) {
  848. $is_required = TRUE;
  849. }
  850. $info[$field_name] = array(
  851. 'field_name' => $field_name,
  852. 'entity_type' => $entity_type,
  853. 'bundle' => $bundle->name,
  854. 'label' => 'Organism',
  855. 'description' => 'Select an organism.',
  856. 'required' => $is_required,
  857. 'settings' => array(
  858. 'auto_attach' => TRUE,
  859. 'chado_table' => $table_name,
  860. 'chado_column' => 'organism_id',
  861. 'base_table' => $table_name,
  862. ),
  863. 'widget' => array(
  864. 'type' => 'obi__organism_widget',
  865. 'settings' => array(
  866. 'display_label' => 1,
  867. ),
  868. ),
  869. 'display' => array(
  870. 'default' => array(
  871. 'label' => 'inline',
  872. 'type' => 'obi__organism_formatter',
  873. 'settings' => array(),
  874. ),
  875. ),
  876. );
  877. }
  878. // BASE ORGANISM_ID
  879. if ($table_name == 'cvterm') {
  880. $field_name = 'sio__vocabulary';
  881. $is_required = TRUE;
  882. $info[$field_name] = array(
  883. 'field_name' => $field_name,
  884. 'entity_type' => $entity_type,
  885. 'bundle' => $bundle->name,
  886. 'label' => 'Vocabulary',
  887. 'description' => 'Select a vocabulary.',
  888. 'required' => $is_required,
  889. 'settings' => array(
  890. 'auto_attach' => TRUE,
  891. 'chado_table' => $table_name,
  892. 'chado_column' => 'cv_id',
  893. 'base_table' => $table_name,
  894. ),
  895. 'widget' => array(
  896. 'type' => 'sio__vocabulary_widget',
  897. 'settings' => array(
  898. 'display_label' => 1,
  899. ),
  900. ),
  901. 'display' => array(
  902. 'default' => array(
  903. 'label' => 'inline',
  904. 'type' => 'sio__vocabulary_formatter',
  905. 'settings' => array(),
  906. ),
  907. ),
  908. );
  909. }
  910. // BASE DBXREF
  911. if (array_key_exists('dbxref_id', $schema['fields'])) {
  912. $field_name = 'data__accession';
  913. $info[$field_name] = array(
  914. 'field_name' => $field_name,
  915. 'entity_type' => $entity_type,
  916. 'bundle' => $bundle->name,
  917. 'label' => 'Accession',
  918. 'description' => 'This field specifies the unique stable accession (ID) for
  919. this record. It requires that this site have a database entry.',
  920. 'required' => FALSE,
  921. 'settings' => array(
  922. 'auto_attach' => TRUE,
  923. 'chado_table' => $table_name,
  924. 'chado_column' => 'dbxref_id',
  925. 'base_table' => $table_name,
  926. ),
  927. 'widget' => array(
  928. 'type' => 'data__accession_widget',
  929. 'settings' => array(
  930. 'display_label' => 1,
  931. ),
  932. ),
  933. 'display' => array(
  934. 'default' => array(
  935. 'label' => 'inline',
  936. 'type' => 'data__accession_formatter',
  937. 'settings' => array(),
  938. ),
  939. ),
  940. );
  941. }
  942. // FEATURE MD5CHECKSUM
  943. if ($table_name == 'feature') {
  944. $field_name = 'data__sequence_checksum';
  945. $info[$field_name] = array(
  946. 'field_name' => $field_name,
  947. 'entity_type' => $entity_type,
  948. 'bundle' => $bundle->name,
  949. 'label' => 'Sequence Checksum',
  950. 'description' => 'The MD5 checksum for the sequence. The checksum here
  951. will always be unique for the raw unformatted sequence. To verify that the
  952. sequence has not been corrupted, download the raw sequence and use an MD5 tool
  953. to calculate the value. If the value calculated is identical the one shown
  954. here, then the downloaded sequence is uncorrupted.',
  955. 'required' => FALSE,
  956. 'settings' => array(
  957. 'auto_attach' => TRUE,
  958. 'chado_table' => $table_name,
  959. 'chado_column' => 'md5checksum',
  960. 'base_table' => $table_name,
  961. ),
  962. 'widget' => array(
  963. 'type' => 'data__sequence_checksum_widget',
  964. 'settings' => array(
  965. 'display_label' => 1,
  966. 'md5_fieldname' => 'feature__md5checksum',
  967. ),
  968. ),
  969. 'display' => array(
  970. 'default' => array(
  971. 'label' => 'inline',
  972. 'type' => 'data__sequence_checksum_formatter',
  973. 'settings' => array(),
  974. ),
  975. ),
  976. );
  977. }
  978. // FEATURE RESIDUES
  979. if ($table_name == 'feature') {
  980. $field_name = 'data__sequence';
  981. $info[$field_name] = array(
  982. 'field_name' => $field_name,
  983. 'entity_type' => $entity_type,
  984. 'bundle' => $bundle->name,
  985. 'label' => 'Sequence',
  986. 'description' => 'All available sequences for this record.',
  987. 'required' => FALSE,
  988. 'settings' => array(
  989. 'auto_attach' => FALSE,
  990. 'chado_table' => $table_name,
  991. 'chado_column' => 'residues',
  992. 'base_table' => $table_name,
  993. ),
  994. 'widget' => array(
  995. 'type' => 'data__sequence_widget',
  996. 'settings' => array(
  997. 'display_label' => 1,
  998. ),
  999. ),
  1000. 'display' => array(
  1001. 'default' => array(
  1002. 'label' => 'above',
  1003. 'type' => 'data__sequence_formatter',
  1004. 'settings' => array(),
  1005. ),
  1006. ),
  1007. );
  1008. }
  1009. // FEATURE SEQLEN
  1010. if ($table_name == 'feature') {
  1011. $field_name = 'data__sequence_length';
  1012. $info[$field_name] = array(
  1013. 'field_name' => $field_name,
  1014. 'entity_type' => $entity_type,
  1015. 'bundle' => $bundle->name,
  1016. 'label' => 'Sequence Length',
  1017. 'description' => 'The number of residues in the raw sequence. This length
  1018. is only for the assigned raw sequence and does not represent the length of any
  1019. sequences derived from alignments. If this value is zero but aligned sequences
  1020. are present then this record has no official assigned sequence.',
  1021. 'required' => FALSE,
  1022. 'settings' => array(
  1023. 'auto_attach' => TRUE,
  1024. 'chado_table' => $table_name,
  1025. 'chado_column' => 'seqlen',
  1026. 'base_table' => $table_name,
  1027. ),
  1028. 'widget' => array(
  1029. 'type' => 'data__sequence_length_widget',
  1030. 'settings' => array(
  1031. 'display_label' => 1,
  1032. ),
  1033. ),
  1034. 'display' => array(
  1035. 'default' => array(
  1036. 'label' => 'inline',
  1037. 'type' => 'data__sequence_length_formatter',
  1038. 'settings' => array(),
  1039. ),
  1040. ),
  1041. );
  1042. }
  1043. // PROTEIN & CDS SEQUENCES.
  1044. if ($table_name == 'feature' and
  1045. ($bundle->label == 'mRNA' or $bundle->label == 'transcript')) {
  1046. $field_name = 'data__protein_sequence';
  1047. $info[$field_name] = array(
  1048. 'field_name' => $field_name,
  1049. 'entity_type' => $entity_type,
  1050. 'bundle' => $bundle->name,
  1051. 'label' => 'Protein Sequence',
  1052. 'description' => 'Protein sequences.',
  1053. 'required' => FALSE,
  1054. 'settings' => array(
  1055. 'auto_attach' => FALSE,
  1056. 'chado_table' => 'feature',
  1057. 'chado_column' => 'residues',
  1058. 'base_table' => 'feature',
  1059. ),
  1060. 'widget' => array(
  1061. 'type' => 'data__protein_sequence_widget',
  1062. 'settings' => array(
  1063. 'display_label' => 1,
  1064. ),
  1065. ),
  1066. 'display' => array(
  1067. 'default' => array(
  1068. 'label' => 'above',
  1069. 'type' => 'data__protein_sequence_formatter',
  1070. 'settings' => array(),
  1071. ),
  1072. ),
  1073. );
  1074. $field_name = 'so__cds';
  1075. $info[$field_name] = array(
  1076. 'field_name' => $field_name,
  1077. 'entity_type' => $entity_type,
  1078. 'bundle' => $bundle->name,
  1079. 'label' => 'Coding Sequence',
  1080. 'description' => 'Coding sequences.',
  1081. 'required' => FALSE,
  1082. 'settings' => array(
  1083. 'auto_attach' => FALSE,
  1084. 'chado_table' => 'featureloc',
  1085. 'chado_column' => '',
  1086. 'base_table' => 'featureloc',
  1087. ),
  1088. 'widget' => array(
  1089. 'type' => 'so__cds_widget',
  1090. 'settings' => array(
  1091. 'display_label' => 1,
  1092. ),
  1093. ),
  1094. 'display' => array(
  1095. 'default' => array(
  1096. 'label' => 'above',
  1097. 'type' => 'so__cds_formatter',
  1098. 'settings' => array(),
  1099. ),
  1100. ),
  1101. );
  1102. }
  1103. // GENE TRANSCRIPTS
  1104. $rel_table = $table_name . '_relationship';
  1105. if (chado_table_exists($rel_table) and $bundle->label == 'gene') {
  1106. $field_name = 'so__transcript';
  1107. $info[$field_name] = array(
  1108. 'field_name' => $field_name,
  1109. 'entity_type' => $entity_type,
  1110. 'bundle' => $bundle->name,
  1111. 'label' => 'Transcripts',
  1112. 'description' => 'Transcripts that are part of this gene.',
  1113. 'required' => FALSE,
  1114. 'settings' => array(
  1115. 'auto_attach' => FALSE,
  1116. 'chado_table' => $rel_table,
  1117. 'chado_column' => '',
  1118. 'base_table' => $table_name,
  1119. ),
  1120. 'widget' => array(
  1121. 'type' => 'so__transcript_widget',
  1122. 'settings' => array(
  1123. 'display_label' => 1,
  1124. ),
  1125. ),
  1126. 'display' => array(
  1127. 'default' => array(
  1128. 'label' => 'above',
  1129. 'type' => 'so__transcript_formatter',
  1130. 'settings' => array(),
  1131. ),
  1132. ),
  1133. );
  1134. }
  1135. // ORGANISM TYPE_ID
  1136. // if ($table_name == 'organism' and array_key_exists('type_id', $schema['fields'])) {
  1137. // $field_name = 'taxarank__infraspecific_taxon';
  1138. // $info[$field_name] = array(
  1139. // 'field_name' => $field_name,
  1140. // 'entity_type' => $entity_type,
  1141. // 'bundle' => $bundle->name,
  1142. // 'label' => 'Infraspecific Taxon',
  1143. // 'description' => 'The Infraspecific Taxon.',
  1144. // 'required' => FALSE,
  1145. // 'settings' => array(
  1146. // 'auto_attach' => TRUE,
  1147. // 'chado_table' => 'organism',
  1148. // 'chado_column' => 'type_id',
  1149. // 'base_table' => 'organism',
  1150. // ),
  1151. // 'widget' => array(
  1152. // 'type' => 'taxarank__infraspecific_taxon_widget',
  1153. // 'settings' => array(
  1154. // 'display_label' => 1,
  1155. // ),
  1156. // ),
  1157. // 'display' => array(
  1158. // 'default' => array(
  1159. // 'label' => 'inline',
  1160. // 'type' => 'taxarank__infraspecific_taxon_formatter',
  1161. // 'settings' => array(),
  1162. // ),
  1163. // ),
  1164. // );
  1165. // }
  1166. // FEATURE SEQLEN
  1167. if ($table_name == 'featuremap') {
  1168. $field_name = 'uo__unit';
  1169. $info[$field_name] = array(
  1170. 'field_name' => $field_name,
  1171. 'entity_type' => $entity_type,
  1172. 'bundle' => $bundle->name,
  1173. 'label' => 'Units',
  1174. 'description' => 'The map\'s unit type.',
  1175. 'required' => TRUE,
  1176. 'settings' => array(
  1177. 'auto_attach' => TRUE,
  1178. 'chado_table' => $table_name,
  1179. 'chado_column' => 'unittype_id',
  1180. 'base_table' => $table_name,
  1181. ),
  1182. 'widget' => array(
  1183. 'type' => 'uo__unit_widget',
  1184. 'settings' => array(
  1185. 'display_label' => 1,
  1186. ),
  1187. ),
  1188. 'display' => array(
  1189. 'default' => array(
  1190. 'label' => 'inline',
  1191. 'type' => 'uo__unit_formatter',
  1192. 'settings' => array(),
  1193. ),
  1194. ),
  1195. );
  1196. }
  1197. // the analysis source.
  1198. if ($table_name == 'analysis') {
  1199. $field_name = 'local__source_data';
  1200. $info[$field_name] = array(
  1201. 'field_name' => $field_name,
  1202. 'entity_type' => $entity_type,
  1203. 'bundle' => $bundle->name,
  1204. 'label' => 'Data Source',
  1205. 'description' => 'The source where data was obtained for this analysis.',
  1206. 'required' => FALSE,
  1207. 'settings' => array(
  1208. 'auto_attach' => TRUE,
  1209. 'chado_table' => $table_name,
  1210. 'chado_column' => 'analysis_id',
  1211. 'base_table' => $table_name,
  1212. ),
  1213. 'widget' => array(
  1214. 'type' => 'local__source_data_widget',
  1215. 'settings' => array(
  1216. 'display_label' => 1,
  1217. ),
  1218. ),
  1219. 'display' => array(
  1220. 'default' => array(
  1221. 'label' => 'inline',
  1222. 'type' => 'local__source_data_formatter',
  1223. 'settings' => array(),
  1224. ),
  1225. ),
  1226. );
  1227. }
  1228. // Add an image field to the Organism type. This is a Drupal field and
  1229. // not stored in Chado, but is used for backwards compatibility.
  1230. if ($table_name == 'organism') {
  1231. $field_name = 'data__image';
  1232. $info[$field_name] = array(
  1233. 'field_name' => $field_name,
  1234. 'entity_type' => $entity_type,
  1235. 'bundle' => $bundle->name,
  1236. 'label' => 'Organism Image',
  1237. 'description' => 'An image for the organism',
  1238. 'required' => FALSE,
  1239. 'settings' => array(
  1240. 'term_vocabulary' => 'data',
  1241. 'term_name' => 'Image',
  1242. 'term_accession' => '2968',
  1243. ),
  1244. 'display' => array(
  1245. 'default' => array(
  1246. 'label' => 'hidden',
  1247. ),
  1248. ),
  1249. );
  1250. }
  1251. }
  1252. /**
  1253. *
  1254. * @param unknown $entity_type
  1255. * @param unknown $bundle
  1256. * @param unknown $details
  1257. */
  1258. function tripal_chado_bundle_instances_info_linker(&$info, $entity_type, $bundle, $details) {
  1259. $table_name = $details['chado_table'];
  1260. $type_table = $details['chado_type_table'];
  1261. $type_field = $details['chado_type_column'];
  1262. $cvterm_id = $details['chado_cvterm_id'];
  1263. // CONTACTS
  1264. $contact_table = $table_name . '_contact';
  1265. if (chado_table_exists($contact_table)) {
  1266. $field_name = $table_name . '_contact';
  1267. $info[$field_name] = $info[$field_name] = array(
  1268. 'field_name' => $field_name,
  1269. 'entity_type' => $entity_type,
  1270. 'bundle' => $bundle->name,
  1271. 'label' => 'Contact',
  1272. 'description' => 'Associates an indviddual or organization with this record',
  1273. 'required' => FALSE,
  1274. 'settings' => array(
  1275. 'auto_attach' => FALSE,
  1276. 'chado_table' => $contact_table,
  1277. 'base_table' => $table_name,
  1278. 'chado_column' => 'contact_id',
  1279. ),
  1280. 'widget' => array(
  1281. 'type' => 'local__contact_widget',
  1282. 'settings' => array(
  1283. 'display_label' => 1,
  1284. ),
  1285. ),
  1286. 'display' => array(
  1287. 'default' => array(
  1288. 'label' => 'hidden',
  1289. 'type' => 'local__contact_formatter',
  1290. 'settings' => array(),
  1291. ),
  1292. ),
  1293. );
  1294. }
  1295. // DBXREF
  1296. $dbxref_table = $table_name . '_dbxref';
  1297. if (chado_table_exists($dbxref_table)) {
  1298. $field_name = 'sbo__database_cross_reference';
  1299. $schema = chado_get_schema($dbxref_table);
  1300. $pkey = $schema['primary key'][0];
  1301. $info[$field_name] = array(
  1302. 'field_name' => $field_name,
  1303. 'entity_type' => $entity_type,
  1304. 'bundle' => $bundle->name,
  1305. 'label' => 'Cross Reference',
  1306. 'description' => 'The IDs where this record may be available in other external online databases.',
  1307. 'required' => FALSE,
  1308. 'settings' => array(
  1309. 'auto_attach' => FALSE,
  1310. 'chado_table' => $dbxref_table,
  1311. 'chado_column' => $pkey,
  1312. 'base_table' => $table_name,
  1313. ),
  1314. 'widget' => array(
  1315. 'type' => 'sbo__database_cross_reference_widget',
  1316. 'settings' => array(
  1317. 'display_label' => 1,
  1318. ),
  1319. ),
  1320. 'display' => array(
  1321. 'default' => array(
  1322. 'label' => 'hidden',
  1323. 'type' => 'sbo__database_cross_reference_formatter',
  1324. 'settings' => array(),
  1325. ),
  1326. ),
  1327. );
  1328. }
  1329. // EXPRESSION
  1330. // TODO: this should only show up on gene or mRNA and the GO must be
  1331. // laoded or this field will crash things.
  1332. // $expression_table = $table_name . '_expression';
  1333. // if (chado_table_exists($expression_table)) {
  1334. // $field_name = 'go__gene_expression';
  1335. // $schema = chado_get_schema($expression_table);
  1336. // $pkey = $schema['primary key'][0];
  1337. // $info[$field_name] = array(
  1338. // 'field_name' => $field_name,
  1339. // 'entity_type' => $entity_type,
  1340. // 'bundle' => $bundle->name,
  1341. // 'label' => 'Gene expression',
  1342. // 'description' => 'Information about the expression of this record.',
  1343. // 'required' => FALSE,
  1344. // 'settings' => array(
  1345. // 'auto_attach' => FALSE,
  1346. // 'chado_table' => $expression_table,
  1347. // 'chado_column' => $pkey,
  1348. // 'base_table' => $table_name,
  1349. // ),
  1350. // 'widget' => array(
  1351. // 'type' => 'go__gene_expression_widget',
  1352. // 'settings' => array(
  1353. // 'display_label' => 1,
  1354. // ),
  1355. // ),
  1356. // 'display' => array(
  1357. // 'default' => array(
  1358. // 'label' => 'above',
  1359. // 'type' => 'go__gene_expression_formatter',
  1360. // 'settings' => array(),
  1361. // ),
  1362. // ),
  1363. // );
  1364. // }
  1365. // FEATURELOC
  1366. if ($table_name == 'feature') {
  1367. $field_name = 'data__sequence_coordinates';
  1368. $schema = chado_get_schema('featureloc');
  1369. $pkey = $schema['primary key'][0];
  1370. $info[$field_name] = array(
  1371. 'field_name' => $field_name,
  1372. 'entity_type' => $entity_type,
  1373. 'bundle' => $bundle->name,
  1374. 'label' => 'Sequence Coordinates',
  1375. 'description' => 'The locations on other genomic sequences where this
  1376. record has been aligned.',
  1377. 'required' => FALSE,
  1378. 'settings' => array(
  1379. 'auto_attach' => FALSE,
  1380. 'chado_table' => 'featureloc',
  1381. 'chado_column' => $pkey,
  1382. 'base_table' => 'feature',
  1383. ),
  1384. 'widget' => array(
  1385. 'type' => 'data__sequence_coordinates_widget',
  1386. 'settings' => array(
  1387. 'display_label' => 1,
  1388. ),
  1389. ),
  1390. 'display' => array(
  1391. 'default' => array(
  1392. 'label' => 'hidden',
  1393. 'type' => 'data__sequence_coordinates_formatter',
  1394. 'settings' => array(),
  1395. ),
  1396. ),
  1397. );
  1398. }
  1399. // FEATUREPOS
  1400. if ($table_name == 'feature') {
  1401. $field_name = 'ogi__location_on_map';
  1402. $schema = chado_get_schema('featurepos');
  1403. $pkey = $schema['primary key'][0];
  1404. $info[$field_name] = array(
  1405. 'field_name' => $field_name,
  1406. 'entity_type' => $entity_type,
  1407. 'bundle' => $bundle->name,
  1408. 'label' => 'Location on Map',
  1409. 'description' => 'The positions on a genetic map.',
  1410. 'required' => FALSE,
  1411. 'settings' => array(
  1412. 'auto_attach' => FALSE,
  1413. 'chado_table' => 'featurepos',
  1414. 'chado_column' => $pkey,
  1415. 'base_table' => 'feature',
  1416. ),
  1417. 'widget' => array(
  1418. 'type' => 'ogi__location_on_map_widget',
  1419. 'settings' => array(
  1420. 'display_label' => 1,
  1421. ),
  1422. ),
  1423. 'display' => array(
  1424. 'default' => array(
  1425. 'label' => 'hidden',
  1426. 'type' => 'ogi__location_on_map_formatter',
  1427. 'settings' => array(),
  1428. ),
  1429. ),
  1430. );
  1431. }
  1432. // GENOTYPE
  1433. $genotype_table = $table_name . '_genotype';
  1434. if (chado_table_exists($genotype_table)) {
  1435. $field_name = 'so__genotype';
  1436. $schema = chado_get_schema($genotype_table);
  1437. $pkey = $schema['primary key'][0];
  1438. $info[$field_name] = array(
  1439. 'field_name' => $field_name,
  1440. 'entity_type' => $entity_type,
  1441. 'bundle' => $bundle->name,
  1442. 'label' => 'Genotype',
  1443. 'description' => 'The genotypes associated with this record.',
  1444. 'required' => FALSE,
  1445. 'settings' => array(
  1446. 'auto_attach' => FALSE,
  1447. 'chado_table' => $genotype_table,
  1448. 'chado_column' => $pkey,
  1449. 'base_table' => $table_name,
  1450. ),
  1451. 'widget' => array(
  1452. 'type' => 'so__genotype_widget',
  1453. 'settings' => array(
  1454. 'display_label' => 1,
  1455. ),
  1456. ),
  1457. 'display' => array(
  1458. 'default' => array(
  1459. 'label' => 'hidden',
  1460. 'type' => 'so__genotype_formatter',
  1461. 'settings' => array(),
  1462. ),
  1463. ),
  1464. );
  1465. }
  1466. // PHENOTYPE
  1467. $phenotype_table = $table_name . '_phenotype';
  1468. if (chado_table_exists($phenotype_table)) {
  1469. $field_name = 'sbo__phenotype';
  1470. $schema = chado_get_schema($phenotype_table);
  1471. $pkey = $schema['primary key'][0];
  1472. $info[$field_name] = array(
  1473. 'field_name' => $field_name,
  1474. 'entity_type' => $entity_type,
  1475. 'bundle' => $bundle->name,
  1476. 'label' => 'Phenotype',
  1477. 'description' => 'The phenotypes associated with this record.',
  1478. 'required' => FALSE,
  1479. 'settings' => array(
  1480. 'auto_attach' => FALSE,
  1481. 'chado_table' => $phenotype_table,
  1482. 'chado_column' => $pkey,
  1483. 'base_table' => $table_name,
  1484. ),
  1485. 'widget' => array(
  1486. 'type' => 'sbo__phenotype_widget',
  1487. 'settings' => array(
  1488. 'display_label' => 1,
  1489. ),
  1490. ),
  1491. 'display' => array(
  1492. 'default' => array(
  1493. 'label' => 'hidden',
  1494. 'type' => 'sbo__phenotype_formatter',
  1495. 'settings' => array(),
  1496. ),
  1497. ),
  1498. );
  1499. }
  1500. // PROPERTIES
  1501. $prop_table = $table_name . 'prop';
  1502. if (chado_table_exists($prop_table)) {
  1503. $tschema = chado_get_schema($table_name);
  1504. $schema = chado_get_schema($prop_table);
  1505. $tpkey = $tschema['primary key'][0];
  1506. $pkey = $schema['primary key'][0];
  1507. // Get the list of existing property types for this table.
  1508. $args = array();
  1509. $sql = "
  1510. SELECT DISTINCT P.type_id
  1511. FROM {" . $prop_table . "} P
  1512. ";
  1513. if (array_key_exists('type_id', $tschema['fields'])) {
  1514. $sql .= "
  1515. INNER JOIN {" . $table_name . "} T on T.$tpkey = P.$tpkey
  1516. WHERE T.type_id = :cvterm_id
  1517. ";
  1518. $args[':cvterm_id'] = $cvterm_id;
  1519. }
  1520. $props = chado_query($sql, $args);
  1521. while ($prop = $props->fetchObject()) {
  1522. $term = chado_generate_var('cvterm', array('cvterm_id' => $prop->type_id));
  1523. // The tripal_analysis_KEGG, tripal_analysis_blast, and
  1524. // tripal_analysis_interpro modules store results in the analysisprop
  1525. // table which is probably not the best place, but we don't want to
  1526. // create a ton of fields for this.
  1527. if ($prop_table == 'analysisprop' and
  1528. ($term->dbxref_id->db_id->name == 'KEGG_BRITE' or
  1529. $term->dbxref_id->db_id->name == 'tripal')) {
  1530. continue;
  1531. }
  1532. $field_name = strtolower(preg_replace('/[^\w]/','_', $term->dbxref_id->db_id->name . '__' . $term->name));
  1533. $field_name = substr($field_name, 0, 32);
  1534. $info[$field_name] = array(
  1535. 'field_name' => $field_name,
  1536. 'entity_type' => $entity_type,
  1537. 'bundle' => $bundle->name,
  1538. 'label' => ucwords(preg_replace('/_/', ' ', $term->name)),
  1539. 'description' => $term->definition,
  1540. 'required' => FALSE,
  1541. 'settings' => array(
  1542. 'auto_attach' => TRUE,
  1543. 'term_vocabulary' => $term->dbxref_id->db_id->name,
  1544. 'term_accession' => $term->dbxref_id->accession,
  1545. 'term_name' => $term->name,
  1546. 'base_table' => $table_name,
  1547. 'chado_table' => $prop_table,
  1548. 'chado_column' => $pkey,
  1549. ),
  1550. 'widget' => array(
  1551. 'type' => 'chado_linker__prop_widget',
  1552. 'settings' => array(
  1553. 'display_label' => 1,
  1554. ),
  1555. ),
  1556. 'display' => array(
  1557. 'default' => array(
  1558. 'label' => 'hidden',
  1559. 'type' => 'chado_linker__prop_formatter',
  1560. 'settings' => array(),
  1561. ),
  1562. ),
  1563. );
  1564. }
  1565. }
  1566. // PUBLICATIONS
  1567. $pub_table = $table_name . '_pub';
  1568. if (chado_table_exists($pub_table)) {
  1569. $field_name = 'schema__publication';
  1570. $schema = chado_get_schema($pub_table);
  1571. $pkey = $schema['primary key'][0];
  1572. $info[$field_name] = array(
  1573. 'field_name' => $field_name,
  1574. 'entity_type' => $entity_type,
  1575. 'bundle' => $bundle->name,
  1576. 'label' => 'Publication',
  1577. 'description' => 'This record has been referenced or is sourced from these publications.',
  1578. 'required' => FALSE,
  1579. 'settings' => array(
  1580. 'auto_attach' => FALSE,
  1581. 'chado_table' => $pub_table,
  1582. 'chado_column' => $pkey,
  1583. 'base_table' => $table_name,
  1584. ),
  1585. 'widget' => array(
  1586. 'type' => 'schema__publication_widget',
  1587. 'settings' => array(
  1588. 'display_label' => 1,
  1589. ),
  1590. ),
  1591. 'display' => array(
  1592. 'default' => array(
  1593. 'label' => 'hidden',
  1594. 'type' => 'schema__publication_formatter',
  1595. 'settings' => array(),
  1596. ),
  1597. ),
  1598. );
  1599. }
  1600. // PUBLICATIONS (in reverse)
  1601. // We want to be able to show all of the content that a publication links
  1602. // to. The sio__references field does that.
  1603. if ($table_name == 'pub') {
  1604. $field_name = 'sio__references';
  1605. $schema = chado_get_schema($table_name);
  1606. $pkey = $schema['primary key'][0];
  1607. $info[$field_name] = array(
  1608. 'field_name' => $field_name,
  1609. 'entity_type' => $entity_type,
  1610. 'bundle' => $bundle->name,
  1611. 'label' => 'References',
  1612. 'description' => 'Records that are referred to by the publication.',
  1613. 'required' => FALSE,
  1614. 'settings' => array(
  1615. 'auto_attach' => FALSE,
  1616. 'chado_table' => $table_name,
  1617. 'chado_column' => $pkey,
  1618. 'base_table' => $table_name,
  1619. ),
  1620. 'widget' => array(
  1621. 'type' => 'sio__references_widget',
  1622. 'settings' => array(
  1623. 'display_label' => 1,
  1624. ),
  1625. ),
  1626. 'display' => array(
  1627. 'default' => array(
  1628. 'label' => 'hidden',
  1629. 'type' => 'sio__references_formatter',
  1630. 'settings' => array(),
  1631. ),
  1632. ),
  1633. );
  1634. }
  1635. // RELATIONSHIPS
  1636. // If the linker table does not exists then we don't want to add attach.
  1637. $rel_table = $table_name . '_relationship';
  1638. if (chado_table_exists($rel_table)) {
  1639. $field_name = 'sbo__relationship';
  1640. $schema = chado_get_schema($rel_table);
  1641. $pkey = $schema['primary key'][0];
  1642. $info[$field_name] = array(
  1643. 'field_name' => $field_name,
  1644. 'entity_type' => $entity_type,
  1645. 'bundle' => $bundle->name,
  1646. 'label' => 'Relationship',
  1647. 'description' => 'Other records with relationships to this record.',
  1648. 'required' => FALSE,
  1649. 'settings' => array(
  1650. 'auto_attach' => FALSE,
  1651. 'chado_table' => $rel_table,
  1652. 'chado_column' => $pkey,
  1653. 'base_table' => $table_name,
  1654. ),
  1655. 'widget' => array(
  1656. 'type' => 'sbo__relationship_widget',
  1657. 'settings' => array(
  1658. 'display_label' => 1,
  1659. ),
  1660. ),
  1661. 'display' => array(
  1662. 'default' => array(
  1663. 'label' => 'hidden',
  1664. 'type' => 'sbo__relationship_formatter',
  1665. 'settings' => array(),
  1666. ),
  1667. ),
  1668. );
  1669. }
  1670. // SYNONYMS
  1671. $syn_table = $table_name . '_synonym';
  1672. if (chado_table_exists($syn_table)) {
  1673. $field_name = 'schema__alternate_name';
  1674. $schema = chado_get_schema($syn_table);
  1675. $pkey = $schema['primary key'][0];
  1676. $info[$field_name] = array(
  1677. 'field_name' => $field_name,
  1678. 'entity_type' => $entity_type,
  1679. 'bundle' => $bundle->name,
  1680. 'label' => 'Synonyms',
  1681. 'description' => 'Alternate names, aliases or synonyms for this record.',
  1682. 'required' => FALSE,
  1683. 'settings' => array(
  1684. 'auto_attach' => FALSE,
  1685. 'chado_table' => $syn_table,
  1686. 'chado_column' => $pkey,
  1687. 'base_table' => $table_name,
  1688. ),
  1689. 'widget' => array(
  1690. 'type' => 'schema__alternate_name_widget',
  1691. 'settings' => array(
  1692. 'display_label' => 1,
  1693. ),
  1694. ),
  1695. 'display' => array(
  1696. 'default' => array(
  1697. 'label' => 'hidden',
  1698. 'type' => 'schema__alternate_name_formatter',
  1699. 'settings' => array(),
  1700. ),
  1701. ),
  1702. );
  1703. }
  1704. }
  1705. /**
  1706. * Implements hook_bundle_create_user_field().
  1707. *
  1708. * A priviledged user has the ability to add new fields to the bundle. The
  1709. * chado_linker__prop and chado_linker__cvterm fields are allowed to be
  1710. * added dynamically by the user. But, Drupal doesn't know how to deal with
  1711. * it, so this function is called for any field attached to a TripalEntity
  1712. * bundle type. Any fields whose TripalField::$module argument is set to
  1713. * 'tripal_chado' and that can be added dynamically will result in a call
  1714. * to this function.
  1715. */
  1716. function tripal_chado_bundle_create_user_field($new_field, $bundle) {
  1717. // Get the table this bundle is mapped to.
  1718. $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
  1719. $vocab = $term->vocab;
  1720. $params = array(
  1721. 'vocabulary' => $vocab->vocabulary,
  1722. 'accession' => $term->accession,
  1723. );
  1724. $chado_table = $bundle->data_table;
  1725. $chado_type_table = $bundle->type_linker_table;
  1726. $chado_type_column = $bundle->type_column;
  1727. $chado_type_id = $bundle->type_id;
  1728. $chado_type_value = $bundle->type_value;
  1729. // We allow site admins to add new chado_linker__prop fields to an entity.
  1730. // This function will allow us to properly add them. But at this point we
  1731. // don't know the controlled vocabulary term. We'll have to use the
  1732. // defaults and let the user set it using the interface.
  1733. if ($new_field['type'] == 'chado_linker__prop') {
  1734. $table_name = $chado_table . 'prop';
  1735. if (chado_table_exists($table_name)) {
  1736. $schema = chado_get_schema($table_name);
  1737. $pkey = $schema['primary key'][0];
  1738. $field_name = $new_field['field_name'];
  1739. $field_type = 'chado_linker__prop';
  1740. // First add the field.
  1741. field_create_field(array(
  1742. 'field_name' => $field_name,
  1743. 'type' => $field_type,
  1744. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  1745. 'locked' => FALSE,
  1746. 'storage' => array(
  1747. 'type' => 'field_chado_storage',
  1748. ),
  1749. ));
  1750. // Now add the instance
  1751. field_create_instance(array(
  1752. 'field_name' => $field_name,
  1753. 'entity_type' => 'TripalEntity',
  1754. 'bundle' => $bundle->name,
  1755. 'label' => $new_field['label'],
  1756. 'description' => '',
  1757. 'required' => FALSE,
  1758. 'settings' => array(
  1759. 'auto_attach' => TRUE,
  1760. 'base_table' => $chado_table,
  1761. 'chado_table' => $table_name,
  1762. 'chado_column' => $pkey,
  1763. 'term_vocabulary' => '',
  1764. 'term_accession' => '',
  1765. 'term_name' => ''
  1766. ),
  1767. 'widget' => array(
  1768. 'type' => 'chado_linker__prop_widget',
  1769. 'settings' => array(
  1770. 'display_label' => 1,
  1771. ),
  1772. ),
  1773. 'display' => array(
  1774. 'default' => array(
  1775. 'label' => 'inline',
  1776. 'type' => 'chado_linker__prop_formatter',
  1777. 'settings' => array(),
  1778. ),
  1779. ),
  1780. ));
  1781. }
  1782. else {
  1783. drupal_set_message('Cannot add a property field to this entity. Chado does not support properties for this data type.', 'error');
  1784. }
  1785. }
  1786. // We allow site admins to add new chado_linker__cvterm fields to an entity.
  1787. // This function will allow us to properly add them. But at this point we
  1788. // don't know the controlled vocabulary term. We'll have to use the
  1789. // defaults and let the user set it using the interface.
  1790. if ($new_field['type'] == 'chado_linker__cvterm') {
  1791. $table_name = $chado_table . '_cvterm';
  1792. if (chado_table_exists($table_name)) {
  1793. $schema = chado_get_schema($table_name);
  1794. $pkey = $schema['primary key'][0];
  1795. $field_name = $new_field['field_name'];
  1796. $field_type = 'chado_linker__cvterm';
  1797. // First add the field.
  1798. field_create_field(array(
  1799. 'field_name' => $field_name,
  1800. 'type' => $field_type,
  1801. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  1802. 'locked' => FALSE,
  1803. 'storage' => array(
  1804. 'type' => 'field_chado_storage',
  1805. ),
  1806. 'settings' => array(
  1807. 'base_table' => $chado_table,
  1808. 'chado_table' => $table_name,
  1809. 'chado_column' => $pkey,
  1810. ),
  1811. ));
  1812. // Now add the instance
  1813. field_create_instance(array(
  1814. 'field_name' => $field_name,
  1815. 'entity_type' => 'TripalEntity',
  1816. 'bundle' => $bundle->name,
  1817. 'label' => $new_field['label'],
  1818. 'description' => '',
  1819. 'required' => FALSE,
  1820. 'settings' => array(
  1821. 'auto_attach' => TRUE,
  1822. ),
  1823. 'widget' => array(
  1824. 'type' => 'chado_linker__cvterm_widget',
  1825. 'settings' => array(
  1826. 'display_label' => 1,
  1827. ),
  1828. ),
  1829. 'display' => array(
  1830. 'default' => array(
  1831. 'label' => 'above',
  1832. 'type' => 'chado_linker__cvterm_formatter',
  1833. 'settings' => array(),
  1834. ),
  1835. ),
  1836. ));
  1837. }
  1838. else {
  1839. drupal_set_message('Cannot add a property field to this entity. Chado does not support annotations for this data type.', 'error');
  1840. }
  1841. }
  1842. }
  1843. /**
  1844. * Allows for altering of a field's instance setting form.
  1845. *
  1846. * This appears to be a Drupal hook but is actually a custom function created
  1847. * by this module. It is called by the tripal_form_alter() function of this
  1848. * module.
  1849. *
  1850. * Here we put additional form elements for any field, regardless if it is
  1851. * a tripalField or not.
  1852. *
  1853. * @param $form
  1854. * The form array. Alterations to the form can be made within this array.
  1855. * @param $form_state
  1856. * The form state array.
  1857. */
  1858. function tripal_chado_field_instance_settings_form_alter(&$form, $form_state) {
  1859. global $language;
  1860. $field = $form['#field'];
  1861. $instance = $form['#instance'];
  1862. $base_table = array_key_exists('base_table', $instance['settings']) ? $instance['settings']['base_table'] : '';
  1863. $chado_table = array_key_exists('chado_table', $instance['settings']) ? $instance['settings']['chado_table'] : '';
  1864. $chado_column = array_key_exists('chado_column', $instance['settings']) ? $instance['settings']['chado_column'] : '';
  1865. if ($chado_table) {
  1866. // Construct a table for the vocabulary information.
  1867. $headers = array();
  1868. $rows = array();
  1869. $rows[] = array(
  1870. array(
  1871. 'data' => 'Base Table',
  1872. 'header' => TRUE,
  1873. 'width' => '20%',
  1874. ),
  1875. $base_table
  1876. );
  1877. $rows[] = array(
  1878. array(
  1879. 'data' => 'Record Table',
  1880. 'header' => TRUE,
  1881. 'width' => '20%',
  1882. ),
  1883. $chado_table
  1884. );
  1885. $rows[] = array(
  1886. array(
  1887. 'data' => 'ID Column',
  1888. 'header' => TRUE,
  1889. 'width' => '20%',
  1890. ),
  1891. $chado_column
  1892. );
  1893. $table = array(
  1894. 'header' => $headers,
  1895. 'rows' => $rows,
  1896. 'attributes' => array(
  1897. ),
  1898. 'sticky' => FALSE,
  1899. 'caption' => '',
  1900. 'colgroups' => array(),
  1901. 'empty' => '',
  1902. );
  1903. $form['chado_mapping'] = array(
  1904. '#type' => 'fieldset',
  1905. '#title' => 'Chado Mapping',
  1906. '#description' => t('This field maps to data in Chado to the following table:'),
  1907. );
  1908. $form['chado_mapping']['details'] = array(
  1909. '#type' => 'item',
  1910. '#markup' => theme_table($table),
  1911. );
  1912. }
  1913. }
  1914. /**
  1915. * Implements hook_form_FROM_ID_alter()
  1916. */
  1917. function tripal_chado_form_tripalbundle_form_alter(&$form, $form_state) {
  1918. global $language;
  1919. $bundle = $form['#bundle'];
  1920. $term = entity_load('TripalTerm', array('id' => $bundle->term_id));
  1921. $term = reset($term);
  1922. $vocab = $term->vocab;
  1923. $params = array(
  1924. 'vocabulary' => $vocab->vocabulary,
  1925. 'accession' => $term->accession,
  1926. );
  1927. $chado_table = $bundle->data_table;
  1928. $chado_column = $bundle->type_column;
  1929. $chado_type_table = $bundle->type_linker_table;
  1930. $chado_type_id = $bundle->type_id;
  1931. $chado_type_value = $bundle->type_value;
  1932. // Construct a table for the vocabulary information.
  1933. $headers = array();
  1934. $rows = array();
  1935. $rows[] = array(
  1936. array(
  1937. 'data' => 'Chado Table',
  1938. 'header' => TRUE,
  1939. 'width' => '20%',
  1940. ),
  1941. $chado_table
  1942. );
  1943. if ($chado_column) {
  1944. $rows[] = array(
  1945. array(
  1946. 'data' => 'Type Column',
  1947. 'header' => TRUE,
  1948. 'width' => '20%',
  1949. ),
  1950. $chado_column
  1951. );
  1952. }
  1953. if ($chado_type_table) {
  1954. $rows[] = array(
  1955. array(
  1956. 'data' => 'Association Table',
  1957. 'header' => TRUE,
  1958. 'width' => '20%',
  1959. ),
  1960. $chado_type_table
  1961. );
  1962. }
  1963. if ($chado_type_table and $chado_type_id) {
  1964. $rows[] = array(
  1965. array(
  1966. 'data' => 'CVTerm Table ID',
  1967. 'header' => TRUE,
  1968. 'width' => '20%',
  1969. ),
  1970. $chado_type_id
  1971. );
  1972. }
  1973. if ($chado_type_value) {
  1974. $rows[] = array(
  1975. array(
  1976. 'data' => 'Property Value',
  1977. 'header' => TRUE,
  1978. 'width' => '20%',
  1979. ),
  1980. $chado_type_value
  1981. );
  1982. }
  1983. $table = array(
  1984. 'header' => $headers,
  1985. 'rows' => $rows,
  1986. 'attributes' => array(
  1987. ),
  1988. 'sticky' => FALSE,
  1989. 'caption' => '',
  1990. 'colgroups' => array(),
  1991. 'empty' => '',
  1992. );
  1993. $form['chado_mapping'] = array(
  1994. '#type' => 'item',
  1995. '#title' => 'Chado Mapping',
  1996. '#markup' => theme_table($table),
  1997. '#description' => t('This content type maps to the table in Chado
  1998. listed above. Chado allows multiple data types to be housed
  1999. in a single table. Therefore, the column that is used to
  2000. differentiate between data types is also listed above.'),
  2001. '#weight' => 0,
  2002. );
  2003. }