gff_loader.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. <?php
  2. /**
  3. * @defgroup gff3_loader GFF3 Feature Loader
  4. * @{
  5. * Provides gff3 loading functionality. Creates features based on their specification in a GFF3 file.
  6. * @}
  7. */
  8. // TODO: The rank column on the feature_relationship table needs to be used to
  9. // make sure the ordering of CDS (exons) is correct.
  10. // The entries in the GFF file are not in order so the order of the relationships
  11. // is not put in correctly.
  12. /**
  13. *
  14. *
  15. * @ingroup tripal_feature
  16. * @ingroup gff3_loader
  17. */
  18. function tripal_core_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.obo). 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. In some cases some of the fields may not apply. For
  97. data downloaded from Genbank, the 'program' field does not apply but is
  98. required. In this case, simply provide the name of the data source
  99. (e.g. NCBI Genbank) for the program and use the date accessed for the
  100. program version. For the description, be sure to include the search
  101. criteria used for selecting data from GenBank."),
  102. );
  103. // get the list of organisms
  104. $sql = "SELECT * FROM {analysis} ORDER BY name";
  105. $previous_db = tripal_db_set_active('chado'); // use chado database
  106. $org_rset = db_query($sql);
  107. tripal_db_set_active($previous_db); // now use drupal database
  108. $analyses = array();
  109. $analyses[''] = '';
  110. while($analysis = db_fetch_object($org_rset)){
  111. $analyses[$analysis->analysis_id] = "$analysis->name ($analysis->program $analysis->programversion, $analysis->sourcename)";
  112. }
  113. $form['analysis']['analysis_id'] = array (
  114. '#title' => t('Analysis'),
  115. '#type' => t('select'),
  116. '#description' => t("Choose the analysis to which these features are associated "),
  117. '#required' => TRUE,
  118. '#options' => $analyses,
  119. );
  120. $form['button'] = array(
  121. '#type' => 'submit',
  122. '#value' => t('Import GFF3 file'),
  123. '#weight' => 10,
  124. );
  125. return $form;
  126. }
  127. /**
  128. *
  129. *
  130. * @ingroup tripal_feature
  131. * @ingroup gff3_loader
  132. */
  133. function tripal_core_gff3_load_form_validate ($form, &$form_state){
  134. $gff_file = $form_state['values']['gff_file'];
  135. $organism_id = $form_state['values']['organism_id'];
  136. $add_only = $form_state['values']['add_only'];
  137. $update = $form_state['values']['update'];
  138. $refresh = $form_state['values']['refresh'];
  139. $remove = $form_state['values']['remove'];
  140. // check to see if the file is located local to Drupal
  141. $dfile = $_SERVER['DOCUMENT_ROOT'] . base_path() . $gff_file;
  142. if(!file_exists($dfile)){
  143. // if not local to Drupal, the file must be someplace else, just use
  144. // the full path provided
  145. $dfile = $gff_file;
  146. }
  147. if(!file_exists($dfile)){
  148. 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."));
  149. }
  150. if (($add_only and ($update or $refresh or $remove)) or
  151. ($update and ($add_only or $refresh or $remove)) or
  152. ($refresh and ($update or $add_only or $remove)) or
  153. ($remove and ($update or $refresh or $add_only))){
  154. form_set_error('add_only',t("Please select only one checkbox from the import options section"));
  155. }
  156. }
  157. /**
  158. *
  159. * @ingroup tripal_feature
  160. * @ingroup gff3_loader
  161. */
  162. function tripal_core_gff3_load_form_submit ($form, &$form_state){
  163. global $user;
  164. $gff_file = $form_state['values']['gff_file'];
  165. $organism_id = $form_state['values']['organism_id'];
  166. $add_only = $form_state['values']['add_only'];
  167. $update = $form_state['values']['update'];
  168. $refresh = $form_state['values']['refresh'];
  169. $remove = $form_state['values']['remove'];
  170. $analysis = $form_state['values']['analysis_id'];
  171. $args = array($gff_file,$organism_id,$analysis_id,$add_only,$update,$refresh,$remove);
  172. $type = '';
  173. if($add_only){
  174. $type = 'import only new features';
  175. }
  176. if($update){
  177. $type = 'import all and update';
  178. }
  179. if($refresh){
  180. $type = 'import all and replace';
  181. }
  182. if($remove){
  183. $type = 'delete features';
  184. }
  185. tripal_add_job("$type GFF3 file $gff_file",'tripal_core',
  186. 'tripal_core_load_gff3',$args,$user->uid);
  187. return '';
  188. }
  189. /**
  190. *
  191. *
  192. * @ingroup tripal_feature
  193. * @ingroup gff3_loader
  194. */
  195. function tripal_core_load_gff3($gff_file, $organism_id,$analysis_id,$add_only =0,
  196. $update = 0, $refresh = 0, $remove = 0, $job = NULL)
  197. {
  198. // this array is used to cache all of the features in the GFF file and
  199. // used to lookup parent and target relationships
  200. $gff_features = array();
  201. // check to see if the file is located local to Drupal
  202. $dfile = $_SERVER['DOCUMENT_ROOT'] . base_path() . $gff_file;
  203. if(!file_exists($dfile)){
  204. // if not local to Drupal, the file must be someplace else, just use
  205. // the full path provided
  206. $dfile = $gff_file;
  207. }
  208. if(!file_exists($dfile)){
  209. print "ERROR: cannot find the file: $dfile\n";
  210. return 0;
  211. }
  212. $previous_db = tripal_db_set_active('chado');
  213. print "Opening $gff_file\n";
  214. $lines = file($dfile,FILE_SKIP_EMPTY_LINES);
  215. $i = 0;
  216. // get the controlled vocaubulary that we'll be using. The
  217. // default is the 'sequence' ontology
  218. $sql = "SELECT * FROM cv WHERE name = '%s'";
  219. $cv = db_fetch_object(db_query($sql,'sequence'));
  220. if(!$cv){
  221. print "ERROR: cannot find the 'sequence' ontology\n";
  222. return '';
  223. }
  224. // get the organism for which this GFF3 file belongs
  225. $sql = "SELECT * FROM organism WHERE organism_id = %d";
  226. $organism = db_fetch_object(db_query($sql,$organism_id));
  227. $num_lines = sizeof($lines);
  228. $interval = intval($num_lines * 0.01);
  229. foreach ($lines as $line_num => $line) {
  230. $i++; // update the line count
  231. // update the job status every 1% features
  232. if($job and $i % $interval == 0){
  233. tripal_job_set_progress($job,intval(($i/$num_lines)*100));
  234. }
  235. // skip comments
  236. if(preg_match('/^#/',$line)){
  237. continue;
  238. }
  239. // skip empty lines
  240. if(preg_match('/^\s*$/',$line)){
  241. continue;
  242. }
  243. // TODO: remove URL encoding
  244. $cols = explode("\t",$line);
  245. if(sizeof($cols) != 9){
  246. print "ERROR: improper number of columns on line $i\n";
  247. return '';
  248. }
  249. // get the column values
  250. $landmark = $cols[0];
  251. $source = $cols[1];
  252. $type = $cols[2];
  253. $start = $cols[3];
  254. $end = $cols[4];
  255. $score = $cols[5];
  256. $strand = $cols[6];
  257. $phase = $cols[7];
  258. $attrs = explode(";",$cols[8]); // split by a semi-colon
  259. // ready the start and stop for chado
  260. $fmin = $start;
  261. $fmax = $end;
  262. if($end < $start){
  263. $fmin = $end;
  264. $fmax = $start;
  265. }
  266. // format the strand for chado
  267. if(strcmp($strand,'.')==0){
  268. $strand = 0;
  269. }
  270. elseif(strcmp($strand,'+')==0){
  271. $strand = 1;
  272. }
  273. elseif(strcmp($strand,'-')==0){
  274. $strand = -1;
  275. }
  276. if(strcmp($phase,'.')==0){
  277. $phase = '';
  278. }
  279. // get the type record
  280. $cvtermsql = "SELECT CVT.cvterm_id, CVT.cv_id, CVT.name, CVT.definition,
  281. CVT.dbxref_id, CVT.is_obsolete, CVT.is_relationshiptype
  282. FROM {cvterm} CVT
  283. INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  284. LEFT JOIN {cvtermsynonym} CVTS on CVTS.cvterm_id = CVT.cvterm_id
  285. WHERE CV.cv_id = %d and (CVT.name = '%s' or CVTS.synonym = '%s')";
  286. $cvterm = db_fetch_object(db_query($cvtermsql,$cv->cv_id,$type,$type));
  287. if(!$cvterm){
  288. print "ERROR: cannot find ontology term '$type' on line $i.\n";
  289. return '';
  290. }
  291. // break apart each of the attributes
  292. $tags = array();
  293. $attr_name = '';
  294. $attr_uniquename = '';
  295. $attr_residue_info = '';
  296. $attr_locgroup = 0;
  297. $attr_fmin_partial = 'f';
  298. $attr_fmax_partial = 'f';
  299. $attr_is_obsolete = 'f';
  300. $attr_is_analysis = 'f';
  301. $attr_others = '';
  302. $residues = '';
  303. $generate_name = 0;
  304. foreach($attrs as $attr){
  305. $attr = rtrim($attr);
  306. $attr = ltrim($attr);
  307. if(strcmp($attr,'')==0){
  308. continue;
  309. }
  310. if(!preg_match('/^[^\=]+\=[^\=]+$/',$attr)){
  311. print "ERROR: attribute is not correctly formatted on line $i: $attr\n";
  312. return '';
  313. }
  314. // break apart each tag
  315. $tag = explode("=",$attr); // split by equals sign
  316. // multiple instances of an attribute are separated by commas
  317. $tags[$tag[0]] = explode(",",$tag[1]); // split by comma
  318. if(strcmp($tag[0],'ID')==0){
  319. $attr_uniquename = $tag[1];
  320. }
  321. elseif(strcmp($tag[0],'Name')==0){
  322. $attr_name = $tag[1];
  323. }
  324. // get the list of other attributes other than those reserved ones.
  325. elseif(strcmp($tag[0],'Alias')!=0 and strcmp($tag[0],'Parent')!=0 and
  326. strcmp($tag[0],'Target')!=0 and strcmp($tag[0],'Gap')!=0 and
  327. strcmp($tag[0],'Derives_from')!=0 and strcmp($tag[0],'Note')!=0 and
  328. strcmp($tag[0],'Dbxref')!=0 and strcmp($tag[0],'Ontology_term')!=0 and
  329. strcmp($tag[0],'Is_circular')!=0){
  330. $attr_others[$tag[0]] = $tag[1];
  331. }
  332. }
  333. // if neither name nor uniquename are provided then generate one
  334. if(!$attr_uniquename and !$attr_name){
  335. $generate_name = 1;
  336. }
  337. // if a name is not specified then use the unique name
  338. if(strcmp($attr_name,'')==0){
  339. $attr_name = $attr_uniquename;
  340. }
  341. // if an ID attribute is not specified then use the attribute name and
  342. // hope for the best
  343. if(!$attr_uniquename){
  344. $attr_uniquename = $attr_name;
  345. }
  346. // make sure the landmark sequence exists in the database. We don't
  347. // know the type of the landmark so we'll hope that it's unique across
  348. // all types. If not we'll error out. This test is only necessary if
  349. // if the landmark and the uniquename are different. If they are the same
  350. // then this is the information for the landmark
  351. if(strcmp($landmark,$attr_uniquename)!=0){
  352. $feature_sql = "SELECT count(*) as num_landmarks
  353. FROM {feature}
  354. WHERE organism_id = %d and uniquename = '%s'";
  355. $count = db_fetch_object(db_query($feature_sql,$organism_id,$landmark));
  356. if(!$count or $count->num_landmarks == 0){
  357. print "ERROR: the landmark '$landmark' cannot be found for this organism. ".
  358. "Please add the landmark and then retry the import of this GFF3 ".
  359. "file.\n";
  360. return '';
  361. }
  362. if($count->num_landmarks > 1){
  363. print "ERROR: the landmark '$landmark' is not unique for this organism. ".
  364. "The features cannot be associated.\n";
  365. return '';
  366. }
  367. }
  368. // if the option is to remove or refresh then we want to remove
  369. // the feature from the database.
  370. if(!$generate_name and ($remove or $refresh)){
  371. print "Removing feature '$attr_uniquename'\n";
  372. $sql = "DELETE FROM {feature}
  373. WHERE organism_id = %d and uniquename = '%s' and type_id = %d";
  374. $result = db_query($sql,$organism->organism_id,$attr_uniquename,$cvterm->cvterm_id);
  375. if(!$result){
  376. print "ERROR: cannot delete feature $attr_uniquename\n";
  377. }
  378. $feature = 0;
  379. }
  380. // add or update the feature and all properties
  381. if($update or $refresh or $add_only){
  382. // add/update the feature
  383. $feature = tripal_core_load_gff3_feature($organism,$analysis_id,$cvterm,
  384. $attr_uniquename,$attr_name,$residues,$attr_is_analysis,
  385. $attr_is_obsolete, $add_only,$generate_name);
  386. // store all of the features so far use later by parent and target
  387. // relationships
  388. $gff_features[$feature->uniquename]['type'] = $type;
  389. if($feature){
  390. if($generate_name){
  391. $attr_uniquename = $feature->uniquename;
  392. }
  393. // add/update the featureloc if the landmark and the ID are not the same
  394. // if they are the same then this entry in the GFF is probably a landmark identifier
  395. if(strcmp($landmark,$attr_uniquename)!=0){
  396. tripal_core_load_gff3_featureloc($feature,$organism,
  397. $landmark,$fmin,$fmax,$strand,$phase,$attr_fmin_partial,
  398. $attr_fmax_partial,$attr_residue_info,$attr_locgroup);
  399. }
  400. // add any aliases for this feature
  401. if(array_key_exists('Alias',$tags)){
  402. tripal_core_load_gff3_alias($feature,$tags['Alias']);
  403. }
  404. // add any aliases for this feature
  405. if(array_key_exists('Dbxref',$tags)){
  406. tripal_core_load_gff3_dbxref($feature,$tags['Dbxref']);
  407. }
  408. // add parent relationships
  409. if(array_key_exists('Parent',$tags)){
  410. tripal_core_load_gff3_parents($feature,$cvterm,$tags['Parent'],$gff_features,$organism_id);
  411. }
  412. }
  413. }
  414. }
  415. tripal_db_set_active($previous_db);
  416. return '';
  417. }
  418. /**
  419. *
  420. *
  421. * @ingroup tripal_feature
  422. * @ingroup gff3_loader
  423. */
  424. function tripal_core_load_gff3_parents($feature,$cvterm,$parents,$gff_features,$organism_id){
  425. $uname = $feature->uniquename;
  426. $type = $cvterm->name;
  427. $rel_type = 'part_of';
  428. // create these SQL statements that will be used repeatedly below.
  429. $cvtermsql = "SELECT CVT.cvterm_id
  430. FROM {cvterm} CVT
  431. INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  432. LEFT JOIN {cvtermsynonym} CVTS on CVTS.cvterm_id = CVT.cvterm_id
  433. WHERE cv.name = '%s' and (CVT.name = '%s' or CVTS.synonym = '%s')";
  434. $feature_sql = "SELECT * FROM {feature}
  435. WHERE organism_id = %d and uniquename = '%s' and type_id = %d";
  436. // iterate through the parents in the list
  437. foreach($parents as $parent){
  438. $parent_type = $gff_features[$parent]['type'];
  439. $parentcvterm = db_fetch_object(db_query($cvtermsql,'sequence',$parent_type,$parent_type));
  440. $relcvterm = db_fetch_object(db_query($cvtermsql,'relationship',$rel_type,$rel_type));
  441. $parent_feature = db_fetch_object(db_query($feature_sql,$organism_id,$parent,$parentcvterm->cvterm_id));
  442. if($parent_feature){
  443. // check to see if the relationship already exists
  444. $sql = "SELECT * FROM {feature_relationship} WHERE subject_id = %d and object_id = %d and type_id = %d";
  445. $rel = db_fetch_object(db_query($sql,$feature->feature_id,$parent_feature->feature_id,$relcvterm->cvterm_id));
  446. if($rel){
  447. print " Relationship already exists, skipping '$uname' ($type) $rel_type '$parent' ($parent_type)\n";
  448. } else {
  449. $sql = "INSERT INTO {feature_relationship} (subject_id,object_id,type_id)
  450. VALUES (%d,%d,%d)";
  451. $result = db_query($sql,$feature->feature_id,$parent_feature->feature_id,$relcvterm->cvterm_id);
  452. if(!$result){
  453. print "WARNING: failed to insert feature relationship '$uname' ($type) $rel_type '$parent' ($parent_type)\n";
  454. } else {
  455. print " Inserted relationship relationship: '$uname' ($type) $rel_type '$parent' ($parent_type)\n";
  456. }
  457. }
  458. }
  459. else {
  460. print "WARNING: cannot establish relationship '$uname' ($type) $rel_type '$parent' ($parent_type): Cannot find the parent\n";
  461. }
  462. }
  463. }
  464. /**
  465. *
  466. *
  467. * @ingroup tripal_feature
  468. * @ingroup gff3_loader
  469. */
  470. function tripal_core_load_gff3_dbxref($feature,$dbxrefs){
  471. // iterate through each of the dbxrefs
  472. foreach($dbxrefs as $dbxref){
  473. // get the database name from the reference. If it doesn't exist then create one.
  474. $ref = explode(":",$dbxref);
  475. $dbname = $ref[0];
  476. $accession = $ref[1];
  477. // first look for the database name if it doesn't exist then create one.
  478. // first check for the fully qualified URI (e.g. DB:<dbname>. If that
  479. // can't be found then look for the name as is. If it still can't be found
  480. // the create the database
  481. $db = tripal_core_chado_select('db',array('db_id'),array('name' => "DB:$dbname"));
  482. if(sizeof($db) == 0){
  483. $db = tripal_core_chado_select('db',array('db_id'),array('name' => "$dbname"));
  484. }
  485. if(sizeof($db) == 0){
  486. $ret = tripal_core_chado_insert('db',array('name' => $dbname,
  487. 'description' => 'Added automatically by the GFF loader'));
  488. if($ret){
  489. print "Added new database: $dbname\n";
  490. $db = tripal_core_chado_select('db',array('db_id'),array('name' => "$dbname"));
  491. } else {
  492. print "ERROR: cannot find or add the database $dbname\n";
  493. return 0;
  494. }
  495. }
  496. $db = $db[0];
  497. // now check to see if the accession exists
  498. $dbxref = tripal_core_chado_select('dbxref',array('dbxref_id'),array(
  499. 'accession' => $accession,'db_id' => $db->db_id));
  500. // if the accession doesn't exist then we want to add it
  501. if(sizeof($dbxref) == 0){
  502. $ret = tripal_core_chado_insert('dbxref',array('db_id' => $db->db_id,
  503. 'accession' => $accession,'version' => ''));
  504. $dbxref = tripal_core_chado_select('dbxref',array('dbxref_id'),array(
  505. 'accession' => $accession,'db_id' => $db->db_id));
  506. }
  507. $dbxref = $dbxref[0];
  508. // check to see if tihs feature dbxref already exists
  509. $fdbx = tripal_core_chado_select('feature_dbxref',array('feature_dbxref_id'),
  510. array('dbxref_id' => $dbxref->dbxref_id,'feature_id' => $feature->feature_id));
  511. // now associate this feature with the database reference if it doesn't
  512. // already exist
  513. if(sizeof($fdbx)==0){
  514. $ret = tripal_core_chado_insert('feature_dbxref',array(
  515. 'feature_id' => $feature->feature_id,
  516. 'dbxref_id' => $dbxref->dbxref_id));
  517. if($ret){
  518. print "Adding dbxref $dbname:$accession\n";
  519. } else {
  520. print "ERROR: failed to insert dbxref: $dbname:$accession\n";
  521. return 0;
  522. }
  523. } else {
  524. print "Dbxref already exists, skipping $dbname:$accession\n";
  525. }
  526. }
  527. return 1;
  528. }
  529. /**
  530. *
  531. *
  532. * @ingroup tripal_feature
  533. * @ingroup gff3_loader
  534. */
  535. function tripal_core_load_gff3_alias($feature,$aliases){
  536. // make sure we have a 'synonym_type' vocabulary
  537. $sql = "SELECT * FROM {cv} WHERE name='synonym_type'";
  538. $syncv = db_fetch_object(db_query($sql));
  539. if(!$syncv){
  540. $sql = "INSERT INTO {cv} (name,definition) VALUES ('synonym_type','')";
  541. if(!db_query($sql)){
  542. print("ERROR: Failed to add the synonyms type vocabulary");
  543. return 0;
  544. }
  545. $syncv = db_fetch_object(db_query($sql));
  546. }
  547. // get the 'exact' cvterm, which is the type of synonym we're adding
  548. $cvtsql = "
  549. SELECT * FROM {cvterm} CVT
  550. INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id
  551. WHERE CVT.name = '%s' and CV.name = '%s'
  552. ";
  553. $syntype = db_fetch_object(db_query($cvtsql,'exact','synonym_type'));
  554. if(!$syntype){
  555. $term = array(
  556. 'name' => array('exact'),
  557. 'id' => array("internal:exact"),
  558. 'definition' => array(''),
  559. 'is_obsolete' => array(0),
  560. );
  561. $syntype = tripal_cv_obo_add_cv_term($term,$syncv,0,1);
  562. if(!$syntype){
  563. tripal_cv_obo_quiterror("Cannot add synonym type: internal:$type");
  564. }
  565. }
  566. // iterate through all of the aliases and add each one
  567. foreach($aliases as $alias){
  568. print " Adding Alias $alias\n";
  569. // check to see if the alias already exists in the synonym table
  570. // if not, then add it
  571. $synsql = "SELECT * FROM {synonym}
  572. WHERE name = '%s' and type_id = %d";
  573. $synonym = db_fetch_object(db_query($synsql,$alias,$syntype->cvterm_id));
  574. if(!$synonym){
  575. $sql = "INSERT INTO {synonym}
  576. (name,type_id,synonym_sgml)
  577. VALUES ('%s',%d,'%s')";
  578. $result = db_query($sql,$alias,$syntype->cvterm_id,'');
  579. if(!$result){
  580. print "ERROR: cannot add alias $alias to synonym table\n";
  581. }
  582. }
  583. $synonym = db_fetch_object(db_query($synsql,$alias,$syntype->cvterm_id));
  584. // check to see if we have a NULL publication in the pub table. If not,
  585. // then add one.
  586. $pubsql = "SELECT * FROM {pub} WHERE uniquename = 'null'";
  587. $pub = db_fetch_object(db_query($pubsql));
  588. if(!$pub){
  589. $sql = "INSERT INTO pub (uniquename,type_id) VALUES ('%s',
  590. (SELECT cvterm_id
  591. FROM cvterm CVT
  592. INNER JOIN dbxref DBX on DBX.dbxref_id = CVT.dbxref_id
  593. INNER JOIN db DB on DB.db_id = DBX.db_id
  594. WHERE CVT.name = 'null' and DB.name = 'null')";
  595. $result = db_query($sql,'null');
  596. if(!$result){
  597. print "ERROR: cannot add null publication needed for setup of alias\n";
  598. return 0;
  599. }
  600. }
  601. $pub = db_fetch_object(db_query($pubsql));
  602. // check to see if the synonym exists in the feature_synonym table
  603. // if not, then add it.
  604. $synsql = "SELECT * FROM {feature_synonym}
  605. WHERE synonym_id = %d and feature_id = %d and pub_id = %d";
  606. $fsyn = db_fetch_object(db_query($synsql,$synonym->synonym_id,$feature->feature_id,$pub->pub_id));
  607. if(!$fsyn){
  608. $sql = "INSERT INTO {feature_synonym}
  609. (synonym_id,feature_id,pub_id)
  610. VALUES (%d,%d,%d)";
  611. $result = db_query($sql,$synonym->synonym_id,$feature->feature_id,$pub->pub_id);
  612. if(!$result){
  613. print "ERROR: cannot add alias $alias to feature synonym table\n";
  614. return 0;
  615. }
  616. } else {
  617. print "Synonym $alias already exists. Skipping\n";
  618. }
  619. }
  620. return 1;
  621. }
  622. /**
  623. *
  624. *
  625. * @ingroup tripal_feature
  626. * @ingroup gff3_loader
  627. */
  628. function tripal_core_load_gff3_feature($organism,$analysis_id,$cvterm,$uniquename,$name,
  629. $residues,$is_analysis='f',$is_obsolete='f',$add_only,$generate_name) {
  630. if($generate_name){
  631. $uniquename = 'tripal_temp_loading_name' . rand(1,99999999);
  632. }
  633. // check to see if the feature already exists
  634. $feature_sql = "SELECT * FROM {feature}
  635. WHERE organism_id = %d and uniquename = '%s' and type_id = %d";
  636. $feature = db_fetch_object(db_query($feature_sql,$organism->organism_id,$uniquename,$cvterm->cvterm_id));
  637. if(strcmp($is_obsolete,'f')==0){
  638. $is_obsolete = 'false';
  639. }
  640. if(strcmp($is_analysis,'f')==0){
  641. $is_analysis = 'false';
  642. }
  643. // insert the feature if it does not exist otherwise perform an update
  644. if(!$feature){
  645. print "Adding feature '$uniquename' ($cvterm->name)\n";
  646. $isql = "INSERT INTO {feature} (organism_id, name, uniquename, residues, seqlen,
  647. md5checksum, type_id,is_analysis,is_obsolete)
  648. VALUES(%d,'%s','%s','%s',%d, '%s', %d, %s, %s)";
  649. $result = db_query($isql,$organism->organism_id,$name,$uniquename,$residues,strlen($residues),
  650. md5($residues),$cvterm->cvterm_id,$is_analysis,$is_obsolete);
  651. if(!$result){
  652. print "ERROR: failed to insert feature '$uniquename' ($cvterm->name)\n";
  653. return 0;
  654. }
  655. }
  656. elseif(!$add_only) {
  657. print "Updating feature '$uniquename' ($cvterm->name)\n";
  658. $usql = "UPDATE {feature}
  659. SET name = '%s', residues = '%s', seqlen = '%s', md5checksum = '%s',
  660. is_analysis = %s, is_obsolete = %s
  661. WHERE organism_id = %d and uniquename = '%s' and type_id = %d";
  662. $result = db_query($usql,$name,$residues,strlen($residues),md5($residues),$is_analysis,$is_obsolete,
  663. $organism_id,$uniquename,$cvterm->cvterm_id);
  664. if(!$result){
  665. print "ERROR: failed to update feature '$uniquename' ($cvterm->name)\n";
  666. return 0;
  667. }
  668. }
  669. else {
  670. // the feature exists and we don't want to update it so return
  671. // a value of 0. This will stop all downstream property additions
  672. print "Skipping existing feature: '$uniquename' ($cvterm->name).\n";
  673. return 0;
  674. }
  675. // get the newly added feature
  676. $feature = db_fetch_object(db_query($feature_sql,$organism->organism_id,$uniquename,$cvterm->cvterm_id));
  677. // add the analysisfeature entry to the analysisfeature table if it doesn't already exist
  678. $af_values = array('analysis_id' => $analysis_id, 'feature_id' => $feature->feature_id);
  679. if(tripal_core_chado_select('analysisfeature','analysisfeature_id',$af,true)){
  680. if(!tripal_core_chado_insert('analysisfeature',$af_values)){
  681. print "ERROR: could not add analysisfeature record: $analysis_id, $feature->feature_id\n";
  682. return 0;
  683. }
  684. }
  685. // now that we've added the feature let's reset it's uniquename to be
  686. // the feature id
  687. if($generate_name){
  688. $usql = "UPDATE {feature}
  689. SET name = '%s', uniquename = '%s'
  690. WHERE feature_id = %d";
  691. print " Renaming feature '$uniquename' => ";
  692. $uniquename = "$feature->feature_id";
  693. print "$uniquename\n";
  694. $result = db_query($usql,$uniquename,$uniquename,$feature_id);
  695. if(!$result){
  696. print "ERROR: failed to update unnamed feature '$uniquename' with generic name\n";
  697. return 0;
  698. }
  699. $feature->name = $uniquename;
  700. $feature->uniquename = $uniquename;
  701. }
  702. return $feature;
  703. }
  704. /**
  705. *
  706. *
  707. * @ingroup tripal_feature
  708. * @ingroup gff3_loader
  709. */
  710. function tripal_core_load_gff3_featureloc($feature,$organism,$landmark,$fmin,
  711. $fmax,$strand,$phase,$is_fmin_partial,$is_fmax_partial,$residue_info,$locgroup)
  712. {
  713. // get the source feature
  714. $sql = "SELECT * FROM {feature}
  715. WHERE organism_id = %d and uniquename = '%s'";
  716. $srcfeature = db_fetch_object(db_query($sql,$organism->organism_id,$landmark));
  717. if(!$srcfeature){
  718. print "ERROR: cannot find landmark feature $landmark. Cannot add the feature location record\n";
  719. return 0;
  720. }
  721. // TODO: create an attribute that recognizes the residue_info,locgroup, is_fmin_partial and is_fmax_partial, right now these are
  722. // hardcoded to be false and 0 below.
  723. // check to see if this featureloc already exists, but also keep track of the
  724. // last rank value
  725. $rank = -1;
  726. $exists = 0;
  727. $featureloc_sql = "SELECT FL.featureloc_id,FL.fmin,FL.fmax, FL.is_fmin_partial,
  728. FL.is_fmax_partial, FL.strand, FL.phase, FL.residue_info,
  729. FL.locgroup, F.uniquename as srcname
  730. FROM {featureloc} FL
  731. INNER JOIN {feature} F on F.feature_id = FL.srcfeature_id
  732. WHERE FL.feature_id = %d
  733. ORDER BY rank ASC";
  734. $recs = db_query($featureloc_sql,$feature->feature_id);
  735. while ($featureloc = db_fetch_object($recs)){
  736. if(strcmp($featureloc->srcname,$landmark)==0 and
  737. $featureloc->fmin == $fmin and strcmp($featureloc->is_fmin_partial,$is_fmin_partial)==0 and
  738. $featureloc->fmax == $fmax and strcmp($featureloc->is_fmax_partial,$is_fmax_partial)==0 and
  739. $featureloc->phase == $phase and $featureloc->strand == $strand and
  740. strcmp($featureloc->residue_info,$residue_info)==0 and
  741. $featureloc->locgroup == $locgroup){
  742. // this is the same featureloc, so do nothing... no need to update
  743. //TODO: need more checks here
  744. print " No change to featureloc\n";
  745. $exists = 1;
  746. }
  747. $rank = $featureloc->rank;
  748. }
  749. if(!$exists){
  750. $rank++;
  751. // this feature location is new so add it
  752. if(!$phase){
  753. $phase = 'NULL';
  754. }
  755. if(strcmp($is_fmin_partial,'f')==0){
  756. $is_fmin_partial = 'false';
  757. }
  758. elseif(strcmp($is_fmin_partial,'t')==0){
  759. $is_fmin_partial = 'true';
  760. }
  761. if(strcmp($is_fmax_partial,'f')==0){
  762. $is_fmax_partial = 'false';
  763. }
  764. elseif(strcmp($is_fmax_partial,'t')==0){
  765. $is_fmax_partial = 'true';
  766. }
  767. print " Adding featureloc $srcfeature->uniquename fmin: $fmin, fmax: $fmax, strand: $strand, phase: $phase, rank: $rank\n";
  768. $fl_isql = "INSERT INTO {featureloc}
  769. (feature_id, srcfeature_id, fmin, is_fmin_partial, fmax, is_fmax_partial,
  770. strand, phase, residue_info, locgroup, rank)
  771. VALUES (%d,%d,%d,%s,%d,%s,%d,%s,'%s',%d,%d)";
  772. $result = db_query($fl_isql,$feature->feature_id,$srcfeature->feature_id,$fmin,$is_fmin_partial,$fmax,$is_fmax_partial,
  773. $strand,$phase,$residue_info,$locgroup,$rank);
  774. if(!$result){
  775. print "ERROR: failed to insert featureloc\n";
  776. exit;
  777. return 0;
  778. }
  779. }
  780. return 1;
  781. }