tripal_chado.setup.inc 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864
  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. // TODO: change this to foaf:Project
  295. tripal_insert_cvterm(array(
  296. 'id' => 'local:project',
  297. 'name' => 'project',
  298. 'definition' => 'A plan or proposal for accomplishing something. ' .
  299. '(American Heritage® Dictionary of the English Language, Fifth Edition. ' .
  300. 'Copyright © 2011 by Houghton Mifflin Harcourt Publishing Company).',
  301. 'cv_name' => 'local',
  302. ));
  303. tripal_insert_cvterm(array(
  304. 'id' => 'local:contact',
  305. 'name' => 'contact',
  306. 'definition' => 'An entity (e.g. individual or organization) through ' .
  307. 'whom a person can gain access to information, favors, ' .
  308. 'influential people, and the like.',
  309. 'cv_name' => 'local',
  310. ));
  311. tripal_insert_cvterm(array(
  312. 'id' => 'local:md5_checksum',
  313. 'name' => 'md5_checksum',
  314. 'definition' => 'The MD5 message-digest algorithm is a widely used ' .
  315. 'cryptographic hash function producing a 128-bit (16-byte) hash ' .
  316. 'value, typically expressed in text format as a 32-digit hexadecimal ' .
  317. 'number. MD5 has been utilized in a wide variety of cryptographic ' .
  318. 'applications and is also commonly used to verify data integrity (Wikipedia)',
  319. 'cv_name' => 'local',
  320. ));
  321. tripal_insert_cvterm(array(
  322. 'id' => 'local:relationship',
  323. 'name' => 'relationship',
  324. 'definition' => 'The way in which two things are connected.',
  325. 'cv_name' => 'local',
  326. ));
  327. // For the TripalBundle entities we will want to associate the cvterm_id,
  328. // and the chado table and field that it maps to. We will use a few
  329. // variables to do this:
  330. tripal_insert_variable(
  331. 'chado_cv_id',
  332. 'The ID of the controlled vocabulary in the chado.cv table..'
  333. );
  334. tripal_insert_variable(
  335. 'chado_cvterm_id',
  336. 'The ID of the controlled vocabulary term in the chado.cvterm table.'
  337. );
  338. tripal_insert_variable(
  339. 'chado_table',
  340. 'The name of the table to which a TripalBundle maps.'
  341. );
  342. tripal_insert_variable(
  343. 'chado_type_table',
  344. 'The table that houses the foreign key the cvterm table.'
  345. );
  346. tripal_insert_variable(
  347. 'chado_type_column',
  348. 'The name of the column within the type table that houses the cvterm.'
  349. );
  350. // We want to provide a set of commonly used entity types by default. This
  351. // way when a user first installs Tripal there are some commonly used
  352. // formats.
  353. module_load_include('inc', 'tripal', 'api/tripal.api');
  354. module_load_include('inc', 'tripal', 'includes/tripal.admin');
  355. // Create the 'Organism' entity type. This uses the local:organism term.
  356. $error = '';
  357. $term = array('name' => 'organism', 'cv_id' => array('name' => 'local'));
  358. $cvterm = chado_generate_var('cvterm', $term);
  359. if (!tripal_create_bundle('local', 'organism', 'organism', $error)) {
  360. throw new Exception($error);
  361. }
  362. // Create the 'Analysis' entity type. This uses the local:analysis term.
  363. $error = '';
  364. $term = array('name' => 'analysis', 'cv_id' => array('name' => 'local'));
  365. $cvterm = chado_generate_var('cvterm', $term);
  366. if (!tripal_create_bundle('local', 'analysis', 'analysis', $error)) {
  367. throw new Exception($error);
  368. }
  369. // Create the 'Project' entity type. This uses the local:project term.
  370. $error = '';
  371. $term = array('name' => 'project', 'cv_id' => array('name' => 'local'));
  372. $cvterm = chado_generate_var('cvterm', $term);
  373. if (!tripal_create_bundle('local', 'project', 'project', $error)) {
  374. throw new Exception($error);
  375. }
  376. // Finally deal with the semantic web associations.
  377. module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.semweb');
  378. tripal_chado_populate_chado_semweb_table();
  379. // Set a variable to indicate the site is prepared.
  380. variable_set('tripal_chado_is_prepared', TRUE);
  381. }
  382. catch (Exception $e) {
  383. throw new Exception($e);
  384. }
  385. }
  386. /**
  387. * Creates a materialized view that stores the type & number of stocks per organism
  388. *
  389. * @ingroup tripal_stock
  390. */
  391. function tripal_stock_add_organism_count_mview() {
  392. $view_name = 'organism_stock_count';
  393. $comment = 'Stores the type and number of stocks per organism';
  394. $schema = array(
  395. 'description' => $comment,
  396. 'table' => $view_name,
  397. 'fields' => array(
  398. 'organism_id' => array(
  399. 'size' => 'big',
  400. 'type' => 'int',
  401. 'not null' => TRUE,
  402. ),
  403. 'genus' => array(
  404. 'type' => 'varchar',
  405. 'length' => '255',
  406. 'not null' => TRUE,
  407. ),
  408. 'species' => array(
  409. 'type' => 'varchar',
  410. 'length' => '255',
  411. 'not null' => TRUE,
  412. ),
  413. 'common_name' => array(
  414. 'type' => 'varchar',
  415. 'length' => '255',
  416. 'not null' => FALSE,
  417. ),
  418. 'num_stocks' => array(
  419. 'type' => 'int',
  420. 'not null' => TRUE,
  421. ),
  422. 'cvterm_id' => array(
  423. 'size' => 'big',
  424. 'type' => 'int',
  425. 'not null' => TRUE,
  426. ),
  427. 'stock_type' => array(
  428. 'type' => 'varchar',
  429. 'length' => '255',
  430. 'not null' => TRUE,
  431. ),
  432. ),
  433. 'indexes' => array(
  434. 'organism_stock_count_idx1' => array('organism_id'),
  435. 'organism_stock_count_idx2' => array('cvterm_id'),
  436. 'organism_stock_count_idx3' => array('stock_type'),
  437. ),
  438. );
  439. $sql = "
  440. SELECT
  441. O.organism_id, O.genus, O.species, O.common_name,
  442. count(S.stock_id) as num_stocks,
  443. CVT.cvterm_id, CVT.name as stock_type
  444. FROM organism O
  445. INNER JOIN stock S ON O.Organism_id = S.organism_id
  446. INNER JOIN cvterm CVT ON S.type_id = CVT.cvterm_id
  447. GROUP BY
  448. O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
  449. ";
  450. tripal_add_mview($view_name, 'tripal_stock', $schema, $sql, $comment);
  451. }
  452. /**
  453. * Add cvs related to publications
  454. *
  455. * @ingroup tripal_pub
  456. */
  457. function tripal_stock_add_cvs() {
  458. // Add cv for relationship types
  459. tripal_insert_cv(
  460. 'stock_relationship',
  461. 'Contains types of relationships between stocks.'
  462. );
  463. tripal_insert_cv(
  464. 'stock_property',
  465. 'Contains properties for stocks.'
  466. );
  467. tripal_insert_cv(
  468. 'stock_type',
  469. 'Contains a list of types for stocks.'
  470. );
  471. }
  472. /**
  473. * Add custom table related to publications
  474. * - pubauthor_contact
  475. *
  476. * @ingroup tripal_pub
  477. */
  478. function tripal_pub_add_custom_tables() {
  479. $schema = array (
  480. 'table' => 'pubauthor_contact',
  481. 'fields' => array (
  482. 'pubauthor_contact_id' => array (
  483. 'type' => 'serial',
  484. 'not null' => true,
  485. ),
  486. 'contact_id' => array (
  487. 'type' => 'int',
  488. 'not null' => true,
  489. ),
  490. 'pubauthor_id' => array (
  491. 'type' => 'int',
  492. 'not null' => true,
  493. ),
  494. ),
  495. 'primary key' => array (
  496. 0 => 'pubauthor_contact_id',
  497. ),
  498. 'unique keys' => array (
  499. 'pubauthor_contact_c1' => array (
  500. 0 => 'contact_id',
  501. 1 => 'pubauthor_id',
  502. ),
  503. ),
  504. 'foreign keys' => array (
  505. 'contact' => array (
  506. 'table' => 'contact',
  507. 'columns' => array (
  508. 'contact_id' => 'contact_id',
  509. ),
  510. ),
  511. 'pubauthor' => array (
  512. 'table' => 'pubauthor',
  513. 'columns' => array (
  514. 'pubauthor_id' => 'pubauthor_id',
  515. ),
  516. ),
  517. ),
  518. );
  519. chado_create_custom_table('pubauthor_contact', $schema, TRUE);
  520. }
  521. /**
  522. * Adds dbs related to publications
  523. *
  524. * @ingroup tripal_pub
  525. */
  526. function tripal_pub_add_dbs() {
  527. // make sure we have our supported databases
  528. tripal_insert_db(
  529. array(
  530. 'name' => 'PMID',
  531. 'description' => 'PubMed',
  532. 'url' => 'http://www.ncbi.nlm.nih.gov/pubmed',
  533. 'urlprefix' => 'http://www.ncbi.nlm.nih.gov/pubmed/'
  534. ),
  535. array('update_existing' => TRUE)
  536. );
  537. tripal_insert_db(
  538. array(
  539. 'name' => 'AGL',
  540. 'description' => 'USDA National Agricultural Library',
  541. 'url' => 'http://agricola.nal.usda.gov/'
  542. ),
  543. array('update_existing' => TRUE)
  544. );
  545. }
  546. /**
  547. * Add cvs related to publications
  548. *
  549. * @ingroup tripal_pub
  550. */
  551. function tripal_pub_add_cvs() {
  552. // Add the cv for pub properties
  553. tripal_insert_cv(
  554. 'tripal_pub',
  555. '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.'
  556. );
  557. // Add the cv for pub types
  558. tripal_insert_cv(
  559. 'pub_type',
  560. 'Contains types of publications. This can be used if the tripal_pub vocabulary (which is default for publications in Tripal) is not desired.'
  561. );
  562. // Add the cv for pub properties
  563. tripal_insert_cv(
  564. 'pub_property',
  565. 'Contains properties for publications. This can be used if the tripal_pub vocabulary (which is default for publications in Tripal) is not desired.'
  566. );
  567. // Add cv for relationship types
  568. tripal_insert_cv(
  569. 'pub_relationship',
  570. 'Contains types of relationships between publications.'
  571. );
  572. }
  573. /**
  574. * Add cvs pertaining to projects
  575. *
  576. * @ingroup tripal_project
  577. */
  578. function tripal_project_add_cvs() {
  579. // Add the cv for project properties
  580. tripal_insert_cv(
  581. 'project_property',
  582. 'Contains properties for projects'
  583. );
  584. // Add cv for relationship types
  585. tripal_insert_cv(
  586. 'project_relationship',
  587. 'Contains Types of relationships between projects.'
  588. );
  589. }
  590. /**
  591. * Add cvterms pertaining to projects
  592. *
  593. * @ingroup tripal_project
  594. */
  595. function tripal_project_add_cvterms() {
  596. // Insert cvterm 'Project Description' into cvterm table of chado
  597. // database. This CV term is used to keep track of the project
  598. // description in the projectprop table.
  599. tripal_insert_cvterm(
  600. array(
  601. 'name' => 'Project Description',
  602. 'definition' => 'Description of a project',
  603. 'cv_name' => 'project_property',
  604. 'is_relationship' => 0,
  605. 'db_name' => 'local'
  606. ),
  607. array('update_existing' => TRUE)
  608. );
  609. }
  610. /**
  611. * Add cvterms related to natural diversity
  612. *
  613. * @ingroup tripal_natural_diversity
  614. */
  615. function tripal_natural_diversity_add_cvterms(){
  616. // add cvterms for the nd_experiment_types
  617. tripal_insert_cvterm(
  618. array(
  619. 'name' => 'Genotyping',
  620. 'definition' => 'An experiment where genotypes of individuals are identified.',
  621. 'cv_name' => 'nd_experiment_types',
  622. 'is_relationship' => 0,
  623. 'db_name' => 'local'
  624. ),
  625. array('update_existing' => TRUE)
  626. );
  627. tripal_insert_cvterm(
  628. array(
  629. 'name' => 'Phenotyping',
  630. 'definition' => 'An experiment where phenotypes of individuals are identified.',
  631. 'cv_name' => 'nd_experiment_types',
  632. 'is_relationship' => 0,
  633. 'db_name' => 'local'
  634. ),
  635. array('update_existing' => TRUE)
  636. );
  637. tripal_insert_cvterm(
  638. array(
  639. 'name' => 'Location',
  640. 'definition' => 'The name of the location.',
  641. 'cv_name' => 'nd_geolocation_property',
  642. 'is_relationship' => 0,
  643. 'db_name' => 'local'
  644. ),
  645. array('update_existing' => TRUE)
  646. );
  647. }
  648. /**
  649. * Adds a materialized view keeping track of the type of features associated with each library
  650. *
  651. * @ingroup tripal_library
  652. */
  653. function tripal_library_add_mview_library_feature_count(){
  654. $view_name = 'library_feature_count';
  655. $comment = 'Provides count of feature by type that are associated with all libraries';
  656. $schema = array(
  657. 'table' => $view_name,
  658. 'description' => $comment,
  659. 'fields' => array(
  660. 'library_id' => array(
  661. 'size' => 'big',
  662. 'type' => 'int',
  663. 'not null' => TRUE,
  664. ),
  665. 'name' => array(
  666. 'type' => 'varchar',
  667. 'length' => 255,
  668. 'not null' => TRUE,
  669. ),
  670. 'num_features' => array(
  671. 'type' => 'int',
  672. 'not null' => TRUE,
  673. ),
  674. 'feature_type' => array(
  675. 'type' => 'varchar',
  676. 'length' => 255,
  677. 'not null' => TRUE,
  678. ),
  679. ),
  680. 'indexes' => array(
  681. 'library_feature_count_idx1' => array('library_id'),
  682. ),
  683. );
  684. $sql = "
  685. SELECT
  686. L.library_id, L.name,
  687. count(F.feature_id) as num_features,
  688. CVT.name as feature_type
  689. FROM library L
  690. INNER JOIN library_feature LF ON LF.library_id = L.library_id
  691. INNER JOIN feature F ON LF.feature_id = F.feature_id
  692. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  693. GROUP BY L.library_id, L.name, CVT.name
  694. ";
  695. tripal_add_mview($view_name, 'tripal_library', $schema, $sql, $comment);
  696. }
  697. /**
  698. * Adds cvterms needed for the library module
  699. *
  700. * @ingroup tripal_library
  701. */
  702. function tripal_library_add_cvterms() {
  703. // Insert cvterm 'library_description' into cvterm table of chado
  704. // database. This CV term is used to keep track of the library
  705. // description in the libraryprop table.
  706. tripal_insert_cvterm(
  707. array(
  708. 'id' => 'local:library_description',
  709. 'name' => 'Library Description',
  710. 'definition' => 'Description of a library',
  711. 'cv_name' => 'library_property',
  712. 'is_relationship' => 0,
  713. 'db_name' => 'local'
  714. ),
  715. array('update_existing' => TRUE)
  716. );
  717. // add cvterms for the map unit types
  718. tripal_insert_cvterm(
  719. array(
  720. 'id' => 'local:cdna_library',
  721. 'name' => 'cdna_library',
  722. 'definition' => 'cDNA library',
  723. 'cv_name' => 'library_type',
  724. 'is_relationship' => 0,
  725. 'db_name' => 'local'
  726. ),
  727. array('update_existing' => TRUE)
  728. );
  729. tripal_insert_cvterm(
  730. array(
  731. 'id' => 'local:bac_library',
  732. 'name' => 'bac_library',
  733. 'definition' => 'Bacterial Artifical Chromsome (BAC) library',
  734. 'cv_name' => 'library_type',
  735. 'is_relationship' => 0,
  736. 'db_name' => 'local'
  737. ),
  738. array('update_existing' => TRUE)
  739. );
  740. tripal_insert_cvterm(
  741. array(
  742. 'id' => 'local:fosmid_library',
  743. 'name' => 'fosmid_library',
  744. 'definition' => 'Fosmid library',
  745. 'cv_name' => 'library_type',
  746. 'is_relationship' => 0,
  747. 'db_name' => 'local'
  748. ),
  749. array('update_existing' => TRUE)
  750. );
  751. tripal_insert_cvterm(
  752. array(
  753. 'id' => 'local:cosmid_library',
  754. 'name' => 'cosmid_library',
  755. 'definition' => 'Cosmid library',
  756. 'cv_name' => 'library_type',
  757. 'is_relationship' => 0,
  758. 'db_name' => 'local'
  759. ),
  760. array('update_existing' => TRUE)
  761. );
  762. tripal_insert_cvterm(
  763. array(
  764. 'id' => 'local:yac_library',
  765. 'name' => 'yac_library',
  766. 'definition' => 'Yeast Artificial Chromosome (YAC) library',
  767. 'cv_name' => 'library_type',
  768. 'is_relationship' => 0,
  769. 'db_name' => 'local'
  770. ),
  771. array('update_existing' => TRUE)
  772. );
  773. tripal_insert_cvterm(
  774. array(
  775. 'id' => 'local:genomic_library',
  776. 'name' => 'genomic_library',
  777. 'definition' => 'Genomic Library',
  778. 'cv_name' => 'library_type',
  779. 'is_relationship' => 0,
  780. 'db_name' => 'local'
  781. ),
  782. array('update_existing' => TRUE)
  783. );
  784. }
  785. /**
  786. * Adds new CV's used by this module
  787. *
  788. * @ingroup tripal_library
  789. */
  790. function tripal_library_add_cvs(){
  791. tripal_insert_cv(
  792. 'library_property',
  793. 'Contains properties for libraries.'
  794. );
  795. tripal_insert_cv(
  796. 'library_type',
  797. 'Contains terms for types of libraries (e.g. BAC, cDNA, FOSMID, etc).'
  798. );
  799. }
  800. /**
  801. * Add custom tables needed by the feature map module
  802. * - featuremapprop
  803. * - featuremap_dbxref
  804. * - featureposprop
  805. *
  806. * @ingroup tripal_featuremap
  807. */
  808. function tripal_featuremap_add_custom_tables(){
  809. // add the featuremaprop table to Chado
  810. $schema = array (
  811. 'table' => 'featuremapprop',
  812. 'fields' => array (
  813. 'featuremapprop_id' => array (
  814. 'type' => 'serial',
  815. 'not null' => true,
  816. ),
  817. 'featuremap_id' => array (
  818. 'type' => 'int',
  819. 'not null' => true,
  820. ),
  821. 'type_id' => array (
  822. 'type' => 'int',
  823. 'not null' => true,
  824. ),
  825. 'value' => array (
  826. 'type' => 'text',
  827. 'not null' => false,
  828. ),
  829. 'rank' => array (
  830. 'type' => 'int',
  831. 'not null' => true,
  832. 'default' => 0,
  833. ),
  834. ),
  835. 'primary key' => array (
  836. 0 => 'featuremapprop_id',
  837. ),
  838. 'unique keys' => array (
  839. 'featuremapprop_c1' => array (
  840. 0 => 'featuremap_id',
  841. 1 => 'type_id',
  842. 2 => 'rank',
  843. ),
  844. ),
  845. 'indexes' => array (
  846. 'featuremapprop_idx1' => array (
  847. 0 => 'featuremap_id',
  848. ),
  849. 'featuremapprop_idx2' => array (
  850. 0 => 'type_id',
  851. ),
  852. ),
  853. 'foreign keys' => array (
  854. 'cvterm' => array (
  855. 'table' => 'cvterm',
  856. 'columns' => array (
  857. 'type_id' => 'cvterm_id',
  858. ),
  859. ),
  860. 'featuremap' => array (
  861. 'table' => 'featuremap',
  862. 'columns' => array (
  863. 'featuremap_id' => 'featuremap_id',
  864. ),
  865. ),
  866. ),
  867. );
  868. chado_create_custom_table('featuremapprop', $schema, TRUE);
  869. // add the featuremap_dbxref table to Chado
  870. $schema = array (
  871. 'table' => 'featuremap_dbxref',
  872. 'fields' => array (
  873. 'featuremap_dbxref_id' => array (
  874. 'type' => 'serial',
  875. 'not null' => true,
  876. ),
  877. 'featuremap_id' => array (
  878. 'type' => 'int',
  879. 'not null' => true,
  880. ),
  881. 'dbxref_id' => array (
  882. 'type' => 'int',
  883. 'not null' => true,
  884. ),
  885. ),
  886. 'primary key' => array (
  887. 0 => 'featuremap_dbxref_id',
  888. ),
  889. 'unique keys' => array (
  890. 'featuremap_dbxref_c1' => array (
  891. 0 => 'featuremap_id',
  892. 1 => 'dbxref_id',
  893. ),
  894. ),
  895. 'indexes' => array (
  896. 'featuremap_dbxref_idx1' => array (
  897. 0 => 'featuremap_dbxref_id',
  898. ),
  899. 'featuremap_dbxref_idx2' => array (
  900. 0 => 'dbxref_id',
  901. ),
  902. ),
  903. 'foreign keys' => array (
  904. 'dbxref' => array (
  905. 'table' => 'dbxref',
  906. 'columns' => array (
  907. 'dbxref_id' => 'dbxref_id',
  908. ),
  909. ),
  910. 'featuremap' => array (
  911. 'table' => 'featuremap',
  912. 'columns' => array (
  913. 'featuremap_id' => 'featuremap_id',
  914. ),
  915. ),
  916. ),
  917. 'referring_tables' => NULL,
  918. );
  919. chado_create_custom_table('featuremap_dbxref', $schema, TRUE);
  920. $schema = array (
  921. 'table' => 'featureposprop',
  922. 'fields' => array (
  923. 'featureposprop_id' => array (
  924. 'type' => 'serial',
  925. 'not null' => true,
  926. ),
  927. 'featurepos_id' => array (
  928. 'type' => 'int',
  929. 'not null' => true,
  930. ),
  931. 'type_id' => array (
  932. 'type' => 'int',
  933. 'not null' => true,
  934. ),
  935. 'value' => array (
  936. 'type' => 'text',
  937. 'not null' => false,
  938. ),
  939. 'rank' => array (
  940. 'type' => 'int',
  941. 'not null' => true,
  942. 'default' => 0,
  943. ),
  944. ),
  945. 'primary key' => array (
  946. 0 => 'featureposprop_id',
  947. ),
  948. 'unique keys' => array (
  949. 'featureposprop_id' => array (
  950. 0 => 'featurepos_id',
  951. 1 => 'type_id',
  952. 2 => 'rank',
  953. ),
  954. ),
  955. 'indexes' => array (
  956. 'featureposprop_c1' => array (
  957. 0 => 'featurepos_id',
  958. ),
  959. 'featureposprop_idx2' => array (
  960. 0 => 'type_id',
  961. ),
  962. ),
  963. 'foreign keys' => array (
  964. 'cvterm' => array (
  965. 'table' => 'cvterm',
  966. 'columns' => array (
  967. 'type_id' => 'cvterm_id',
  968. ),
  969. ),
  970. 'featurepos' => array (
  971. 'table' => 'featurepos',
  972. 'columns' => array (
  973. 'featurepos_id' => 'featurepos_id',
  974. ),
  975. ),
  976. ),
  977. );
  978. chado_create_custom_table('featureposprop', $schema, TRUE);
  979. }
  980. /**
  981. * Add cv terms needed by the featuremap module
  982. *
  983. * @ingroup tripal_featuremap
  984. */
  985. function tripal_featuremap_add_cvterms() {
  986. // add cvterms for the map unit types
  987. tripal_insert_cvterm(
  988. array(
  989. 'name' => 'cM',
  990. 'definition' => 'Centimorgan units',
  991. 'cv_name' => 'featuremap_units',
  992. 'is_relationship' => 0,
  993. 'db_name' => 'local'
  994. ),
  995. array('update_existing' => TRUE)
  996. );
  997. tripal_insert_cvterm(
  998. array(
  999. 'name' => 'bp',
  1000. 'definition' => 'Base pairs units',
  1001. 'cv_name' => 'featuremap_units',
  1002. 'is_relationship' => 0,
  1003. 'db_name' => 'local'
  1004. ),
  1005. array('update_existing' => TRUE)
  1006. );
  1007. tripal_insert_cvterm(
  1008. array(
  1009. 'name' => 'bin_unit',
  1010. 'definition' => 'The bin unit',
  1011. 'cv_name' => 'featuremap_units',
  1012. 'is_relationship' => 0,
  1013. 'db_name' => 'local'
  1014. ),
  1015. array('update_existing' => TRUE)
  1016. );
  1017. tripal_insert_cvterm(
  1018. array(
  1019. 'name' => 'marker_order',
  1020. 'definition' => 'Units simply to define marker order.',
  1021. 'cv_name' => 'featuremap_units',
  1022. 'is_relationship' => 0,
  1023. 'db_name' => 'local'
  1024. ),
  1025. array('update_existing' => TRUE)
  1026. );
  1027. tripal_insert_cvterm(
  1028. array(
  1029. 'name' => 'undefined',
  1030. 'definition' => 'A catch-all for an undefined unit type',
  1031. 'cv_name' => 'featuremap_units',
  1032. 'is_relationship' => 0,
  1033. 'db_name' => 'local'
  1034. ),
  1035. array('update_existing' => TRUE)
  1036. );
  1037. // featurepos properties
  1038. tripal_insert_cvterm(
  1039. array(
  1040. 'name' => 'start',
  1041. 'definition' => 'The start coordinate for a map feature.',
  1042. 'cv_name' => 'featurepos_property',
  1043. 'is_relationship' => 0,
  1044. 'db_name' => 'local'
  1045. ),
  1046. array('update_existing' => TRUE)
  1047. );
  1048. tripal_insert_cvterm(
  1049. array(
  1050. 'name' => 'stop',
  1051. 'definition' => 'The end coordinate for a map feature',
  1052. 'cv_name' => 'featurepos_property',
  1053. 'is_relationship' => 0,
  1054. 'db_name' => 'local'
  1055. ),
  1056. array('update_existing' => TRUE)
  1057. );
  1058. // add cvterms for map properties
  1059. tripal_insert_cvterm(
  1060. array(
  1061. 'name' => 'Map Dbxref',
  1062. 'definition' => 'A unique identifer for the map in a remote database. The '
  1063. . 'format is a database abbreviation and a unique accession separated '
  1064. . 'by a colon. (e.g. Gramene:tsh1996a)',
  1065. 'cv_name' => 'featuremap_property',
  1066. 'is_relationship' => 0,
  1067. 'db_name' => 'local'
  1068. ),
  1069. array('update_existing' => TRUE)
  1070. );
  1071. tripal_insert_cvterm(
  1072. array(
  1073. 'name' => 'Map Type',
  1074. 'definition' => 'The type of Map (e.g. QTL, Physical, etc.)',
  1075. 'cv_name' => 'featuremap_property',
  1076. 'is_relationship' => 0,
  1077. 'db_name' => 'local'
  1078. ),
  1079. array('update_existing' => TRUE)
  1080. );
  1081. tripal_insert_cvterm(
  1082. array(
  1083. 'name' => 'Genome Group',
  1084. 'definition' => '',
  1085. 'cv_name' => 'featuremap_property',
  1086. 'is_relationship' => 0,
  1087. 'db_name' => 'local'
  1088. ),
  1089. array('update_existing' => TRUE)
  1090. );
  1091. tripal_insert_cvterm(
  1092. array(
  1093. 'name' => 'URL',
  1094. 'definition' => 'A univeral resource locator (URL) reference where the '
  1095. . 'publication can be found. For maps found online, this would be '
  1096. . 'the web address for the map.',
  1097. 'cv_name' => 'featuremap_property',
  1098. 'is_relationship' => 0,
  1099. 'db_name' => 'local'
  1100. ),
  1101. array('update_existing' => TRUE)
  1102. );
  1103. tripal_insert_cvterm(
  1104. array(
  1105. 'name' => 'Population Type',
  1106. 'definition' => 'A brief description of the population type used to generate '
  1107. . 'the map (e.g. RIL, F2, BC1, etc).',
  1108. 'cv_name' => 'featuremap_property',
  1109. 'is_relationship' => 0,
  1110. 'db_name' => 'local'
  1111. ),
  1112. array('update_existing' => TRUE)
  1113. );
  1114. tripal_insert_cvterm(
  1115. array(
  1116. 'name' => 'Population Size',
  1117. 'definition' => 'The size of the population used to construct the map.',
  1118. 'cv_name' => 'featuremap_property',
  1119. 'is_relationship' => 0,
  1120. 'db_name' => 'local'
  1121. ),
  1122. array('update_existing' => TRUE)
  1123. );
  1124. tripal_insert_cvterm(
  1125. array(
  1126. 'name' => 'Methods',
  1127. 'definition' => 'A brief description of the methods used to construct the map.',
  1128. 'cv_name' => 'featuremap_property',
  1129. 'is_relationship' => 0,
  1130. 'db_name' => 'local'
  1131. ),
  1132. array('update_existing' => TRUE)
  1133. );
  1134. tripal_insert_cvterm(
  1135. array(
  1136. 'name' => 'Software',
  1137. 'definition' => 'The software used to construct the map.',
  1138. 'cv_name' => 'featuremap_property',
  1139. 'is_relationship' => 0,
  1140. 'db_name' => 'local'
  1141. ),
  1142. array('update_existing' => TRUE)
  1143. );
  1144. }
  1145. /**
  1146. * Add cvs needed by the featuremap module
  1147. *
  1148. * @ingroup tripal_featuremap
  1149. */
  1150. function tripal_featuremap_add_cvs() {
  1151. tripal_insert_cv(
  1152. 'featuremap_units',
  1153. 'Contains map unit types for the unittype_id column of the featuremap table.'
  1154. );
  1155. tripal_insert_cv(
  1156. 'featurepos_property',
  1157. 'Contains terms map properties.'
  1158. );
  1159. tripal_insert_cv(
  1160. 'featuremap_property',
  1161. 'Contains positional types for the feature positions'
  1162. );
  1163. }
  1164. /**
  1165. * Add cvs related to features
  1166. *
  1167. * @ingroup tripal_pub
  1168. */
  1169. function tripal_feature_add_cvs() {
  1170. // Add cv for relationship types
  1171. tripal_insert_cv(
  1172. 'feature_relationship',
  1173. 'Contains types of relationships between features.'
  1174. );
  1175. // The feature_property CV may already exists. It comes with Chado, but
  1176. // we need to add it just in case it doesn't get added before the feature
  1177. // module is installed. But as of Tripal v3.0 the Chado version of this
  1178. // vocabulary is no longer loaded by default.
  1179. tripal_insert_cv(
  1180. 'feature_property',
  1181. 'Stores properties about features'
  1182. );
  1183. // the feature type vocabulary should be the sequence ontology, and even though
  1184. // this ontology should get loaded we will create it here just so that we can
  1185. // set the default vocabulary for the feature.type_id field
  1186. tripal_insert_cv(
  1187. 'sequence',
  1188. 'The Sequence Ontology'
  1189. );
  1190. }
  1191. /**
  1192. *
  1193. */
  1194. function tripal_feature_add_tripal_gff_temp_table() {
  1195. $schema = array(
  1196. 'table' => 'tripal_gff_temp',
  1197. 'fields' => array(
  1198. 'feature_id' => array(
  1199. 'type' => 'int',
  1200. 'not null' => TRUE,
  1201. ),
  1202. 'organism_id' => array(
  1203. 'type' => 'int',
  1204. 'not null' => TRUE,
  1205. ),
  1206. 'uniquename' => array(
  1207. 'type' => 'text',
  1208. 'not null' => TRUE,
  1209. ),
  1210. 'type_name' => array(
  1211. 'type' => 'varchar',
  1212. 'length' => '1024',
  1213. 'not null' => TRUE,
  1214. ),
  1215. ),
  1216. 'indexes' => array(
  1217. 'tripal_gff_temp_idx0' => array('feature_id'),
  1218. 'tripal_gff_temp_idx0' => array('organism_id'),
  1219. 'tripal_gff_temp_idx1' => array('uniquename'),
  1220. ),
  1221. 'unique keys' => array(
  1222. 'tripal_gff_temp_uq0' => array('feature_id'),
  1223. 'tripal_gff_temp_uq1' => array('uniquename', 'organism_id', 'type_name'),
  1224. ),
  1225. );
  1226. chado_create_custom_table('tripal_gff_temp', $schema, TRUE);
  1227. }
  1228. /**
  1229. *
  1230. */
  1231. function tripal_feature_add_tripal_gffcds_temp_table($skip_recreate = TRUE) {
  1232. $schema = array(
  1233. 'table' => 'tripal_gffcds_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. 'phase' => array(
  1244. 'type' => 'int',
  1245. 'not null' => TRUE,
  1246. ),
  1247. 'strand' => array(
  1248. 'type' => 'int',
  1249. 'not null' => TRUE,
  1250. ),
  1251. 'fmin' => array(
  1252. 'type' => 'int',
  1253. 'not null' => TRUE,
  1254. ),
  1255. 'fmax' => array(
  1256. 'type' => 'int',
  1257. 'not null' => TRUE,
  1258. ),
  1259. ),
  1260. 'indexes' => array(
  1261. 'tripal_gff_temp_idx0' => array('feature_id'),
  1262. 'tripal_gff_temp_idx0' => array('parent_id'),
  1263. ),
  1264. );
  1265. chado_create_custom_table('tripal_gffcds_temp', $schema, $skip_recreate);
  1266. }
  1267. /**
  1268. *
  1269. */
  1270. function tripal_feature_add_tripal_gffprotein_temp_table() {
  1271. $schema = array(
  1272. 'table' => 'tripal_gffprotein_temp',
  1273. 'fields' => array(
  1274. 'feature_id' => array(
  1275. 'type' => 'int',
  1276. 'not null' => TRUE,
  1277. ),
  1278. 'parent_id' => array(
  1279. 'type' => 'int',
  1280. 'not null' => TRUE,
  1281. ),
  1282. 'fmin' => array(
  1283. 'type' => 'int',
  1284. 'not null' => TRUE,
  1285. ),
  1286. 'fmax' => array(
  1287. 'type' => 'int',
  1288. 'not null' => TRUE,
  1289. ),
  1290. ),
  1291. 'indexes' => array(
  1292. 'tripal_gff_temp_idx0' => array('feature_id'),
  1293. 'tripal_gff_temp_idx0' => array('parent_id'),
  1294. ),
  1295. 'unique keys' => array(
  1296. 'tripal_gff_temp_uq0' => array('feature_id'),
  1297. ),
  1298. );
  1299. chado_create_custom_table('tripal_gffprotein_temp', $schema, TRUE);
  1300. }
  1301. /**
  1302. * Creates a materialized view that stores the type & number of features per organism
  1303. *
  1304. * @ingroup tripal_feature
  1305. */
  1306. function tripal_feature_add_organism_count_mview() {
  1307. $view_name = 'organism_feature_count';
  1308. $comment = 'Stores the type and number of features per organism';
  1309. $schema = array(
  1310. 'description' => $comment,
  1311. 'table' => $view_name,
  1312. 'fields' => array(
  1313. 'organism_id' => array(
  1314. 'size' => 'big',
  1315. 'type' => 'int',
  1316. 'not null' => TRUE,
  1317. ),
  1318. 'genus' => array(
  1319. 'type' => 'varchar',
  1320. 'length' => '255',
  1321. 'not null' => TRUE,
  1322. ),
  1323. 'species' => array(
  1324. 'type' => 'varchar',
  1325. 'length' => '255',
  1326. 'not null' => TRUE,
  1327. ),
  1328. 'common_name' => array(
  1329. 'type' => 'varchar',
  1330. 'length' => '255',
  1331. 'not null' => FALSE,
  1332. ),
  1333. 'num_features' => array(
  1334. 'type' => 'int',
  1335. 'not null' => TRUE,
  1336. ),
  1337. 'cvterm_id' => array(
  1338. 'size' => 'big',
  1339. 'type' => 'int',
  1340. 'not null' => TRUE,
  1341. ),
  1342. 'feature_type' => array(
  1343. 'type' => 'varchar',
  1344. 'length' => '255',
  1345. 'not null' => TRUE,
  1346. ),
  1347. ),
  1348. 'indexes' => array(
  1349. 'organism_feature_count_idx1' => array('organism_id'),
  1350. 'organism_feature_count_idx2' => array('cvterm_id'),
  1351. 'organism_feature_count_idx3' => array('feature_type'),
  1352. ),
  1353. );
  1354. $sql = "
  1355. SELECT
  1356. O.organism_id, O.genus, O.species, O.common_name,
  1357. count(F.feature_id) as num_features,
  1358. CVT.cvterm_id, CVT.name as feature_type
  1359. FROM organism O
  1360. INNER JOIN feature F ON O.Organism_id = F.organism_id
  1361. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  1362. GROUP BY
  1363. O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
  1364. ";
  1365. tripal_add_mview($view_name, 'tripal_feature', $schema, $sql, $comment);
  1366. }
  1367. /**
  1368. * Adds any cvs needed by this module.
  1369. *
  1370. * @ingroup tripal_contact
  1371. */
  1372. function tripal_contact_add_cvs() {
  1373. // Add the cv for contact properties. This is a default vocabulary in the event
  1374. // that a user does not want to use the tripal_contact vocabulary
  1375. tripal_insert_cv(
  1376. 'contact_property',
  1377. 'Contains properties for contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
  1378. );
  1379. // add the cv for the contact type. This is a default vocabulary in the event
  1380. // that a user does not want to use the tripal_contact vocabulary
  1381. tripal_insert_cv(
  1382. 'contact_type',
  1383. 'Contains types of contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
  1384. );
  1385. // Add the cv for the tripal_contact vocabulary which is loaded via the OBO
  1386. tripal_insert_cv(
  1387. 'tripal_contact',
  1388. '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.'
  1389. );
  1390. // add the cv for contact relationships
  1391. tripal_insert_cv(
  1392. 'contact_relationship',
  1393. 'Contains types of relationships between contacts.'
  1394. );
  1395. }
  1396. /**
  1397. * Add any custom tables needed by this module.
  1398. * - Contactprop: keep track of properties of contact
  1399. *
  1400. * @ingroup tripal_contact
  1401. */
  1402. function tripal_contact_add_custom_tables(){
  1403. $schema = array (
  1404. 'table' => 'contactprop',
  1405. 'fields' => array (
  1406. 'contactprop_id' => array (
  1407. 'type' => 'serial',
  1408. 'not null' => true,
  1409. ),
  1410. 'contact_id' => array (
  1411. 'type' => 'int',
  1412. 'not null' => true,
  1413. ),
  1414. 'type_id' => array (
  1415. 'type' => 'int',
  1416. 'not null' => true,
  1417. ),
  1418. 'value' => array (
  1419. 'type' => 'text',
  1420. 'not null' => false,
  1421. ),
  1422. 'rank' => array (
  1423. 'type' => 'int',
  1424. 'not null' => true,
  1425. 'default' => 0,
  1426. ),
  1427. ),
  1428. 'primary key' => array (
  1429. 0 => 'contactprop_id',
  1430. ),
  1431. 'unique keys' => array (
  1432. 'contactprop_c1' => array (
  1433. 0 => 'contact_id',
  1434. 1 => 'type_id',
  1435. 2 => 'rank',
  1436. ),
  1437. ),
  1438. 'indexes' => array (
  1439. 'contactprop_idx1' => array (
  1440. 0 => 'contact_id',
  1441. ),
  1442. 'contactprop_idx2' => array (
  1443. 0 => 'type_id',
  1444. ),
  1445. ),
  1446. 'foreign keys' => array (
  1447. 'cvterm' => array (
  1448. 'table' => 'cvterm',
  1449. 'columns' => array (
  1450. 'type_id' => 'cvterm_id',
  1451. ),
  1452. ),
  1453. 'contact' => array (
  1454. 'table' => 'contact',
  1455. 'columns' => array (
  1456. 'contact_id' => 'contact_id',
  1457. ),
  1458. ),
  1459. ),
  1460. );
  1461. chado_create_custom_table('contactprop', $schema, TRUE);
  1462. }
  1463. /**
  1464. * Create a legacy custom chado table (analysisfeatureprop) to store properties of
  1465. * analysisfeature links.
  1466. *
  1467. * @ingroup tripal_analysis
  1468. */
  1469. function tripal_analysis_create_analysisfeatureprop() {
  1470. // Create analysisfeatureprop table in chado. This is needed for Chado
  1471. // version 1.11, the table exists in Chado 1.2.
  1472. if (!db_table_exists('chado.analysisfeatureprop')) {
  1473. $sql = "
  1474. CREATE TABLE {analysisfeatureprop} (
  1475. analysisfeatureprop_id SERIAL PRIMARY KEY,
  1476. analysisfeature_id INTEGER NOT NULL,
  1477. type_id INTEGER NOT NULL,
  1478. value TEXT,
  1479. rank INTEGER NOT NULL,
  1480. CONSTRAINT analysisfeature_id_type_id_rank UNIQUE (analysisfeature_id, type_id, rank),
  1481. CONSTRAINT analysisfeatureprop_analysisfeature_id_fkey FOREIGN KEY (analysisfeature_id) REFERENCES {analysisfeature}(analysisfeature_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
  1482. CONSTRAINT analysisfeatureprop_type_id_fkey FOREIGN KEY (type_id) REFERENCES {cvterm}(cvterm_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  1483. )
  1484. ";
  1485. chado_query($sql);
  1486. }
  1487. }
  1488. /**
  1489. * Creates a view showing the link between an organism & it's analysis through associated features.
  1490. *
  1491. * @ingroup tripal_analysis
  1492. */
  1493. function tripal_analysis_add_mview_analysis_organism() {
  1494. $view_name = 'analysis_organism';
  1495. $comment = t('This view is for associating an organism (via it\'s associated features) to an analysis.');
  1496. // this is the SQL used to identify the organism to which an analsysis
  1497. // has been used. This is obtained though the analysisfeature -> feature -> organism
  1498. // joins
  1499. $sql = "
  1500. SELECT DISTINCT A.analysis_id, O.organism_id
  1501. FROM analysis A
  1502. INNER JOIN analysisfeature AF ON A.analysis_id = AF.analysis_id
  1503. INNER JOIN feature F ON AF.feature_id = F.feature_id
  1504. INNER JOIN organism O ON O.organism_id = F.organism_id
  1505. ";
  1506. // the schema array for describing this view
  1507. $schema = array(
  1508. 'table' => $view_name,
  1509. 'description' => $comment,
  1510. 'fields' => array(
  1511. 'analysis_id' => array(
  1512. 'size' => 'big',
  1513. 'type' => 'int',
  1514. 'not null' => TRUE,
  1515. ),
  1516. 'organism_id' => array(
  1517. 'size' => 'big',
  1518. 'type' => 'int',
  1519. 'not null' => TRUE,
  1520. ),
  1521. ),
  1522. 'indexes' => array(
  1523. 'networkmod_qtl_indx0' => array('analysis_id'),
  1524. 'networkmod_qtl_indx1' => array('organism_id'),
  1525. ),
  1526. 'foreign keys' => array(
  1527. 'analysis' => array(
  1528. 'table' => 'analysis',
  1529. 'columns' => array(
  1530. 'analysis_id' => 'analysis_id',
  1531. ),
  1532. ),
  1533. 'organism' => array(
  1534. 'table' => 'organism',
  1535. 'columns' => array(
  1536. 'organism_id' => 'organism_id',
  1537. ),
  1538. ),
  1539. ),
  1540. );
  1541. // add the view
  1542. tripal_add_mview($view_name, 'tripal_analysis', $schema, $sql, $comment);
  1543. }
  1544. /**
  1545. * Adds controlled vocabulary terms needed by this module.
  1546. *
  1547. * @ingroup tripal_analysis
  1548. */
  1549. function tripal_analysis_add_cvterms() {
  1550. tripal_insert_cv(
  1551. 'tripal_analysis',
  1552. 'Terms used for managing analyses in Tripal'
  1553. );
  1554. // add analysis_date. This is no longer used (as far as we can tell) but we don't
  1555. // get rid of it in case it is used, so just keep it in the Tripal CV
  1556. tripal_insert_cvterm(
  1557. array(
  1558. 'name' => 'analysis_date',
  1559. 'definition' => 'The date that an analysis was performed.',
  1560. 'cv_name' => 'tripal_analysis',
  1561. 'is_relationship' => 0,
  1562. 'db_name' => 'local'
  1563. ),
  1564. array('update_existing' => TRUE)
  1565. );
  1566. // add analysis_short_name. This is no longer used (as far as we can tell) but we don't
  1567. // get rid of it in case it is used, so just keep it in the Tripal CV
  1568. tripal_insert_cvterm(
  1569. array(
  1570. 'name' => 'analysis_short_name',
  1571. 'definition' => 'A computer legible (no spaces or special characters) '
  1572. . 'abbreviation for the analysis.',
  1573. 'cv_name' => 'tripal_analysis',
  1574. 'is_relationship' => 0,
  1575. 'db_name' => 'local'
  1576. ),
  1577. array('update_existing' => TRUE)
  1578. );
  1579. // the 'analysis_property' vocabulary is for user definable properties wo we
  1580. // will add an 'Analysis Type' to this vocubulary
  1581. tripal_insert_cvterm(
  1582. array(
  1583. 'name' => 'Analysis Type',
  1584. 'definition' => 'The type of analysis that was performed.',
  1585. 'cv_name' => 'analysis_property',
  1586. 'is_relationship' => 0,
  1587. 'db_name' => 'local'
  1588. ),
  1589. array('update_existing' => TRUE)
  1590. );
  1591. }
  1592. /**
  1593. * Add's defaults to the tripal_cv_obo table
  1594. *
  1595. * @ingroup tripal_cv
  1596. */
  1597. function tripal_cv_add_obo_defaults() {
  1598. // Insert commonly used ontologies into the tables.
  1599. $ontologies = array(
  1600. // array('Relationship Ontology', 'http://purl.obolibrary.org/obo/ro.obo'),
  1601. // array('Relationship Ontology (older deprecated version)', 'http://www.obofoundry.org/ro/ro.obo'),
  1602. array('Sequence Ontology', 'https://raw.githubusercontent.com/The-Sequence-Ontology/SO-Ontologies/master/so-xp-simple.obo'),
  1603. array('Gene Ontology', 'http://geneontology.org/ontology/go.obo'),
  1604. // array('Cell Ontology', 'https://raw.githubusercontent.com/obophenotype/cell-ontology/master/cl.obo'),
  1605. // array('Plant Structure Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_anatomy.obo?view=co'),
  1606. // array('Plant Growth and Development Stages Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_temporal.obo?view=co')
  1607. );
  1608. foreach ($ontologies as $o) {
  1609. db_query("INSERT INTO {tripal_cv_obo} (name,path) VALUES (:name, :path)", array(':name' => $o[0], ':path' => $o[1]));
  1610. }
  1611. }
  1612. /**
  1613. * Add a materialized view of root terms for all chado cvs. This is needed for viewing cv trees
  1614. *
  1615. * @ingroup tripal_cv
  1616. */
  1617. function tripal_cv_add_cv_root_mview() {
  1618. $mv_name = 'cv_root_mview';
  1619. $comment = 'A list of the root terms for all controlled vocabularies. This is needed for viewing CV trees';
  1620. $schema = array(
  1621. 'table' => $mv_name,
  1622. 'description' => $comment,
  1623. 'fields' => array(
  1624. 'name' => array(
  1625. 'type' => 'varchar',
  1626. 'length' => 255,
  1627. 'not null' => TRUE,
  1628. ),
  1629. 'cvterm_id' => array(
  1630. 'size' => 'big',
  1631. 'type' => 'int',
  1632. 'not null' => TRUE,
  1633. ),
  1634. 'cv_id' => array(
  1635. 'size' => 'big',
  1636. 'type' => 'int',
  1637. 'not null' => TRUE,
  1638. ),
  1639. 'cv_name' => array(
  1640. 'type' => 'varchar',
  1641. 'length' => 255,
  1642. 'not null' => TRUE,
  1643. ),
  1644. ),
  1645. 'indexes' => array(
  1646. 'cv_root_mview_indx1' => array('cvterm_id'),
  1647. 'cv_root_mview_indx2' => array('cv_id'),
  1648. ),
  1649. );
  1650. $sql = "
  1651. SELECT DISTINCT CVT.name,CVT.cvterm_id, CV.cv_id, CV.name
  1652. FROM cvterm_relationship CVTR
  1653. INNER JOIN cvterm CVT on CVTR.object_id = CVT.cvterm_id
  1654. INNER JOIN cv CV on CV.cv_id = CVT.cv_id
  1655. WHERE CVTR.object_id not in
  1656. (SELECT subject_id FROM cvterm_relationship)
  1657. ";
  1658. // Create the MView
  1659. tripal_add_mview($mv_name, 'tripal_cv', $schema, $sql, $comment);
  1660. }
  1661. /**
  1662. * Creates a temporary table to store obo details while loading an obo file
  1663. *
  1664. * @ingroup tripal_cv
  1665. */
  1666. function tripal_cv_create_tripal_obo_temp() {
  1667. // the tripal_obo_temp table is used for temporary housing of records when loading OBO files
  1668. // we create it here using plain SQL because we want it to be in the chado schema but we
  1669. // do not want to use the Tripal Custom Table API because we don't want it to appear in the
  1670. // list of custom tables. It needs to be available for the Tripal Chado API so we create it
  1671. // here and then define it in the tripal_cv/api/tripal_cv.schema.api.inc
  1672. if (!db_table_exists('chado.tripal_obo_temp')) {
  1673. $sql = "
  1674. CREATE TABLE {tripal_obo_temp} (
  1675. id character varying(255) NOT NULL,
  1676. stanza text NOT NULL,
  1677. type character varying(50) NOT NULL,
  1678. CONSTRAINT tripal_obo_temp_uq0 UNIQUE (id)
  1679. );
  1680. ";
  1681. chado_query($sql);
  1682. $sql = "CREATE INDEX tripal_obo_temp_idx0 ON {tripal_obo_temp} USING btree (id)";
  1683. chado_query($sql);
  1684. $sql = "CREATE INDEX tripal_obo_temp_idx1 ON {tripal_obo_temp} USING btree (type)";
  1685. chado_query($sql);
  1686. }
  1687. }
  1688. /**
  1689. * Adds generic CVterms.
  1690. */
  1691. function tripal_insert_misc_cvterms() {
  1692. tripal_insert_cvterm(
  1693. array(
  1694. 'name' => 'exact',
  1695. 'id' => "local:exact",
  1696. 'definition' => 'An exact equivalent; interchangeable with the term name.',
  1697. 'is_obsolete' => 0,
  1698. 'cv_name' => 'synonym_type',
  1699. 'is_relationship' => FALSE
  1700. ),
  1701. array('update_existing' => TRUE)
  1702. );
  1703. tripal_insert_cvterm(
  1704. array(
  1705. 'name' => 'broad',
  1706. 'id' => "local:broad",
  1707. 'definition' => 'The synonym is broader than the name.',
  1708. 'is_obsolete' => 0,
  1709. 'cv_name' => 'synonym_type',
  1710. 'is_relationship' => FALSE
  1711. ),
  1712. array('update_existing' => TRUE)
  1713. );
  1714. tripal_insert_cvterm(
  1715. array(
  1716. 'name' => 'narrow',
  1717. 'id' => "local:narrow",
  1718. 'definition' => 'The synonym is narrower or more precise than the name.',
  1719. 'is_obsolete' => 0,
  1720. 'cv_name' => 'synonym_type',
  1721. 'is_relationship' => FALSE
  1722. ),
  1723. array('update_existing' => TRUE)
  1724. );
  1725. tripal_insert_cvterm(
  1726. array(
  1727. 'name' => 'related',
  1728. 'id' => "local:related",
  1729. 'definition' => 'The synonym is related in some way.',
  1730. 'is_obsolete' => 0,
  1731. 'cv_name' => 'synonym_type',
  1732. 'is_relationship' => FALSE
  1733. ),
  1734. array('update_existing' => TRUE)
  1735. );
  1736. }