tripal_chado.setup.inc 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820
  1. <?php
  2. /**
  3. * @file
  4. * Functions to install chado schema through Drupal
  5. */
  6. /**
  7. * Prepares Chado for Tripal use
  8. */
  9. function tripal_chado_prepare_form($form, $form_state) {
  10. $form = array();
  11. if (variable_get('tripal_chado_is_prepared') == TRUE) {
  12. drupal_set_message('Your site is prepared.');
  13. }
  14. $form['instructions'] = array(
  15. '#type' => 'item',
  16. '#title' => 'Prepare Drupal for Chado.',
  17. '#description' => t("Before a Drupal site can use Chado (via Tripal), both
  18. Chado and Drupal must be prepared a bit more. Tripal will add some new
  19. materialized views, custom tables and controlled vocabularies to Chado.
  20. It will also add some management tables to Drupal. You only are
  21. required to prepare your Drupal site if this is a brand-new Drupal
  22. installation or if Chado was installed outside of Tripal. If you
  23. installed Chado using Tripal then you do not need to run this step.
  24. If you are upgrading from a previous version of Tripal, you do not
  25. need to prepare your site, and you can click the 'Skip' button."),
  26. );
  27. $form['prepare-button'] = array(
  28. '#type' => 'submit',
  29. '#value' => t('Prepare this site'),
  30. '#name' => 'prepare-chado',
  31. );
  32. $form['skip-button'] = array(
  33. '#type' => 'submit',
  34. '#value' => t('Skip'),
  35. '#name' => 'prepare-skip',
  36. );
  37. return $form;
  38. }
  39. /**
  40. * Submit function for the tripal_chado_prepare_form().
  41. *
  42. * @param $form
  43. * @param $form_state
  44. */
  45. function tripal_chado_prepare_form_submit($form, $form_state) {
  46. if ($form_state['clicked_button']['#name'] == "prepare-chado") {
  47. global $user;
  48. $args = array();
  49. $includes = array(
  50. module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.setup'),
  51. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader'),
  52. );
  53. tripal_add_job('Prepare Chado', 'tripal_chado',
  54. 'tripal_chado_prepare_chado', $args,
  55. $user->uid, 10, $includes);
  56. }
  57. if ($form_state['clicked_button']['#name'] == "prepare-skip") {
  58. variable_set('tripal_chado_is_prepared', TRUE);
  59. }
  60. }
  61. /**
  62. * Prepares Chado for use by Tripal.
  63. */
  64. function tripal_chado_prepare_chado() {
  65. try {
  66. // we want to force the version of Chado to be set properly
  67. $real_version = chado_get_version(TRUE);
  68. // get the effective version. Pass true as second argument
  69. // to warn the user if the current version is not compatible
  70. $version = chado_get_version(FALSE, FALSE);
  71. ///////////////////////////////////////////////////////////////////////////
  72. // Chado DB Module
  73. ///////////////////////////////////////////////////////////////////////////
  74. tripal_insert_db(array(
  75. 'name' => 'null',
  76. 'description' => 'No online database.'
  77. ));
  78. tripal_insert_db(array(
  79. 'name' => 'local',
  80. 'description' => variable_get('site_name', 'This site.'),
  81. ));
  82. ///////////////////////////////////////////////////////////////////////////
  83. // Chado CV Module
  84. ///////////////////////////////////////////////////////////////////////////
  85. // Add the cv_root_mview.
  86. tripal_cv_add_cv_root_mview();
  87. // Add defaults to the tables that correlate OBO files/references with
  88. // a chado CV.
  89. tripal_cv_add_obo_defaults();
  90. // Create the temp table we will use for loading OBO files.
  91. tripal_cv_create_tripal_obo_temp();
  92. // Add the synonym_type vocabulary.
  93. tripal_insert_cv(
  94. 'synonym_type',
  95. 'A vocabulary used for synonym types (e.g. exact, broad, narrow, related).'
  96. );
  97. // Add the Chado ontology CV.
  98. //$obo_path = '{tripal_chado}/files/cv_property.obo';
  99. //$obo_id = tripal_insert_obo('Chado CV Properties', $obo_path);
  100. //tripal_chado_load_obo_v1_2_id($obo_id);
  101. //tripal_submit_obo_job(array('obo_id' => $obo_id));
  102. ///////////////////////////////////////////////////////////////////////////
  103. // Chado Misc
  104. ///////////////////////////////////////////////////////////////////////////
  105. tripal_insert_cv(
  106. 'synonym_type',
  107. 'A vocabulary for storing synonym types.'
  108. );
  109. // set the default vocabularies
  110. tripal_set_default_cv('synonym', 'type_id', 'synonym_type');
  111. // Add cvterms.
  112. tripal_insert_misc_cvterms();
  113. /////////////////////////////////////////////////////////////////////////////
  114. // Chado Organism Module
  115. /////////////////////////////////////////////////////////////////////////////
  116. tripal_insert_cv(
  117. 'organism_property',
  118. 'Contains properties for organisms'
  119. );
  120. // Set the default vocabularies.
  121. tripal_set_default_cv('organismprop', 'type_id', 'organism_property');
  122. // Add taxonomic terms.
  123. $obo_id = tripal_insert_obo('Taxonomic Rank', '{tripal_chado}/files/taxrank.obo');
  124. $cv = chado_select_record('cv', array('name'), array('name' => 'taxonomic_rank'));
  125. if (count($cv) == 0 and $obo_id) {
  126. tripal_chado_load_obo_v1_2_id($obo_id);
  127. }
  128. /////////////////////////////////////////////////////////////////////////////
  129. // Chado CompAnalysis Module
  130. /////////////////////////////////////////////////////////////////////////////
  131. // we may need the analysisfeatureprop table if it doesn't already exist
  132. tripal_analysis_create_analysisfeatureprop();
  133. // add cvterms
  134. tripal_analysis_add_cvterms();
  135. // add materialized views
  136. tripal_analysis_add_mview_analysis_organism();
  137. // set the default vocabularies
  138. tripal_set_default_cv('analysisprop', 'type_id', 'analysis_property');
  139. /////////////////////////////////////////////////////////////////////////////
  140. // Chado Contact Module
  141. /////////////////////////////////////////////////////////////////////////////
  142. // Add the contactprop table to Chado.
  143. tripal_contact_add_custom_tables();
  144. tripal_insert_db(array(
  145. 'name' => 'TContact',
  146. 'description' => 'The Tripal Contact controlled vocabulary.',
  147. ));
  148. tripal_insert_db(array(
  149. 'name' => 'TPUB',
  150. 'description' => 'The Tripal Publication controlled vocabulary.',
  151. ));
  152. // Add loading of the the tripal contact ontology to the job queue.
  153. $obo_path = '{tripal_chado}/files/tcontact.obo';
  154. $obo_id = tripal_insert_obo('Tripal Contacts', $obo_path);
  155. $cv = chado_select_record('cv', array('name'), array('name' => 'tripal_contact'));
  156. if (count($cv) == 0 and $obo) {
  157. tripal_chado_load_obo_v1_2_id($obo_id);
  158. }
  159. //tripal_submit_obo_job(array('obo_id' => $obo_id));
  160. // Add cvterms for relationship types.
  161. tripal_contact_add_cvs();
  162. // Set the default vocabularies.
  163. tripal_set_default_cv('contact', 'type_id', 'tripal_contact');
  164. tripal_set_default_cv('contactprop', 'type_id', 'tripal_contact');
  165. tripal_set_default_cv('contact_relationship', 'type_id', 'contact_relationship');
  166. /////////////////////////////////////////////////////////////////////////////
  167. // Chado Feature Module
  168. /////////////////////////////////////////////////////////////////////////////
  169. // Note: the feature_property OBO that came with Chado v1.2 should not
  170. // be automatically installed. Some of the terms are duplicates of
  171. // others in better maintained vocabularies. New Tripal sites should
  172. // use those.
  173. // $obo_path = '{tripal_feature}/files/feature_property.obo';
  174. // $obo_id = tripal_insert_obo('Chado Feature Properties', $obo_path);
  175. // tripal_chado_load_obo_v1_2_id($obo_id);
  176. //// tripal_submit_obo_job(array('obo_id' => $obo_id));
  177. // Add the vocabularies used by the feature module.
  178. tripal_feature_add_cvs();
  179. // Add the materialized view.
  180. tripal_feature_add_organism_count_mview();
  181. // Add the custom tables.
  182. tripal_feature_add_tripal_gff_temp_table();
  183. tripal_feature_add_tripal_gffcds_temp_table();
  184. tripal_feature_add_tripal_gffprotein_temp_table();
  185. // Load the sequence ontology if it isn't already loaded
  186. $cv = chado_select_record('cv', array('name'), array('name' => 'sequence'));
  187. $obo = tripal_get_obo(array('obo_name' => 'Sequence Ontology'));
  188. if (count($cv) == 0 and $obo) {
  189. print "Installing the Sequence Ontology... This may take a while...\n";
  190. tripal_chado_load_obo_v1_2_id($obo->obo_id);
  191. }
  192. // Set the default vocabularies.
  193. tripal_set_default_cv('feature', 'type_id', 'sequence');
  194. tripal_set_default_cv('featureprop', 'type_id', 'feature_property');
  195. tripal_set_default_cv('feature_relationship', 'type_id', 'feature_relationship');
  196. /////////////////////////////////////////////////////////////////////////////
  197. // Chado Map Module
  198. /////////////////////////////////////////////////////////////////////////////
  199. // add the featuremapprop table to Chado
  200. tripal_featuremap_add_custom_tables();
  201. // Add cvterms
  202. tripal_featuremap_add_cvs();
  203. tripal_featuremap_add_cvterms();
  204. // set the default vocabularies
  205. tripal_set_default_cv('featuremapprop', 'type_id', 'featuremap_property');
  206. tripal_set_default_cv('featureposprop', 'type_id', 'featurepos_property');
  207. tripal_set_default_cv('featuremap', 'unittype_id', 'featuremap_units');
  208. /////////////////////////////////////////////////////////////////////////////
  209. // Chado Library Module
  210. /////////////////////////////////////////////////////////////////////////////
  211. // add the materialized view
  212. tripal_library_add_mview_library_feature_count();
  213. // add cvterms
  214. tripal_library_add_cvs();
  215. tripal_library_add_cvterms();
  216. // set the default vocabularies
  217. tripal_set_default_cv('libraryprop', 'type_id', 'library_property');
  218. tripal_set_default_cv('library', 'type_id', 'library_type');
  219. /////////////////////////////////////////////////////////////////////////////
  220. // Chado NatDiv Module
  221. /////////////////////////////////////////////////////////////////////////////
  222. // add cvterms
  223. tripal_natural_diversity_add_cvterms();
  224. /////////////////////////////////////////////////////////////////////////////
  225. // Chado Project Module
  226. /////////////////////////////////////////////////////////////////////////////
  227. tripal_project_add_cvs();
  228. tripal_project_add_cvterms();
  229. // set the default vocabularies
  230. tripal_set_default_cv('projectprop', 'type_id', 'project_property');
  231. tripal_set_default_cv('project_relationship', 'type_id', 'project_relationship');
  232. /////////////////////////////////////////////////////////////////////////////
  233. // Chado Pub Module
  234. /////////////////////////////////////////////////////////////////////////////
  235. global $base_path;
  236. // add loading of the the tripal pub ontology to the job queue
  237. $obo_path = '{tripal_chado}/files/tpub.obo';
  238. $obo_id = tripal_insert_obo('Tripal Publication', $obo_path);
  239. $cv = chado_select_record('cv', array('name'), array('name' => 'tripal_pub'));
  240. if (count($cv) == 0 and $obo) {
  241. tripal_chado_load_obo_v1_2_id($obo_id);
  242. }
  243. tripal_pub_add_cvs();
  244. tripal_pub_add_dbs();
  245. // add the custom tables
  246. tripal_pub_add_custom_tables();
  247. // set the default vocabularies
  248. tripal_set_default_cv('pub', 'type_id', 'tripal_pub');
  249. tripal_set_default_cv('pubprop', 'type_id', 'tripal_pub');
  250. tripal_set_default_cv('pub_relationship', 'type_id', 'pub_relationship');
  251. // Add the supported loaders
  252. variable_set('tripal_pub_supported_dbs', array('PMID', 'AGL'));
  253. /////////////////////////////////////////////////////////////////////////////
  254. // Chado Stock Module
  255. /////////////////////////////////////////////////////////////////////////////
  256. // add some controlled vocabularies
  257. tripal_stock_add_cvs();
  258. // set the default vocabularies
  259. tripal_set_default_cv('stock', 'type_id', 'stock_type');
  260. tripal_set_default_cv('stockprop', 'type_id', 'stock_property');
  261. tripal_set_default_cv('stock_relationship', 'type_id', 'stock_relationship');
  262. // add the materialized view
  263. tripal_stock_add_organism_count_mview();
  264. /////////////////////////////////////////////////////////////////////////////
  265. // Entity Bundles
  266. /////////////////////////////////////////////////////////////////////////////
  267. // Unfortunately, some Chado base tables do not have a type_id, so we must
  268. // take special action for those tables. These include: organism and
  269. // analysis. Until we can find an appropriate controlled vocabulary
  270. // that is well supported by the community with types for these tables we
  271. // will have to use in-house terms.
  272. // Add a term to be used for an inherent 'type_id' for the organism table.
  273. tripal_insert_cvterm(array(
  274. 'id' => 'local:organism',
  275. 'name' => 'organism',
  276. 'definition' => 'An individual form of life, such as a bacterium, protist, ' .
  277. 'fungus, plant, or animal, composed of a single cell or a complex of cells ' .
  278. 'in which organelles or organs work together to carry out the various ' .
  279. 'processes of life. (American Heritage® Dictionary of the English ' .
  280. 'Language, Fifth Edition. Copyright © 2011 by Houghton Mifflin ' .
  281. 'Harcourt Publishing Company).',
  282. 'cv_name' => 'local',
  283. ));
  284. // Add a term to be used for an inherent 'type_id' for the organism table.
  285. tripal_insert_cvterm(array(
  286. 'id' => 'local:analysis',
  287. 'name' => 'analysis',
  288. 'definition' => 'A process as a method of studying the nature of something ' .
  289. 'or of determining its essential features and their relations. ' .
  290. '(Random House Kernerman Webster\'s College Dictionary, © 2010 K ' .
  291. 'Dictionaries Ltd).',
  292. 'cv_name' => 'local',
  293. ));
  294. tripal_insert_cvterm(array(
  295. 'id' => 'local:project',
  296. 'name' => 'project',
  297. 'definition' => 'A plan or proposal for accomplishing something. ' .
  298. '(American Heritage® Dictionary of the English Language, Fifth Edition. ' .
  299. 'Copyright © 2011 by Houghton Mifflin Harcourt Publishing Company).',
  300. 'cv_name' => 'local',
  301. ));
  302. // For the TripalBundle entities we will want to associate the cvterm_id,
  303. // and the chado table and field that it maps to. We will use a few
  304. // variables to do this:
  305. tripal_insert_variable(
  306. 'chado_cvterm_id',
  307. 'The cvterm_id that a TripalBundle maps to.'
  308. );
  309. tripal_insert_variable(
  310. 'chado_table',
  311. 'The name of the table to which a TripalBundle maps.'
  312. );
  313. tripal_insert_variable(
  314. 'chado_column',
  315. 'The name of the column within the table that a TripalBundle maps to.'
  316. );
  317. // We want to provide a set of commonly used entity types by default. This
  318. // way when a user first installs Tripal there are some commonly used
  319. // formats.
  320. module_load_include('inc', 'tripal', 'api/tripal.api');
  321. module_load_include('inc', 'tripal', 'includes/tripal.admin');
  322. // Create the 'Organism' entity type. This uses the local:organism term.
  323. $error = '';
  324. $term = array('name' => 'organism', 'cv_id' => array('name' => 'local'));
  325. $cvterm = chado_generate_var('cvterm', $term);
  326. if (!tripal_create_bundle('local', 'organism', 'organism', $error)) {
  327. throw new Exception($error);
  328. }
  329. // Create the 'Analysis' entity type. This uses the local:analysis term.
  330. $error = '';
  331. $term = array('name' => 'analysis', 'cv_id' => array('name' => 'local'));
  332. $cvterm = chado_generate_var('cvterm', $term);
  333. if (!tripal_create_bundle('local', 'analysis', 'analysis', $error)) {
  334. throw new Exception($error);
  335. }
  336. // Create the 'Project' entity type. This uses the local:project term.
  337. $error = '';
  338. $term = array('name' => 'project', 'cv_id' => array('name' => 'local'));
  339. $cvterm = chado_generate_var('cvterm', $term);
  340. if (!tripal_create_bundle('local', 'project', 'project', $error)) {
  341. throw new Exception($error);
  342. }
  343. // Set a variable to indicate the site is prepared.
  344. variable_set('tripal_chado_is_prepared', TRUE);
  345. }
  346. catch (Exception $e) {
  347. throw new Exception($e);
  348. }
  349. }
  350. /**
  351. * Creates a materialized view that stores the type & number of stocks per organism
  352. *
  353. * @ingroup tripal_stock
  354. */
  355. function tripal_stock_add_organism_count_mview() {
  356. $view_name = 'organism_stock_count';
  357. $comment = 'Stores the type and number of stocks per organism';
  358. $schema = array(
  359. 'description' => $comment,
  360. 'table' => $view_name,
  361. 'fields' => array(
  362. 'organism_id' => array(
  363. 'size' => 'big',
  364. 'type' => 'int',
  365. 'not null' => TRUE,
  366. ),
  367. 'genus' => array(
  368. 'type' => 'varchar',
  369. 'length' => '255',
  370. 'not null' => TRUE,
  371. ),
  372. 'species' => array(
  373. 'type' => 'varchar',
  374. 'length' => '255',
  375. 'not null' => TRUE,
  376. ),
  377. 'common_name' => array(
  378. 'type' => 'varchar',
  379. 'length' => '255',
  380. 'not null' => FALSE,
  381. ),
  382. 'num_stocks' => array(
  383. 'type' => 'int',
  384. 'not null' => TRUE,
  385. ),
  386. 'cvterm_id' => array(
  387. 'size' => 'big',
  388. 'type' => 'int',
  389. 'not null' => TRUE,
  390. ),
  391. 'stock_type' => array(
  392. 'type' => 'varchar',
  393. 'length' => '255',
  394. 'not null' => TRUE,
  395. ),
  396. ),
  397. 'indexes' => array(
  398. 'organism_stock_count_idx1' => array('organism_id'),
  399. 'organism_stock_count_idx2' => array('cvterm_id'),
  400. 'organism_stock_count_idx3' => array('stock_type'),
  401. ),
  402. );
  403. $sql = "
  404. SELECT
  405. O.organism_id, O.genus, O.species, O.common_name,
  406. count(S.stock_id) as num_stocks,
  407. CVT.cvterm_id, CVT.name as stock_type
  408. FROM organism O
  409. INNER JOIN stock S ON O.Organism_id = S.organism_id
  410. INNER JOIN cvterm CVT ON S.type_id = CVT.cvterm_id
  411. GROUP BY
  412. O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
  413. ";
  414. tripal_add_mview($view_name, 'tripal_stock', $schema, $sql, $comment);
  415. }
  416. /**
  417. * Add cvs related to publications
  418. *
  419. * @ingroup tripal_pub
  420. */
  421. function tripal_stock_add_cvs() {
  422. // Add cv for relationship types
  423. tripal_insert_cv(
  424. 'stock_relationship',
  425. 'Contains types of relationships between stocks.'
  426. );
  427. tripal_insert_cv(
  428. 'stock_property',
  429. 'Contains properties for stocks.'
  430. );
  431. tripal_insert_cv(
  432. 'stock_type',
  433. 'Contains a list of types for stocks.'
  434. );
  435. }
  436. /**
  437. * Add custom table related to publications
  438. * - pubauthor_contact
  439. *
  440. * @ingroup tripal_pub
  441. */
  442. function tripal_pub_add_custom_tables() {
  443. $schema = array (
  444. 'table' => 'pubauthor_contact',
  445. 'fields' => array (
  446. 'pubauthor_contact_id' => array (
  447. 'type' => 'serial',
  448. 'not null' => true,
  449. ),
  450. 'contact_id' => array (
  451. 'type' => 'int',
  452. 'not null' => true,
  453. ),
  454. 'pubauthor_id' => array (
  455. 'type' => 'int',
  456. 'not null' => true,
  457. ),
  458. ),
  459. 'primary key' => array (
  460. 0 => 'pubauthor_contact_id',
  461. ),
  462. 'unique keys' => array (
  463. 'pubauthor_contact_c1' => array (
  464. 0 => 'contact_id',
  465. 1 => 'pubauthor_id',
  466. ),
  467. ),
  468. 'foreign keys' => array (
  469. 'contact' => array (
  470. 'table' => 'contact',
  471. 'columns' => array (
  472. 'contact_id' => 'contact_id',
  473. ),
  474. ),
  475. 'pubauthor' => array (
  476. 'table' => 'pubauthor',
  477. 'columns' => array (
  478. 'pubauthor_id' => 'pubauthor_id',
  479. ),
  480. ),
  481. ),
  482. );
  483. chado_create_custom_table('pubauthor_contact', $schema, TRUE);
  484. }
  485. /**
  486. * Adds dbs related to publications
  487. *
  488. * @ingroup tripal_pub
  489. */
  490. function tripal_pub_add_dbs() {
  491. // make sure we have our supported databases
  492. tripal_insert_db(
  493. array(
  494. 'name' => 'PMID',
  495. 'description' => 'PubMed',
  496. 'url' => 'http://www.ncbi.nlm.nih.gov/pubmed',
  497. 'urlprefix' => 'http://www.ncbi.nlm.nih.gov/pubmed/'
  498. ),
  499. array('update_existing' => TRUE)
  500. );
  501. tripal_insert_db(
  502. array(
  503. 'name' => 'AGL',
  504. 'description' => 'USDA National Agricultural Library',
  505. 'url' => 'http://agricola.nal.usda.gov/'
  506. ),
  507. array('update_existing' => TRUE)
  508. );
  509. }
  510. /**
  511. * Add cvs related to publications
  512. *
  513. * @ingroup tripal_pub
  514. */
  515. function tripal_pub_add_cvs() {
  516. // Add the cv for pub properties
  517. tripal_insert_cv(
  518. 'tripal_pub',
  519. 'A heirarchical set of terms for describing a publication. It is intended to be used as the default vocabularies in Tripal for publication types and contact properties.'
  520. );
  521. // Add the cv for pub types
  522. tripal_insert_cv(
  523. 'pub_type',
  524. 'Contains types of publications. This can be used if the tripal_pub vocabulary (which is default for publications in Tripal) is not desired.'
  525. );
  526. // Add the cv for pub properties
  527. tripal_insert_cv(
  528. 'pub_property',
  529. 'Contains properties for publications. This can be used if the tripal_pub vocabulary (which is default for publications in Tripal) is not desired.'
  530. );
  531. // Add cv for relationship types
  532. tripal_insert_cv(
  533. 'pub_relationship',
  534. 'Contains types of relationships between publications.'
  535. );
  536. }
  537. /**
  538. * Add cvs pertaining to projects
  539. *
  540. * @ingroup tripal_project
  541. */
  542. function tripal_project_add_cvs() {
  543. // Add the cv for project properties
  544. tripal_insert_cv(
  545. 'project_property',
  546. 'Contains properties for projects'
  547. );
  548. // Add cv for relationship types
  549. tripal_insert_cv(
  550. 'project_relationship',
  551. 'Contains Types of relationships between projects.'
  552. );
  553. }
  554. /**
  555. * Add cvterms pertaining to projects
  556. *
  557. * @ingroup tripal_project
  558. */
  559. function tripal_project_add_cvterms() {
  560. // Insert cvterm 'Project Description' into cvterm table of chado
  561. // database. This CV term is used to keep track of the project
  562. // description in the projectprop table.
  563. tripal_insert_cvterm(
  564. array(
  565. 'name' => 'Project Description',
  566. 'definition' => 'Description of a project',
  567. 'cv_name' => 'project_property',
  568. 'is_relationship' => 0,
  569. 'db_name' => 'local'
  570. ),
  571. array('update_existing' => TRUE)
  572. );
  573. }
  574. /**
  575. * Add cvterms related to natural diversity
  576. *
  577. * @ingroup tripal_natural_diversity
  578. */
  579. function tripal_natural_diversity_add_cvterms(){
  580. // add cvterms for the nd_experiment_types
  581. tripal_insert_cvterm(
  582. array(
  583. 'name' => 'Genotyping',
  584. 'definition' => 'An experiment where genotypes of individuals are identified.',
  585. 'cv_name' => 'nd_experiment_types',
  586. 'is_relationship' => 0,
  587. 'db_name' => 'local'
  588. ),
  589. array('update_existing' => TRUE)
  590. );
  591. tripal_insert_cvterm(
  592. array(
  593. 'name' => 'Phenotyping',
  594. 'definition' => 'An experiment where phenotypes of individuals are identified.',
  595. 'cv_name' => 'nd_experiment_types',
  596. 'is_relationship' => 0,
  597. 'db_name' => 'local'
  598. ),
  599. array('update_existing' => TRUE)
  600. );
  601. tripal_insert_cvterm(
  602. array(
  603. 'name' => 'Location',
  604. 'definition' => 'The name of the location.',
  605. 'cv_name' => 'nd_geolocation_property',
  606. 'is_relationship' => 0,
  607. 'db_name' => 'local'
  608. ),
  609. array('update_existing' => TRUE)
  610. );
  611. }
  612. /**
  613. * Adds a materialized view keeping track of the type of features associated with each library
  614. *
  615. * @ingroup tripal_library
  616. */
  617. function tripal_library_add_mview_library_feature_count(){
  618. $view_name = 'library_feature_count';
  619. $comment = 'Provides count of feature by type that are associated with all libraries';
  620. $schema = array(
  621. 'table' => $view_name,
  622. 'description' => $comment,
  623. 'fields' => array(
  624. 'library_id' => array(
  625. 'size' => 'big',
  626. 'type' => 'int',
  627. 'not null' => TRUE,
  628. ),
  629. 'name' => array(
  630. 'type' => 'varchar',
  631. 'length' => 255,
  632. 'not null' => TRUE,
  633. ),
  634. 'num_features' => array(
  635. 'type' => 'int',
  636. 'not null' => TRUE,
  637. ),
  638. 'feature_type' => array(
  639. 'type' => 'varchar',
  640. 'length' => 255,
  641. 'not null' => TRUE,
  642. ),
  643. ),
  644. 'indexes' => array(
  645. 'library_feature_count_idx1' => array('library_id'),
  646. ),
  647. );
  648. $sql = "
  649. SELECT
  650. L.library_id, L.name,
  651. count(F.feature_id) as num_features,
  652. CVT.name as feature_type
  653. FROM library L
  654. INNER JOIN library_feature LF ON LF.library_id = L.library_id
  655. INNER JOIN feature F ON LF.feature_id = F.feature_id
  656. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  657. GROUP BY L.library_id, L.name, CVT.name
  658. ";
  659. tripal_add_mview($view_name, 'tripal_library', $schema, $sql, $comment);
  660. }
  661. /**
  662. * Adds cvterms needed for the library module
  663. *
  664. * @ingroup tripal_library
  665. */
  666. function tripal_library_add_cvterms() {
  667. // Insert cvterm 'library_description' into cvterm table of chado
  668. // database. This CV term is used to keep track of the library
  669. // description in the libraryprop table.
  670. tripal_insert_cvterm(
  671. array(
  672. 'id' => 'local:library_description',
  673. 'name' => 'Library Description',
  674. 'definition' => 'Description of a library',
  675. 'cv_name' => 'library_property',
  676. 'is_relationship' => 0,
  677. 'db_name' => 'local'
  678. ),
  679. array('update_existing' => TRUE)
  680. );
  681. // add cvterms for the map unit types
  682. tripal_insert_cvterm(
  683. array(
  684. 'id' => 'local:cdna_library',
  685. 'name' => 'cdna_library',
  686. 'definition' => 'cDNA library',
  687. 'cv_name' => 'library_type',
  688. 'is_relationship' => 0,
  689. 'db_name' => 'local'
  690. ),
  691. array('update_existing' => TRUE)
  692. );
  693. tripal_insert_cvterm(
  694. array(
  695. 'id' => 'local:bac_library',
  696. 'name' => 'bac_library',
  697. 'definition' => 'Bacterial Artifical Chromsome (BAC) library',
  698. 'cv_name' => 'library_type',
  699. 'is_relationship' => 0,
  700. 'db_name' => 'local'
  701. ),
  702. array('update_existing' => TRUE)
  703. );
  704. tripal_insert_cvterm(
  705. array(
  706. 'id' => 'local:fosmid_library',
  707. 'name' => 'fosmid_library',
  708. 'definition' => 'Fosmid library',
  709. 'cv_name' => 'library_type',
  710. 'is_relationship' => 0,
  711. 'db_name' => 'local'
  712. ),
  713. array('update_existing' => TRUE)
  714. );
  715. tripal_insert_cvterm(
  716. array(
  717. 'id' => 'local:cosmid_library',
  718. 'name' => 'cosmid_library',
  719. 'definition' => 'Cosmid library',
  720. 'cv_name' => 'library_type',
  721. 'is_relationship' => 0,
  722. 'db_name' => 'local'
  723. ),
  724. array('update_existing' => TRUE)
  725. );
  726. tripal_insert_cvterm(
  727. array(
  728. 'id' => 'local:yac_library',
  729. 'name' => 'yac_library',
  730. 'definition' => 'Yeast Artificial Chromosome (YAC) library',
  731. 'cv_name' => 'library_type',
  732. 'is_relationship' => 0,
  733. 'db_name' => 'local'
  734. ),
  735. array('update_existing' => TRUE)
  736. );
  737. tripal_insert_cvterm(
  738. array(
  739. 'id' => 'local:genomic_library',
  740. 'name' => 'genomic_library',
  741. 'definition' => 'Genomic Library',
  742. 'cv_name' => 'library_type',
  743. 'is_relationship' => 0,
  744. 'db_name' => 'local'
  745. ),
  746. array('update_existing' => TRUE)
  747. );
  748. }
  749. /**
  750. * Adds new CV's used by this module
  751. *
  752. * @ingroup tripal_library
  753. */
  754. function tripal_library_add_cvs(){
  755. tripal_insert_cv(
  756. 'library_property',
  757. 'Contains properties for libraries.'
  758. );
  759. tripal_insert_cv(
  760. 'library_type',
  761. 'Contains terms for types of libraries (e.g. BAC, cDNA, FOSMID, etc).'
  762. );
  763. }
  764. /**
  765. * Add custom tables needed by the feature map module
  766. * - featuremapprop
  767. * - featuremap_dbxref
  768. * - featureposprop
  769. *
  770. * @ingroup tripal_featuremap
  771. */
  772. function tripal_featuremap_add_custom_tables(){
  773. // add the featuremaprop table to Chado
  774. $schema = array (
  775. 'table' => 'featuremapprop',
  776. 'fields' => array (
  777. 'featuremapprop_id' => array (
  778. 'type' => 'serial',
  779. 'not null' => true,
  780. ),
  781. 'featuremap_id' => array (
  782. 'type' => 'int',
  783. 'not null' => true,
  784. ),
  785. 'type_id' => array (
  786. 'type' => 'int',
  787. 'not null' => true,
  788. ),
  789. 'value' => array (
  790. 'type' => 'text',
  791. 'not null' => false,
  792. ),
  793. 'rank' => array (
  794. 'type' => 'int',
  795. 'not null' => true,
  796. 'default' => 0,
  797. ),
  798. ),
  799. 'primary key' => array (
  800. 0 => 'featuremapprop_id',
  801. ),
  802. 'unique keys' => array (
  803. 'featuremapprop_c1' => array (
  804. 0 => 'featuremap_id',
  805. 1 => 'type_id',
  806. 2 => 'rank',
  807. ),
  808. ),
  809. 'indexes' => array (
  810. 'featuremapprop_idx1' => array (
  811. 0 => 'featuremap_id',
  812. ),
  813. 'featuremapprop_idx2' => array (
  814. 0 => 'type_id',
  815. ),
  816. ),
  817. 'foreign keys' => array (
  818. 'cvterm' => array (
  819. 'table' => 'cvterm',
  820. 'columns' => array (
  821. 'type_id' => 'cvterm_id',
  822. ),
  823. ),
  824. 'featuremap' => array (
  825. 'table' => 'featuremap',
  826. 'columns' => array (
  827. 'featuremap_id' => 'featuremap_id',
  828. ),
  829. ),
  830. ),
  831. );
  832. chado_create_custom_table('featuremapprop', $schema, TRUE);
  833. // add the featuremap_dbxref table to Chado
  834. $schema = array (
  835. 'table' => 'featuremap_dbxref',
  836. 'fields' => array (
  837. 'featuremap_dbxref_id' => array (
  838. 'type' => 'serial',
  839. 'not null' => true,
  840. ),
  841. 'featuremap_id' => array (
  842. 'type' => 'int',
  843. 'not null' => true,
  844. ),
  845. 'dbxref_id' => array (
  846. 'type' => 'int',
  847. 'not null' => true,
  848. ),
  849. ),
  850. 'primary key' => array (
  851. 0 => 'featuremap_dbxref_id',
  852. ),
  853. 'unique keys' => array (
  854. 'featuremap_dbxref_c1' => array (
  855. 0 => 'featuremap_id',
  856. 1 => 'dbxref_id',
  857. ),
  858. ),
  859. 'indexes' => array (
  860. 'featuremap_dbxref_idx1' => array (
  861. 0 => 'featuremap_dbxref_id',
  862. ),
  863. 'featuremap_dbxref_idx2' => array (
  864. 0 => 'dbxref_id',
  865. ),
  866. ),
  867. 'foreign keys' => array (
  868. 'dbxref' => array (
  869. 'table' => 'dbxref',
  870. 'columns' => array (
  871. 'dbxref_id' => 'dbxref_id',
  872. ),
  873. ),
  874. 'featuremap' => array (
  875. 'table' => 'featuremap',
  876. 'columns' => array (
  877. 'featuremap_id' => 'featuremap_id',
  878. ),
  879. ),
  880. ),
  881. 'referring_tables' => NULL,
  882. );
  883. chado_create_custom_table('featuremap_dbxref', $schema, TRUE);
  884. $schema = array (
  885. 'table' => 'featureposprop',
  886. 'fields' => array (
  887. 'featureposprop_id' => array (
  888. 'type' => 'serial',
  889. 'not null' => true,
  890. ),
  891. 'featurepos_id' => array (
  892. 'type' => 'int',
  893. 'not null' => true,
  894. ),
  895. 'type_id' => array (
  896. 'type' => 'int',
  897. 'not null' => true,
  898. ),
  899. 'value' => array (
  900. 'type' => 'text',
  901. 'not null' => false,
  902. ),
  903. 'rank' => array (
  904. 'type' => 'int',
  905. 'not null' => true,
  906. 'default' => 0,
  907. ),
  908. ),
  909. 'primary key' => array (
  910. 0 => 'featureposprop_id',
  911. ),
  912. 'unique keys' => array (
  913. 'featureposprop_id' => array (
  914. 0 => 'featurepos_id',
  915. 1 => 'type_id',
  916. 2 => 'rank',
  917. ),
  918. ),
  919. 'indexes' => array (
  920. 'featureposprop_c1' => array (
  921. 0 => 'featurepos_id',
  922. ),
  923. 'featureposprop_idx2' => array (
  924. 0 => 'type_id',
  925. ),
  926. ),
  927. 'foreign keys' => array (
  928. 'cvterm' => array (
  929. 'table' => 'cvterm',
  930. 'columns' => array (
  931. 'type_id' => 'cvterm_id',
  932. ),
  933. ),
  934. 'featurepos' => array (
  935. 'table' => 'featurepos',
  936. 'columns' => array (
  937. 'featurepos_id' => 'featurepos_id',
  938. ),
  939. ),
  940. ),
  941. );
  942. chado_create_custom_table('featureposprop', $schema, TRUE);
  943. }
  944. /**
  945. * Add cv terms needed by the featuremap module
  946. *
  947. * @ingroup tripal_featuremap
  948. */
  949. function tripal_featuremap_add_cvterms() {
  950. // add cvterms for the map unit types
  951. tripal_insert_cvterm(
  952. array(
  953. 'name' => 'cM',
  954. 'definition' => 'Centimorgan units',
  955. 'cv_name' => 'featuremap_units',
  956. 'is_relationship' => 0,
  957. 'db_name' => 'local'
  958. ),
  959. array('update_existing' => TRUE)
  960. );
  961. tripal_insert_cvterm(
  962. array(
  963. 'name' => 'bp',
  964. 'definition' => 'Base pairs units',
  965. 'cv_name' => 'featuremap_units',
  966. 'is_relationship' => 0,
  967. 'db_name' => 'local'
  968. ),
  969. array('update_existing' => TRUE)
  970. );
  971. tripal_insert_cvterm(
  972. array(
  973. 'name' => 'bin_unit',
  974. 'definition' => 'The bin unit',
  975. 'cv_name' => 'featuremap_units',
  976. 'is_relationship' => 0,
  977. 'db_name' => 'local'
  978. ),
  979. array('update_existing' => TRUE)
  980. );
  981. tripal_insert_cvterm(
  982. array(
  983. 'name' => 'marker_order',
  984. 'definition' => 'Units simply to define marker order.',
  985. 'cv_name' => 'featuremap_units',
  986. 'is_relationship' => 0,
  987. 'db_name' => 'local'
  988. ),
  989. array('update_existing' => TRUE)
  990. );
  991. tripal_insert_cvterm(
  992. array(
  993. 'name' => 'undefined',
  994. 'definition' => 'A catch-all for an undefined unit type',
  995. 'cv_name' => 'featuremap_units',
  996. 'is_relationship' => 0,
  997. 'db_name' => 'local'
  998. ),
  999. array('update_existing' => TRUE)
  1000. );
  1001. // featurepos properties
  1002. tripal_insert_cvterm(
  1003. array(
  1004. 'name' => 'start',
  1005. 'definition' => 'The start coordinate for a map feature.',
  1006. 'cv_name' => 'featurepos_property',
  1007. 'is_relationship' => 0,
  1008. 'db_name' => 'local'
  1009. ),
  1010. array('update_existing' => TRUE)
  1011. );
  1012. tripal_insert_cvterm(
  1013. array(
  1014. 'name' => 'stop',
  1015. 'definition' => 'The end coordinate for a map feature',
  1016. 'cv_name' => 'featurepos_property',
  1017. 'is_relationship' => 0,
  1018. 'db_name' => 'local'
  1019. ),
  1020. array('update_existing' => TRUE)
  1021. );
  1022. // add cvterms for map properties
  1023. tripal_insert_cvterm(
  1024. array(
  1025. 'name' => 'Map Dbxref',
  1026. 'definition' => 'A unique identifer for the map in a remote database. The '
  1027. . 'format is a database abbreviation and a unique accession separated '
  1028. . 'by a colon. (e.g. Gramene:tsh1996a)',
  1029. 'cv_name' => 'featuremap_property',
  1030. 'is_relationship' => 0,
  1031. 'db_name' => 'local'
  1032. ),
  1033. array('update_existing' => TRUE)
  1034. );
  1035. tripal_insert_cvterm(
  1036. array(
  1037. 'name' => 'Map Type',
  1038. 'definition' => 'The type of Map (e.g. QTL, Physical, etc.)',
  1039. 'cv_name' => 'featuremap_property',
  1040. 'is_relationship' => 0,
  1041. 'db_name' => 'local'
  1042. ),
  1043. array('update_existing' => TRUE)
  1044. );
  1045. tripal_insert_cvterm(
  1046. array(
  1047. 'name' => 'Genome Group',
  1048. 'definition' => '',
  1049. 'cv_name' => 'featuremap_property',
  1050. 'is_relationship' => 0,
  1051. 'db_name' => 'local'
  1052. ),
  1053. array('update_existing' => TRUE)
  1054. );
  1055. tripal_insert_cvterm(
  1056. array(
  1057. 'name' => 'URL',
  1058. 'definition' => 'A univeral resource locator (URL) reference where the '
  1059. . 'publication can be found. For maps found online, this would be '
  1060. . 'the web address for the map.',
  1061. 'cv_name' => 'featuremap_property',
  1062. 'is_relationship' => 0,
  1063. 'db_name' => 'local'
  1064. ),
  1065. array('update_existing' => TRUE)
  1066. );
  1067. tripal_insert_cvterm(
  1068. array(
  1069. 'name' => 'Population Type',
  1070. 'definition' => 'A brief description of the population type used to generate '
  1071. . 'the map (e.g. RIL, F2, BC1, etc).',
  1072. 'cv_name' => 'featuremap_property',
  1073. 'is_relationship' => 0,
  1074. 'db_name' => 'local'
  1075. ),
  1076. array('update_existing' => TRUE)
  1077. );
  1078. tripal_insert_cvterm(
  1079. array(
  1080. 'name' => 'Population Size',
  1081. 'definition' => 'The size of the population used to construct the map.',
  1082. 'cv_name' => 'featuremap_property',
  1083. 'is_relationship' => 0,
  1084. 'db_name' => 'local'
  1085. ),
  1086. array('update_existing' => TRUE)
  1087. );
  1088. tripal_insert_cvterm(
  1089. array(
  1090. 'name' => 'Methods',
  1091. 'definition' => 'A brief description of the methods used to construct the map.',
  1092. 'cv_name' => 'featuremap_property',
  1093. 'is_relationship' => 0,
  1094. 'db_name' => 'local'
  1095. ),
  1096. array('update_existing' => TRUE)
  1097. );
  1098. tripal_insert_cvterm(
  1099. array(
  1100. 'name' => 'Software',
  1101. 'definition' => 'The software used to construct the map.',
  1102. 'cv_name' => 'featuremap_property',
  1103. 'is_relationship' => 0,
  1104. 'db_name' => 'local'
  1105. ),
  1106. array('update_existing' => TRUE)
  1107. );
  1108. }
  1109. /**
  1110. * Add cvs needed by the featuremap module
  1111. *
  1112. * @ingroup tripal_featuremap
  1113. */
  1114. function tripal_featuremap_add_cvs() {
  1115. tripal_insert_cv(
  1116. 'featuremap_units',
  1117. 'Contains map unit types for the unittype_id column of the featuremap table.'
  1118. );
  1119. tripal_insert_cv(
  1120. 'featurepos_property',
  1121. 'Contains terms map properties.'
  1122. );
  1123. tripal_insert_cv(
  1124. 'featuremap_property',
  1125. 'Contains positional types for the feature positions'
  1126. );
  1127. }
  1128. /**
  1129. * Add cvs related to features
  1130. *
  1131. * @ingroup tripal_pub
  1132. */
  1133. function tripal_feature_add_cvs() {
  1134. // Add cv for relationship types
  1135. tripal_insert_cv(
  1136. 'feature_relationship',
  1137. 'Contains types of relationships between features.'
  1138. );
  1139. // The feature_property CV may already exists. It comes with Chado, but
  1140. // we need to add it just in case it doesn't get added before the feature
  1141. // module is installed. But as of Tripal v3.0 the Chado version of this
  1142. // vocabulary is no longer loaded by default.
  1143. tripal_insert_cv(
  1144. 'feature_property',
  1145. 'Stores properties about features'
  1146. );
  1147. // the feature type vocabulary should be the sequence ontology, and even though
  1148. // this ontology should get loaded we will create it here just so that we can
  1149. // set the default vocabulary for the feature.type_id field
  1150. tripal_insert_cv(
  1151. 'sequence',
  1152. 'The Sequence Ontology'
  1153. );
  1154. }
  1155. function tripal_feature_add_tripal_gff_temp_table() {
  1156. $schema = array(
  1157. 'table' => 'tripal_gff_temp',
  1158. 'fields' => array(
  1159. 'feature_id' => array(
  1160. 'type' => 'int',
  1161. 'not null' => TRUE,
  1162. ),
  1163. 'organism_id' => array(
  1164. 'type' => 'int',
  1165. 'not null' => TRUE,
  1166. ),
  1167. 'uniquename' => array(
  1168. 'type' => 'text',
  1169. 'not null' => TRUE,
  1170. ),
  1171. 'type_name' => array(
  1172. 'type' => 'varchar',
  1173. 'length' => '1024',
  1174. 'not null' => TRUE,
  1175. ),
  1176. ),
  1177. 'indexes' => array(
  1178. 'tripal_gff_temp_idx0' => array('feature_id'),
  1179. 'tripal_gff_temp_idx0' => array('organism_id'),
  1180. 'tripal_gff_temp_idx1' => array('uniquename'),
  1181. ),
  1182. 'unique keys' => array(
  1183. 'tripal_gff_temp_uq0' => array('feature_id'),
  1184. 'tripal_gff_temp_uq1' => array('uniquename', 'organism_id', 'type_name'),
  1185. ),
  1186. );
  1187. chado_create_custom_table('tripal_gff_temp', $schema, TRUE);
  1188. }
  1189. /**
  1190. *
  1191. */
  1192. function tripal_feature_add_tripal_gffcds_temp_table($skip_recreate = TRUE) {
  1193. $schema = array(
  1194. 'table' => 'tripal_gffcds_temp',
  1195. 'fields' => array(
  1196. 'feature_id' => array(
  1197. 'type' => 'int',
  1198. 'not null' => TRUE,
  1199. ),
  1200. 'parent_id' => array(
  1201. 'type' => 'int',
  1202. 'not null' => TRUE,
  1203. ),
  1204. 'phase' => array(
  1205. 'type' => 'int',
  1206. 'not null' => TRUE,
  1207. ),
  1208. 'strand' => array(
  1209. 'type' => 'int',
  1210. 'not null' => TRUE,
  1211. ),
  1212. 'fmin' => array(
  1213. 'type' => 'int',
  1214. 'not null' => TRUE,
  1215. ),
  1216. 'fmax' => array(
  1217. 'type' => 'int',
  1218. 'not null' => TRUE,
  1219. ),
  1220. ),
  1221. 'indexes' => array(
  1222. 'tripal_gff_temp_idx0' => array('feature_id'),
  1223. 'tripal_gff_temp_idx0' => array('parent_id'),
  1224. ),
  1225. );
  1226. chado_create_custom_table('tripal_gffcds_temp', $schema, $skip_recreate);
  1227. }
  1228. /**
  1229. *
  1230. */
  1231. function tripal_feature_add_tripal_gffprotein_temp_table() {
  1232. $schema = array(
  1233. 'table' => 'tripal_gffprotein_temp',
  1234. 'fields' => array(
  1235. 'feature_id' => array(
  1236. 'type' => 'int',
  1237. 'not null' => TRUE,
  1238. ),
  1239. 'parent_id' => array(
  1240. 'type' => 'int',
  1241. 'not null' => TRUE,
  1242. ),
  1243. 'fmin' => array(
  1244. 'type' => 'int',
  1245. 'not null' => TRUE,
  1246. ),
  1247. 'fmax' => array(
  1248. 'type' => 'int',
  1249. 'not null' => TRUE,
  1250. ),
  1251. ),
  1252. 'indexes' => array(
  1253. 'tripal_gff_temp_idx0' => array('feature_id'),
  1254. 'tripal_gff_temp_idx0' => array('parent_id'),
  1255. ),
  1256. 'unique keys' => array(
  1257. 'tripal_gff_temp_uq0' => array('feature_id'),
  1258. ),
  1259. );
  1260. chado_create_custom_table('tripal_gffprotein_temp', $schema, TRUE);
  1261. }
  1262. /**
  1263. * Creates a materialized view that stores the type & number of features per organism
  1264. *
  1265. * @ingroup tripal_feature
  1266. */
  1267. function tripal_feature_add_organism_count_mview() {
  1268. $view_name = 'organism_feature_count';
  1269. $comment = 'Stores the type and number of features per organism';
  1270. $schema = array(
  1271. 'description' => $comment,
  1272. 'table' => $view_name,
  1273. 'fields' => array(
  1274. 'organism_id' => array(
  1275. 'size' => 'big',
  1276. 'type' => 'int',
  1277. 'not null' => TRUE,
  1278. ),
  1279. 'genus' => array(
  1280. 'type' => 'varchar',
  1281. 'length' => '255',
  1282. 'not null' => TRUE,
  1283. ),
  1284. 'species' => array(
  1285. 'type' => 'varchar',
  1286. 'length' => '255',
  1287. 'not null' => TRUE,
  1288. ),
  1289. 'common_name' => array(
  1290. 'type' => 'varchar',
  1291. 'length' => '255',
  1292. 'not null' => FALSE,
  1293. ),
  1294. 'num_features' => array(
  1295. 'type' => 'int',
  1296. 'not null' => TRUE,
  1297. ),
  1298. 'cvterm_id' => array(
  1299. 'size' => 'big',
  1300. 'type' => 'int',
  1301. 'not null' => TRUE,
  1302. ),
  1303. 'feature_type' => array(
  1304. 'type' => 'varchar',
  1305. 'length' => '255',
  1306. 'not null' => TRUE,
  1307. ),
  1308. ),
  1309. 'indexes' => array(
  1310. 'organism_feature_count_idx1' => array('organism_id'),
  1311. 'organism_feature_count_idx2' => array('cvterm_id'),
  1312. 'organism_feature_count_idx3' => array('feature_type'),
  1313. ),
  1314. );
  1315. $sql = "
  1316. SELECT
  1317. O.organism_id, O.genus, O.species, O.common_name,
  1318. count(F.feature_id) as num_features,
  1319. CVT.cvterm_id, CVT.name as feature_type
  1320. FROM organism O
  1321. INNER JOIN feature F ON O.Organism_id = F.organism_id
  1322. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  1323. GROUP BY
  1324. O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
  1325. ";
  1326. tripal_add_mview($view_name, 'tripal_feature', $schema, $sql, $comment);
  1327. }
  1328. /**
  1329. * Adds any cvs needed by this module.
  1330. *
  1331. * @ingroup tripal_contact
  1332. */
  1333. function tripal_contact_add_cvs() {
  1334. // Add the cv for contact properties. This is a default vocabulary in the event
  1335. // that a user does not want to use the tripal_contact vocabulary
  1336. tripal_insert_cv(
  1337. 'contact_property',
  1338. 'Contains properties for contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
  1339. );
  1340. // add the cv for the contact type. This is a default vocabulary in the event
  1341. // that a user does not want to use the tripal_contact vocabulary
  1342. tripal_insert_cv(
  1343. 'contact_type',
  1344. 'Contains types of contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
  1345. );
  1346. // Add the cv for the tripal_contact vocabulary which is loaded via the OBO
  1347. tripal_insert_cv(
  1348. 'tripal_contact',
  1349. 'A heirarchical set of terms for describing a contact. It is intended to be used as the default vocabularies in Tripal for contact types and contact properties.'
  1350. );
  1351. // add the cv for contact relationships
  1352. tripal_insert_cv(
  1353. 'contact_relationship',
  1354. 'Contains types of relationships between contacts.'
  1355. );
  1356. }
  1357. /**
  1358. * Add any custom tables needed by this module.
  1359. * - Contactprop: keep track of properties of contact
  1360. *
  1361. * @ingroup tripal_contact
  1362. */
  1363. function tripal_contact_add_custom_tables(){
  1364. $schema = array (
  1365. 'table' => 'contactprop',
  1366. 'fields' => array (
  1367. 'contactprop_id' => array (
  1368. 'type' => 'serial',
  1369. 'not null' => true,
  1370. ),
  1371. 'contact_id' => array (
  1372. 'type' => 'int',
  1373. 'not null' => true,
  1374. ),
  1375. 'type_id' => array (
  1376. 'type' => 'int',
  1377. 'not null' => true,
  1378. ),
  1379. 'value' => array (
  1380. 'type' => 'text',
  1381. 'not null' => false,
  1382. ),
  1383. 'rank' => array (
  1384. 'type' => 'int',
  1385. 'not null' => true,
  1386. 'default' => 0,
  1387. ),
  1388. ),
  1389. 'primary key' => array (
  1390. 0 => 'contactprop_id',
  1391. ),
  1392. 'unique keys' => array (
  1393. 'contactprop_c1' => array (
  1394. 0 => 'contact_id',
  1395. 1 => 'type_id',
  1396. 2 => 'rank',
  1397. ),
  1398. ),
  1399. 'indexes' => array (
  1400. 'contactprop_idx1' => array (
  1401. 0 => 'contact_id',
  1402. ),
  1403. 'contactprop_idx2' => array (
  1404. 0 => 'type_id',
  1405. ),
  1406. ),
  1407. 'foreign keys' => array (
  1408. 'cvterm' => array (
  1409. 'table' => 'cvterm',
  1410. 'columns' => array (
  1411. 'type_id' => 'cvterm_id',
  1412. ),
  1413. ),
  1414. 'contact' => array (
  1415. 'table' => 'contact',
  1416. 'columns' => array (
  1417. 'contact_id' => 'contact_id',
  1418. ),
  1419. ),
  1420. ),
  1421. );
  1422. chado_create_custom_table('contactprop', $schema, TRUE);
  1423. }
  1424. /**
  1425. * Create a legacy custom chado table (analysisfeatureprop) to store properties of
  1426. * analysisfeature links.
  1427. *
  1428. * @ingroup tripal_analysis
  1429. */
  1430. function tripal_analysis_create_analysisfeatureprop() {
  1431. // Create analysisfeatureprop table in chado. This is needed for Chado
  1432. // version 1.11, the table exists in Chado 1.2.
  1433. if (!db_table_exists('chado.analysisfeatureprop')) {
  1434. $sql = "
  1435. CREATE TABLE {analysisfeatureprop} (
  1436. analysisfeatureprop_id SERIAL PRIMARY KEY,
  1437. analysisfeature_id INTEGER NOT NULL,
  1438. type_id INTEGER NOT NULL,
  1439. value TEXT,
  1440. rank INTEGER NOT NULL,
  1441. CONSTRAINT analysisfeature_id_type_id_rank UNIQUE (analysisfeature_id, type_id, rank),
  1442. CONSTRAINT analysisfeatureprop_analysisfeature_id_fkey FOREIGN KEY (analysisfeature_id) REFERENCES {analysisfeature}(analysisfeature_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
  1443. CONSTRAINT analysisfeatureprop_type_id_fkey FOREIGN KEY (type_id) REFERENCES {cvterm}(cvterm_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  1444. )
  1445. ";
  1446. chado_query($sql);
  1447. }
  1448. }
  1449. /**
  1450. * Creates a view showing the link between an organism & it's analysis through associated features.
  1451. *
  1452. * @ingroup tripal_analysis
  1453. */
  1454. function tripal_analysis_add_mview_analysis_organism() {
  1455. $view_name = 'analysis_organism';
  1456. $comment = t('This view is for associating an organism (via it\'s associated features) to an analysis.');
  1457. // this is the SQL used to identify the organism to which an analsysis
  1458. // has been used. This is obtained though the analysisfeature -> feature -> organism
  1459. // joins
  1460. $sql = "
  1461. SELECT DISTINCT A.analysis_id, O.organism_id
  1462. FROM analysis A
  1463. INNER JOIN analysisfeature AF ON A.analysis_id = AF.analysis_id
  1464. INNER JOIN feature F ON AF.feature_id = F.feature_id
  1465. INNER JOIN organism O ON O.organism_id = F.organism_id
  1466. ";
  1467. // the schema array for describing this view
  1468. $schema = array(
  1469. 'table' => $view_name,
  1470. 'description' => $comment,
  1471. 'fields' => array(
  1472. 'analysis_id' => array(
  1473. 'size' => 'big',
  1474. 'type' => 'int',
  1475. 'not null' => TRUE,
  1476. ),
  1477. 'organism_id' => array(
  1478. 'size' => 'big',
  1479. 'type' => 'int',
  1480. 'not null' => TRUE,
  1481. ),
  1482. ),
  1483. 'indexes' => array(
  1484. 'networkmod_qtl_indx0' => array('analysis_id'),
  1485. 'networkmod_qtl_indx1' => array('organism_id'),
  1486. ),
  1487. 'foreign keys' => array(
  1488. 'analysis' => array(
  1489. 'table' => 'analysis',
  1490. 'columns' => array(
  1491. 'analysis_id' => 'analysis_id',
  1492. ),
  1493. ),
  1494. 'organism' => array(
  1495. 'table' => 'organism',
  1496. 'columns' => array(
  1497. 'organism_id' => 'organism_id',
  1498. ),
  1499. ),
  1500. ),
  1501. );
  1502. // add the view
  1503. tripal_add_mview($view_name, 'tripal_analysis', $schema, $sql, $comment);
  1504. }
  1505. /**
  1506. * Adds controlled vocabulary terms needed by this module.
  1507. *
  1508. * @ingroup tripal_analysis
  1509. */
  1510. function tripal_analysis_add_cvterms() {
  1511. tripal_insert_cv(
  1512. 'tripal_analysis',
  1513. 'Terms used for managing analyses in Tripal'
  1514. );
  1515. // add analysis_date. This is no longer used (as far as we can tell) but we don't
  1516. // get rid of it in case it is used, so just keep it in the Tripal CV
  1517. tripal_insert_cvterm(
  1518. array(
  1519. 'name' => 'analysis_date',
  1520. 'definition' => 'The date that an analysis was performed.',
  1521. 'cv_name' => 'tripal_analysis',
  1522. 'is_relationship' => 0,
  1523. 'db_name' => 'local'
  1524. ),
  1525. array('update_existing' => TRUE)
  1526. );
  1527. // add analysis_short_name. This is no longer used (as far as we can tell) but we don't
  1528. // get rid of it in case it is used, so just keep it in the Tripal CV
  1529. tripal_insert_cvterm(
  1530. array(
  1531. 'name' => 'analysis_short_name',
  1532. 'definition' => 'A computer legible (no spaces or special characters) '
  1533. . 'abbreviation for the analysis.',
  1534. 'cv_name' => 'tripal_analysis',
  1535. 'is_relationship' => 0,
  1536. 'db_name' => 'local'
  1537. ),
  1538. array('update_existing' => TRUE)
  1539. );
  1540. // the 'analysis_property' vocabulary is for user definable properties wo we
  1541. // will add an 'Analysis Type' to this vocubulary
  1542. tripal_insert_cvterm(
  1543. array(
  1544. 'name' => 'Analysis Type',
  1545. 'definition' => 'The type of analysis that was performed.',
  1546. 'cv_name' => 'analysis_property',
  1547. 'is_relationship' => 0,
  1548. 'db_name' => 'local'
  1549. ),
  1550. array('update_existing' => TRUE)
  1551. );
  1552. }
  1553. /**
  1554. * Add's defaults to the tripal_cv_obo table
  1555. *
  1556. * @ingroup tripal_cv
  1557. */
  1558. function tripal_cv_add_obo_defaults() {
  1559. // Insert commonly used ontologies into the tables.
  1560. $ontologies = array(
  1561. // array('Relationship Ontology', 'http://purl.obolibrary.org/obo/ro.obo'),
  1562. // array('Relationship Ontology (older deprecated version)', 'http://www.obofoundry.org/ro/ro.obo'),
  1563. array('Sequence Ontology', 'https://raw.githubusercontent.com/The-Sequence-Ontology/SO-Ontologies/master/so-xp-simple.obo'),
  1564. array('Gene Ontology', 'http://geneontology.org/ontology/go.obo'),
  1565. // array('Cell Ontology', 'https://raw.githubusercontent.com/obophenotype/cell-ontology/master/cl.obo'),
  1566. // array('Plant Structure Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_anatomy.obo?view=co'),
  1567. // array('Plant Growth and Development Stages Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_temporal.obo?view=co')
  1568. );
  1569. foreach ($ontologies as $o) {
  1570. db_query("INSERT INTO {tripal_cv_obo} (name,path) VALUES (:name, :path)", array(':name' => $o[0], ':path' => $o[1]));
  1571. }
  1572. }
  1573. /**
  1574. * Add a materialized view of root terms for all chado cvs. This is needed for viewing cv trees
  1575. *
  1576. * @ingroup tripal_cv
  1577. */
  1578. function tripal_cv_add_cv_root_mview() {
  1579. $mv_name = 'cv_root_mview';
  1580. $comment = 'A list of the root terms for all controlled vocabularies. This is needed for viewing CV trees';
  1581. $schema = array(
  1582. 'table' => $mv_name,
  1583. 'description' => $comment,
  1584. 'fields' => array(
  1585. 'name' => array(
  1586. 'type' => 'varchar',
  1587. 'length' => 255,
  1588. 'not null' => TRUE,
  1589. ),
  1590. 'cvterm_id' => array(
  1591. 'size' => 'big',
  1592. 'type' => 'int',
  1593. 'not null' => TRUE,
  1594. ),
  1595. 'cv_id' => array(
  1596. 'size' => 'big',
  1597. 'type' => 'int',
  1598. 'not null' => TRUE,
  1599. ),
  1600. 'cv_name' => array(
  1601. 'type' => 'varchar',
  1602. 'length' => 255,
  1603. 'not null' => TRUE,
  1604. ),
  1605. ),
  1606. 'indexes' => array(
  1607. 'cv_root_mview_indx1' => array('cvterm_id'),
  1608. 'cv_root_mview_indx2' => array('cv_id'),
  1609. ),
  1610. );
  1611. $sql = "
  1612. SELECT DISTINCT CVT.name,CVT.cvterm_id, CV.cv_id, CV.name
  1613. FROM cvterm_relationship CVTR
  1614. INNER JOIN cvterm CVT on CVTR.object_id = CVT.cvterm_id
  1615. INNER JOIN cv CV on CV.cv_id = CVT.cv_id
  1616. WHERE CVTR.object_id not in
  1617. (SELECT subject_id FROM cvterm_relationship)
  1618. ";
  1619. // Create the MView
  1620. tripal_add_mview($mv_name, 'tripal_cv', $schema, $sql, $comment);
  1621. }
  1622. /**
  1623. * Creates a temporary table to store obo details while loading an obo file
  1624. *
  1625. * @ingroup tripal_cv
  1626. */
  1627. function tripal_cv_create_tripal_obo_temp() {
  1628. // the tripal_obo_temp table is used for temporary housing of records when loading OBO files
  1629. // we create it here using plain SQL because we want it to be in the chado schema but we
  1630. // do not want to use the Tripal Custom Table API because we don't want it to appear in the
  1631. // list of custom tables. It needs to be available for the Tripal Chado API so we create it
  1632. // here and then define it in the tripal_cv/api/tripal_cv.schema.api.inc
  1633. if (!db_table_exists('chado.tripal_obo_temp')) {
  1634. $sql = "
  1635. CREATE TABLE {tripal_obo_temp} (
  1636. id character varying(255) NOT NULL,
  1637. stanza text NOT NULL,
  1638. type character varying(50) NOT NULL,
  1639. CONSTRAINT tripal_obo_temp_uq0 UNIQUE (id)
  1640. );
  1641. ";
  1642. chado_query($sql);
  1643. $sql = "CREATE INDEX tripal_obo_temp_idx0 ON {tripal_obo_temp} USING btree (id)";
  1644. chado_query($sql);
  1645. $sql = "CREATE INDEX tripal_obo_temp_idx1 ON {tripal_obo_temp} USING btree (type)";
  1646. chado_query($sql);
  1647. }
  1648. }
  1649. /**
  1650. * Adds generic CVterms.
  1651. */
  1652. function tripal_insert_misc_cvterms() {
  1653. tripal_insert_cvterm(
  1654. array(
  1655. 'name' => 'exact',
  1656. 'id' => "local:exact",
  1657. 'definition' => 'An exact equivalent; interchangeable with the term name.',
  1658. 'is_obsolete' => 0,
  1659. 'cv_name' => 'synonym_type',
  1660. 'is_relationship' => FALSE
  1661. ),
  1662. array('update_existing' => TRUE)
  1663. );
  1664. tripal_insert_cvterm(
  1665. array(
  1666. 'name' => 'broad',
  1667. 'id' => "local:broad",
  1668. 'definition' => 'The synonym is broader than the name.',
  1669. 'is_obsolete' => 0,
  1670. 'cv_name' => 'synonym_type',
  1671. 'is_relationship' => FALSE
  1672. ),
  1673. array('update_existing' => TRUE)
  1674. );
  1675. tripal_insert_cvterm(
  1676. array(
  1677. 'name' => 'narrow',
  1678. 'id' => "local:narrow",
  1679. 'definition' => 'The synonym is narrower or more precise than the name.',
  1680. 'is_obsolete' => 0,
  1681. 'cv_name' => 'synonym_type',
  1682. 'is_relationship' => FALSE
  1683. ),
  1684. array('update_existing' => TRUE)
  1685. );
  1686. tripal_insert_cvterm(
  1687. array(
  1688. 'name' => 'related',
  1689. 'id' => "local:related",
  1690. 'definition' => 'The synonym is related in some way.',
  1691. 'is_obsolete' => 0,
  1692. 'cv_name' => 'synonym_type',
  1693. 'is_relationship' => FALSE
  1694. ),
  1695. array('update_existing' => TRUE)
  1696. );
  1697. }