gff_loader.inc 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839
  1. <?php
  2. /**
  3. * @file
  4. * @todo Add file header description
  5. */
  6. /**
  7. * @defgroup gff3_loader GFF3 Feature Loader
  8. * @{
  9. * Provides gff3 loading functionality. Creates features based on their specification in a GFF3 file.
  10. * @}
  11. * @ingroup tripal_feature
  12. */
  13. /**
  14. *
  15. *
  16. * @ingroup gff3_loader
  17. */
  18. function tripal_feature_gff3_load_form() {
  19. $form['gff_file']= array(
  20. '#type' => 'textfield',
  21. '#title' => t('GFF3 File'),
  22. '#description' => t('Please enter the full system path for the GFF file, or a path within the Drupal
  23. installation (e.g. /sites/default/files/xyz.gff). The path must be accessible to the
  24. server on which this Drupal instance is running.'),
  25. '#required' => TRUE,
  26. );
  27. // get the list of organisms
  28. $sql = "SELECT * FROM {organism} ORDER BY genus, species";
  29. $org_rset = chado_query($sql);
  30. $organisms = array();
  31. $organisms[''] = '';
  32. while ($organism = db_fetch_object($org_rset)) {
  33. $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  34. }
  35. $form['organism_id'] = array(
  36. '#title' => t('Organism'),
  37. '#type' => t('select'),
  38. '#description' => t("Choose the organism to which these sequences are associated"),
  39. '#required' => TRUE,
  40. '#options' => $organisms,
  41. );
  42. // get the list of analyses
  43. $sql = "SELECT * FROM {analysis} ORDER BY name";
  44. $org_rset = chado_query($sql);
  45. $analyses = array();
  46. $analyses[''] = '';
  47. while ($analysis = db_fetch_object($org_rset)) {
  48. $analyses[$analysis->analysis_id] = "$analysis->name ($analysis->program $analysis->programversion, $analysis->sourcename)";
  49. }
  50. $form['analysis_id'] = array(
  51. '#title' => t('Analysis'),
  52. '#type' => t('select'),
  53. '#description' => t("Choose the analysis to which these features are associated.
  54. Why specify an analysis for a data load? All data comes
  55. from some place, even if downloaded from Genbank. By specifying
  56. analysis details for all data imports it allows an end user to reproduce the
  57. data set, but at least indicates the source of the data."),
  58. '#required' => TRUE,
  59. '#options' => $analyses,
  60. );
  61. $form['import_options'] = array(
  62. '#type' => 'fieldset',
  63. '#title' => t('Import Options'),
  64. '#collapsed' => TRUE
  65. );
  66. $form['import_options']['use_transaction']= array(
  67. '#type' => 'checkbox',
  68. '#title' => t('Use a transaction'),
  69. '#required' => FALSE,
  70. '#description' => t('Use a database transaction when loading the GFF file. If an error occurs
  71. the entire datset loaded prior to the failure will be rolled back and will not be available
  72. in the database. If this option is unchecked and failure occurs all records up to the point
  73. of failure will be present in the database.'),
  74. );
  75. $form['import_options']['add_only']= array(
  76. '#type' => 'checkbox',
  77. '#title' => t('Import only new features'),
  78. '#required' => FALSE,
  79. '#description' => t('The job will skip features in the GFF file that already
  80. exist in the database and import only new features.'),
  81. );
  82. $form['import_options']['update']= array(
  83. '#type' => 'checkbox',
  84. '#title' => t('Import all and update'),
  85. '#required' => FALSE,
  86. '#default_value' => 'checked',
  87. '#description' => t('Existing features will be updated and new features will be added. Attributes
  88. for a feature that are not present in the GFF but which are present in the
  89. database will not be altered.'),
  90. );
  91. $form['import_options']['refresh']= array(
  92. '#type' => 'checkbox',
  93. '#title' => t('Import all and replace'),
  94. '#required' => FALSE,
  95. '#description' => t('Existing features will be updated and feature properties not
  96. present in the GFF file will be removed.'),
  97. );
  98. $form['import_options']['remove']= array(
  99. '#type' => 'checkbox',
  100. '#title' => t('Delete features'),
  101. '#required' => FALSE,
  102. '#description' => t('Features present in the GFF file that exist in the database
  103. will be removed rather than imported'),
  104. );
  105. $form['targets'] = array(
  106. '#type' => 'fieldset',
  107. '#title' => t('Targets'),
  108. '#collapsed' => TRUE
  109. );
  110. $form['targets']['adesc'] = array(
  111. '#type' => 'markup',
  112. '#value' => t("When alignments are represented in the GFF file (e.g. such as
  113. alignments of cDNA sequences to a whole genome, or blast matches), they are
  114. represented using two feature types: 'match' (or cDNA_match, EST_match, etc.)
  115. and 'match_part'. These features may also have a 'Target' attribute to
  116. specify the sequence that is being aligned.
  117. However, the organism to which the aligned sequence belongs may not be present in the
  118. GFF file. Here you can specify the organism and feature type of the target sequences.
  119. The options here will apply to all targets unless the organism and type are explicity
  120. set in the GFF file using the 'target_organism' and 'target_type' attributes."),
  121. );
  122. $form['targets']['target_organism_id'] = array(
  123. '#title' => t('Target Organism'),
  124. '#type' => t('select'),
  125. '#description' => t("Optional. Choose the organism to which target sequences belong.
  126. Select this only if target sequences belong to a different organism than the
  127. one specified above. And only choose an organism here if all of the target sequences
  128. belong to the same species. If the targets in the GFF file belong to multiple
  129. different species then the organism must be specified using the 'target_organism=genus,species'
  130. attribute in the GFF file."),
  131. '#options' => $organisms,
  132. );
  133. $form['targets']['target_type'] = array(
  134. '#title' => t('Target Type'),
  135. '#type' => t('textfield'),
  136. '#description' => t("Optional. If the unique name for a target sequence is not unique (e.g. a protein
  137. and an mRNA have the same name) then you must specify the type for all targets in the GFF file. If
  138. the targets are of different types then the type must be specified using the 'target_type=type' attribute
  139. in the GFF file. This must be a valid Sequence Ontology (SO) term."),
  140. );
  141. $form['targets']['create_target']= array(
  142. '#type' => 'checkbox',
  143. '#title' => t('Create Target'),
  144. '#required' => FALSE,
  145. '#description' => t("If the target feature cannot be found, create one using the organism and type specified above, or
  146. using the 'target_organism' and 'target_type' fields specified in the GFF file. Values specified in the
  147. GFF file take precedence over those specified above."),
  148. );
  149. $form['button'] = array(
  150. '#type' => 'submit',
  151. '#value' => t('Import GFF3 file'),
  152. '#weight' => 10,
  153. );
  154. return $form;
  155. }
  156. /**
  157. *
  158. *
  159. * @ingroup gff3_loader
  160. */
  161. function tripal_feature_gff3_load_form_validate($form, &$form_state) {
  162. $gff_file = $form_state['values']['gff_file'];
  163. $organism_id = $form_state['values']['organism_id'];
  164. $target_organism_id = $form_state['values']['target_organism_id'];
  165. $target_type = $form_state['values']['target_type'];
  166. $create_target = $form_state['values']['create_target'];
  167. $add_only = $form_state['values']['add_only'];
  168. $update = $form_state['values']['update'];
  169. $refresh = $form_state['values']['refresh'];
  170. $remove = $form_state['values']['remove'];
  171. $use_transaction = $form_state['values']['use_transaction'];
  172. // check to see if the file is located local to Drupal
  173. $gff_file = trim($gff_file);
  174. $dfile = $_SERVER['DOCUMENT_ROOT'] . base_path() . $gff_file;
  175. if (!file_exists($dfile)) {
  176. // if not local to Drupal, the file must be someplace else, just use
  177. // the full path provided
  178. $dfile = $gff_file;
  179. }
  180. if (!file_exists($dfile)) {
  181. form_set_error('gff_file', t("Cannot find the file on the system. Check that the file exists or that the web server has permissions to read the file."));
  182. }
  183. // @coder-ignore: there are no functions being called here
  184. if (($add_only AND ($update OR $refresh OR $remove)) OR
  185. ($update AND ($add_only OR $refresh OR $remove)) OR
  186. ($refresh AND ($update OR $add_only OR $remove)) OR
  187. ($remove AND ($update OR $refresh OR $add_only))) {
  188. form_set_error('add_only', t("Please select only one checkbox from the import options section"));
  189. }
  190. }
  191. /**
  192. *
  193. * @ingroup gff3_loader
  194. */
  195. function tripal_feature_gff3_load_form_submit($form, &$form_state) {
  196. global $user;
  197. $gff_file = $form_state['values']['gff_file'];
  198. $organism_id = $form_state['values']['organism_id'];
  199. $add_only = $form_state['values']['add_only'];
  200. $update = $form_state['values']['update'];
  201. $refresh = $form_state['values']['refresh'];
  202. $remove = $form_state['values']['remove'];
  203. $analysis_id = $form_state['values']['analysis_id'];
  204. $use_transaction = $form_state['values']['use_transaction'];
  205. $target_organism_id = $form_state['values']['target_organism_id'];
  206. $target_type = $form_state['values']['target_type'];
  207. $create_target = $form_state['values']['create_target'];
  208. $args = array($gff_file, $organism_id, $analysis_id, $add_only,
  209. $update, $refresh, $remove, $use_transaction, $target_organism_id,
  210. $target_type, $create_target);
  211. $type = '';
  212. if ($add_only) {
  213. $type = 'import only new features';
  214. }
  215. if ($update) {
  216. $type = 'import all and update';
  217. }
  218. if ($refresh) {
  219. $type = 'import all and replace';
  220. }
  221. if ($remove) {
  222. $type = 'delete features';
  223. }
  224. $fname = preg_replace("/.*\/(.*)/", "$1", $gff_file);
  225. tripal_add_job("$type GFF3 file: $fname", 'tripal_feature',
  226. 'tripal_feature_load_gff3', $args, $user->uid);
  227. return '';
  228. }
  229. /**
  230. *
  231. *
  232. * @ingroup gff3_loader
  233. */
  234. function tripal_feature_load_gff3($gff_file, $organism_id, $analysis_id,
  235. $add_only =0, $update = 0, $refresh = 0, $remove = 0, $use_transaction = 1,
  236. $target_organism_id = NULL, $target_type = NULL, $create_target = 0,
  237. $job = NULL) {
  238. // make sure our temporary table exists
  239. $ret = array();
  240. if (!db_table_exists('tripal_gff_temp')) {
  241. $schema = tripal_feature_get_custom_tables('tripal_gff_temp');
  242. $success = tripal_core_create_custom_table($ret, 'tripal_gff_temp', $schema['tripal_gff_temp']);
  243. if (!$success) {
  244. watchdog('T_gff3_loader', "Cannot create temporary loading table", array(), WATCHDOG_ERROR);
  245. return;
  246. }
  247. }
  248. // empty the temp table
  249. $sql = "DELETE FROM tripal_gff_temp";
  250. chado_query($sql);
  251. // get a persistent connection
  252. $connection = tripal_db_persistent_chado();
  253. if (!$connection) {
  254. print "A persistant connection was not obtained. Loading will be slow\n";
  255. }
  256. // begin the transaction
  257. if ($use_transaction) {
  258. tripal_db_start_transaction();
  259. // if we cannot get a connection then let the user know the loading will be slow
  260. if (!$connection) {
  261. print "A persistant connection was not obtained. Loading will be slow\n";
  262. }
  263. else {
  264. print "\nNOTE: Loading of this GFF file is performed using a database transaction. \n" .
  265. "If the load fails or is terminated prematurely then the entire set of \n" .
  266. "insertions/updates is rolled back and will not be found in the database\n\n";
  267. }
  268. }
  269. // check to see if the file is located local to Drupal
  270. $dfile = $_SERVER['DOCUMENT_ROOT'] . base_path() . $gff_file;
  271. if (!file_exists($dfile)) {
  272. // if not local to Drupal, the file must be someplace else, just use
  273. // the full path provided
  274. $dfile = $gff_file;
  275. }
  276. if (!file_exists($dfile)) {
  277. watchdog('T_gff3_loader', "Cannot find the file: %dfile",
  278. array('%dfile' => $dfile), WATCHDOG_ERROR);
  279. return 0;
  280. }
  281. print "Opening $gff_file\n";
  282. //$lines = file($dfile,FILE_SKIP_EMPTY_LINES);
  283. $fh = fopen($dfile, 'r');
  284. if (!$fh) {
  285. watchdog('T_gff3_loader', "cannot open file: %dfile",
  286. array('%dfile' => $dfile), WATCHDOG_ERROR);
  287. return 0;
  288. }
  289. $filesize = filesize($dfile);
  290. // get the controlled vocaubulary that we'll be using. The
  291. // default is the 'sequence' ontology
  292. // @coder-ignore: non-drupal schema thus table prefixing does not apply
  293. $sql = "SELECT * FROM cv WHERE name = '%s'";
  294. $cv = db_fetch_object(chado_query($sql, 'sequence'));
  295. if (!$cv) {
  296. watchdog('T_gff3_loader', "Cannot find the 'sequence' ontology",
  297. array(), WATCHDOG_ERROR);
  298. return '';
  299. }
  300. // get the organism for which this GFF3 file belongs
  301. // @coder-ignore: non-drupal schema thus table prefixing does not apply
  302. $sql = "SELECT * FROM organism WHERE organism_id = %d";
  303. $organism = db_fetch_object(chado_query($sql, $organism_id));
  304. $interval = intval($filesize * 0.0001);
  305. if ($interval == 0) {
  306. $interval = 1;
  307. }
  308. $in_fasta = 0;
  309. $line_num = 0;
  310. $num_read = 0;
  311. $intv_read = 0;
  312. // prepare the statement used to get the cvterm for each feature.
  313. if (!tripal_core_is_sql_prepared('sel_cvterm_idnasy')) {
  314. $psql = "PREPARE sel_cvterm_idnasy (int, text, text) AS
  315. SELECT CVT.cvterm_id, CVT.cv_id, CVT.name, CVT.definition,
  316. CVT.dbxref_id, CVT.is_obsolete, CVT.is_relationshiptype
  317. FROM {cvterm} CVT
  318. INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  319. LEFT JOIN {cvtermsynonym} CVTS on CVTS.cvterm_id = CVT.cvterm_id
  320. WHERE CV.cv_id = $1 and
  321. (lower(CVT.name) = lower($2) or lower(CVTS.synonym) = lower($3))";
  322. $status = tripal_core_chado_prepare('sel_cvterm_idnasy', $psql, array('int','text','text'));
  323. if (!$status) {
  324. watchdog('T_gff3_loader', 'cannot prepare statement \'sel_cvterm_idnasy\'.',
  325. array(), WATCHDOG_ERROR);
  326. return '';
  327. }
  328. }
  329. // iterate through each line of the GFF file
  330. print "Parsing Line $line_num (0.00%). Memory: " . number_format(memory_get_usage()) . " bytes\r";
  331. while ($line = fgets($fh)) {
  332. $line_num++;
  333. $size = drupal_strlen($line);
  334. $num_read += $size;
  335. $intv_read += $size;
  336. // update the job status every 1% features
  337. if ($job and $intv_read >= $interval) {
  338. $intv_read = 0;
  339. $percent = sprintf("%.2f", ($num_read / $filesize) * 100);
  340. print "Parsing Line $line_num (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes.\r";
  341. tripal_job_set_progress($job, intval(($num_read / $filesize) * 100));
  342. }
  343. // check to see if we have FASTA section, if so then set the variable
  344. // to start parsing
  345. if (preg_match('/^##FASTA/i', $line)) {
  346. tripal_feature_load_gff_fasta($fh, $interval, $num_read, $intv_read, $line_num);
  347. continue;
  348. }
  349. // skip comments
  350. if (preg_match('/^#/', $line)) {
  351. continue;
  352. }
  353. // skip empty lines
  354. if (preg_match('/^\s*$/', $line)) {
  355. continue;
  356. }
  357. // get the columns
  358. $cols = explode("\t", $line);
  359. if (sizeof($cols) != 9) {
  360. watchdog('T_gff3_loader', 'improper number of columns on line %line_num',
  361. array('%line_num' => $line_num), WATCHDOG_ERROR);
  362. return '';
  363. }
  364. // get the column values
  365. $landmark = $cols[0];
  366. $source = $cols[1];
  367. $type = $cols[2];
  368. $start = $cols[3];
  369. $end = $cols[4];
  370. $score = $cols[5];
  371. $strand = $cols[6];
  372. $phase = $cols[7];
  373. $attrs = explode(";", $cols[8]); // split by a semicolon
  374. // ready the start and stop for chado. Chado expects these positions
  375. // to be zero-based, so we substract 1 from the fmin
  376. $fmin = $start - 1;
  377. $fmax = $end;
  378. if ($end < $start) {
  379. $fmin = $end - 1;
  380. $fmax = $start;
  381. }
  382. // format the strand for chado
  383. if (strcmp($strand, '.') == 0) {
  384. $strand = 0;
  385. }
  386. elseif (strcmp($strand, '+') == 0) {
  387. $strand = 1;
  388. }
  389. elseif (strcmp($strand, '-') == 0) {
  390. $strand = -1;
  391. }
  392. if (strcmp($phase, '.') == 0) {
  393. $phase = '';
  394. }
  395. $result = chado_query("EXECUTE sel_cvterm_idnasy (%d, '%s', '%s')", $cv->cv_id, $type, $type);
  396. $cvterm = db_fetch_object($result);
  397. if (!$cvterm) {
  398. watchdog('T_gff3_loader', 'cannot find feature term \'%type\' on line %line_num of the GFF file',
  399. array('%type' => $type, '%line_num' => $line_num), WATCHDOG_ERROR);
  400. return '';
  401. }
  402. // break apart each of the attributes
  403. $tags = array();
  404. $attr_name = '';
  405. $attr_uniquename = '';
  406. $attr_residue_info = '';
  407. $attr_locgroup = 0;
  408. $attr_fmin_partial = 'f';
  409. $attr_fmax_partial = 'f';
  410. $attr_is_obsolete = 'f';
  411. $attr_is_analysis = 'f';
  412. $attr_others = '';
  413. $residues = '';
  414. foreach ($attrs as $attr) {
  415. $attr = rtrim($attr);
  416. $attr = ltrim($attr);
  417. if (strcmp($attr, '')==0) {
  418. continue;
  419. }
  420. if (!preg_match('/^[^\=]+\=.+$/', $attr)) {
  421. watchdog('T_gff3_loader', 'Attribute is not correctly formatted on line %line_num: %attr',
  422. array('%line_num' => $line_num, '%attr' => $attr), WATCHDOG_ERROR);
  423. return '';
  424. }
  425. // break apart each tag
  426. $tag = preg_split("/=/", $attr, 2); // split by equals sign
  427. // multiple instances of an attribute are separated by commas
  428. $tag_name = $tag[0];
  429. if (!array_key_exists($tag_name, $tags)) {
  430. $tags[$tag_name] = array();
  431. }
  432. $tags[$tag_name] = array_merge($tags[$tag_name], explode(",", $tag[1])); // split by comma
  433. // replace the URL escape codes for each tag
  434. for ($i = 0; $i < count($tags[$tag_name]); $i++) {
  435. $tags[$tag_name][$i] = urldecode($tags[$tag_name][$i]);
  436. }
  437. // get the name and ID tags
  438. if (strcmp($tag_name, 'ID') == 0) {
  439. $attr_uniquename = urldecode($tag[1]);
  440. }
  441. elseif (strcmp($tag_name, 'Name') == 0) {
  442. $attr_name = urldecode($tag[1]);
  443. }
  444. // get the list of non-reserved attributes
  445. elseif (strcmp($tag_name, 'Alias') !=0 and strcmp($tag_name, 'Parent') !=0 and
  446. strcmp($tag_name, 'Target') !=0 and strcmp($tag_name, 'Gap') !=0 and
  447. strcmp($tag_name, 'Derives_from') !=0 and strcmp($tag_name, 'Note') !=0 and
  448. strcmp($tag_name, 'Dbxref') !=0 and strcmp($tag_name, 'Ontology_term') !=0 and
  449. strcmp($tag_name, 'Is_circular') !=0 and strcmp($tag_name, 'target_organism') !=0 and
  450. strcmp($tag_name, 'target_type') != 0) {
  451. foreach ($tags[$tag_name] as $value) {
  452. $attr_others[$tag_name][] = $value;
  453. }
  454. }
  455. }
  456. // if neither name nor uniquename are provided then generate one
  457. if (!$attr_uniquename and !$attr_name) {
  458. if (array_key_exists('Parent', $tags)) {
  459. $attr_uniquename = $tags['Parent'][0] . "-$type-$landmark:$fmin..$fmax";
  460. }
  461. else {
  462. watchdog('T_gff3_loader', 'Cannot generate a uniquename for feature on line %line_num',
  463. array('%line_num' => $line_num), WATCHDOG_ERROR);
  464. exit;
  465. }
  466. $attr_name = $attr_uniquename;
  467. }
  468. // if a name is not specified then use the unique name
  469. if (strcmp($attr_name, '')==0) {
  470. $attr_name = $attr_uniquename;
  471. }
  472. // if an ID attribute is not specified then use the attribute name and
  473. // hope for the best
  474. if (!$attr_uniquename) {
  475. $attr_uniquename = $attr_name;
  476. }
  477. // make sure the landmark sequence exists in the database. We don't
  478. // know the type of the landmark so we'll hope that it's unique across
  479. // all types. If not we'll error out. This test is only necessary if
  480. // if the landmark and the uniquename are different. If they are the same
  481. // then this is the information for the landmark
  482. if (strcmp($landmark, $attr_uniquename) != 0 ) {
  483. $select = array(
  484. 'organism_id' => $organism_id,
  485. 'uniquename' => $landmark,
  486. );
  487. $columns = array('count(*) as num_landmarks');
  488. $options = array('statement_name' => 'sel_feature_numland');
  489. $count = tripal_core_chado_select('feature', $columns, $select, $options);
  490. if (!$count or $count[0]->num_landmarks == 0) {
  491. watchdog('T_gff3_loader', "The landmark '%landmark' cannot be found for this organism. ".
  492. "Please add the landmark and then retry the import of this GFF3 ".
  493. "file", array('%landmark' => $landmark), WATCHDOG_ERROR);
  494. return '';
  495. }
  496. if ($count[0]->num_landmarks > 1) {
  497. watchdog('T_gff3_loader', "The landmark '%landmark' is not unique for this organism. ".
  498. "The features cannot be associated", array('%landmark' => $landmark), WATCHDOG_ERROR);
  499. return '';
  500. }
  501. }
  502. // if the option is to remove or refresh then we want to remove
  503. // the feature from the database.
  504. if ($remove or $refresh) {
  505. $sql = "DELETE FROM {feature}
  506. WHERE organism_id = %d and uniquename = '%s' and type_id = %d";
  507. $match = array(
  508. 'organism_id' => $organism->organism_id,
  509. 'uniquename' => $attr_uniquename,
  510. 'type_id' => $cvterm->cvterm_id
  511. );
  512. $result = tripal_core_chado_delete('feature', $match);
  513. if (!$result) {
  514. watchdog('T_gff3_loader', "cannot delete feature %attr_uniquename",
  515. array('%attr_uniquename' => $attr_uniquename), WATCHDOG_ERROR);
  516. }
  517. $feature = 0;
  518. unset($result);
  519. }
  520. // add or update the feature and all properties
  521. if ($update or $refresh or $add_only) {
  522. // add/update the feature
  523. $feature = tripal_feature_load_gff3_feature($organism, $analysis_id, $cvterm,
  524. $attr_uniquename, $attr_name, $residues, $attr_is_analysis,
  525. $attr_is_obsolete, $add_only, $score);
  526. if ($feature) {
  527. // add a record for this feature to the tripal_gff_temp table for
  528. // later lookup
  529. $values = array(
  530. 'feature_id' => $feature->feature_id,
  531. 'organism_id' => $feature->organism_id,
  532. 'type_name' => $type,
  533. 'uniquename' => $feature->uniquename
  534. );
  535. // make sure this record doesn't already exist in oru temp table
  536. $options = array('statement_name' => 'sel_tripalgfftemp_all');
  537. $results = tripal_core_chado_select('tripal_gff_temp', array('*'), $values, $options);
  538. if (count($results) == 0) {
  539. $options = array('statement_name' => 'ins_tripalgfftemp');
  540. $result = tripal_core_chado_insert('tripal_gff_temp', $values, $options);
  541. if (!$result) {
  542. watchdog('T_gff3_loader', "Cound not save record in temporary table, Cannot continue.", array(), WATCHDOG_ERROR);
  543. exit;
  544. }
  545. }
  546. // add/update the featureloc if the landmark and the ID are not the same
  547. // if they are the same then this entry in the GFF is probably a landmark identifier
  548. if (strcmp($landmark, $attr_uniquename) !=0 ) {
  549. tripal_feature_load_gff3_featureloc($feature, $organism,
  550. $landmark, $fmin, $fmax, $strand, $phase, $attr_fmin_partial,
  551. $attr_fmax_partial, $attr_residue_info, $attr_locgroup);
  552. }
  553. // add any aliases for this feature
  554. if (array_key_exists('Alias', $tags)) {
  555. tripal_feature_load_gff3_alias($feature, $tags['Alias']);
  556. }
  557. // add any dbxrefs for this feature
  558. if (array_key_exists('Dbxref', $tags)) {
  559. tripal_feature_load_gff3_dbxref($feature, $tags['Dbxref']);
  560. }
  561. // add any ontology terms for this feature
  562. if (array_key_exists('Ontology_term', $tags)) {
  563. tripal_feature_load_gff3_ontology($feature, $tags['Ontology_term']);
  564. }
  565. // add parent relationships
  566. if (array_key_exists('Parent', $tags)) {
  567. tripal_feature_load_gff3_parents($feature, $cvterm, $tags['Parent'], $organism_id, $fmin);
  568. }
  569. // add target relationships
  570. if (array_key_exists('Target', $tags)) {
  571. // format is: "target_id start end [strand]", where strand is optional and may be "+" or "-"
  572. $matched = preg_match('/^(.*?)\s+(\d+)\s+(\d+)(\s+[\+|\-])*$/', trim($tags['Target'][0]), $matches);
  573. // the organism and type of the target may also be specified as an attribute. If so, then get that
  574. // information
  575. $gff_target_organism = array_key_exists('target_organism', $tags) ? $tags['target_organism'][0] : '';
  576. $gff_target_type = array_key_exists('target_type', $tags) ? $tags['target_type'][0] : '';
  577. // if we have matches and the Target is in the correct format then load the alignment
  578. if ($matched) {
  579. $target_feature = $matches[1];
  580. $start = $matches[2];
  581. $end = $matches[3];
  582. // if we have an optional strand, convert it to a numeric value.
  583. if ($matches[4]) {
  584. if (preg_match('/^\+$/', trim($matches[4]))) {
  585. $target_strand = 1;
  586. }
  587. elseif (preg_match('/^\-$/', trim($matches[4]))) {
  588. $target_strand = -1;
  589. }
  590. else {
  591. $target_strand = 0;
  592. }
  593. }
  594. else {
  595. $target_strand = 0;
  596. }
  597. $target_fmin = $start - 1;
  598. $target_fmax = $end;
  599. if ($end < $start) {
  600. $target_fmin = $end - 1;
  601. $target_fmax = $start;
  602. }
  603. // default the target organism to be the value passed into the function, but if the GFF
  604. // file species the target organism then use that instead.
  605. $t_organism_id = $target_organism_id;
  606. if ($gff_target_organism) {
  607. // get the genus and species
  608. $success = preg_match('/^(.*?),(.*?)$/', $gff_target_organism, $matches);
  609. if ($success) {
  610. $values = array(
  611. 'genus' => $matches[1],
  612. 'species' => $matches[2],
  613. );
  614. $options = array('statement_name' => 'sel_organism_gesp');
  615. $organism = tripal_core_chado_select('organism', array('organism_id'), $values, $options);
  616. if (count($organism) == 1) {
  617. $t_organism_id = $organism[0]->organism_id;
  618. }
  619. else {
  620. watchdog('T_gff3_loader', "Cannot find organism for target %target.",
  621. array('%target' => $gff_target_organism), WATCHDOG_WARNING);
  622. $t_organism_id = '';
  623. }
  624. }
  625. else {
  626. watchdog('T_gff3_loader', "The target_organism attribute is improperly formatted: %target.
  627. It should be target_organism=genus,species.",
  628. array('%target' => $gff_target_organism), WATCHDOG_WARNING);
  629. $t_organism_id = '';
  630. }
  631. }
  632. // default the target type to be the value passed into the function, but if the GFF file
  633. // species the target type then use that instead
  634. $t_type_id = '';
  635. if ($target_type) {
  636. $values = array(
  637. 'name' => $target_type,
  638. 'cv_id' => array(
  639. 'name' => 'sequence',
  640. )
  641. );
  642. $options = array('statement_name' => 'sel_cvterm_nacv');
  643. $type = tripal_core_chado_select('cvterm', array('cvterm_id'), $values, $options);
  644. if (count($type) == 1) {
  645. $t_type_id = $type[0]->cvterm_id;
  646. }
  647. else {
  648. watchdog('T_gff3_loader', "The target type does not exist in the sequence ontology: %type. ",
  649. array('%type' => $target_type), WATCHDOG_ERROR);
  650. exit;
  651. }
  652. }
  653. if ($gff_target_type) {
  654. $values = array(
  655. 'name' => $gff_target_type,
  656. 'cv_id' => array(
  657. 'name' => 'sequence',
  658. )
  659. );
  660. $options = array('statement_name' => 'sel_cvterm_nacv');
  661. $type = tripal_core_chado_select('cvterm', array('cvterm_id'), $values, $options);
  662. if (count($type) == 1) {
  663. $t_type_id = $type[0]->cvterm_id;
  664. }
  665. else {
  666. watchdog('T_gff3_loader', "The target_type attribute does not exist in the sequence ontology: %type. ",
  667. array('%type' => $gff_target_type), WATCHDOG_WARNING);
  668. $t_type_id = '';
  669. }
  670. }
  671. // we want to add a featureloc record that uses the target feature as the srcfeature (landmark)
  672. // and the landmark as the feature.
  673. tripal_feature_load_gff3_featureloc($feature, $organism, $target_feature, $target_fmin,
  674. $target_fmax, $target_strand, $phase, $attr_fmin_partial, $attr_fmax_partial, $attr_residue_info,
  675. $attr_locgroup, $t_type_id, $t_organism_id, $create_target, TRUE);
  676. }
  677. // the target attribute is not correctly formatted
  678. else {
  679. watchdog('T_gff3_loader', "Could not add 'Target' alignment as it is improperly formatted: '%target'",
  680. array('%target' => $tags['Target'][0]), WATCHDOG_ERROR);
  681. }
  682. }
  683. // add gap information. This goes in simply as a property
  684. if (array_key_exists('Gap', $tags)) {
  685. foreach ($tags['Gap'] as $value) {
  686. tripal_feature_load_gff3_property($feature, 'Gap', $value);
  687. }
  688. }
  689. // add notes. This goes in simply as a property
  690. if (array_key_exists('Note', $tags)) {
  691. foreach ($tags['Note'] as $value) {
  692. tripal_feature_load_gff3_property($feature, 'Note', $value);
  693. }
  694. }
  695. // add the Derives_from relationship (e.g. polycistronic genes).
  696. if (array_key_exists('Derives_from', $tags)) {
  697. tripal_feature_load_gff3_derives_from($feature, $tags['Derives_from'][0], $organism);
  698. }
  699. // add in the GFF3_source dbxref so that GBrowse can find the feature using the source column
  700. $source_ref = array('GFF_source:' . $source);
  701. tripal_feature_load_gff3_dbxref($feature, $source_ref);
  702. // add any additional attributes
  703. if ($attr_others) {
  704. foreach ($attr_others as $tag_name => $values) {
  705. foreach ($values as $value) {
  706. tripal_feature_load_gff3_property($feature, $tag_name, $value);
  707. }
  708. }
  709. }
  710. }
  711. }
  712. }
  713. print "\nSetting ranks of children...\n";
  714. // get features in a relationship that are also children of an alignment
  715. $sql = "SELECT DISTINCT F.feature_id, F.organism_id, F.type_id,
  716. F.uniquename, FL.strand
  717. FROM tripal_gff_temp TGT
  718. INNER JOIN feature F ON TGT.feature_id = F.feature_id
  719. INNER JOIN feature_relationship FR ON FR.object_id = TGT.feature_id
  720. INNER JOIN cvterm CVT ON CVT.cvterm_id = FR.type_id
  721. INNER JOIN featureloc FL ON FL.feature_id = F.feature_id
  722. WHERE CVT.name = 'part_of'";
  723. $parents = chado_query($sql);
  724. // build and prepare the SQL for selecting the children relationship
  725. $sql = "SELECT DISTINCT FR.feature_relationship_id, FL.fmin, FR.rank
  726. FROM feature_relationship FR
  727. INNER JOIN featureloc FL on FL.feature_id = FR.subject_id";
  728. if (!$connection) {
  729. $sql .= "WHERE FR.object_id = %d ".
  730. "ORDER BY FL.fmin ASC ";
  731. }
  732. else {
  733. $sql = "PREPARE sel_gffchildren (int) AS " . $sql . " WHERE FR.object_id = \$1 ORDER BY FL.fmin ASC";
  734. }
  735. if (!tripal_core_is_sql_prepared('sel_gffchildren')) {
  736. $success = tripal_core_chado_prepare('sel_gffchildren', $sql, array('int'));
  737. if (!$success) {
  738. watchdog("T_gff3_loader", "Cannot prepare statement 'sel_gffchildren' and cannot set children ranks.",
  739. array(), WATCHDOG_WARNING);
  740. return 0;
  741. }
  742. }
  743. // now set the rank of any parent/child relationships. The order is based
  744. // on the fmin. The start rank is 1. This allows features with other
  745. // relationships to be '0' (the default), and doesn't interfer with the
  746. // ordering defined here.
  747. while ($parent = db_fetch_object($parents)) {
  748. // get the children
  749. if ($connection) {
  750. $result = chado_query('EXECUTE sel_gffchildren (%d)', $parent->feature_id);
  751. }
  752. else {
  753. $result = chado_query($sql, $parent->feature_id);
  754. }
  755. // build an array of the children
  756. $children = array();
  757. while ($child = db_fetch_object($result)) {
  758. $children[] = $child;
  759. }
  760. // the children list comes sorted in ascending fmin
  761. // but if the parent is on the reverse strand we need to
  762. // reverse the order of the children.
  763. if ($parent->strand == -1) {
  764. arsort($children);
  765. }
  766. // first set the ranks to a negative number so that we don't
  767. // get a duplicate error message when we try to change any of them
  768. $rank = -1;
  769. foreach ($children as $child) {
  770. $match = array('feature_relationship_id' => $child->feature_relationship_id);
  771. $options = array('statement_name' => 'upd_featurerelationship_rank');
  772. $values = array('rank' => $rank);
  773. tripal_core_chado_update('feature_relationship', $match, $values, $options);
  774. $rank--;
  775. }
  776. // now set the rank correctly. The rank should start at 0.
  777. $rank = 0;
  778. foreach ($children as $child) {
  779. $match = array('feature_relationship_id' => $child->feature_relationship_id);
  780. $options = array('statement_name' => 'upd_featurerelationship_rank');
  781. $values = array('rank' => $rank);
  782. //print "Was: " . $child->rank . " now $rank ($parent->strand)\n" ;
  783. tripal_core_chado_update('feature_relationship', $match, $values, $options);
  784. $rank++;
  785. }
  786. }
  787. // commit the transaction
  788. if ($use_transaction) {
  789. tripal_db_commit_transaction();
  790. }
  791. print "Done\n";
  792. return 1;
  793. }
  794. /**
  795. *
  796. *
  797. * @ingroup gff3_loader
  798. */
  799. function tripal_feature_load_gff3_derives_from($feature, $subject, $organism) {
  800. // get the subject type
  801. $values = array(
  802. 'organism_id' => $organism->organism_id,
  803. 'uniquename' => $subject,
  804. );
  805. $options = array('statement_name' => 'sel_tripalgfftemp_orun');
  806. $result = tripal_core_chado_select('tripal_gff_temp', array('type_name'), $values, $options);
  807. if (count($result) == 0) {
  808. watchdog("T_gff3_loader", "Cannot find subject type for feature in 'derives_from' relationship: %subject", array('%subject' => $subject), WATCHDOG_WARNING);
  809. return '';
  810. }
  811. $subject_type = $result[0]->type_name;
  812. // get the subject feature
  813. $match = array(
  814. 'organism_id' => $organism->organism_id,
  815. 'uniquename' => $subject,
  816. 'type_id' => array(
  817. 'name' => $subject_type,
  818. 'cv_id' => array(
  819. 'name' => 'sequence'
  820. ),
  821. ),
  822. );
  823. $options = array('statement_name' => 'sel_feature_orunty');
  824. $sfeature = tripal_core_chado_select('feature', array('feature_id'), $match, $options);
  825. if (count($sfeature)==0) {
  826. watchdog('T_gff3_loader', "Could not add 'Derives_from' relationship ".
  827. "for %uniquename and %subject. Subject feature, '%subject', ".
  828. "cannot be found", array('%uniquename' => $feature->uniquename, '%subject' => $subject), WATCHDOG_ERROR);
  829. return;
  830. }
  831. // now check to see if the relationship already exists
  832. $values = array(
  833. 'object_id' => $sfeature[0]->feature_id,
  834. 'subject_id' => $feature->feature_id,
  835. 'type_id' => array(
  836. 'cv_id' => array(
  837. 'name' => 'relationship'
  838. ),
  839. 'name' => 'derives_from',
  840. ),
  841. 'rank' => 0
  842. );
  843. $options = array('statement_name' => 'sel_featurerelationship_objectid_subjectid_typeid_rank');
  844. $rel = tripal_core_chado_select('feature_relationship', array('*'), $values, $options);
  845. if (count($rel) > 0) {
  846. return;
  847. }
  848. // finally insert the relationship if it doesn't exist
  849. $options = array('statement_name' => 'ins_featurerelationship_objectid_subjectid_typeid_rank');
  850. $ret = tripal_core_chado_insert('feature_relationship', $values, $options);
  851. if (!$ret) {
  852. watchdog("T_gff3_loader", "Could not add 'Derives_from' relationship for $feature->uniquename and $subject",
  853. array(), WATCHDOG_WARNING);
  854. }
  855. }
  856. /**
  857. *
  858. *
  859. * @ingroup gff3_loader
  860. */
  861. function tripal_feature_load_gff3_parents($feature, $cvterm, $parents, $organism_id, $fmin) {
  862. $uname = $feature->uniquename;
  863. $type = $cvterm->name;
  864. $rel_type = 'part_of';
  865. // prepare these SQL statements that will be used repeatedly.
  866. if (!tripal_core_is_sql_prepared('sel_cvterm_cvname_cvtname_synonym')) {
  867. $psql = "PREPARE sel_cvterm_cvname_cvtname_synonym (text, text, text) AS
  868. SELECT CVT.cvterm_id
  869. FROM cvterm CVT
  870. INNER JOIN cv CV on CVT.cv_id = CV.cv_id
  871. LEFT JOIN cvtermsynonym CVTS on CVTS.cvterm_id = CVT.cvterm_id
  872. WHERE cv.name = $1 and (CVT.name = $2 or CVTS.synonym = $3)";
  873. $status = tripal_core_chado_prepare('sel_cvterm_cvname_cvtname_synonym', $psql, array('text', 'text' ,'text'));
  874. if (!$status) {
  875. watchdog("T_gff3_loader", "Cannot prepare statement 'sel_cvterm_cvname_cvtname_synonym' for ontology term",
  876. array(), WATCHDOG_WARNING);
  877. return '';
  878. }
  879. }
  880. // iterate through the parents in the list
  881. foreach ($parents as $parent) {
  882. // get the parent cvterm
  883. $values = array(
  884. 'organism_id' => $organism_id,
  885. 'uniquename' => $parent,
  886. );
  887. $options = array('statement_name' => 'sel_tripalgfftemp_orun');
  888. $result = tripal_core_chado_select('tripal_gff_temp', array('type_name'), $values, $options);
  889. if (count($result) == 0) {
  890. watchdog("T_gff3_loader", "Cannot find parent type: %parent", array('%parent' => $parent), WATCHDOG_WARNING);
  891. return '';
  892. }
  893. $parent_type = $result[0]->type_name;
  894. // try to find the parent
  895. $parentcvterm = db_fetch_object(chado_query("EXECUTE sel_cvterm_cvname_cvtname_synonym ('%s', '%s', '%s')", 'sequence', $parent_type, $parent_type));
  896. $relcvterm = db_fetch_object(chado_query("EXECUTE sel_cvterm_cvname_cvtname_synonym ('%s', '%s', '%s')", 'relationship', $rel_type, $rel_type));
  897. $values = array(
  898. 'organism_id' => $organism_id,
  899. 'uniquename' => $parent,
  900. 'type_id' => $parentcvterm->cvterm_id,
  901. );
  902. $options = array('statement_name' => 'sel_feature_orunty');
  903. $result = tripal_core_chado_select('feature', array('feature_id'), $values, $options);
  904. $parent_feature = $result[0];
  905. // if the parent exists then add the relationship otherwise print error and skip
  906. if ($parent_feature) {
  907. // check to see if the relationship already exists
  908. $values = array(
  909. 'object_id' => $parent_feature->feature_id,
  910. 'subject_id' => $feature->feature_id,
  911. 'type_id' => $relcvterm->cvterm_id,
  912. );
  913. $options = array('statement_name' => 'sel_featurerelationship_objectid_subjectid_typeid');
  914. $rel = tripal_core_chado_select('feature_relationship', array('*'), $values, $options);
  915. if (count($rel) > 0) {
  916. }
  917. else {
  918. // the relationship doesn't already exist, so add it.
  919. $values = array(
  920. 'subject_id' => $feature->feature_id,
  921. 'object_id' => $parent_feature->feature_id,
  922. 'type_id' => $relcvterm->cvterm_id,
  923. );
  924. $options = array('statement_name' => 'ins_featurerelationship_subjectid_objectid_typeid');
  925. $result = tripal_core_chado_insert('feature_relationship', $values, $options);
  926. if (!$result) {
  927. watchdog("T_gff3_loader", "Failed to insert feature relationship '$uname' ($type) $rel_type '$parent' ($parent_type)",
  928. array(), WATCHDOG_WARNING);
  929. }
  930. }
  931. }
  932. else {
  933. watchdog("T_gff3_loader", "Cannot establish relationship '$uname' ($type) $rel_type '$parent' ($parent_type): Cannot find the parent",
  934. array(), WATCHDOG_WARNING);
  935. }
  936. }
  937. }
  938. /**
  939. *
  940. *
  941. * @ingroup gff3_loader
  942. */
  943. function tripal_feature_load_gff3_dbxref($feature, $dbxrefs) {
  944. // iterate through each of the dbxrefs
  945. foreach ($dbxrefs as $dbxref) {
  946. // get the database name from the reference. If it doesn't exist then create one.
  947. $ref = explode(":", $dbxref);
  948. $dbname = $ref[0];
  949. $accession = $ref[1];
  950. // first look for the database name if it doesn't exist then create one.
  951. // first check for the fully qualified URI (e.g. DB:<dbname>. If that
  952. // can't be found then look for the name as is. If it still can't be found
  953. // the create the database
  954. $values = array('name' => "DB:$dbname");
  955. $options = array('statement_name' => 'sel_db_name');
  956. $db = tripal_core_chado_select('db', array('db_id'), $values, $options);
  957. if (count($db) == 0) {
  958. $values = array('name' => "$dbname");
  959. $db = tripal_core_chado_select('db', array('db_id'), $values, $options);
  960. }
  961. if (count($db) == 0) {
  962. $values = array(
  963. 'name' => $dbname,
  964. 'description' => 'Added automatically by the GFF loader'
  965. );
  966. $options = array('statement_name' => 'ins_db_name');
  967. $success = tripal_core_chado_insert('db', $values, $options);
  968. if ($success) {
  969. $values = array('name' => "$dbname");
  970. $options = array('statement_name' => 'sel_db_name');
  971. $db = tripal_core_chado_select('db', array('db_id'), $values, $options);
  972. }
  973. else {
  974. watchdog("T_gff3_loader", "Cannot find or add the database $dbname", array(), WATCHDOG_WARNING);
  975. return 0;
  976. }
  977. }
  978. $db = $db[0];
  979. // now check to see if the accession exists
  980. $values = array(
  981. 'accession' => $accession,
  982. 'db_id' => $db->db_id
  983. );
  984. $options = array('statement_name' => 'sel_dbxref_accession_dbid');
  985. $dbxref = tripal_core_chado_select('dbxref', array('dbxref_id'), $values, $options);
  986. // if the accession doesn't exist then we want to add it
  987. if (sizeof($dbxref) == 0) {
  988. $values = array(
  989. 'db_id' => $db->db_id,
  990. 'accession' => $accession,
  991. 'version' => ''
  992. );
  993. $options = array('statement_name' => 'ins_dbxref_dbid_accession_version');
  994. $ret = tripal_core_chado_insert('dbxref', $values, $options);
  995. $values = array(
  996. 'accession' => $accession,
  997. 'db_id' => $db->db_id
  998. );
  999. $options = array('statement_name' => 'sel_dbxref_accession_dbid');
  1000. $dbxref = tripal_core_chado_select('dbxref', array('dbxref_id'), $values, $options);
  1001. }
  1002. $dbxref = $dbxref[0];
  1003. // check to see if this feature dbxref already exists
  1004. $values = array(
  1005. 'dbxref_id' => $dbxref->dbxref_id,
  1006. 'feature_id' => $feature->feature_id
  1007. );
  1008. $options = array('statement_name' => 'sel_featuredbxref_dbxrefid_featureid');
  1009. $fdbx = tripal_core_chado_select('feature_dbxref', array('feature_dbxref_id'), $values, $options);
  1010. // now associate this feature with the database reference if it doesn't
  1011. // already exist
  1012. if (sizeof($fdbx) == 0) {
  1013. $values = array(
  1014. 'dbxref_id' => $dbxref->dbxref_id,
  1015. 'feature_id' => $feature->feature_id
  1016. );
  1017. $options = array('statement_name' => 'ins_featuredbxref_dbxrefid_featureid');
  1018. $success = tripal_core_chado_insert('feature_dbxref', $values, $options);
  1019. if (!$success) {
  1020. watchdog("T_gff3_loader", "Failed to insert Dbxref: $dbname:$accession", array(), WATCHDOG_WARNING);
  1021. return 0;
  1022. }
  1023. }
  1024. }
  1025. return 1;
  1026. }
  1027. /**
  1028. *
  1029. *
  1030. * @ingroup gff3_loader
  1031. */
  1032. function tripal_feature_load_gff3_ontology($feature, $dbxrefs) {
  1033. // iterate through each of the dbxrefs
  1034. foreach ($dbxrefs as $dbxref) {
  1035. // get the database name from the reference. If it doesn't exist then create one.
  1036. $ref = explode(":", $dbxref);
  1037. $dbname = $ref[0];
  1038. $accession = $ref[1];
  1039. // first look for the database name
  1040. $options = array('statement_name' => 'sel_db_name');
  1041. $db = tripal_core_chado_select('db', array('db_id'), array('name' => "DB:$dbname"), $options);
  1042. if (sizeof($db) == 0) {
  1043. // now look for the name without the 'DB:' prefix.
  1044. $db = tripal_core_chado_select('db', array('db_id'), array('name' => "$dbname"), $options);
  1045. if (sizeof($db) == 0) {
  1046. watchdog("T_gff3_loader", "Database, $dbname, is not present. Cannot associate term: $dbname:$accession", array(), WATCHDOG_WARNING);
  1047. return 0;
  1048. }
  1049. }
  1050. $db = $db[0];
  1051. // now check to see if the accession exists
  1052. $options = array('statement_name' => 'sel_dbxref_accession_dbid');
  1053. $dbxref = tripal_core_chado_select('dbxref', array('dbxref_id'),
  1054. array('accession' => $accession, 'db_id' => $db->db_id), $options);
  1055. if (sizeof($dbxref) == 0) {
  1056. watchdog("T_gff3_loader", "Accession, $accession is missing for reference: $dbname:$accession", array(), WATCHDOG_WARNING);
  1057. return 0;
  1058. }
  1059. $dbxref = $dbxref[0];
  1060. // now check to see if the cvterm exists
  1061. $options = array('statement_name' => 'sel_cvterm_dbxrefid');
  1062. $cvterm = tripal_core_chado_select('cvterm', array('cvterm_id'), array(
  1063. 'dbxref_id' => $dbxref->dbxref_id), $options);
  1064. // if it doesn't exist in the cvterm table, look for an alternate id
  1065. if (sizeof($cvterm) == 0) {
  1066. $options = array('statement_name' => 'sel_cvtermdbxref_dbxrefid');
  1067. $cvterm = tripal_core_chado_select('cvterm_dbxref', array('cvterm_id'), array(
  1068. 'dbxref_id' => $dbxref->dbxref_id), $options);
  1069. if (sizeof($cvterm) == 0) {
  1070. watchdog("T_gff3_loader", "CV Term is missing for reference: $dbname:$accession", array(), WATCHDOG_WARNING);
  1071. return 0;
  1072. }
  1073. }
  1074. $cvterm = $cvterm[0];
  1075. // check to see if this feature cvterm already exists
  1076. $options = array('statement_name' => 'sel_featurecvterm_cvtermid_featureid');
  1077. $fcvt = tripal_core_chado_select('feature_cvterm', array('feature_cvterm_id'),
  1078. array('cvterm_id' => $cvterm->cvterm_id, 'feature_id' => $feature->feature_id),
  1079. $options);
  1080. // now associate this feature with the cvterm if it doesn't already exist
  1081. if (sizeof($fcvt)==0) {
  1082. $values = array(
  1083. 'cvterm_id' => $cvterm->cvterm_id,
  1084. 'feature_id' => $feature->feature_id,
  1085. 'pub_id' => array(
  1086. 'uniquename' => 'null',
  1087. ),
  1088. );
  1089. $options = array('statement_name' => 'ins_featurecvterm_cvtermid_featureid_pubid');
  1090. $success = tripal_core_chado_insert('feature_cvterm', $values, $options);
  1091. if (!$success) {
  1092. watchdog("T_gff3_loader", "Failed to insert ontology term: $dbname:$accession", array(), WATCHDOG_WARNING);
  1093. return 0;
  1094. }
  1095. }
  1096. }
  1097. return 1;
  1098. }
  1099. /**
  1100. *
  1101. *
  1102. * @ingroup gff3_loader
  1103. */
  1104. function tripal_feature_load_gff3_alias($feature, $aliases) {
  1105. // make sure we have a 'synonym_type' vocabulary
  1106. $select = array('name' => 'synonym_type');
  1107. $options = array('statement_name' => 'sel_cv_name');
  1108. $results = tripal_core_chado_select('cv', array('*'), $select, $options);
  1109. if (count($results) == 0) {
  1110. // insert the 'synonym_type' vocabulary
  1111. $values = array(
  1112. 'name' => 'synonym_type',
  1113. 'definition' => 'vocabulary for synonym types',
  1114. );
  1115. $options = array('statement_name' => 'ins_cv_name_definition');
  1116. $success = tripal_core_chado_insert('cv', $values, $options);
  1117. if (!$success) {
  1118. watchdog("T_gff3_loader", "Failed to add the synonyms type vocabulary", array(), WATCHDOG_WARNING);
  1119. return 0;
  1120. }
  1121. // now that we've added the cv we need to get the record
  1122. $options = array('statement_name' => 'sel_cv_name');
  1123. $results = tripal_core_chado_select('cv', array('*'), $select, $options);
  1124. if (count($results) > 0) {
  1125. $syncv = $results[0];
  1126. }
  1127. }
  1128. else {
  1129. $syncv = $results[0];
  1130. }
  1131. // get the 'exact' cvterm, which is the type of synonym we're adding
  1132. $select = array(
  1133. 'name' => 'exact',
  1134. 'cv_id' => array(
  1135. 'name' => 'synonym_type'
  1136. ),
  1137. );
  1138. $options = array('statement_name' => 'sel_cvterm_name_cvid');
  1139. $result = tripal_core_chado_select('cvterm', array('*'), $select, $options);
  1140. if (count($result) == 0) {
  1141. $term = array(
  1142. 'name' => 'exact',
  1143. 'id' => "internal:exact",
  1144. 'definition' => '',
  1145. 'is_obsolete' => 0,
  1146. );
  1147. // TODO: fix the function so it uses prepared statements
  1148. $syntype = tripal_cv_add_cvterm($term, $syncv->name, 0, 1);
  1149. if (!$syntype) {
  1150. watchdog("T_gff3_loader", "Cannot add synonym type: internal:$type", array(), WATCHDOG_WARNING);
  1151. return 0;
  1152. }
  1153. }
  1154. else {
  1155. $syntype = $result[0];
  1156. }
  1157. // iterate through all of the aliases and add each one
  1158. foreach ($aliases as $alias) {
  1159. // check to see if the alias already exists in the synonym table
  1160. // if not, then add it
  1161. $select = array(
  1162. 'name' => $alias,
  1163. 'type_id' => $syntype->cvterm_id,
  1164. );
  1165. $options = array('statement_name' => 'sel_synonym_name_typeid');
  1166. $result = tripal_core_chado_select('synonym', array('*'), $select, $options);
  1167. if (count($result) == 0) {
  1168. $values = array(
  1169. 'name' => $alias,
  1170. 'type_id' => $syntype->cvterm_id,
  1171. 'synonym_sgml' => '',
  1172. );
  1173. $options = array('statement_name' => 'ins_synonym_name_typeid_synonymsgml');
  1174. $success = tripal_core_chado_insert('synonym', $values, $options);
  1175. if (!$success) {
  1176. watchdog("T_gff3_loader", "Cannot add alias $alias to synonym table", array(), WATCHDOG_WARNING);
  1177. return 0;
  1178. }
  1179. $options = array('statement_name' => 'sel_synonym_name_typeid');
  1180. $result = tripal_core_chado_select('synonym', array('*'), $select, $options);
  1181. $synonym = $result[0];
  1182. }
  1183. else {
  1184. $synonym = $result[0];
  1185. }
  1186. // check to see if we have a NULL publication in the pub table. If not,
  1187. // then add one.
  1188. // @coder-ignore: non-drupal schema thus table prefixing does not apply
  1189. $select = array('uniquename' => 'null');
  1190. $options = array('statement_name' => 'sel_pub_uniquename');
  1191. $result = tripal_core_chado_select('pub', array('*'), $select, $options);
  1192. if (count($result) == 0) {
  1193. // prepare the statement
  1194. if (!tripal_core_is_sql_prepared('ins_pub_uniquename_typeid')) {
  1195. $psql = "PREPARE ins_pub_uniquename_typeid (text, text) AS
  1196. INSERT INTO pub (uniquename,type_id) VALUES ('%s',
  1197. (SELECT cvterm_id
  1198. FROM cvterm CVT
  1199. INNER JOIN dbxref DBX on DBX.dbxref_id = CVT.dbxref_id
  1200. INNER JOIN db DB on DB.db_id = DBX.db_id
  1201. WHERE CVT.name = $1 and DB.name = $2)";
  1202. $status = tripal_core_chado_prepare('ins_pub_uniquename_typeid', $psql, args('text', 'text'));
  1203. if (!$status) {
  1204. watchdog("T_gff3_loader", "Cannot prepare statement 'ins_pub_uniquename_typeid", array(), WATCHDOG_WARNING);
  1205. return 0;
  1206. }
  1207. }
  1208. // insert the null pub
  1209. $result = db_fetch_object(chado_query("EXECUTE ins_pub_uniquename_typeid ('%s', '%s')", 'null', 'null'));
  1210. if (!$result) {
  1211. watchdog("T_gff3_loader", "Cannot add null publication needed for setup of alias", array(), WATCHDOG_WARNING);
  1212. return 0;
  1213. }
  1214. $options = array('statement_name' => 'sel_pub_uniquename');
  1215. $result = tripal_core_chado_select('pub', array('*'), $select, $options);
  1216. $pub = $result[0];
  1217. }
  1218. else {
  1219. $pub = $result[0];
  1220. }
  1221. // check to see if the synonym exists in the feature_synonym table
  1222. // if not, then add it.
  1223. $values = array(
  1224. 'synonym_id' => $synonym->synonym_id,
  1225. 'feature_id' => $feature->feature_id,
  1226. 'pub_id' => $pub->pub_id,
  1227. );
  1228. $columns = array('feature_synonym_id');
  1229. $options = array('statement_name' => 'sel_featuresynonym_syfepu');
  1230. $result = tripal_core_chado_select('feature_synonym', $columns, $values, $options);
  1231. if (count($result) == 0) {
  1232. $values = array(
  1233. 'synonym_id' => $synonym->synonym_id,
  1234. 'feature_id' => $feature->feature_id,
  1235. 'pub_id' => $pub->pub_id,
  1236. );
  1237. $ins_options = array('statement_name' => 'ins_featuresynonym_syfepu');
  1238. $success = tripal_core_chado_insert('feature_synonym', $values, $ins_options);
  1239. if (!$success) {
  1240. watchdog("T_gff3_loader", "Cannot add alias $alias to feature synonym table", array(), WATCHDOG_WARNING);
  1241. return 0;
  1242. }
  1243. }
  1244. }
  1245. return 1;
  1246. }
  1247. /**
  1248. *
  1249. *
  1250. * @ingroup gff3_loader
  1251. */
  1252. function tripal_feature_load_gff3_feature($organism, $analysis_id, $cvterm, $uniquename, $name,
  1253. $residues, $is_analysis = 'f', $is_obsolete = 'f', $add_only, $score) {
  1254. // check to see if the feature already exists
  1255. $feature = NULL;
  1256. $fselect = array(
  1257. 'organism_id' => $organism->organism_id,
  1258. 'uniquename' => $uniquename,
  1259. 'type_id' => $cvterm->cvterm_id
  1260. );
  1261. $options = array('statement_name' => 'sel_feature_orunty');
  1262. $columns = array('feature_id', 'name', 'uniquename', 'seqlen', 'organism_id', 'type_id');
  1263. $result = tripal_core_chado_select('feature', $columns, $fselect, $options);
  1264. if (count($result) > 0) {
  1265. $feature = $result[0];
  1266. }
  1267. if (strcmp($is_obsolete, 'f')==0 or $is_obsolete == 0) {
  1268. $is_obsolete = 'FALSE';
  1269. }
  1270. if (strcmp($is_obsolete, 't')==0 or $is_obsolete == 1) {
  1271. $is_obsolete = 'TRUE';
  1272. }
  1273. if (strcmp($is_analysis, 'f')==0 or $is_analysis == 0) {
  1274. $is_analysis = 'FALSE';
  1275. }
  1276. if (strcmp($is_analysis, 't')==0 or $is_analysis == 1) {
  1277. $is_analysis = 'TRUE';
  1278. }
  1279. // insert the feature if it does not exist otherwise perform an update
  1280. if (!$feature) {
  1281. $values = array(
  1282. 'organism_id' => $organism->organism_id,
  1283. 'name' => $name,
  1284. 'uniquename' => $uniquename,
  1285. // 'residues' => $residues,
  1286. // 'seqlen' => drupal_strlen($residues),
  1287. 'md5checksum' => md5($residues),
  1288. 'type_id' => $cvterm->cvterm_id,
  1289. 'is_analysis' => $is_analysis,
  1290. 'is_obsolete' => $is_obsolete,
  1291. );
  1292. $options = array('statement_name' => 'ins_feature_all');
  1293. $result = tripal_core_chado_insert('feature', $values, $options);
  1294. if (!$result) {
  1295. watchdog("T_gff3_loader", "Failed to insert feature '$uniquename' ($cvterm->name)", array(), WATCHDOG_WARNING);
  1296. return 0;
  1297. }
  1298. }
  1299. elseif (!$add_only) {
  1300. $values = array(
  1301. 'name' => $name,
  1302. // 'residues' => $residues,
  1303. // 'seqlen' => drupal_strlen($residues),
  1304. 'md5checksum' => md5($residues),
  1305. 'is_analysis' => $is_analysis,
  1306. 'is_obsolete' => $is_obsolete,
  1307. );
  1308. $match = array(
  1309. 'organism_id' => $organism->organism_id,
  1310. 'uniquename' => $uniquename,
  1311. 'type_id' => $cvterm->cvterm_id,
  1312. );
  1313. $options = array('statement_name' => 'upd_feature');
  1314. $result = tripal_core_chado_update('feature', $match, $values, $options);
  1315. if (!$result) {
  1316. watchdog("T_gff3_loader", "Failed to update feature '$uniquename' ($cvterm->name)", array(), WATCHDOG_WARNING);
  1317. return 0;
  1318. }
  1319. }
  1320. else {
  1321. // the feature exists and we don't want to update it so return
  1322. // a value of 0. This will stop all downstream property additions
  1323. return 0;
  1324. }
  1325. // get the newly added feature
  1326. $options = array('statement_name' => 'sel_feature_orunty');
  1327. $columns = array('feature_id', 'name', 'uniquename', 'seqlen', 'organism_id', 'type_id');
  1328. $result = tripal_core_chado_select('feature', $columns, $fselect, $options);
  1329. $feature = $result[0];
  1330. // add the analysisfeature entry to the analysisfeature table if it doesn't already exist
  1331. $af_values = array(
  1332. 'analysis_id' => $analysis_id,
  1333. 'feature_id' => $feature->feature_id
  1334. );
  1335. $options = array('statement_name' => 'sel_analysisfeature_analysisid_featureid');
  1336. $afeature = tripal_core_chado_select('analysisfeature', array('analysisfeature_id'), $af_values, $options);
  1337. if (count($afeature)==0) {
  1338. // if a score is available then set that to be the significance field
  1339. if (strcmp($score, '.') != 0) {
  1340. $af_values['significance'] = $score;
  1341. $options = array('statement_name' => 'ins_analysisfeature_analysisid_featureid_significance');
  1342. }
  1343. else {
  1344. $options = array('statement_name' => 'ins_analysisfeature_analysisid_featureid');
  1345. }
  1346. if (!tripal_core_chado_insert('analysisfeature', $af_values, $options)) {
  1347. watchdog("T_gff3_loader", "Could not add analysisfeature record: $analysis_id, $feature->feature_id", array(), WATCHDOG_WARNING);
  1348. }
  1349. }
  1350. else {
  1351. // if a score is available then set that to be the significance field
  1352. $new_vals = array();
  1353. if (strcmp($score, '.')!=0) {
  1354. $new_vals['significance'] = $score;
  1355. }
  1356. else {
  1357. $new_vals['significance'] = '__NULL__';
  1358. }
  1359. if (!$add_only) {
  1360. $options = array('statement_name' => 'upd_analysisfeature');
  1361. $ret = tripal_core_chado_update('analysisfeature', $af_values, $new_vals, $options);
  1362. if (!$ret) {
  1363. watchdog("T_gff3_loader", "Could not update analysisfeature record: $analysis_id, $feature->feature_id", array(), WATCHDOG_WARNING);
  1364. }
  1365. }
  1366. }
  1367. return $feature;
  1368. }
  1369. /**
  1370. *
  1371. *
  1372. * @ingroup gff3_loader
  1373. */
  1374. function tripal_feature_load_gff3_featureloc($feature, $organism, $landmark, $fmin,
  1375. $fmax, $strand, $phase, $is_fmin_partial, $is_fmax_partial, $residue_info, $locgroup,
  1376. $landmark_type_id = '', $landmark_organism_id = '', $create_landmark = 0, $landmark_is_target = 0) {
  1377. $select = array(
  1378. 'organism_id' => $landmark_organism_id ? $landmark_organism_id : $organism->organism_id,
  1379. 'uniquename' => $landmark,
  1380. );
  1381. $options = array('statement_name' => 'sel_feature_orun');
  1382. if ($landmark_type_id) {
  1383. $select['type_id'] = $landmark_type_id;
  1384. $options = array('statement_name' => 'sel_feature_orunty');
  1385. }
  1386. $results = tripal_core_chado_select('feature', array('feature_id'), $select, $options);
  1387. $srcfeature = '';
  1388. if (count($results)==0) {
  1389. // so we couldn't find the landmark using the uniquename. Let's try the 'name'.
  1390. // if we return only a single result then we can proceed. Otherwise give an
  1391. $select = array(
  1392. 'organism_id' => $landmark_organism_id ? $landmark_organism_id : $organism->organism_id,
  1393. 'name' => $landmark,
  1394. );
  1395. $options = array('statement_name' => 'sel_feature_orna');
  1396. if ($landmark_type_id) {
  1397. $select['type_id'] = $landmark_type_id;
  1398. $options = array('statement_name' => 'sel_feature_ornaty');
  1399. }
  1400. $results = tripal_core_chado_select('feature', array('feature_id'), $select, $options);
  1401. if (count($results) == 0) {
  1402. // if the landmark is the target feature in a matched alignment then try one more time to
  1403. // find it by querying any feature with the same uniquename. If we find one then use it.
  1404. if ($landmark_is_target) {
  1405. $select = array('uniquename' => $landmark);
  1406. $options = array('statement_name' => 'sel_feature_un');
  1407. $results = tripal_core_chado_select('feature', array('feature_id'), $select, $options);
  1408. if (count($results) == 1) {
  1409. $srcfeature = $results[0];
  1410. }
  1411. }
  1412. if (!$srcfeature) {
  1413. // we couldn't find the landmark feature, so if the user has requested we create it then do so
  1414. // but only if we have a type id
  1415. if ($create_landmark and $landmark_type_id) {
  1416. $values = array(
  1417. 'organism_id' => $landmark_organism_id ? $landmark_organism_id : $organism->organism_id,
  1418. 'name' => $landmark,
  1419. 'uniquename' => $landmark,
  1420. 'type_id' => $landmark_type_id
  1421. );
  1422. $options = array('statement_name' => 'ins_feature_ornaunty');
  1423. $results = tripal_core_chado_insert('feature', $values, $options);
  1424. if (!$results) {
  1425. watchdog("T_gff3_loader", "Cannot find landmark feature: '%landmark', nor could it be inserted",
  1426. array('%landmark' => $landmark), WATCHDOG_WARNING);
  1427. return 0;
  1428. }
  1429. $srcfeature = new stdClass();
  1430. $srcfeature->feature_id = $results->feature_id;
  1431. }
  1432. else {
  1433. watchdog("T_gff3_loader", "Cannot find unique landmark feature: '%landmark'.",
  1434. array('%landmark' => $landmark), WATCHDOG_WARNING);
  1435. return 0;
  1436. }
  1437. }
  1438. }
  1439. elseif (count($results) > 1) {
  1440. watchdog("T_gff3_loader", "multiple landmarks exist with the name: '%landmark'. Cannot
  1441. resolve which one to use. Cannot add the feature location record",
  1442. array('%landmark' => $landmark), WATCHDOG_WARNING);
  1443. return 0;
  1444. }
  1445. else {
  1446. $srcfeature = $results[0];
  1447. }
  1448. }
  1449. elseif (count($results) > 1) {
  1450. watchdog("T_gff3_loader", "multiple landmarks exist with the name: '%landmark'. Cannot
  1451. resolve which one to use. Cannot add the feature location record",
  1452. array('%landmark' => $landmark), WATCHDOG_WARNING);
  1453. return 0;
  1454. }
  1455. else {
  1456. $srcfeature = $results[0];
  1457. }
  1458. // TODO: create an attribute that recognizes the residue_info,locgroup,
  1459. // is_fmin_partial and is_fmax_partial, right now these are
  1460. // hardcoded to be false and 0 below.
  1461. // check to see if this featureloc already exists, but also keep track of the
  1462. // last rank value
  1463. $rank = 0;
  1464. $exists = 0;
  1465. $select = array('feature_id' => $feature->feature_id);
  1466. $options = array(
  1467. 'statement_name' => 'sel_featureloc_feature_id',
  1468. 'order_by' => array(
  1469. 'rank' => 'ASC'
  1470. ),
  1471. );
  1472. $locrecs = tripal_core_chado_select('featureloc', array('*'), $select, $options);
  1473. foreach ($locrecs as $featureloc) {
  1474. $select = array('feature_id' => $featureloc->srcfeature_id);
  1475. $options = array('statement_name' => 'sel_feature_feature_id');
  1476. $columns = array('feature_id', 'name');
  1477. $locsfeature = tripal_core_chado_select('feature', $columns, $select, $options);
  1478. // the source feature name and at least the fmin and fmax must be the same
  1479. // for an update of the featureloc, otherwise we'll insert a new record.
  1480. if (strcmp($locsfeature[0]->name, $landmark)==0 and
  1481. ($featureloc->fmin == $fmin or $featureloc->fmax == $fmax)) {
  1482. $match = array('featureloc_id' => $featureloc->featureloc_id);
  1483. $values = array();
  1484. $exists = 1;
  1485. if ($featureloc->fmin != $fmin) {
  1486. $values['fmin'] = $fmin;
  1487. }
  1488. if ($featureloc->fmax != $fmax) {
  1489. $values['fmax'] = $fmax;
  1490. }
  1491. if ($featureloc->strand != $strand) {
  1492. $values['strand'] = $strand;
  1493. }
  1494. if (count($values) > 0) {
  1495. $options = array('statement_name' => 'upd_featureloc_all');
  1496. tripal_core_chado_update('featureloc', $match, $values, $options);
  1497. }
  1498. }
  1499. $rank = $featureloc->rank + 1;
  1500. }
  1501. if (!$exists) {
  1502. // this feature location is new so add it
  1503. if (strcmp($is_fmin_partial, 'f')==0 or !$is_fmin_partial) {
  1504. $is_fmin_partial = 'FALSE';
  1505. }
  1506. elseif (strcmp($is_fmin_partial, 't')==0 or $is_fmin_partial = 1) {
  1507. $is_fmin_partial = 'TRUE';
  1508. }
  1509. if (strcmp($is_fmax_partial, 'f')==0 or !$is_fmax_partial) {
  1510. $is_fmax_partial = 'FALSE';
  1511. }
  1512. elseif (strcmp($is_fmax_partial, 't')==0 or $is_fmax_partial = 1) {
  1513. $is_fmax_partial = 'TRUE';
  1514. }
  1515. $values = array(
  1516. 'feature_id' => $feature->feature_id,
  1517. 'srcfeature_id' => $srcfeature->feature_id,
  1518. 'fmin' => $fmin,
  1519. 'is_fmin_partial' => $is_fmin_partial,
  1520. 'fmax' => $fmax,
  1521. 'is_fmax_partial' => $is_fmax_partial,
  1522. 'strand' => $strand,
  1523. 'residue_info' => $residue_info,
  1524. 'locgroup' => $locgroup,
  1525. 'rank' => $rank
  1526. );
  1527. $options = array('statement_name' => 'ins_featureloc_all');
  1528. if ($phase) {
  1529. $values['phase'] = $phase;
  1530. $options = array('statement_name' => 'ins_featureloc_allphase');
  1531. }
  1532. $success = tripal_core_chado_insert('featureloc', $values, $options);
  1533. if (!$success) {
  1534. watchdog("T_gff3_loader", "Failed to insert featureloc", array(), WATCHDOG_WARNING);
  1535. exit;
  1536. return 0;
  1537. }
  1538. }
  1539. return 1;
  1540. }
  1541. /**
  1542. *
  1543. *
  1544. * @ingroup gff3_loader
  1545. */
  1546. function tripal_feature_load_gff3_property($feature, $property, $value) {
  1547. // first make sure the cvterm exists. if not, then add it
  1548. $select = array(
  1549. 'name' => $property,
  1550. 'cv_id' => array(
  1551. 'name' => 'feature_property',
  1552. ),
  1553. );
  1554. $options = array('statement_name' => 'sel_cvterm_name_cvid');
  1555. $result = tripal_core_chado_select('cvterm', array('*'), $select, $options);
  1556. // if we don't have a property like this already, then add it otherwise, just return
  1557. if (count($result) == 0) {
  1558. $term = array(
  1559. 'id' => "null:$property",
  1560. 'name' => $property,
  1561. 'namespace' => 'feature_property',
  1562. 'is_obsolete' => 0,
  1563. );
  1564. $cvterm = (object) tripal_cv_add_cvterm($term, 'feature_property', 0, 0);
  1565. if (!$cvterm) {
  1566. watchdog("T_gff3_loader", "Cannot add cvterm, $property", array(), WATCHDOG_WARNING);
  1567. return 0;
  1568. }
  1569. }
  1570. else {
  1571. $cvterm = $result[0];
  1572. }
  1573. // check to see if the property already exists for this feature
  1574. // if it does but the value is unique then increment the rank and add it.
  1575. // if the value is not unique then don't add it.
  1576. $add = 1;
  1577. $rank = 0;
  1578. $select = array(
  1579. 'feature_id' => $feature->feature_id,
  1580. 'type_id' => $cvterm->cvterm_id,
  1581. );
  1582. $options = array(
  1583. 'statement_name' => 'sel_featureprop_featureid_typeid',
  1584. 'order_by' => array(
  1585. 'rank' => 'ASC',
  1586. ),
  1587. );
  1588. $results = tripal_core_chado_select('featureprop', array('*'), $select, $options);
  1589. foreach ($results as $prop) {
  1590. if (strcmp($prop->value, $value)==0) {
  1591. $add = NULL; // don't add it, it already exists
  1592. }
  1593. $rank = $prop->rank + 1;
  1594. }
  1595. // add the property if we pass the check above
  1596. if ($add) {
  1597. $values = array(
  1598. 'feature_id' => $feature->feature_id,
  1599. 'type_id' => $cvterm->cvterm_id,
  1600. 'value' => $value,
  1601. 'rank' => $rank,
  1602. );
  1603. $options = array('statement_name' => 'ins_featureprop_all');
  1604. $result = tripal_core_chado_insert('featureprop', $values, $options);
  1605. if (!$result) {
  1606. watchdog("T_gff3_loader", "cannot add featureprop, $property", array(), WATCHDOG_WARNING);
  1607. }
  1608. }
  1609. }
  1610. /*
  1611. *
  1612. */
  1613. function tripal_feature_load_gff_fasta($fh, $interval, &$num_read, &$intv_read, &$line_num) {
  1614. print "Loading FASTA sequences\n";
  1615. $residues = '';
  1616. $sql = "
  1617. PREPARE sel_gfftemp_un (text) AS
  1618. SELECT feature_id FROM tripal_gff_temp
  1619. WHERE uniquename = $1
  1620. ";
  1621. $status = tripal_core_chado_prepare('sel_gfftemp_un', $sql, array('text'));
  1622. if (!$status) {
  1623. watchdog('T_gff3_loader', 'Cannot prepare statement \'sel_gfftemp_un\'.',
  1624. array(), WATCHDOG_ERROR);
  1625. return '';
  1626. }
  1627. $id = NULL;
  1628. // iterate through the remaining lines of the file
  1629. while ($line = fgets($fh)) {
  1630. $line_num++;
  1631. $size = drupal_strlen($line);
  1632. $num_read += $size;
  1633. $intv_read += $size;
  1634. $line = trim($line);
  1635. // update the job status every 1% features
  1636. if ($job and $intv_read >= $interval) {
  1637. $intv_read = 0;
  1638. $percent = sprintf("%.2f", ($num_read / $filesize) * 100);
  1639. print "Parsing Line $line_num (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes.\r";
  1640. tripal_job_set_progress($job, intval(($num_read / $filesize) * 100));
  1641. }
  1642. // if we encounter a definition line then get the name, uniquename,
  1643. // accession and relationship subject from the definition line
  1644. if (preg_match('/^>/', $line)) {
  1645. // if we are beginning a new sequence then save the last one we
  1646. // just finished.
  1647. if ($id) {
  1648. $sql = "EXECUTE sel_gfftemp_un('%s')";
  1649. $result = tripal_core_chado_execute_prepared('sel_gfftemp_un', $sql, array($id));
  1650. if (!$result) {
  1651. watchdog('T_gff3_loader', 'Cannot find feature to assign FASTA sequence: %uname',
  1652. array('%uname' => $id), WATCHDOG_WARNING);
  1653. }
  1654. // if we have a feature then add the residues
  1655. else {
  1656. $feature = db_fetch_object($result);
  1657. $values = array('residues' => $residues);
  1658. $match = array('feature_id' => $feature->feature_id);
  1659. $options = array('statement_name' => 'upd_feature_re');
  1660. tripal_core_chado_update('feature', $match, $values, $options);
  1661. }
  1662. }
  1663. // get the feature ID for this ID from the tripal_gff_temp table
  1664. $id = preg_replace('/^>(.*)$/', '\1', $line);
  1665. $residues = '';
  1666. }
  1667. else {
  1668. $residues .= trim($line);
  1669. }
  1670. }
  1671. // add in the last sequence
  1672. $sql = "EXECUTE sel_gfftemp_un('%s')";
  1673. $result = tripal_core_chado_execute_prepared('sel_gfftemp_un', $sql, array($id));
  1674. if (!$result) {
  1675. watchdog('T_gff3_loader', 'Cannot find feature to assign FASTA sequence: %uname',
  1676. array('%uname' => $id), WATCHDOG_WARNING);
  1677. }
  1678. // if we have a feature then add the residues
  1679. else {
  1680. $feature = db_fetch_object($result);
  1681. $values = array('residues' => $residues);
  1682. $match = array('feature_id' => $feature->feature_id);
  1683. $options = array('statement_name' => 'upd_feature_re');
  1684. tripal_core_chado_update('feature', $match, $values, $options);
  1685. }
  1686. }