tripal_chado.fields.inc 51 KB

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