tripal_chado.fields.inc 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150
  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. }
  736. elseif ($base_info['label'] == 'Timeaccessioned') {
  737. $base_info['label'] = 'Time Accessioned';
  738. $base_info['description'] = 'Please enter the time that this record was first added to the database.';
  739. }
  740. elseif ($base_info['label'] == 'Timelastmodified') {
  741. $base_info['label'] = 'Time Last Modified';
  742. $base_info['description'] = 'Please enter the time that this record was last modified. The default is the current time.';
  743. }
  744. //
  745. // ORGANISM TABLE
  746. //
  747. elseif ($table_name == 'organism' and $column_name == 'comment') {
  748. $base_info['label'] = 'Description';
  749. }
  750. //
  751. // PUB TABLE
  752. //
  753. elseif ($table_name == 'pub' and $column_name == 'uniquename') {
  754. $base_info['widget_type'] = 'text_textfield';
  755. }
  756. //
  757. // ANALYSIS TABLE
  758. //
  759. elseif ($table_name == 'analysis' and $column_name == 'name') {
  760. $base_info['required'] = TRUE;
  761. }
  762. elseif ($table_name == 'analysis' and $column_name == 'program') {
  763. $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.';
  764. $base_info['label'] = 'Program, Pipeline, Workflow or Method Name';
  765. }
  766. elseif ($table_name == 'analysis' and $column_name == 'algorithm') {
  767. $base_info['label'] = 'Source Version';
  768. $base_info['description'] = 'The name of the algorithm used to produce the dataset if different from the program.';
  769. }
  770. elseif ($table_name == 'analysis' and $column_name == 'programversion') {
  771. $base_info['label'] = 'Program Version';
  772. $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.';
  773. }
  774. elseif ($table_name == 'analysis' and $column_name == 'timeexecuted') {
  775. $base_info['label'] = 'Date Performed';
  776. $base_info['description'] = 'The date and time when the analysis was performed.';
  777. }
  778. if ($table_name == 'analysis' and ($column_name == 'sourceuri' or
  779. $column_name == 'sourceversion' or $column_name == 'sourcename')) {
  780. // Skip the source columns in the analysis table. We have a custom
  781. // field for those columns
  782. continue;
  783. }
  784. //
  785. // PROJECT TABLE
  786. //
  787. elseif ($table_name == 'project' and $column_name == 'description') {
  788. $base_info['label'] = 'Short Description';
  789. }
  790. $info[$field_name] = $base_info;
  791. }
  792. }
  793. /**
  794. * Helper function for the hook_create_tripalfield_instance().
  795. *
  796. * Adds custom fields for base fields. These override the settings provided
  797. * in the tripal_chado_create_tripalfield_instance_base() function.
  798. *
  799. * @param $entity_type
  800. * @param $bundle
  801. * @param $details
  802. */
  803. function tripal_chado_bundle_instances_info_custom(&$info, $entity_type, $bundle, $details) {
  804. $table_name = $details['chado_table'];
  805. $type_table = $details['chado_type_table'];
  806. $type_field = $details['chado_type_column'];
  807. $cvterm_id = $details['chado_cvterm_id'];
  808. $schema = chado_get_schema($table_name);
  809. // BASE TYPE_ID
  810. // Exclude the type_id field on base tables where content types are mapped
  811. // by the type_id field or on the organism table where the type_id is mean
  812. // as an infraspecific name type.
  813. if (array_key_exists('type_id', $schema['fields']) and
  814. $table_name != 'organism' and $type_field != 'type_id') {
  815. $field_name = 'schema__additional_type';
  816. $is_required = FALSE;
  817. if (array_key_exists('not null', $schema['fields']['type_id']) and
  818. $schema['fields']['type_id']['not null']) {
  819. $is_required = TRUE;
  820. }
  821. $info[$field_name] = array(
  822. 'field_name' => $field_name,
  823. 'entity_type' => $entity_type,
  824. 'bundle' => $bundle->name,
  825. 'label' => 'Type',
  826. 'description' => 'Select a type for this record.',
  827. 'required' => $is_required,
  828. 'settings' => array(
  829. 'auto_attach' => TRUE,
  830. 'chado_table' => $table_name,
  831. 'chado_column' => 'type_id',
  832. 'base_table' => $table_name,
  833. ),
  834. 'widget' => array(
  835. 'type' => 'schema__additional_type_widget',
  836. 'settings' => array(
  837. 'display_label' => 1,
  838. ),
  839. ),
  840. 'display' => array(
  841. 'default' => array(
  842. 'label' => 'inline',
  843. 'type' => 'schema__additional_type_formatter',
  844. 'settings' => array(),
  845. ),
  846. ),
  847. );
  848. }
  849. // BASE ORGANISM_ID
  850. if ($table_name != 'organism' and array_key_exists('organism_id', $schema['fields'])) {
  851. $field_name = 'obi__organism';
  852. $is_required = FALSE;
  853. if (array_key_exists('not null', $schema['fields']['organism_id']) and
  854. $schema['fields']['organism_id']['not null']) {
  855. $is_required = TRUE;
  856. }
  857. $info[$field_name] = array(
  858. 'field_name' => $field_name,
  859. 'entity_type' => $entity_type,
  860. 'bundle' => $bundle->name,
  861. 'label' => 'Organism',
  862. 'description' => 'Select an organism.',
  863. 'required' => $is_required,
  864. 'settings' => array(
  865. 'auto_attach' => TRUE,
  866. 'chado_table' => $table_name,
  867. 'chado_column' => 'organism_id',
  868. 'base_table' => $table_name,
  869. ),
  870. 'widget' => array(
  871. 'type' => 'obi__organism_widget',
  872. 'settings' => array(
  873. 'display_label' => 1,
  874. ),
  875. ),
  876. 'display' => array(
  877. 'default' => array(
  878. 'label' => 'inline',
  879. 'type' => 'obi__organism_formatter',
  880. 'settings' => array(),
  881. ),
  882. ),
  883. );
  884. }
  885. // BASE ORGANISM_ID
  886. if ($table_name == 'cvterm') {
  887. $field_name = 'sio__vocabulary';
  888. $is_required = TRUE;
  889. $info[$field_name] = array(
  890. 'field_name' => $field_name,
  891. 'entity_type' => $entity_type,
  892. 'bundle' => $bundle->name,
  893. 'label' => 'Vocabulary',
  894. 'description' => 'Select a vocabulary.',
  895. 'required' => $is_required,
  896. 'settings' => array(
  897. 'auto_attach' => TRUE,
  898. 'chado_table' => $table_name,
  899. 'chado_column' => 'cv_id',
  900. 'base_table' => $table_name,
  901. ),
  902. 'widget' => array(
  903. 'type' => 'sio__vocabulary_widget',
  904. 'settings' => array(
  905. 'display_label' => 1,
  906. ),
  907. ),
  908. 'display' => array(
  909. 'default' => array(
  910. 'label' => 'inline',
  911. 'type' => 'sio__vocabulary_formatter',
  912. 'settings' => array(),
  913. ),
  914. ),
  915. );
  916. }
  917. // BASE DBXREF
  918. if (array_key_exists('dbxref_id', $schema['fields'])) {
  919. $field_name = 'data__accession';
  920. $info[$field_name] = array(
  921. 'field_name' => $field_name,
  922. 'entity_type' => $entity_type,
  923. 'bundle' => $bundle->name,
  924. 'label' => 'Accession',
  925. 'description' => 'This field specifies the unique stable accession (ID) for
  926. this record. It requires that this site have a database entry.',
  927. 'required' => FALSE,
  928. 'settings' => array(
  929. 'auto_attach' => TRUE,
  930. 'chado_table' => $table_name,
  931. 'chado_column' => 'dbxref_id',
  932. 'base_table' => $table_name,
  933. ),
  934. 'widget' => array(
  935. 'type' => 'data__accession_widget',
  936. 'settings' => array(
  937. 'display_label' => 1,
  938. ),
  939. ),
  940. 'display' => array(
  941. 'default' => array(
  942. 'label' => 'inline',
  943. 'type' => 'data__accession_formatter',
  944. 'settings' => array(),
  945. ),
  946. ),
  947. );
  948. }
  949. // FEATURE MD5CHECKSUM
  950. if ($table_name == 'feature') {
  951. $field_name = 'data__sequence_checksum';
  952. $info[$field_name] = array(
  953. 'field_name' => $field_name,
  954. 'entity_type' => $entity_type,
  955. 'bundle' => $bundle->name,
  956. 'label' => 'Sequence Checksum',
  957. 'description' => 'The MD5 checksum for the sequence. The checksum here
  958. will always be unique for the raw unformatted sequence. To verify that the
  959. sequence has not been corrupted, download the raw sequence and use an MD5 tool
  960. to calculate the value. If the value calculated is identical the one shown
  961. here, then the downloaded sequence is uncorrupted.',
  962. 'required' => FALSE,
  963. 'settings' => array(
  964. 'auto_attach' => TRUE,
  965. 'chado_table' => $table_name,
  966. 'chado_column' => 'md5checksum',
  967. 'base_table' => $table_name,
  968. ),
  969. 'widget' => array(
  970. 'type' => 'data__sequence_checksum_widget',
  971. 'settings' => array(
  972. 'display_label' => 1,
  973. 'md5_fieldname' => 'feature__md5checksum',
  974. ),
  975. ),
  976. 'display' => array(
  977. 'default' => array(
  978. 'label' => 'inline',
  979. 'type' => 'data__sequence_checksum_formatter',
  980. 'settings' => array(),
  981. ),
  982. ),
  983. );
  984. }
  985. // FEATURE RESIDUES
  986. if ($table_name == 'feature') {
  987. $field_name = 'data__sequence';
  988. $info[$field_name] = array(
  989. 'field_name' => $field_name,
  990. 'entity_type' => $entity_type,
  991. 'bundle' => $bundle->name,
  992. 'label' => 'Sequence',
  993. 'description' => 'All available sequences for this record.',
  994. 'required' => FALSE,
  995. 'settings' => array(
  996. 'auto_attach' => FALSE,
  997. 'chado_table' => $table_name,
  998. 'chado_column' => 'residues',
  999. 'base_table' => $table_name,
  1000. ),
  1001. 'widget' => array(
  1002. 'type' => 'data__sequence_widget',
  1003. 'settings' => array(
  1004. 'display_label' => 1,
  1005. ),
  1006. ),
  1007. 'display' => array(
  1008. 'default' => array(
  1009. 'label' => 'above',
  1010. 'type' => 'data__sequence_formatter',
  1011. 'settings' => array(),
  1012. ),
  1013. ),
  1014. );
  1015. }
  1016. // FEATURE SEQLEN
  1017. if ($table_name == 'feature') {
  1018. $field_name = 'data__sequence_length';
  1019. $info[$field_name] = array(
  1020. 'field_name' => $field_name,
  1021. 'entity_type' => $entity_type,
  1022. 'bundle' => $bundle->name,
  1023. 'label' => 'Sequence Length',
  1024. 'description' => 'The number of residues in the raw sequence. This length
  1025. is only for the assigned raw sequence and does not represent the length of any
  1026. sequences derived from alignments. If this value is zero but aligned sequences
  1027. are present then this record has no official assigned sequence.',
  1028. 'required' => FALSE,
  1029. 'settings' => array(
  1030. 'auto_attach' => TRUE,
  1031. 'chado_table' => $table_name,
  1032. 'chado_column' => 'seqlen',
  1033. 'base_table' => $table_name,
  1034. ),
  1035. 'widget' => array(
  1036. 'type' => 'data__sequence_length_widget',
  1037. 'settings' => array(
  1038. 'display_label' => 1,
  1039. ),
  1040. ),
  1041. 'display' => array(
  1042. 'default' => array(
  1043. 'label' => 'inline',
  1044. 'type' => 'data__sequence_length_formatter',
  1045. 'settings' => array(),
  1046. ),
  1047. ),
  1048. );
  1049. }
  1050. // PROTEIN & CDS SEQUENCES.
  1051. if ($table_name == 'feature' and
  1052. ($bundle->label == 'mRNA' or $bundle->label == 'transcript')) {
  1053. $field_name = 'data__protein_sequence';
  1054. $info[$field_name] = array(
  1055. 'field_name' => $field_name,
  1056. 'entity_type' => $entity_type,
  1057. 'bundle' => $bundle->name,
  1058. 'label' => 'Protein Sequence',
  1059. 'description' => 'Protein sequences.',
  1060. 'required' => FALSE,
  1061. 'settings' => array(
  1062. 'auto_attach' => FALSE,
  1063. 'chado_table' => 'feature',
  1064. 'chado_column' => 'residues',
  1065. 'base_table' => 'feature',
  1066. ),
  1067. 'widget' => array(
  1068. 'type' => 'data__protein_sequence_widget',
  1069. 'settings' => array(
  1070. 'display_label' => 1,
  1071. ),
  1072. ),
  1073. 'display' => array(
  1074. 'default' => array(
  1075. 'label' => 'above',
  1076. 'type' => 'data__protein_sequence_formatter',
  1077. 'settings' => array(),
  1078. ),
  1079. ),
  1080. );
  1081. // $field_name = 'so__cds';
  1082. // $info[$field_name] = array(
  1083. // 'field_name' => $field_name,
  1084. // 'entity_type' => $entity_type,
  1085. // 'bundle' => $bundle->name,
  1086. // 'label' => 'Coding Sequence',
  1087. // 'description' => 'Coding sequences.',
  1088. // 'required' => FALSE,
  1089. // 'settings' => array(
  1090. // 'auto_attach' => FALSE,
  1091. // 'chado_table' => 'featureloc',
  1092. // 'chado_column' => '',
  1093. // 'base_table' => 'featureloc',
  1094. // ),
  1095. // 'widget' => array(
  1096. // 'type' => 'so__cds_widget',
  1097. // 'settings' => array(
  1098. // 'display_label' => 1,
  1099. // ),
  1100. // ),
  1101. // 'display' => array(
  1102. // 'default' => array(
  1103. // 'label' => 'above',
  1104. // 'type' => 'so__cds_formatter',
  1105. // 'settings' => array(),
  1106. // ),
  1107. // ),
  1108. // );
  1109. }
  1110. // GENE TRANSCRIPTS
  1111. $rel_table = $table_name . '_relationship';
  1112. if (chado_table_exists($rel_table) and $bundle->label == 'gene') {
  1113. $field_name = 'so__transcript';
  1114. $info[$field_name] = array(
  1115. 'field_name' => $field_name,
  1116. 'entity_type' => $entity_type,
  1117. 'bundle' => $bundle->name,
  1118. 'label' => 'Transcripts',
  1119. 'description' => 'Transcripts that are part of this gene.',
  1120. 'required' => FALSE,
  1121. 'settings' => array(
  1122. 'auto_attach' => FALSE,
  1123. 'chado_table' => $rel_table,
  1124. 'chado_column' => '',
  1125. 'base_table' => $table_name,
  1126. ),
  1127. 'widget' => array(
  1128. 'type' => 'so__transcript_widget',
  1129. 'settings' => array(
  1130. 'display_label' => 1,
  1131. ),
  1132. ),
  1133. 'display' => array(
  1134. 'default' => array(
  1135. 'label' => 'above',
  1136. 'type' => 'so__transcript_formatter',
  1137. 'settings' => array(),
  1138. ),
  1139. ),
  1140. );
  1141. }
  1142. // ORGANISM TYPE_ID
  1143. // if ($table_name == 'organism' and array_key_exists('type_id', $schema['fields'])) {
  1144. // $field_name = 'taxarank__infraspecific_taxon';
  1145. // $info[$field_name] = array(
  1146. // 'field_name' => $field_name,
  1147. // 'entity_type' => $entity_type,
  1148. // 'bundle' => $bundle->name,
  1149. // 'label' => 'Infraspecific Taxon',
  1150. // 'description' => 'The Infraspecific Taxon.',
  1151. // 'required' => FALSE,
  1152. // 'settings' => array(
  1153. // 'auto_attach' => TRUE,
  1154. // 'chado_table' => 'organism',
  1155. // 'chado_column' => 'type_id',
  1156. // 'base_table' => 'organism',
  1157. // ),
  1158. // 'widget' => array(
  1159. // 'type' => 'taxarank__infraspecific_taxon_widget',
  1160. // 'settings' => array(
  1161. // 'display_label' => 1,
  1162. // ),
  1163. // ),
  1164. // 'display' => array(
  1165. // 'default' => array(
  1166. // 'label' => 'inline',
  1167. // 'type' => 'taxarank__infraspecific_taxon_formatter',
  1168. // 'settings' => array(),
  1169. // ),
  1170. // ),
  1171. // );
  1172. // }
  1173. // FEATURE SEQLEN
  1174. if ($table_name == 'featuremap') {
  1175. $field_name = 'uo__unit';
  1176. $info[$field_name] = array(
  1177. 'field_name' => $field_name,
  1178. 'entity_type' => $entity_type,
  1179. 'bundle' => $bundle->name,
  1180. 'label' => 'Units',
  1181. 'description' => 'The map\'s unit type.',
  1182. 'required' => TRUE,
  1183. 'settings' => array(
  1184. 'auto_attach' => TRUE,
  1185. 'chado_table' => $table_name,
  1186. 'chado_column' => 'unittype_id',
  1187. 'base_table' => $table_name,
  1188. ),
  1189. 'widget' => array(
  1190. 'type' => 'uo__unit_widget',
  1191. 'settings' => array(
  1192. 'display_label' => 1,
  1193. ),
  1194. ),
  1195. 'display' => array(
  1196. 'default' => array(
  1197. 'label' => 'inline',
  1198. 'type' => 'uo__unit_formatter',
  1199. 'settings' => array(),
  1200. ),
  1201. ),
  1202. );
  1203. }
  1204. // the analysis source.
  1205. if ($table_name == 'analysis') {
  1206. $field_name = 'local__source_data';
  1207. $info[$field_name] = array(
  1208. 'field_name' => $field_name,
  1209. 'entity_type' => $entity_type,
  1210. 'bundle' => $bundle->name,
  1211. 'label' => 'Data Source',
  1212. 'description' => 'The source where data was obtained for this analysis.',
  1213. 'required' => FALSE,
  1214. 'settings' => array(
  1215. 'auto_attach' => TRUE,
  1216. 'chado_table' => $table_name,
  1217. 'chado_column' => 'analysis_id',
  1218. 'base_table' => $table_name,
  1219. ),
  1220. 'widget' => array(
  1221. 'type' => 'local__source_data_widget',
  1222. 'settings' => array(
  1223. 'display_label' => 1,
  1224. ),
  1225. ),
  1226. 'display' => array(
  1227. 'default' => array(
  1228. 'label' => 'inline',
  1229. 'type' => 'local__source_data_formatter',
  1230. 'settings' => array(),
  1231. ),
  1232. ),
  1233. );
  1234. }
  1235. // Add an image field to the Organism type. This is a Drupal field and
  1236. // not stored in Chado, but is used for backwards compatibility.
  1237. if ($table_name == 'organism') {
  1238. $field_name = 'data__image';
  1239. $info[$field_name] = array(
  1240. 'field_name' => $field_name,
  1241. 'entity_type' => $entity_type,
  1242. 'bundle' => $bundle->name,
  1243. 'label' => 'Organism Image',
  1244. 'description' => 'An image for the organism',
  1245. 'required' => FALSE,
  1246. 'settings' => array(
  1247. 'term_vocabulary' => 'data',
  1248. 'term_name' => 'Image',
  1249. 'term_accession' => '2968',
  1250. ),
  1251. 'display' => array(
  1252. 'default' => array(
  1253. 'label' => 'hidden',
  1254. ),
  1255. ),
  1256. );
  1257. }
  1258. }
  1259. /**
  1260. *
  1261. * @param unknown $entity_type
  1262. * @param unknown $bundle
  1263. * @param unknown $details
  1264. */
  1265. function tripal_chado_bundle_instances_info_linker(&$info, $entity_type, $bundle, $details) {
  1266. $table_name = $details['chado_table'];
  1267. $type_table = $details['chado_type_table'];
  1268. $type_field = $details['chado_type_column'];
  1269. $cvterm_id = $details['chado_cvterm_id'];
  1270. // CONTACTS
  1271. $contact_table = $table_name . '_contact';
  1272. if (chado_table_exists($contact_table)) {
  1273. $field_name = $table_name . '_contact';
  1274. $info[$field_name] = $info[$field_name] = array(
  1275. 'field_name' => $field_name,
  1276. 'entity_type' => $entity_type,
  1277. 'bundle' => $bundle->name,
  1278. 'label' => 'Contact',
  1279. 'description' => 'Associates an indviddual or organization with this record',
  1280. 'required' => FALSE,
  1281. 'settings' => array(
  1282. 'auto_attach' => FALSE,
  1283. 'chado_table' => $contact_table,
  1284. 'base_table' => $table_name,
  1285. 'chado_column' => 'contact_id',
  1286. ),
  1287. 'widget' => array(
  1288. 'type' => 'local__contact_widget',
  1289. 'settings' => array(
  1290. 'display_label' => 1,
  1291. ),
  1292. ),
  1293. 'display' => array(
  1294. 'default' => array(
  1295. 'label' => 'hidden',
  1296. 'type' => 'local__contact_formatter',
  1297. 'settings' => array(),
  1298. ),
  1299. ),
  1300. );
  1301. }
  1302. // DBXREF
  1303. $dbxref_table = $table_name . '_dbxref';
  1304. if (chado_table_exists($dbxref_table)) {
  1305. $field_name = 'sbo__database_cross_reference';
  1306. $schema = chado_get_schema($dbxref_table);
  1307. $pkey = $schema['primary key'][0];
  1308. $info[$field_name] = array(
  1309. 'field_name' => $field_name,
  1310. 'entity_type' => $entity_type,
  1311. 'bundle' => $bundle->name,
  1312. 'label' => 'Cross Reference',
  1313. 'description' => 'The IDs where this record may be available in other external online databases.',
  1314. 'required' => FALSE,
  1315. 'settings' => array(
  1316. 'auto_attach' => FALSE,
  1317. 'chado_table' => $dbxref_table,
  1318. 'chado_column' => $pkey,
  1319. 'base_table' => $table_name,
  1320. ),
  1321. 'widget' => array(
  1322. 'type' => 'sbo__database_cross_reference_widget',
  1323. 'settings' => array(
  1324. 'display_label' => 1,
  1325. ),
  1326. ),
  1327. 'display' => array(
  1328. 'default' => array(
  1329. 'label' => 'hidden',
  1330. 'type' => 'sbo__database_cross_reference_formatter',
  1331. 'settings' => array(),
  1332. ),
  1333. ),
  1334. );
  1335. }
  1336. // EXPRESSION
  1337. // TODO: this should only show up on gene or mRNA and the GO must be
  1338. // laoded or this field will crash things.
  1339. // $expression_table = $table_name . '_expression';
  1340. // if (chado_table_exists($expression_table)) {
  1341. // $field_name = 'go__gene_expression';
  1342. // $schema = chado_get_schema($expression_table);
  1343. // $pkey = $schema['primary key'][0];
  1344. // $info[$field_name] = array(
  1345. // 'field_name' => $field_name,
  1346. // 'entity_type' => $entity_type,
  1347. // 'bundle' => $bundle->name,
  1348. // 'label' => 'Gene expression',
  1349. // 'description' => 'Information about the expression of this record.',
  1350. // 'required' => FALSE,
  1351. // 'settings' => array(
  1352. // 'auto_attach' => FALSE,
  1353. // 'chado_table' => $expression_table,
  1354. // 'chado_column' => $pkey,
  1355. // 'base_table' => $table_name,
  1356. // ),
  1357. // 'widget' => array(
  1358. // 'type' => 'go__gene_expression_widget',
  1359. // 'settings' => array(
  1360. // 'display_label' => 1,
  1361. // ),
  1362. // ),
  1363. // 'display' => array(
  1364. // 'default' => array(
  1365. // 'label' => 'above',
  1366. // 'type' => 'go__gene_expression_formatter',
  1367. // 'settings' => array(),
  1368. // ),
  1369. // ),
  1370. // );
  1371. // }
  1372. // FEATURELOC
  1373. if ($table_name == 'feature') {
  1374. $field_name = 'data__sequence_coordinates';
  1375. $schema = chado_get_schema('featureloc');
  1376. $pkey = $schema['primary key'][0];
  1377. $info[$field_name] = array(
  1378. 'field_name' => $field_name,
  1379. 'entity_type' => $entity_type,
  1380. 'bundle' => $bundle->name,
  1381. 'label' => 'Sequence Coordinates',
  1382. 'description' => 'The locations on other genomic sequences where this
  1383. record has been aligned.',
  1384. 'required' => FALSE,
  1385. 'settings' => array(
  1386. 'auto_attach' => FALSE,
  1387. 'chado_table' => 'featureloc',
  1388. 'chado_column' => $pkey,
  1389. 'base_table' => 'feature',
  1390. ),
  1391. 'widget' => array(
  1392. 'type' => 'data__sequence_coordinates_widget',
  1393. 'settings' => array(
  1394. 'display_label' => 1,
  1395. ),
  1396. ),
  1397. 'display' => array(
  1398. 'default' => array(
  1399. 'label' => 'hidden',
  1400. 'type' => 'data__sequence_coordinates_formatter',
  1401. 'settings' => array(),
  1402. ),
  1403. ),
  1404. );
  1405. }
  1406. // FEATUREPOS
  1407. if ($table_name == 'feature') {
  1408. $field_name = 'ogi__location_on_map';
  1409. $schema = chado_get_schema('featurepos');
  1410. $pkey = $schema['primary key'][0];
  1411. $info[$field_name] = array(
  1412. 'field_name' => $field_name,
  1413. 'entity_type' => $entity_type,
  1414. 'bundle' => $bundle->name,
  1415. 'label' => 'Location on Map',
  1416. 'description' => 'The positions on a genetic map.',
  1417. 'required' => FALSE,
  1418. 'settings' => array(
  1419. 'auto_attach' => FALSE,
  1420. 'chado_table' => 'featurepos',
  1421. 'chado_column' => $pkey,
  1422. 'base_table' => 'feature',
  1423. ),
  1424. 'widget' => array(
  1425. 'type' => 'ogi__location_on_map_widget',
  1426. 'settings' => array(
  1427. 'display_label' => 1,
  1428. ),
  1429. ),
  1430. 'display' => array(
  1431. 'default' => array(
  1432. 'label' => 'hidden',
  1433. 'type' => 'ogi__location_on_map_formatter',
  1434. 'settings' => array(),
  1435. ),
  1436. ),
  1437. );
  1438. }
  1439. // GENOTYPE
  1440. $genotype_table = $table_name . '_genotype';
  1441. if (chado_table_exists($genotype_table)) {
  1442. $field_name = 'so__genotype';
  1443. $schema = chado_get_schema($genotype_table);
  1444. $pkey = $schema['primary key'][0];
  1445. $info[$field_name] = array(
  1446. 'field_name' => $field_name,
  1447. 'entity_type' => $entity_type,
  1448. 'bundle' => $bundle->name,
  1449. 'label' => 'Genotype',
  1450. 'description' => 'The genotypes associated with this record.',
  1451. 'required' => FALSE,
  1452. 'settings' => array(
  1453. 'auto_attach' => FALSE,
  1454. 'chado_table' => $genotype_table,
  1455. 'chado_column' => $pkey,
  1456. 'base_table' => $table_name,
  1457. ),
  1458. 'widget' => array(
  1459. 'type' => 'so__genotype_widget',
  1460. 'settings' => array(
  1461. 'display_label' => 1,
  1462. ),
  1463. ),
  1464. 'display' => array(
  1465. 'default' => array(
  1466. 'label' => 'hidden',
  1467. 'type' => 'so__genotype_formatter',
  1468. 'settings' => array(),
  1469. ),
  1470. ),
  1471. );
  1472. }
  1473. // PHENOTYPE
  1474. $phenotype_table = $table_name . '_phenotype';
  1475. if (chado_table_exists($phenotype_table)) {
  1476. $field_name = 'sbo__phenotype';
  1477. $schema = chado_get_schema($phenotype_table);
  1478. $pkey = $schema['primary key'][0];
  1479. $info[$field_name] = array(
  1480. 'field_name' => $field_name,
  1481. 'entity_type' => $entity_type,
  1482. 'bundle' => $bundle->name,
  1483. 'label' => 'Phenotype',
  1484. 'description' => 'The phenotypes associated with this record.',
  1485. 'required' => FALSE,
  1486. 'settings' => array(
  1487. 'auto_attach' => FALSE,
  1488. 'chado_table' => $phenotype_table,
  1489. 'chado_column' => $pkey,
  1490. 'base_table' => $table_name,
  1491. ),
  1492. 'widget' => array(
  1493. 'type' => 'sbo__phenotype_widget',
  1494. 'settings' => array(
  1495. 'display_label' => 1,
  1496. ),
  1497. ),
  1498. 'display' => array(
  1499. 'default' => array(
  1500. 'label' => 'hidden',
  1501. 'type' => 'sbo__phenotype_formatter',
  1502. 'settings' => array(),
  1503. ),
  1504. ),
  1505. );
  1506. }
  1507. // PROPERTIES
  1508. $prop_table = $table_name . 'prop';
  1509. if (chado_table_exists($prop_table)) {
  1510. $tschema = chado_get_schema($table_name);
  1511. $schema = chado_get_schema($prop_table);
  1512. $tpkey = $tschema['primary key'][0];
  1513. $pkey = $schema['primary key'][0];
  1514. // Get the list of existing property types for this table.
  1515. $args = array();
  1516. $sql = 'SELECT DISTINCT type_id FROM {' . $prop_table . '}';
  1517. // $sql = "
  1518. // SELECT DISTINCT P.type_id
  1519. // FROM {" . $prop_table . "} P
  1520. // ";
  1521. // if (array_key_exists('type_id', $tschema['fields'])) {
  1522. // $sql .= "
  1523. // INNER JOIN {" . $table_name . "} T on T.$tpkey = P.$tpkey
  1524. // WHERE T.type_id = :cvterm_id
  1525. // ";
  1526. // $args[':cvterm_id'] = $cvterm_id;
  1527. // }
  1528. $props = chado_query($sql, $args);
  1529. while ($prop = $props->fetchObject()) {
  1530. $term = chado_generate_var('cvterm', array('cvterm_id' => $prop->type_id));
  1531. // The tripal_analysis_KEGG, tripal_analysis_blast, and
  1532. // tripal_analysis_interpro modules store results in the analysisprop
  1533. // table which is probably not the best place, but we don't want to
  1534. // create a ton of fields for this.
  1535. if ($prop_table == 'analysisprop' and
  1536. ($term->dbxref_id->db_id->name == 'KEGG_BRITE' or
  1537. $term->dbxref_id->db_id->name == 'tripal')) {
  1538. continue;
  1539. }
  1540. $field_name = strtolower(preg_replace('/[^\w]/','_', $term->dbxref_id->db_id->name . '__' . $term->name));
  1541. $field_name = substr($field_name, 0, 32);
  1542. $info[$field_name] = array(
  1543. 'field_name' => $field_name,
  1544. 'entity_type' => $entity_type,
  1545. 'bundle' => $bundle->name,
  1546. 'label' => ucwords(preg_replace('/_/', ' ', $term->name)),
  1547. 'description' => $term->definition,
  1548. 'required' => FALSE,
  1549. 'settings' => array(
  1550. 'auto_attach' => TRUE,
  1551. 'term_vocabulary' => $term->dbxref_id->db_id->name,
  1552. 'term_accession' => $term->dbxref_id->accession,
  1553. 'term_name' => $term->name,
  1554. 'base_table' => $table_name,
  1555. 'chado_table' => $prop_table,
  1556. 'chado_column' => $pkey,
  1557. ),
  1558. 'widget' => array(
  1559. 'type' => 'chado_linker__prop_widget',
  1560. 'settings' => array(
  1561. 'display_label' => 1,
  1562. ),
  1563. ),
  1564. 'display' => array(
  1565. 'default' => array(
  1566. 'label' => 'hidden',
  1567. 'type' => 'chado_linker__prop_formatter',
  1568. 'settings' => array(),
  1569. ),
  1570. ),
  1571. );
  1572. }
  1573. }
  1574. // PUBLICATIONS
  1575. $pub_table = $table_name . '_pub';
  1576. if (chado_table_exists($pub_table)) {
  1577. $field_name = 'schema__publication';
  1578. $schema = chado_get_schema($pub_table);
  1579. $pkey = $schema['primary key'][0];
  1580. $info[$field_name] = array(
  1581. 'field_name' => $field_name,
  1582. 'entity_type' => $entity_type,
  1583. 'bundle' => $bundle->name,
  1584. 'label' => 'Publication',
  1585. 'description' => 'This record has been referenced or is sourced from these publications.',
  1586. 'required' => FALSE,
  1587. 'settings' => array(
  1588. 'auto_attach' => FALSE,
  1589. 'chado_table' => $pub_table,
  1590. 'chado_column' => $pkey,
  1591. 'base_table' => $table_name,
  1592. ),
  1593. 'widget' => array(
  1594. 'type' => 'schema__publication_widget',
  1595. 'settings' => array(
  1596. 'display_label' => 1,
  1597. ),
  1598. ),
  1599. 'display' => array(
  1600. 'default' => array(
  1601. 'label' => 'hidden',
  1602. 'type' => 'schema__publication_formatter',
  1603. 'settings' => array(),
  1604. ),
  1605. ),
  1606. );
  1607. }
  1608. // PUBLICATIONS (in reverse)
  1609. // We want to be able to show all of the content that a publication links
  1610. // to. The sio__references field does that.
  1611. if ($table_name == 'pub') {
  1612. $field_name = 'sio__references';
  1613. $schema = chado_get_schema($table_name);
  1614. $pkey = $schema['primary key'][0];
  1615. $info[$field_name] = array(
  1616. 'field_name' => $field_name,
  1617. 'entity_type' => $entity_type,
  1618. 'bundle' => $bundle->name,
  1619. 'label' => 'References',
  1620. 'description' => 'Records that are referred to by the publication.',
  1621. 'required' => FALSE,
  1622. 'settings' => array(
  1623. 'auto_attach' => FALSE,
  1624. 'chado_table' => $table_name,
  1625. 'chado_column' => $pkey,
  1626. 'base_table' => $table_name,
  1627. ),
  1628. 'widget' => array(
  1629. 'type' => 'sio__references_widget',
  1630. 'settings' => array(
  1631. 'display_label' => 1,
  1632. ),
  1633. ),
  1634. 'display' => array(
  1635. 'default' => array(
  1636. 'label' => 'hidden',
  1637. 'type' => 'sio__references_formatter',
  1638. 'settings' => array(),
  1639. ),
  1640. ),
  1641. );
  1642. }
  1643. // RELATIONSHIPS
  1644. // If the linker table does not exists then we don't want to add attach.
  1645. $rel_table = $table_name . '_relationship';
  1646. if (chado_table_exists($rel_table)) {
  1647. $field_name = 'sbo__relationship';
  1648. $schema = chado_get_schema($rel_table);
  1649. $pkey = $schema['primary key'][0];
  1650. $info[$field_name] = array(
  1651. 'field_name' => $field_name,
  1652. 'entity_type' => $entity_type,
  1653. 'bundle' => $bundle->name,
  1654. 'label' => 'Relationship',
  1655. 'description' => 'Other records with relationships to this record.',
  1656. 'required' => FALSE,
  1657. 'settings' => array(
  1658. 'auto_attach' => FALSE,
  1659. 'chado_table' => $rel_table,
  1660. 'chado_column' => $pkey,
  1661. 'base_table' => $table_name,
  1662. ),
  1663. 'widget' => array(
  1664. 'type' => 'sbo__relationship_widget',
  1665. 'settings' => array(
  1666. 'display_label' => 1,
  1667. ),
  1668. ),
  1669. 'display' => array(
  1670. 'default' => array(
  1671. 'label' => 'hidden',
  1672. 'type' => 'sbo__relationship_formatter',
  1673. 'settings' => array(),
  1674. ),
  1675. ),
  1676. );
  1677. }
  1678. // SYNONYMS
  1679. $syn_table = $table_name . '_synonym';
  1680. if (chado_table_exists($syn_table)) {
  1681. $field_name = 'schema__alternate_name';
  1682. $schema = chado_get_schema($syn_table);
  1683. $pkey = $schema['primary key'][0];
  1684. $info[$field_name] = array(
  1685. 'field_name' => $field_name,
  1686. 'entity_type' => $entity_type,
  1687. 'bundle' => $bundle->name,
  1688. 'label' => 'Synonyms',
  1689. 'description' => 'Alternate names, aliases or synonyms for this record.',
  1690. 'required' => FALSE,
  1691. 'settings' => array(
  1692. 'auto_attach' => FALSE,
  1693. 'chado_table' => $syn_table,
  1694. 'chado_column' => $pkey,
  1695. 'base_table' => $table_name,
  1696. ),
  1697. 'widget' => array(
  1698. 'type' => 'schema__alternate_name_widget',
  1699. 'settings' => array(
  1700. 'display_label' => 1,
  1701. ),
  1702. ),
  1703. 'display' => array(
  1704. 'default' => array(
  1705. 'label' => 'hidden',
  1706. 'type' => 'schema__alternate_name_formatter',
  1707. 'settings' => array(),
  1708. ),
  1709. ),
  1710. );
  1711. }
  1712. }
  1713. /**
  1714. * Implements hook_bundle_create_user_field().
  1715. *
  1716. * A priviledged user has the ability to add new fields to the bundle. The
  1717. * chado_linker__prop and chado_linker__cvterm fields are allowed to be
  1718. * added dynamically by the user. But, Drupal doesn't know how to deal with
  1719. * it, so this function is called for any field attached to a TripalEntity
  1720. * bundle type. Any fields whose TripalField::$module argument is set to
  1721. * 'tripal_chado' and that can be added dynamically will result in a call
  1722. * to this function.
  1723. */
  1724. function tripal_chado_bundle_create_user_field($new_field, $bundle) {
  1725. // Get the table this bundle is mapped to.
  1726. $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
  1727. $vocab = $term->vocab;
  1728. $params = array(
  1729. 'vocabulary' => $vocab->vocabulary,
  1730. 'accession' => $term->accession,
  1731. );
  1732. $chado_table = $bundle->data_table;
  1733. $chado_type_table = $bundle->type_linker_table;
  1734. $chado_type_column = $bundle->type_column;
  1735. $chado_type_id = $bundle->type_id;
  1736. $chado_type_value = $bundle->type_value;
  1737. // We allow site admins to add new chado_linker__prop fields to an entity.
  1738. // This function will allow us to properly add them. But at this point we
  1739. // don't know the controlled vocabulary term. We'll have to use the
  1740. // defaults and let the user set it using the interface.
  1741. if ($new_field['type'] == 'chado_linker__prop') {
  1742. $table_name = $chado_table . 'prop';
  1743. if (chado_table_exists($table_name)) {
  1744. $schema = chado_get_schema($table_name);
  1745. $pkey = $schema['primary key'][0];
  1746. $field_name = $new_field['field_name'];
  1747. $field_type = 'chado_linker__prop';
  1748. // First add the field.
  1749. field_create_field(array(
  1750. 'field_name' => $field_name,
  1751. 'type' => $field_type,
  1752. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  1753. 'locked' => FALSE,
  1754. 'storage' => array(
  1755. 'type' => 'field_chado_storage',
  1756. ),
  1757. ));
  1758. // Now add the instance
  1759. field_create_instance(array(
  1760. 'field_name' => $field_name,
  1761. 'entity_type' => 'TripalEntity',
  1762. 'bundle' => $bundle->name,
  1763. 'label' => $new_field['label'],
  1764. 'description' => '',
  1765. 'required' => FALSE,
  1766. 'settings' => array(
  1767. 'auto_attach' => TRUE,
  1768. 'base_table' => $chado_table,
  1769. 'chado_table' => $table_name,
  1770. 'chado_column' => $pkey,
  1771. 'term_vocabulary' => '',
  1772. 'term_accession' => '',
  1773. 'term_name' => ''
  1774. ),
  1775. 'widget' => array(
  1776. 'type' => 'chado_linker__prop_widget',
  1777. 'settings' => array(
  1778. 'display_label' => 1,
  1779. ),
  1780. ),
  1781. 'display' => array(
  1782. 'default' => array(
  1783. 'label' => 'inline',
  1784. 'type' => 'chado_linker__prop_formatter',
  1785. 'settings' => array(),
  1786. ),
  1787. ),
  1788. ));
  1789. }
  1790. else {
  1791. drupal_set_message('Cannot add a property field to this entity. Chado does not support properties for this data type.', 'error');
  1792. }
  1793. }
  1794. // We allow site admins to add new chado_linker__cvterm fields to an entity.
  1795. // This function will allow us to properly add them. But at this point we
  1796. // don't know the controlled vocabulary term. We'll have to use the
  1797. // defaults and let the user set it using the interface.
  1798. if ($new_field['type'] == 'chado_linker__cvterm') {
  1799. $table_name = $chado_table . '_cvterm';
  1800. if (chado_table_exists($table_name)) {
  1801. $schema = chado_get_schema($table_name);
  1802. $pkey = $schema['primary key'][0];
  1803. $field_name = $new_field['field_name'];
  1804. $field_type = 'chado_linker__cvterm';
  1805. // First add the field.
  1806. field_create_field(array(
  1807. 'field_name' => $field_name,
  1808. 'type' => $field_type,
  1809. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  1810. 'locked' => FALSE,
  1811. 'storage' => array(
  1812. 'type' => 'field_chado_storage',
  1813. ),
  1814. 'settings' => array(
  1815. 'base_table' => $chado_table,
  1816. 'chado_table' => $table_name,
  1817. 'chado_column' => $pkey,
  1818. ),
  1819. ));
  1820. // Now add the instance
  1821. field_create_instance(array(
  1822. 'field_name' => $field_name,
  1823. 'entity_type' => 'TripalEntity',
  1824. 'bundle' => $bundle->name,
  1825. 'label' => $new_field['label'],
  1826. 'description' => '',
  1827. 'required' => FALSE,
  1828. 'settings' => array(
  1829. 'auto_attach' => TRUE,
  1830. ),
  1831. 'widget' => array(
  1832. 'type' => 'chado_linker__cvterm_widget',
  1833. 'settings' => array(
  1834. 'display_label' => 1,
  1835. ),
  1836. ),
  1837. 'display' => array(
  1838. 'default' => array(
  1839. 'label' => 'above',
  1840. 'type' => 'chado_linker__cvterm_formatter',
  1841. 'settings' => array(),
  1842. ),
  1843. ),
  1844. ));
  1845. }
  1846. else {
  1847. drupal_set_message('Cannot add a property field to this entity. Chado does not support annotations for this data type.', 'error');
  1848. }
  1849. }
  1850. }
  1851. /**
  1852. * Allows for altering of a field's instance setting form.
  1853. *
  1854. * This appears to be a Drupal hook but is actually a custom function created
  1855. * by this module. It is called by the tripal_form_alter() function of this
  1856. * module.
  1857. *
  1858. * Here we put additional form elements for any field, regardless if it is
  1859. * a tripalField or not.
  1860. *
  1861. * @param $form
  1862. * The form array. Alterations to the form can be made within this array.
  1863. * @param $form_state
  1864. * The form state array.
  1865. */
  1866. function tripal_chado_field_instance_settings_form_alter(&$form, $form_state) {
  1867. global $language;
  1868. $field = $form['#field'];
  1869. $instance = $form['#instance'];
  1870. $base_table = array_key_exists('base_table', $instance['settings']) ? $instance['settings']['base_table'] : '';
  1871. $chado_table = array_key_exists('chado_table', $instance['settings']) ? $instance['settings']['chado_table'] : '';
  1872. $chado_column = array_key_exists('chado_column', $instance['settings']) ? $instance['settings']['chado_column'] : '';
  1873. if ($chado_table) {
  1874. // Construct a table for the vocabulary information.
  1875. $headers = array();
  1876. $rows = array();
  1877. $rows[] = array(
  1878. array(
  1879. 'data' => 'Base Table',
  1880. 'header' => TRUE,
  1881. 'width' => '20%',
  1882. ),
  1883. $base_table
  1884. );
  1885. $rows[] = array(
  1886. array(
  1887. 'data' => 'Record Table',
  1888. 'header' => TRUE,
  1889. 'width' => '20%',
  1890. ),
  1891. $chado_table
  1892. );
  1893. $rows[] = array(
  1894. array(
  1895. 'data' => 'ID Column',
  1896. 'header' => TRUE,
  1897. 'width' => '20%',
  1898. ),
  1899. $chado_column
  1900. );
  1901. $table = array(
  1902. 'header' => $headers,
  1903. 'rows' => $rows,
  1904. 'attributes' => array(
  1905. ),
  1906. 'sticky' => FALSE,
  1907. 'caption' => '',
  1908. 'colgroups' => array(),
  1909. 'empty' => '',
  1910. );
  1911. $form['chado_mapping'] = array(
  1912. '#type' => 'fieldset',
  1913. '#title' => 'Chado Mapping',
  1914. '#description' => t('This field maps to data in Chado to the following table:'),
  1915. );
  1916. $form['chado_mapping']['details'] = array(
  1917. '#type' => 'item',
  1918. '#markup' => theme_table($table),
  1919. );
  1920. }
  1921. }
  1922. /**
  1923. * Implements hook_form_FROM_ID_alter()
  1924. */
  1925. function tripal_chado_form_tripalbundle_form_alter(&$form, $form_state) {
  1926. global $language;
  1927. $bundle = $form['#bundle'];
  1928. $term = entity_load('TripalTerm', array('id' => $bundle->term_id));
  1929. $term = reset($term);
  1930. $vocab = $term->vocab;
  1931. $params = array(
  1932. 'vocabulary' => $vocab->vocabulary,
  1933. 'accession' => $term->accession,
  1934. );
  1935. $chado_table = $bundle->data_table;
  1936. $chado_column = $bundle->type_column;
  1937. $chado_type_table = $bundle->type_linker_table;
  1938. $chado_type_id = $bundle->type_id;
  1939. $chado_type_value = $bundle->type_value;
  1940. // Construct a table for the vocabulary information.
  1941. $headers = array();
  1942. $rows = array();
  1943. $rows[] = array(
  1944. array(
  1945. 'data' => 'Chado Table',
  1946. 'header' => TRUE,
  1947. 'width' => '20%',
  1948. ),
  1949. $chado_table
  1950. );
  1951. if ($chado_column) {
  1952. $rows[] = array(
  1953. array(
  1954. 'data' => 'Type Column',
  1955. 'header' => TRUE,
  1956. 'width' => '20%',
  1957. ),
  1958. $chado_column
  1959. );
  1960. }
  1961. if ($chado_type_table) {
  1962. $rows[] = array(
  1963. array(
  1964. 'data' => 'Association Table',
  1965. 'header' => TRUE,
  1966. 'width' => '20%',
  1967. ),
  1968. $chado_type_table
  1969. );
  1970. }
  1971. if ($chado_type_table and $chado_type_id) {
  1972. $rows[] = array(
  1973. array(
  1974. 'data' => 'CVTerm Table ID',
  1975. 'header' => TRUE,
  1976. 'width' => '20%',
  1977. ),
  1978. $chado_type_id
  1979. );
  1980. }
  1981. if ($chado_type_value) {
  1982. $rows[] = array(
  1983. array(
  1984. 'data' => 'Property Value',
  1985. 'header' => TRUE,
  1986. 'width' => '20%',
  1987. ),
  1988. $chado_type_value
  1989. );
  1990. }
  1991. $table = array(
  1992. 'header' => $headers,
  1993. 'rows' => $rows,
  1994. 'attributes' => array(
  1995. ),
  1996. 'sticky' => FALSE,
  1997. 'caption' => '',
  1998. 'colgroups' => array(),
  1999. 'empty' => '',
  2000. );
  2001. $form['chado_mapping'] = array(
  2002. '#type' => 'item',
  2003. '#title' => 'Chado Mapping',
  2004. '#markup' => theme_table($table),
  2005. '#description' => t('This content type maps to the table in Chado
  2006. listed above. Chado allows multiple data types to be housed
  2007. in a single table. Therefore, the column that is used to
  2008. differentiate between data types is also listed above.'),
  2009. '#weight' => 0,
  2010. );
  2011. }