tripal_chado.fields.inc 55 KB

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