gff_loader.inc 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  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. '#weight' => 1
  27. );
  28. // get the list of organisms
  29. $sql = "SELECT * FROM {organism} ORDER BY genus, species";
  30. $previous_db = tripal_db_set_active('chado'); // use chado database
  31. $org_rset = db_query($sql);
  32. tripal_db_set_active($previous_db); // now use drupal database
  33. $organisms = array();
  34. $organisms[''] = '';
  35. while ($organism = db_fetch_object($org_rset)) {
  36. $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  37. }
  38. $form['organism_id'] = array(
  39. '#title' => t('Organism'),
  40. '#type' => t('select'),
  41. '#description' => t("Choose the organism to which these sequences are associated"),
  42. '#required' => TRUE,
  43. '#options' => $organisms,
  44. );
  45. $form['import_options'] = array(
  46. '#type' => 'fieldset',
  47. '#title' => t('Import Options'),
  48. '#weight' => 6,
  49. '#collapsed' => TRUE
  50. );
  51. $form['import_options']['add_only']= array(
  52. '#type' => 'checkbox',
  53. '#title' => t('Import only new features'),
  54. '#required' => FALSE,
  55. '#description' => t('The job will skip features in the GFF file that already
  56. exist in the database and import only new features.'),
  57. '#weight' => 2
  58. );
  59. $form['import_options']['update']= array(
  60. '#type' => 'checkbox',
  61. '#title' => t('Import all and update'),
  62. '#required' => FALSE,
  63. '#default_value' => 'checked',
  64. '#description' => t('Existing features will be updated and new features will be added. Attributes
  65. for a feature that are not present in the GFF but which are present in the
  66. database will not be altered.'),
  67. '#weight' => 3
  68. );
  69. $form['import_options']['refresh']= array(
  70. '#type' => 'checkbox',
  71. '#title' => t('Import all and replace'),
  72. '#required' => FALSE,
  73. '#description' => t('Existing features will be updated and feature properties not
  74. present in the GFF file will be removed.'),
  75. '#weight' => 4
  76. );
  77. $form['import_options']['remove']= array(
  78. '#type' => 'checkbox',
  79. '#title' => t('Delete features'),
  80. '#required' => FALSE,
  81. '#description' => t('Features present in the GFF file that exist in the database
  82. will be removed rather than imported'),
  83. '#weight' => 5
  84. );
  85. $form['analysis'] = array(
  86. '#type' => 'fieldset',
  87. '#title' => t('Analysis Used to Derive Features'),
  88. '#weight' => 6,
  89. '#collapsed' => TRUE
  90. );
  91. $form['analysis']['desc'] = array(
  92. '#type' => 'markup',
  93. '#value' => t("Why specify an analysis for a data load? All data comes
  94. from some place, even if downloaded from Genbank. By specifying
  95. analysis details for all data uploads, it allows an end user to reproduce the
  96. data set, but at least indicates the source of the data."),
  97. );
  98. // get the list of analyses
  99. $sql = "SELECT * FROM {analysis} ORDER BY name";
  100. $previous_db = tripal_db_set_active('chado'); // use chado database
  101. $org_rset = db_query($sql);
  102. tripal_db_set_active($previous_db); // now use drupal database
  103. $analyses = array();
  104. $analyses[''] = '';
  105. while ($analysis = db_fetch_object($org_rset)) {
  106. $analyses[$analysis->analysis_id] = "$analysis->name ($analysis->program $analysis->programversion, $analysis->sourcename)";
  107. }
  108. $form['analysis']['analysis_id'] = array(
  109. '#title' => t('Analysis'),
  110. '#type' => t('select'),
  111. '#description' => t("Choose the analysis to which these features are associated"),
  112. '#required' => TRUE,
  113. '#options' => $analyses,
  114. );
  115. $form['button'] = array(
  116. '#type' => 'submit',
  117. '#value' => t('Import GFF3 file'),
  118. '#weight' => 10,
  119. );
  120. return $form;
  121. }
  122. /**
  123. *
  124. *
  125. * @ingroup gff3_loader
  126. */
  127. function tripal_feature_gff3_load_form_validate($form, &$form_state) {
  128. $gff_file = $form_state['values']['gff_file'];
  129. $organism_id = $form_state['values']['organism_id'];
  130. $add_only = $form_state['values']['add_only'];
  131. $update = $form_state['values']['update'];
  132. $refresh = $form_state['values']['refresh'];
  133. $remove = $form_state['values']['remove'];
  134. // check to see if the file is located local to Drupal
  135. $dfile = $_SERVER['DOCUMENT_ROOT'] . base_path() . $gff_file;
  136. if (!file_exists($dfile)) {
  137. // if not local to Drupal, the file must be someplace else, just use
  138. // the full path provided
  139. $dfile = $gff_file;
  140. }
  141. if (!file_exists($dfile)) {
  142. 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."));
  143. }
  144. // @coder-ignore: there are no functions being called here
  145. // @todo: break each line of this conditional into separate variables to make more readable
  146. if (($add_only AND ($update OR $refresh OR $remove)) OR
  147. ($update AND ($add_only OR $refresh OR $remove)) OR
  148. ($refresh AND ($update OR $add_only OR $remove)) OR
  149. ($remove AND ($update OR $refresh OR $add_only))) {
  150. form_set_error('add_only', t("Please select only one checkbox from the import options section"));
  151. }
  152. }
  153. /**
  154. *
  155. * @ingroup gff3_loader
  156. */
  157. function tripal_feature_gff3_load_form_submit($form, &$form_state) {
  158. global $user;
  159. $gff_file = $form_state['values']['gff_file'];
  160. $organism_id = $form_state['values']['organism_id'];
  161. $add_only = $form_state['values']['add_only'];
  162. $update = $form_state['values']['update'];
  163. $refresh = $form_state['values']['refresh'];
  164. $remove = $form_state['values']['remove'];
  165. $analysis_id = $form_state['values']['analysis_id'];
  166. $args = array($gff_file, $organism_id, $analysis_id, $add_only, $update, $refresh, $remove);
  167. $type = '';
  168. if ($add_only) {
  169. $type = 'import only new features';
  170. }
  171. if ($update) {
  172. $type = 'import all and update';
  173. }
  174. if ($refresh) {
  175. $type = 'import all and replace';
  176. }
  177. if ($remove) {
  178. $type = 'delete features';
  179. }
  180. $fname = preg_replace("/.*\/(.*)/", "$1", $gff_file);
  181. tripal_add_job("$type GFF3 file: $fname", 'tripal_feature',
  182. 'tripal_feature_load_gff3', $args, $user->uid);
  183. return '';
  184. }
  185. /**
  186. *
  187. *
  188. * @ingroup gff3_loader
  189. */
  190. function tripal_feature_load_gff3($gff_file, $organism_id, $analysis_id,
  191. $add_only =0, $update = 0, $refresh = 0, $remove = 0, $job = NULL) {
  192. // Prepare log
  193. $filename = preg_replace("/.*\/(.*)/", "$1", $blastfile);
  194. $logfile = tempnam(sys_get_temp_dir(), "gff_loader_log.");
  195. $log = fopen($logfile, 'a'); // append parsing results to log file
  196. if (!$log) {
  197. print "ERROR: cannot open log file: $logfile\n";
  198. exit;
  199. }
  200. print "Writing to log file: $logfile\n";
  201. fwrite($log, date("D M j G:i:s Y") . ". Loading $gff_file\n");
  202. // this array is used to cache all of the features in the GFF file and
  203. // used to lookup parent and target relationships
  204. $gff_features = array();
  205. // check to see if the file is located local to Drupal
  206. $dfile = $_SERVER['DOCUMENT_ROOT'] . base_path() . $gff_file;
  207. if (!file_exists($dfile)) {
  208. // if not local to Drupal, the file must be someplace else, just use
  209. // the full path provided
  210. $dfile = $gff_file;
  211. }
  212. if (!file_exists($dfile)) {
  213. print "ERROR: cannot find the file: $dfile\n";
  214. return 0;
  215. }
  216. //$previous_db = tripal_db_set_active('chado');
  217. print "Opening $gff_file\n";
  218. //$lines = file($dfile,FILE_SKIP_EMPTY_LINES);
  219. $fh = fopen($dfile, 'r');
  220. if (!$fh) {
  221. print "ERROR: cannot open file: $dfile\n";
  222. return 0;
  223. }
  224. $filesize = filesize($dfile);
  225. // get the controlled vocaubulary that we'll be using. The
  226. // default is the 'sequence' ontology
  227. // @coder-ignore: non-drupal schema thus table prefixing does not apply
  228. $sql = "SELECT * FROM cv WHERE name = '%s'";
  229. $cv = db_fetch_object(db_query($sql, 'sequence'));
  230. if (!$cv) {
  231. print "ERROR: cannot find the 'sequence' ontology\n";
  232. return '';
  233. }
  234. // get the organism for which this GFF3 file belongs
  235. // @coder-ignore: non-drupal schema thus table prefixing does not apply
  236. $sql = "SELECT * FROM organism WHERE organism_id = %d";
  237. $organism = db_fetch_object(db_query($sql, $organism_id));
  238. $interval = intval($filesize * 0.01);
  239. if ($interval == 0) {
  240. $interval = 1;
  241. }
  242. $in_fasta = 0;
  243. $line_num = 0;
  244. $num_read = 0;
  245. $intv_read = 0;
  246. // iterate through each line of the GFF file
  247. print "Parsing Line $line_num (0.00%).\r";
  248. while ($line = fgets($fh)) {
  249. $line_num++;
  250. $num_read += drupal_strlen($line);
  251. $intv_read += $num_read;
  252. // update the job status every 1% features
  253. if ($job and $intv_read >= $interval) {
  254. $intv_read = 0;
  255. $percent = sprintf("%.2f", ($num_read / $filesize) * 100);
  256. print "Parsing Line $line_num (" . $percent . "%).\r";
  257. fwrite($log, "Parsing line $line_num (" . $percent . "%).\n");
  258. tripal_job_set_progress($job, intval(($num_read / $filesize) * 100));
  259. }
  260. // check to see if we have FASTA section, if so then set the variable
  261. // to start parsing
  262. if (preg_match('/^##FASTA/i', $line)) {
  263. $in_fasta = 1;
  264. break;
  265. }
  266. // skip comments
  267. if (preg_match('/^#/', $line)) {
  268. continue;
  269. }
  270. // skip empty lines
  271. if (preg_match('/^\s*$/', $line)) {
  272. continue;
  273. }
  274. // TODO: handle FASTA section
  275. // get the columns
  276. $cols = explode("\t", $line);
  277. if (sizeof($cols) != 9) {
  278. fwrite($log, "ERROR: improper number of columns on line $line_num\n");
  279. fwrite($log, print_r($cols,1));
  280. return '';
  281. }
  282. // get the column values
  283. $landmark = $cols[0];
  284. $source = $cols[1];
  285. $type = $cols[2];
  286. $start = $cols[3];
  287. $end = $cols[4];
  288. $score = $cols[5];
  289. $strand = $cols[6];
  290. $phase = $cols[7];
  291. $attrs = explode(";", $cols[8]); // split by a semicolon
  292. // ready the start and stop for chado. Chado expects these positions
  293. // to be zero-based, so we substract 1 from the fmin
  294. $fmin = $start - 1;
  295. $fmax = $end;
  296. if ($end < $start) {
  297. $fmin = $end - 1;
  298. $fmax = $start;
  299. }
  300. // format the strand for chado
  301. if (strcmp($strand, '.') == 0) {
  302. $strand = 0;
  303. }
  304. elseif (strcmp($strand, '+') == 0) {
  305. $strand = 1;
  306. }
  307. elseif (strcmp($strand, '-') == 0) {
  308. $strand = -1;
  309. }
  310. if (strcmp($phase, '.') == 0) {
  311. $phase = '';
  312. }
  313. // get the type record
  314. if (!tripal_core_is_sql_prepared('sel_cvterm_cvid_cvtname_synonym')) {
  315. $psql = "PREPARE sel_cvterm_cvid_cvtname_synonym (int, text, text) AS
  316. SELECT CVT.cvterm_id, CVT.cv_id, CVT.name, CVT.definition,
  317. CVT.dbxref_id, CVT.is_obsolete, CVT.is_relationshiptype
  318. FROM {cvterm} CVT
  319. INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  320. LEFT JOIN {cvtermsynonym} CVTS on CVTS.cvterm_id = CVT.cvterm_id
  321. WHERE CV.cv_id = $1 and (CVT.name = $2 or CVTS.synonym = $3)";
  322. $status = chado_query($psql);
  323. if (!$status) {
  324. fwrite($log, "ERROR: cannot prepare statement 'sel_cvterm_cvid_cvtname_synonym' for ontology term $line_num\n");
  325. return '';
  326. }
  327. }
  328. $result = chado_query("EXECUTE sel_cvterm_cvid_cvtname_synonym (%d, '%s', '%s')", $cv->cv_id, $type, $type);
  329. $cvterm = db_fetch_object($result);
  330. if (!$cvterm) {
  331. fwrite($log, "ERROR: cannot find ontology term '$type' on line $line_num.\n");
  332. return '';
  333. }
  334. // break apart each of the attributes
  335. $tags = array();
  336. $attr_name = '';
  337. $attr_uniquename = '';
  338. $attr_residue_info = '';
  339. $attr_locgroup = 0;
  340. $attr_fmin_partial = 'f';
  341. $attr_fmax_partial = 'f';
  342. $attr_is_obsolete = 'f';
  343. $attr_is_analysis = 'f';
  344. $attr_others = '';
  345. $residues = '';
  346. foreach ($attrs as $attr) {
  347. $attr = rtrim($attr);
  348. $attr = ltrim($attr);
  349. if (strcmp($attr, '')==0) {
  350. continue;
  351. }
  352. if (!preg_match('/^[^\=]+\=.+$/', $attr)) {
  353. fwrite($log, "ERROR: attribute is not correctly formatted on line $line_num: $attr\n");
  354. return '';
  355. }
  356. // break apart each tag
  357. $tag = preg_split("/=/", $attr, 2); // split by equals sign
  358. // multiple instances of an attribute are separated by commas
  359. $tags[$tag[0]] = explode(",", $tag[1]); // split by comma
  360. // replace the URL escape codes for each tag
  361. for ($i = 0; $i < count($tags[$tag[0]]); $i++) {
  362. $tags[$tag[0]][$i] = urldecode($tags[$tag[0]][$i]);
  363. }
  364. // get the name and ID tags
  365. if (strcmp($tag[0], 'ID')==0) {
  366. $attr_uniquename = $tag[1];
  367. }
  368. elseif (strcmp($tag[0], 'Name')==0) {
  369. $attr_name = $tag[1];
  370. }
  371. // get the list of non-reserved attributes
  372. elseif (strcmp($tag[0], 'Alias')!=0 and strcmp($tag[0], 'Parent')!=0 and
  373. strcmp($tag[0], 'Target')!=0 and strcmp($tag[0], 'Gap')!=0 and
  374. strcmp($tag[0], 'Derives_from')!=0 and strcmp($tag[0], 'Note')!=0 and
  375. strcmp($tag[0], 'Dbxref')!=0 and strcmp($tag[0], 'Ontology_term')!=0 and
  376. strcmp($tag[0], 'Is_circular')!=0) {
  377. $attr_others[$tag[0]] = $tag[1];
  378. }
  379. }
  380. // if neither name nor uniquename are provided then generate one
  381. if (!$attr_uniquename and !$attr_name) {
  382. if (array_key_exists('Parent', $tags)) {
  383. $attr_uniquename = $tags['Parent'][0] . "-$type-$landmark:$fmin..$fmax";
  384. }
  385. else {
  386. fwrite($log, "ERROR: cannot generate a uniquename for feature on line $line_num\n");
  387. exit;
  388. }
  389. $attr_name = $attr_uniquename;
  390. }
  391. // if a name is not specified then use the unique name
  392. if (strcmp($attr_name, '')==0) {
  393. $attr_name = $attr_uniquename;
  394. }
  395. // if an ID attribute is not specified then use the attribute name and
  396. // hope for the best
  397. if (!$attr_uniquename) {
  398. $attr_uniquename = $attr_name;
  399. }
  400. // make sure the landmark sequence exists in the database. We don't
  401. // know the type of the landmark so we'll hope that it's unique across
  402. // all types. If not we'll error out. This test is only necessary if
  403. // if the landmark and the uniquename are different. If they are the same
  404. // then this is the information for the landmark
  405. if (strcmp($landmark, $attr_uniquename) != 0 ) {
  406. $select = array(
  407. 'organism_id' => $organism_id,
  408. 'uniquename' => $landmark,
  409. );
  410. $columns = array('count(*) as num_landmarks');
  411. $options = array('statement_name' => 'sel_feature_organismid_uniquename');
  412. $count = tripal_core_chado_select('feature', $columns, $select, $options);
  413. if (!$count or $count[0]->num_landmarks == 0) {
  414. fwrite($log, "ERROR: the landmark '$landmark' cannot be found for this organism. ".
  415. "Please add the landmark and then retry the import of this GFF3 ".
  416. "file.\n");
  417. return '';
  418. }
  419. if ($count[0]->num_landmarks > 1) {
  420. fwrite($log, "ERROR: the landmark '$landmark' is not unique for this organism. ".
  421. "The features cannot be associated.\n");
  422. return '';
  423. }
  424. }
  425. // if the option is to remove or refresh then we want to remove
  426. // the feature from the database.
  427. if ($remove or $refresh) {
  428. fwrite($log, "Removing feature '$attr_uniquename'\n");
  429. $sql = "DELETE FROM {feature}
  430. WHERE organism_id = %d and uniquename = '%s' and type_id = %d";
  431. $match = array(
  432. 'organism_id' => $organism->organism_id,
  433. 'uniquename' => $attr_uniquename,
  434. 'type_id' => $cvterm->cvterm_id
  435. );
  436. $result = tripal_core_chado_delete('feature',$match);
  437. if (!$result) {
  438. fwrite($log, "ERROR: cannot delete feature $attr_uniquename\n");
  439. }
  440. $feature = 0;
  441. unset($result);
  442. }
  443. // add or update the feature and all properties
  444. if ($update or $refresh or $add_only) {
  445. // add/update the feature
  446. $feature = tripal_feature_load_gff3_feature($organism, $analysis_id, $cvterm,
  447. $attr_uniquename, $attr_name, $residues, $attr_is_analysis,
  448. $attr_is_obsolete, $add_only, $score, $log);
  449. // store all of the features for use later by parent and target
  450. // relationships
  451. $gff_features[$feature->uniquename]['type'] = $type;
  452. $gff_features[$feature->uniquename]['strand'] = $strand;
  453. if ($feature) {
  454. // add/update the featureloc if the landmark and the ID are not the same
  455. // if they are the same then this entry in the GFF is probably a landmark identifier
  456. if (strcmp($landmark, $attr_uniquename) !=0 ) {
  457. tripal_feature_load_gff3_featureloc($feature, $organism,
  458. $landmark, $fmin, $fmax, $strand, $phase, $attr_fmin_partial,
  459. $attr_fmax_partial, $attr_residue_info, $attr_locgroup, $log);
  460. }
  461. // add any aliases for this feature
  462. if (array_key_exists('Alias', $tags)) {
  463. tripal_feature_load_gff3_alias($feature, $tags['Alias'], $log);
  464. }
  465. // add any dbxrefs for this feature
  466. if (array_key_exists('Dbxref', $tags)) {
  467. tripal_feature_load_gff3_dbxref($feature, $tags['Dbxref'], $log);
  468. }
  469. // add any ontology terms for this feature
  470. if (array_key_exists('Ontology_term', $tags)) {
  471. tripal_feature_load_gff3_ontology($feature, $tags['Ontology_term'], $log);
  472. }
  473. // add parent relationships
  474. if (array_key_exists('Parent', $tags)) {
  475. tripal_feature_load_gff3_parents($feature, $cvterm, $tags['Parent'], $gff_features, $organism_id, $fmin, $log);
  476. }
  477. // add target relationships
  478. if (array_key_exists('Target', $tags)) {
  479. preg_match('/^(.*)\s+\d+\s+\d+\s+(\+|\-)*$/', $tags['Target'][0], $matches);
  480. $target_feature = $matches[1];
  481. $start = $matches[2];
  482. $end = $matches[3];
  483. if ($matches[4]) {
  484. $target_strand = $matches[4];
  485. }
  486. else {
  487. $target_strand = '.';
  488. }
  489. $target_fmin = $start - 1;
  490. $target_fmax = $end;
  491. if ($end < $start) {
  492. $target_fmin = $end - 1;
  493. $target_fmax = $start;
  494. }
  495. #print "Target: $target_feature, $target_fmin-$target_fmax $target_dir\n";
  496. tripal_feature_load_gff3_featureloc($feature, $organism,
  497. $target_feature, $target_fmin, $target_fmax, $target_strand, $phase, $attr_fmin_partial,
  498. $attr_fmax_partial, $attr_residue_info, $attr_locgroup, $log);
  499. }
  500. // add gap information. This goes in simply as a property
  501. if (array_key_exists('Gap', $tags)) {
  502. tripal_feature_load_gff3_property($feature, 'Gap', $tags['Gap'][0], $log);
  503. }
  504. // add notes. This goes in simply as a property
  505. if (array_key_exists('Note', $tags)) {
  506. tripal_feature_load_gff3_property($feature, 'Note', $tags['Note'][0], $log);
  507. }
  508. // add the Derives_from relationship (e.g. polycistronic genes).
  509. if (array_key_exists('Derives_from', $tags)) {
  510. tripal_feature_load_gff3_derives_from($feature, $tags['Derives_from'][0], $gff_features, $organism, $log);
  511. }
  512. // add in the GFF3_source dbxref so that GBrowse can find the feature using the source column
  513. $source_ref = array('GFF_source:' . $source);
  514. tripal_feature_load_gff3_dbxref($feature, $source_ref, $log);
  515. // add any additional attributes
  516. if ($attr_others) {
  517. foreach ($attr_others as $property => $value) {
  518. tripal_feature_load_gff3_property($feature, $property, $value, $log);
  519. }
  520. }
  521. }
  522. }
  523. }
  524. // now set the rank of any parent/child relationships. The order is based
  525. // on the fmin. The start rank is 1. This allows features with other
  526. // relationships to be '0' (the default), and doesn't interfer with the
  527. // ordering defined here.
  528. foreach ($gff_features as $parent => $details) {
  529. // only iterate through parents that have children
  530. if (array_key_exists('children',$details)) {
  531. // get the parent
  532. $values = array(
  533. 'organism_id' => $organism->organism_id,
  534. 'uniquename' => $parent,
  535. 'type_id' => array(
  536. 'cv_id' => array(
  537. 'name' => 'sequence'
  538. ),
  539. 'name' => $details['type'],
  540. ),
  541. );
  542. $options = array('statement_name' => 'sel_feature_organismid_uniquename_typeid');
  543. $result = tripal_core_chado_select('feature', array('*'), $values, $options);
  544. $pfeature = $result[0];
  545. // sort the children by order of their fmin positions (values of assoc. array)
  546. // if the parent is on the reverse strand then sort in reverse
  547. if ($details['strand'] == -1) {
  548. arsort($details['children']);
  549. }
  550. else {
  551. asort($details['children']);
  552. }
  553. // now iterate through the children and set their rank
  554. $rank = 1;
  555. fwrite($log, "Updating child ranks for $parent (" . $details['type'] . ")\n");
  556. foreach ($details['children'] as $kfeature_id => $kfmin) {
  557. $match = array(
  558. 'object_id' => $pfeature->feature_id,
  559. 'subject_id' => $kfeature_id,
  560. 'type_id' => array(
  561. 'cv_id' => array(
  562. 'name' => 'relationship'
  563. ),
  564. 'name' => 'part_of',
  565. ),
  566. );
  567. $values = array(
  568. 'rank' => $rank,
  569. );
  570. $options = array('statement_name' => 'upd_featurerelationship_all');
  571. tripal_core_chado_update('feature_relationship', $match, $values, $options);
  572. $rank++;
  573. }
  574. }
  575. }
  576. //tripal_db_set_active($previous_db);
  577. print "Done.\nSuccessful and failed entries have been saved in the log file:\n $logfile\n";
  578. fwrite($log, "\n");
  579. fclose($log);
  580. return 1;
  581. }
  582. /**
  583. *
  584. *
  585. * @ingroup gff3_loader
  586. */
  587. function tripal_feature_load_gff3_derives_from($feature, $subject, $gff_features, $organism, $log) {
  588. // first get the subject feature
  589. $match = array(
  590. 'organism_id' => $organism->organism_id,
  591. 'uniquename' => $subject,
  592. 'type_id' => array(
  593. 'name' => $gff_features[$subject]['type'],
  594. 'cv_id' => array(
  595. 'name' => 'sequence'
  596. ),
  597. ),
  598. );
  599. $options = array('statement_name' => 'sel_feature_organismid_uniquename_typeid');
  600. $sfeature = tripal_core_chado_select('feature', array('*'), $match, $options);
  601. if (count($sfeature)==0) {
  602. fwrite($log, "ERROR: could not add 'Derives_from' relationship for $feature->uniquename and $subject. Subject feature, '$subject', cannot be found\n");
  603. return;
  604. }
  605. // now check to see if the relationship already exists
  606. $values = array(
  607. 'object_id' => $sfeature[0]->feature_id,
  608. 'subject_id' => $feature->feature_id,
  609. 'type_id' => array(
  610. 'cv_id' => array(
  611. 'name' => 'relationship'
  612. ),
  613. 'name' => 'derives_from',
  614. ),
  615. 'rank' => 0
  616. );
  617. $options = array('statement_name' => 'sel_featurerelationship_objectid_subjectid_typeid_rank');
  618. $rel = tripal_core_chado_select('feature_relationship', array('*'), $values, $options);
  619. if (count($rel) > 0) {
  620. fwrite($log, " Relationship already exists: $feature->uniquename derives_from $subject\n");
  621. return;
  622. }
  623. // finally insert the relationship if it doesn't exist
  624. $options = array('statement_name' => 'ins_featurerelationship_objectid_subjectid_typeid_rank');
  625. $ret = tripal_core_chado_insert('feature_relationship', $values, $options);
  626. if (!$ret) {
  627. fwrite($log, "ERROR: could not add 'Derives_from' relationship for $feature->uniquename and $subject\n");
  628. }
  629. else {
  630. fwrite($log, " Added relationship: $feature->uniquename derives_from $subject\n");
  631. }
  632. }
  633. /**
  634. *
  635. *
  636. * @ingroup gff3_loader
  637. */
  638. function tripal_feature_load_gff3_parents($feature, $cvterm, $parents, &$gff_features, $organism_id, $fmin, $log) {
  639. $uname = $feature->uniquename;
  640. $type = $cvterm->name;
  641. $rel_type = 'part_of';
  642. // prepare these SQL statements that will be used repeatedly.
  643. if (!tripal_core_is_sql_prepared('sel_cvterm_cvname_cvtname_synonym')) {
  644. $psql = "PREPARE sel_cvterm_cvname_cvtname_synonym (text, text, text) AS
  645. SELECT CVT.cvterm_id
  646. FROM cvterm CVT
  647. INNER JOIN cv CV on CVT.cv_id = CV.cv_id
  648. LEFT JOIN cvtermsynonym CVTS on CVTS.cvterm_id = CVT.cvterm_id
  649. WHERE cv.name = $1 and (CVT.name = $2 or CVTS.synonym = $3)";
  650. $status = chado_query($psql);
  651. if (!$status) {
  652. fwrite($log, "ERROR: cannot prepare statement 'sel_cvterm_cvname_cvtname_synonym' for ontology term\n");
  653. return '';
  654. }
  655. }
  656. // iterate through the parents in the list
  657. foreach ($parents as $parent) {
  658. $parent_type = $gff_features[$parent]['type'];
  659. // try to find the parent
  660. $parentcvterm = db_fetch_object(chado_query("EXECUTE sel_cvterm_cvname_cvtname_synonym ('%s', '%s', '%s')", 'sequence', $parent_type, $parent_type));
  661. $relcvterm = db_fetch_object(chado_query("EXECUTE sel_cvterm_cvname_cvtname_synonym ('%s', '%s', '%s')", 'relationship', $rel_type, $rel_type));
  662. $values = array(
  663. 'organism_id' => $organism_id,
  664. 'uniquename' => $parent,
  665. 'type_id' => $parentcvterm->cvterm_id,
  666. );
  667. $options = array('statement_name' => 'sel_feature_organismid_uniquename_typeid');
  668. $result = tripal_core_chado_select('feature', array('*'), $values, $options);
  669. $parent_feature = $result[0];
  670. // we want to add this feature to the child list for the parent
  671. // when the loader finishes, it will go back through the parent
  672. // features and rank the children by position
  673. $gff_features[$parent]['children'][$feature->feature_id] = $fmin;
  674. // if the parent exists then add the relationship otherwise print error and skip
  675. if ($parent_feature) {
  676. // check to see if the relationship already exists
  677. $values = array(
  678. 'object_id' => $parent_feature->feature_id,
  679. 'subject_id' => $feature->feature_id,
  680. 'type_id' => $relcvterm->cvterm_id,
  681. );
  682. $options = array('statement_name' => 'sel_featurerelationship_objectid_subjectid_typeid');
  683. $rel = tripal_core_chado_select('feature_relationship', array('*'), $values, $options);
  684. if (count($rel) > 0) {
  685. fwrite($log, " Relationship already exists, skipping '$uname' ($type) $rel_type '$parent' ($parent_type)\n");
  686. }
  687. else {
  688. // the relationship doesn't already exist, so add it.
  689. $values = array(
  690. 'subject_id' => $feature->feature_id,
  691. 'object_id' => $parent_feature->feature_id,
  692. 'type_id' => $relcvterm->cvterm_id,
  693. );
  694. $options = array('statement_name' => 'ins_featurerelationship_subjectid_objectid_typeid');
  695. $result = tripal_core_chado_insert('feature_relationship', $values, $options);
  696. if (!$result) {
  697. fwrite($log, "WARNING: failed to insert feature relationship '$uname' ($type) $rel_type '$parent' ($parent_type)\n");
  698. }
  699. else {
  700. fwrite($log, " Inserted relationship relationship: '$uname' ($type) $rel_type '$parent' ($parent_type)\n");
  701. }
  702. }
  703. }
  704. else {
  705. fwrite($log, "WARNING: cannot establish relationship '$uname' ($type) $rel_type '$parent' ($parent_type): Cannot find the parent\n");
  706. }
  707. }
  708. }
  709. /**
  710. *
  711. *
  712. * @ingroup gff3_loader
  713. */
  714. function tripal_feature_load_gff3_dbxref($feature, $dbxrefs, $log) {
  715. // iterate through each of the dbxrefs
  716. foreach ($dbxrefs as $dbxref) {
  717. // get the database name from the reference. If it doesn't exist then create one.
  718. $ref = explode(":", $dbxref);
  719. $dbname = $ref[0];
  720. $accession = $ref[1];
  721. // first look for the database name if it doesn't exist then create one.
  722. // first check for the fully qualified URI (e.g. DB:<dbname>. If that
  723. // can't be found then look for the name as is. If it still can't be found
  724. // the create the database
  725. $options = array('statement_name' => 'sel_db_name');
  726. $db = tripal_core_chado_select('db', array('db_id'), array('name' => "DB:$dbname"), $options);
  727. if (count($db) == 0) {
  728. $db = tripal_core_chado_select('db', array('db_id'), array('name' => "$dbname"), $options);
  729. }
  730. if (count($db) == 0) {
  731. $options = array('statement_name' => 'ins_db_name');
  732. $ret = tripal_core_chado_insert('db', array('name' => $dbname,
  733. 'description' => 'Added automatically by the GFF loader'), $options);
  734. if ($ret) {
  735. fwrite($log, " Added new database: $dbname\n");
  736. $options = array('statement_name' => 'sel_db_name');
  737. $db = tripal_core_chado_select('db', array('db_id'), array('name' => "$dbname"), $options);
  738. }
  739. else {
  740. fwrite($log, "ERROR: cannot find or add the database $dbname\n");
  741. exit;
  742. return 0;
  743. }
  744. }
  745. $db = $db[0];
  746. // now check to see if the accession exists
  747. $options = array('statement_name' => 'sel_dbxref_accession_dbid');
  748. $dbxref = tripal_core_chado_select('dbxref', array('dbxref_id'), array(
  749. 'accession' => $accession, 'db_id' => $db->db_id), $options);
  750. // if the accession doesn't exist then we want to add it
  751. if (sizeof($dbxref) == 0) {
  752. $options = array('statement_name' => 'ins_dbxref_dbid_accession_version');
  753. $ret = tripal_core_chado_insert('dbxref', array('db_id' => $db->db_id,
  754. 'accession' => $accession, 'version' => ''), $options);
  755. $options = array('statement_name' => 'sel_dbxref_accession_dbid');
  756. $dbxref = tripal_core_chado_select('dbxref', array('dbxref_id'), array(
  757. 'accession' => $accession, 'db_id' => $db->db_id), $options);
  758. }
  759. $dbxref = $dbxref[0];
  760. // check to see if this feature dbxref already exists
  761. $options = array('statement_name' => 'sel_featuredbxref_dbxrefid_featureid');
  762. $fdbx = tripal_core_chado_select('feature_dbxref', array('feature_dbxref_id'),
  763. array('dbxref_id' => $dbxref->dbxref_id, 'feature_id' => $feature->feature_id), $options);
  764. // now associate this feature with the database reference if it doesn't
  765. // already exist
  766. if (sizeof($fdbx)==0) {
  767. $options = array('statement_name' => 'ins_featuredbxref_dbxrefid_featureid');
  768. $ret = tripal_core_chado_insert('feature_dbxref', array(
  769. 'dbxref_id' => $dbxref->dbxref_id,
  770. 'feature_id' => $feature->feature_id), $options);
  771. if ($ret) {
  772. fwrite($log, " Adding Dbxref $dbname:$accession\n");
  773. }
  774. else {
  775. fwrite($log, "ERROR: failed to insert Dbxref: $dbname:$accession\n");
  776. return 0;
  777. }
  778. }
  779. else {
  780. fwrite($log, " Dbxref already associated, skipping $dbname:$accession\n");
  781. }
  782. }
  783. return 1;
  784. }
  785. /**
  786. *
  787. *
  788. * @ingroup gff3_loader
  789. */
  790. function tripal_feature_load_gff3_ontology($feature, $dbxrefs, $log) {
  791. // iterate through each of the dbxrefs
  792. foreach ($dbxrefs as $dbxref) {
  793. // get the database name from the reference. If it doesn't exist then create one.
  794. $ref = explode(":", $dbxref);
  795. $dbname = $ref[0];
  796. $accession = $ref[1];
  797. // first look for the database name
  798. $options = array('statement_name' => 'sel_db_name');
  799. $db = tripal_core_chado_select('db', array('db_id'), array('name' => "DB:$dbname"), $options);
  800. if (sizeof($db) == 0) {
  801. $db = tripal_core_chado_select('db', array('db_id'), array('name' => "$dbname"), $options);
  802. }
  803. if (sizeof($db) == 0) {
  804. fwrite($log, "ERROR: Database, $dbname is missing for reference: $dbname:$accession\n");
  805. return 0;
  806. }
  807. $db = $db[0];
  808. // now check to see if the accession exists
  809. $options = array('statement_name' => 'sel_dbxref_accession_dbid');
  810. $dbxref = tripal_core_chado_select('dbxref', array('dbxref_id'), array(
  811. 'accession' => $accession, 'db_id' => $db->db_id), $options);
  812. if (sizeof($dbxref) == 0) {
  813. fwrite($log, "ERROR: Accession, $accession is missing for reference: $dbname:$accession\n");
  814. return 0;
  815. }
  816. $dbxref = $dbxref[0];
  817. // now check to see if the cvterm exists
  818. $options = array('statement_name' => 'sel_cvterm_dbxrefid');
  819. $cvterm = tripal_core_chado_select('cvterm', array('cvterm_id'), array(
  820. 'dbxref_id' => $dbxref->dbxref_id), $options);
  821. // if it doesn't exist in the cvterm table, look for an alternate id
  822. if (sizeof($cvterm) == 0) {
  823. $options = array('statement_name' => 'sel_cvtermdbxref_dbxrefid');
  824. $cvterm = tripal_core_chado_select('cvterm_dbxref', array('cvterm_id'), array(
  825. 'dbxref_id' => $dbxref->dbxref_id), $options);
  826. }
  827. if (sizeof($cvterm) == 0) {
  828. fwrite($log, "ERROR: CV Term is missing for reference: $dbname:$accession\n");
  829. return 0;
  830. }
  831. $cvterm = $cvterm[0];
  832. // check to see if this feature cvterm already exists
  833. $options = array('statement_name' => 'sel_featurecvterm_cvtermid_featureid');
  834. $fcvt = tripal_core_chado_select('feature_cvterm', array('feature_cvterm_id'),
  835. array('cvterm_id' => $cvterm->cvterm_id, 'feature_id' => $feature->feature_id),
  836. $options);
  837. // now associate this feature with the cvterm if it doesn't already exist
  838. if (sizeof($fcvt)==0) {
  839. $values = array(
  840. 'cvterm_id' => $cvterm->cvterm_id,
  841. 'feature_id' => $feature->feature_id,
  842. 'pub_id' => array(
  843. 'uniquename' => 'null',
  844. ),
  845. );
  846. $options = array('statement_name' => 'ins_featurecvterm_cvtermid_featureid_pubid');
  847. $ret = tripal_core_chado_insert('feature_cvterm', $values, $options);
  848. if ($ret) {
  849. fwrite($log, " Adding ontology term $dbname:$accession\n");
  850. }
  851. else {
  852. fwrite($log, "ERROR: failed to insert ontology term: $dbname:$accession\n");
  853. return 0;
  854. }
  855. }
  856. else {
  857. fwrite($log, " Ontology term already associated, skipping $dbname:$accession\n");
  858. }
  859. }
  860. return 1;
  861. }
  862. /**
  863. *
  864. *
  865. * @ingroup gff3_loader
  866. */
  867. function tripal_feature_load_gff3_alias($feature, $aliases, $log) {
  868. // make sure we have a 'synonym_type' vocabulary
  869. $select = array('name' => 'synonym_type');
  870. $options = array('statement_name' => 'sel_cv_name');
  871. $results = tripal_core_chado_select('cv', array('*'), $selct, $options);
  872. if (count($results) > 0) {
  873. // insert the 'synonym_type' vocabulary
  874. $values = array(
  875. 'name' => 'synonym_type',
  876. 'definition' => 'vocabulary for synonym types',
  877. );
  878. $options = array('statement_name' => 'ins_cv_name_definition');
  879. $result = tripal_core_chado_insert('cv', $values, $options);
  880. if (!result) {
  881. fwrite($log, "ERROR: Failed to add the synonyms type vocabulary");
  882. return 0;
  883. }
  884. // now that we've added the cv we need to get the record
  885. $options = array('statement_name' => 'sel_cv_name');
  886. $results = tripal_core_chado_select('cv', array('*'), $select, $options);
  887. $syncv = $results[0];
  888. }
  889. else {
  890. $syncv = $results[0];
  891. }
  892. // get the 'exact' cvterm, which is the type of synonym we're adding
  893. $select = array(
  894. 'name' => 'exact',
  895. 'cv_id' => array(
  896. 'name' => 'synonym_type'
  897. ),
  898. );
  899. $options = array('statement_name' => 'sel_cvterm_name_cvid');
  900. $result = tripal_core_chado_select('cvterm', array('*'), $select, $options);
  901. $syntype = $result[0];
  902. if (!$syntype) {
  903. $term = array(
  904. 'name' => array('exact'),
  905. 'id' => array("internal:exact"),
  906. 'definition' => array(''),
  907. 'is_obsolete' => array(0),
  908. );
  909. // TODO: fix the function so it uses prepared statements
  910. $syntype = tripal_cv_add_cvterm($term, $syncv->name, 0, 1);
  911. if (!$syntype) {
  912. fwrite($log, "Cannot add synonym type: internal:$type");
  913. return 0;
  914. }
  915. }
  916. // iterate through all of the aliases and add each one
  917. foreach ($aliases as $alias) {
  918. fwrite($log, " Adding Alias $alias\n");
  919. // check to see if the alias already exists in the synonym table
  920. // if not, then add it
  921. $select = array(
  922. 'name' => $alias,
  923. 'type_id' => $syntype->cvterm_id,
  924. );
  925. $options = array('statement_name' => 'sel_synonym_name_typeid');
  926. $result = tripal_core_chado_select('synonym', array('*'), $select, $options);
  927. $synonym = $result[0];
  928. if (!$synonym) {
  929. $values = array(
  930. 'name' => $alias,
  931. 'type_id' => $syntype->cvterm_id,
  932. 'synonym_sgml' => '',
  933. );
  934. $options = array('statement_name' => 'ins_synonym_name_typeid_synonymsgml');
  935. $result = tripal_core_chado_insert('synonym', $values, $options);
  936. if (!$result) {
  937. fwrite($log, "ERROR: cannot add alias $alias to synonym table\n");
  938. }
  939. }
  940. $options = array('statement_name' => 'sel_synonym_name_typeid');
  941. $result = tripal_core_chado_select('synonym', array('*'), $select, $options);
  942. $synonym = $result[0];
  943. // check to see if we have a NULL publication in the pub table. If not,
  944. // then add one.
  945. // @coder-ignore: non-drupal schema thus table prefixing does not apply
  946. $select = array('uniquename' => 'null');
  947. $options = array('statement_name' => 'sel_pub_uniquename');
  948. $result = tripal_core_chado_select('pub', array('*'), $select, $options);
  949. $pub = $result[0];
  950. if (!$pub) {
  951. // prepare the statement
  952. if (!tripal_core_is_sql_prepared('ins_pub_uniquename_typeid')) {
  953. $psql = "PREPARE ins_pub_uniquename_typeid (text, text) AS
  954. INSERT INTO pub (uniquename,type_id) VALUES ('%s',
  955. (SELECT cvterm_id
  956. FROM cvterm CVT
  957. INNER JOIN dbxref DBX on DBX.dbxref_id = CVT.dbxref_id
  958. INNER JOIN db DB on DB.db_id = DBX.db_id
  959. WHERE CVT.name = $1 and DB.name = $2)";
  960. $status = chado_query($psql);
  961. if (!$status) {
  962. fwrite($log, "ERROR: cannot prepare statement 'ins_pub_uniquename_typeid'\n");
  963. return 0;
  964. }
  965. }
  966. // insert the null pub
  967. $result = db_fetch_object(chado_query("EXECUTE ins_pub_uniquename_typeid ('%s', '%s')", 'null', 'null'));
  968. if (!$result) {
  969. fwrite($log, "ERROR: cannot add null publication needed for setup of alias\n");
  970. return 0;
  971. }
  972. }
  973. $options = array('statement_name' => 'sel_pub_uniquename');
  974. $result = tripal_core_chado_select('pub', array('*'), $select, $options);
  975. $pub = $result[0];
  976. // check to see if the synonym exists in the feature_synonym table
  977. // if not, then add it.
  978. $select = array(
  979. 'synonym_id' => $synonym->synonym_id,
  980. 'feature_id' => $feature->feature_id,
  981. 'pub_id' => $pub->pub_id,
  982. );
  983. $options = array('statement_name', 'sel_synonymfeature_synonymid_featureid_pubid');
  984. $result = tripal_core_chado_select('feature_synonym', array('*'), $select, $options);
  985. $fsyn = $result[0];
  986. if (!$fsyn) {
  987. $values = array(
  988. 'synonym_id' => $synonym->synonym_id,
  989. 'feature_id' => $feature->feature_id,
  990. 'pub_id' => $pub->pub_id,
  991. );
  992. $options = array('statement_name', 'ins_synonymfeature_synonymid_featureid_pubid');
  993. $result = tripal_core_chado_insert('feature_synonym', $values, $options);
  994. if (!$result) {
  995. fwrite($log, "ERROR: cannot add alias $alias to feature synonym table\n");
  996. return 0;
  997. }
  998. }
  999. else {
  1000. fwrite($log, " Synonym $alias already exists. Skipping\n");
  1001. }
  1002. }
  1003. return 1;
  1004. }
  1005. /**
  1006. *
  1007. *
  1008. * @ingroup gff3_loader
  1009. */
  1010. function tripal_feature_load_gff3_feature($organism, $analysis_id, $cvterm, $uniquename, $name,
  1011. $residues, $is_analysis='f', $is_obsolete='f', $add_only, $score, $log) {
  1012. // check to see if the feature already exists
  1013. $fselect = array(
  1014. 'organism_id' => $organism->organism_id,
  1015. 'uniquename' => $uniquename,
  1016. 'type_id' => $cvterm->cvterm_id
  1017. );
  1018. $options = array('statement_name' => 'sel_feature_organismid_uniquename_typeid');
  1019. $result = tripal_core_chado_select('feature', array('*'), $fselect, $options);
  1020. $feature = $result[0];
  1021. if (strcmp($is_obsolete, 'f')==0 or $is_obsolete == 0) {
  1022. $is_obsolete = 'false';
  1023. }
  1024. if (strcmp($is_obsolete, 't')==0 or $is_obsolete == 1) {
  1025. $is_obsolete = 'true';
  1026. }
  1027. if (strcmp($is_analysis, 'f')==0 or $is_analysis == 0) {
  1028. $is_analysis = 'false';
  1029. }
  1030. if (strcmp($is_analysis, 't')==0 or $is_analysis == 1) {
  1031. $is_analysis = 'true';
  1032. }
  1033. // insert the feature if it does not exist otherwise perform an update
  1034. if (!$feature) {
  1035. fwrite($log, " Adding feature '$uniquename' ($cvterm->name)\n");
  1036. $values = array(
  1037. 'organism_id' => $organism->organism_id,
  1038. 'name' => $name,
  1039. 'uniquename' => $uniquename,
  1040. 'residues' => $residues,
  1041. 'seqlen' => drupal_strlen($residues),
  1042. 'md5checksum' => md5($residues),
  1043. 'type_id' => $cvterm->cvterm_id,
  1044. 'is_analysis' => $is_analysis,
  1045. 'is_obsolete' => $is_obsolete,
  1046. );
  1047. $options = array('statement_name' => 'ins_feature_all');
  1048. $result = tripal_core_chado_insert('feature', $values, $options);
  1049. if (!$result) {
  1050. fwrite($log, "ERROR: failed to insert feature '$uniquename' ($cvterm->name)\n");
  1051. return 0;
  1052. }
  1053. }
  1054. elseif (!$add_only) {
  1055. fwrite($log, "Updating feature '$uniquename' ($cvterm->name)\n");
  1056. $values = array(
  1057. 'name' => $name,
  1058. 'residues' => $residues,
  1059. 'seqlen' => drupal_strlen($residues),
  1060. 'md5checksum' => md5($residues),
  1061. 'is_analysis' => $is_analysis,
  1062. 'is_obsolete' => $is_obsolete,
  1063. );
  1064. $match = array(
  1065. 'organism_id' => $organism->organism_id,
  1066. 'uniquename' => $uniquename,
  1067. 'type_id' => $cvterm->cvterm_id,
  1068. );
  1069. $options = array('statement_name' => 'upd_feature');
  1070. $result = tripal_core_chado_update('feature', $match, $values, $options);
  1071. if (!$result) {
  1072. fwrite($log, "ERROR: failed to update feature '$uniquename' ($cvterm->name)\n");
  1073. return 0;
  1074. }
  1075. }
  1076. else {
  1077. // the feature exists and we don't want to update it so return
  1078. // a value of 0. This will stop all downstream property additions
  1079. fwrite($log, "Skipping existing feature: '$uniquename' ($cvterm->name).\n");
  1080. return 0;
  1081. }
  1082. // get the newly added feature
  1083. $options = array('statement_name' => 'sel_feature_organismid_uniquename_typeid');
  1084. $result = tripal_core_chado_select('feature', array('*'), $fselect, $options);
  1085. $feature = $result[0];
  1086. // add the analysisfeature entry to the analysisfeature table if it doesn't already exist
  1087. $af_values = array(
  1088. 'analysis_id' => $analysis_id,
  1089. 'feature_id' => $feature->feature_id
  1090. );
  1091. $options = array('statement_name' => 'sel_analysisfeature_analysisid_featureid');
  1092. $afeature = tripal_core_chado_select('analysisfeature', array('analysisfeature_id'), $af_values, $options);
  1093. if (count($afeature)==0) {
  1094. // if a score is available then set that to be the significance field
  1095. if (strcmp($score, '.') != 0) {
  1096. $af_values['significance'] = $score;
  1097. $options = array('statement_name' => 'ins_analysisfeature_analysisid_featureid_significance');
  1098. }
  1099. else {
  1100. $options = array('statement_name' => 'ins_analysisfeature_analysisid_featureid');
  1101. }
  1102. if (!tripal_core_chado_insert('analysisfeature', $af_values, $options)) {
  1103. fwrite($log, "ERROR: could not add analysisfeature record: $analysis_id, $feature->feature_id\n");
  1104. }
  1105. else {
  1106. fwrite($log, " Added analysisfeature record\n");
  1107. }
  1108. }
  1109. else {
  1110. // if a score is available then set that to be the significance field
  1111. $new_vals = array();
  1112. if (strcmp($score, '.')!=0) {
  1113. $new_vals['significance'] = $score;
  1114. }
  1115. else {
  1116. $new_vals['significance'] = '__NULL__';
  1117. }
  1118. if (!$add_only) {
  1119. $options = array('statement_name' => 'upd_analysisfeature');
  1120. $ret = tripal_core_chado_update('analysisfeature', $af_values, $new_vals, $options);
  1121. if (!$ret) {
  1122. fwrite($log, "ERROR: could not update analysisfeature record: $analysis_id, $feature->feature_id\n");
  1123. }
  1124. else {
  1125. fwrite($log, " Updated analysisfeature record\n");
  1126. }
  1127. }
  1128. }
  1129. return $feature;
  1130. }
  1131. /**
  1132. *
  1133. *
  1134. * @ingroup gff3_loader
  1135. */
  1136. function tripal_feature_load_gff3_featureloc($feature, $organism, $landmark, $fmin,
  1137. $fmax, $strand, $phase, $is_fmin_partial, $is_fmax_partial, $residue_info, $locgroup, $log) {
  1138. $select = array(
  1139. 'organism_id' => $organism->organism_id,
  1140. 'uniquename' => $landmark,
  1141. );
  1142. $options = array('statement_name' => 'sel_feature_organism_id_uniquename');
  1143. $r = tripal_core_chado_select('feature', array('*'), $select, $options);
  1144. if (count($r)==0) {
  1145. // so we couldn't find it using the uniquename. Let's try the 'name'.
  1146. // if we return only a singe result then we can proceed. Otherwise give an
  1147. // error message
  1148. $select = array(
  1149. 'organism_id' => $organism->organism_id,
  1150. 'name' => $landmark,
  1151. );
  1152. $options = array('statement_name' => 'sel_feature_organism_id_name');
  1153. $r = tripal_core_chado_select('feature', array('*'), $select, $options);
  1154. if (count($r) == 0){
  1155. fwrite($log, "ERROR: cannot find landmark feature: '$landmark'. Cannot add the feature location record\n");
  1156. return 0;
  1157. }
  1158. elseif (count($r) > 1) {
  1159. fwrite($log, "ERROR: multiple landmarks exist with the name: '$landmark'. Cannot resolve which one to use. Cannot add the feature location record\n");
  1160. return 0;
  1161. }
  1162. }
  1163. $srcfeature = $r[0];
  1164. // TODO: create an attribute that recognizes the residue_info,locgroup,
  1165. // is_fmin_partial and is_fmax_partial, right now these are
  1166. // hardcoded to be false and 0 below.
  1167. // check to see if this featureloc already exists, but also keep track of the
  1168. // last rank value
  1169. $rank = 0;
  1170. $exists = 0;
  1171. $select = array('feature_id' => $feature->feature_id);
  1172. $options = array(
  1173. 'statement_name' => 'sel_featureloc_feature_id',
  1174. 'order_by' => array(
  1175. 'rank' => 'ASC'
  1176. ),
  1177. );
  1178. $locrecs = tripal_core_chado_select('featureloc', array('*'), $select, $options);
  1179. foreach ($locrecs as $featureloc) {
  1180. $select = array('feature_id' => $featureloc->srcfeature_id);
  1181. $options = array('statement_name' => 'sel_feature_feature_id');
  1182. $locsfeature = tripal_core_chado_select('feature', array('*'), $select, $options);
  1183. // check to make sure we don't already have this featureloc record
  1184. // if we do we don't want to readd it
  1185. // print "Src Name:'" . $locsfeature[0]->name . "' == '$landmark'\n";
  1186. // print "Fmin: '$featureloc->fmin' == '$fmin'\n";
  1187. // print "Fmax: '$featureloc->fmax' == '$fmax'\n";
  1188. // print "Strand: '$featureloc->strand' == '$strand'\n";
  1189. // the source feature name and at least the fmin and fmax must be the same
  1190. // for an update of the featureloc, otherwise we'll insert a new record.
  1191. if (strcmp($locsfeature[0]->name, $landmark)==0 and
  1192. ($featureloc->fmin == $fmin or $featureloc->fmax == $fmax)) {
  1193. $match = array('featureloc_id' => $featureloc->featureloc_id);
  1194. $values = array();
  1195. $exists = 1;
  1196. if ($featureloc->fmin != $fmin) {
  1197. $values['fmin'] = $fmin;
  1198. }
  1199. if ($featureloc->fmax != $fmax) {
  1200. $values['fmax'] = $fmax;
  1201. }
  1202. if ($featureloc->strand != $strand) {
  1203. $values['strand'] = $strand;
  1204. }
  1205. if (count($values) > 0) {
  1206. $options = array('statement_name' => 'upd_featureloc_all');
  1207. tripal_core_chado_update('featureloc', $match, $values, $options);
  1208. fwrite($log, " Updated featureloc\n");
  1209. }
  1210. else {
  1211. fwrite($log, " No change to featureloc\n");
  1212. }
  1213. }
  1214. $rank = $featureloc->rank + 1;
  1215. }
  1216. if (!$exists) {
  1217. // this feature location is new so add it
  1218. if (!$phase) {
  1219. $phase = 'NULL';
  1220. }
  1221. if (strcmp($is_fmin_partial, 'f')==0 or !$is_fmin_partial) {
  1222. $is_fmin_partial = 'false';
  1223. }
  1224. elseif (strcmp($is_fmin_partial, 't')==0 or $is_fmin_partial = 1) {
  1225. $is_fmin_partial = 'true';
  1226. }
  1227. if (strcmp($is_fmax_partial, 'f')==0 or !$is_fmax_partial) {
  1228. $is_fmax_partial = 'false';
  1229. }
  1230. elseif (strcmp($is_fmax_partial, 't')==0 or $is_fmax_partial = 1) {
  1231. $is_fmax_partial = 'true';
  1232. }
  1233. fwrite($log, " Adding featureloc $srcfeature->uniquename fmin: $fmin (is_partial: $is_fmin_partial), fmax: $fmax (is_partial: $is_fmin_partial), strand: $strand, phase: $phase, rank: $rank\n");
  1234. $values = array(
  1235. 'feature_id' => $feature->feature_id,
  1236. 'srcfeature_id' => $srcfeature->feature_id,
  1237. 'fmin' => $fmin,
  1238. 'is_fmin_partial' => $is_fmin_partial,
  1239. 'fmax' => $fmax,
  1240. 'is_fmax_partial' => $is_fmax_partial,
  1241. 'strand' => $strand,
  1242. 'phase' => $phase,
  1243. 'residue_info' => $residue_info,
  1244. 'locgroup' => $locgroup,
  1245. 'rank' => $rank
  1246. );
  1247. $options = array('statement_name' => 'ins_featureloc_all');
  1248. $success = tripal_core_chado_insert('featureloc', $values, $options);
  1249. if (!$success) {
  1250. fwrite($log, "ERROR: failed to insert featureloc\n");
  1251. exit;
  1252. return 0;
  1253. }
  1254. }
  1255. return 1;
  1256. }
  1257. /**
  1258. *
  1259. *
  1260. * @ingroup gff3_loader
  1261. */
  1262. function tripal_feature_load_gff3_property($feature, $property, $value, $log) {
  1263. // first make sure the cvterm exists. if not, then add it
  1264. $select = array(
  1265. 'name' => $property,
  1266. 'cv_id' => array(
  1267. 'name' => 'feature_property',
  1268. ),
  1269. );
  1270. $options = array('statement_name' => 'sel_cvterm_name_cvid');
  1271. $result = tripal_core_chado_select('cvterm', array('*'), $select, $options);
  1272. $cvterm = $result[0];
  1273. if (!$cvterm) {
  1274. $term = array(
  1275. 'id' => "null:$property",
  1276. 'name' => $property,
  1277. 'namespace' => 'feature_property',
  1278. 'is_obsolete' => 0,
  1279. );
  1280. fwrite($log, " Adding cvterm, $property\n");
  1281. $cvterm = (object) tripal_cv_add_cvterm($term, 'feature_property', 0, 0);
  1282. }
  1283. if (!$cvterm) {
  1284. fwrite($log, "ERROR: cannot add cvterm, $property\n");
  1285. exit;
  1286. }
  1287. // check to see if the property already exists for this feature
  1288. // if it does but the value is unique then increment the rank and add it.
  1289. // if the value is not unique then don't add it.
  1290. $add = 1;
  1291. $rank = 0;
  1292. $select = array(
  1293. 'feature_id' => $feature->feature_id,
  1294. 'type_id' => $cvterm->cvterm_id,
  1295. );
  1296. $options = array(
  1297. 'statement_name' => 'sel_featureprop_featureid_typeid',
  1298. 'order_by' => array(
  1299. 'rank' => 'ASC',
  1300. ),
  1301. );
  1302. $results = tripal_core_chado_select('featureprop', array('*'), $select, $options);
  1303. foreach ($results as $prop){
  1304. if (strcmp($prop->value, $value)==0) {
  1305. $add = NULL; // don't add it, it already exists
  1306. fwrite($log, " Property already exists, skipping\n");
  1307. }
  1308. $rank = $prop->rank + 1;
  1309. }
  1310. // add the property if we pass the check above
  1311. if ($add) {
  1312. fwrite($log, " Setting feature property. $property: $value\n");
  1313. $values = array(
  1314. 'feature_id' => $feature->feature_id,
  1315. 'type_id' => $cvterm->cvterm_id,
  1316. 'value' => $value,
  1317. 'rank' => $rank,
  1318. );
  1319. $options = array('statement_name' => 'ins_featureprop_all');
  1320. $result = tripal_core_chado_insert('featureprop', $values, $options);
  1321. if(!$result){
  1322. fwrite($log, "ERROR: cannot add featureprop, $property\n");
  1323. }
  1324. }
  1325. }