tripal_chado.fields.inc 70 KB

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