tripal_chado.fields.inc 65 KB

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