tripal_chado.fields.inc 58 KB

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