tripal_chado.fields.inc 67 KB

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