fasta_loader.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. <?php
  2. /**
  3. * @defgroup fasta_loader FASTA Feature Loader
  4. * @{
  5. * Provides fasta loading functionality. Creates features based on their specification in a fasta file.
  6. * @}
  7. * @ingroup tripal_feature
  8. */
  9. /**
  10. *
  11. *
  12. * @ingroup fasta_loader
  13. */
  14. function tripal_feature_fasta_load_form (){
  15. $form['fasta_file']= array(
  16. '#type' => 'textfield',
  17. '#title' => t('FASTA File'),
  18. '#description' => t('Please enter the full system path for the FASTA file, or a path within the Drupal
  19. installation (e.g. /sites/default/files/xyz.obo). The path must be accessible to the
  20. server on which this Drupal instance is running.'),
  21. '#required' => TRUE,
  22. );
  23. // get the list of organisms
  24. $sql = "SELECT * FROM {organism} ORDER BY genus, species";
  25. $previous_db = tripal_db_set_active('chado'); // use chado database
  26. $org_rset = db_query($sql);
  27. tripal_db_set_active($previous_db); // now use drupal database
  28. $organisms = array();
  29. $organisms[''] = '';
  30. while($organism = db_fetch_object($org_rset)){
  31. $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  32. }
  33. $form['organism_id'] = array (
  34. '#title' => t('Organism'),
  35. '#type' => t('select'),
  36. '#description' => t("Choose the organism to which these sequences are associated "),
  37. '#required' => TRUE,
  38. '#options' => $organisms,
  39. );
  40. $form['seqtype']= array(
  41. '#type' => 'textfield',
  42. '#title' => t('Sequence Type'),
  43. '#required' => TRUE,
  44. '#description' => t('Please enter the Sequence Ontology term that describes the sequences in the FASTA file.'),
  45. );
  46. // get the list of organisms
  47. $sql = "SELECT L.library_id, L.name, CVT.name as type
  48. FROM {library} L
  49. INNER JOIN {cvterm} CVT ON L.type_id = CVT.cvterm_id
  50. ORDER BY name";
  51. $previous_db = tripal_db_set_active('chado'); // use chado database
  52. $lib_rset = db_query($sql);
  53. tripal_db_set_active($previous_db); // now use drupal database
  54. $libraries = array();
  55. $libraries[''] = '';
  56. while($library = db_fetch_object($lib_rset)){
  57. $libraries[$library->library_id] = "$library->name ($library->type)";
  58. }
  59. // $form['library_id'] = array (
  60. // '#title' => t('Library'),
  61. // '#type' => t('select'),
  62. // '#description' => t("Choose the library to which these sequences are associated "),
  63. // '#required' => FALSE,
  64. // '#options' => $libraries,
  65. // '#weight' => 5,
  66. // );
  67. $form['method']= array(
  68. '#type' => 'radios',
  69. '#title' => 'Method',
  70. '#required' => TRUE,
  71. '#options' => array(
  72. t('Insert only'),
  73. t('Update only'),
  74. t('Insert and update'),
  75. ),
  76. '#description' => t('Select how features in the FASTA file are handled.
  77. Select "Insert only" to insert the new features. If a feature already
  78. exists with the same name or unique name and type then it is skipped.
  79. Select "Update only" to only update featues that already exist in the
  80. database. Select "Insert and Update" to insert features that do
  81. not exist and upate those that do.'),
  82. '#default_value' => 2,
  83. );
  84. $form['match_type']= array(
  85. '#type' => 'radios',
  86. '#title' => 'Name Match Type',
  87. '#required' => TRUE,
  88. '#options' => array(
  89. t('Name'),
  90. t('Unique name'),
  91. ),
  92. '#description' => t('Feature data is stored in Chado with both a human-readable
  93. name and a unique name. If the features in your FASTA file are identified using
  94. a human-readable name then select the "Name" button. If your features are
  95. identified using the unique name then select the "Unique name" button. If you
  96. loaded your features first using the GFF loader then the unique name of each
  97. features were indicated by the "ID=" attribute and the name by the "Name=" attribute.
  98. By default, the FASTA loader will use the first word (character string
  99. before the first space) as the name for your feature. If
  100. this does not uniquely identify your feature consider specifying a regular expression in the advanced section below.
  101. Additionally, you may import both a name and a unique name for each sequence using the advanced options.
  102. When updating a sequence, the value selected here will be used to identify the sequence in the
  103. database in combination with any regular expression provided below.'),
  104. '#default_value' => 1,
  105. );
  106. $form['analysis'] = array(
  107. '#type' => 'fieldset',
  108. '#title' => t('Analysis Used to Derive Features'),
  109. '#collapsed' => TRUE
  110. );
  111. $form['analysis']['desc'] = array(
  112. '#type' => 'markup',
  113. '#value' => t("Why specify an analysis for a data load? All data comes
  114. from some place, even if downloaded from Genbank. By specifying
  115. analysis details for all data uploads, it allows an end user to reproduce the
  116. data set, but at least indicates the source of the data."),
  117. );
  118. // get the list of organisms
  119. $sql = "SELECT * FROM {analysis} ORDER BY name";
  120. $previous_db = tripal_db_set_active('chado'); // use chado database
  121. $org_rset = db_query($sql);
  122. tripal_db_set_active($previous_db); // now use drupal database
  123. $analyses = array();
  124. $analyses[''] = '';
  125. while($analysis = db_fetch_object($org_rset)){
  126. $analyses[$analysis->analysis_id] = "$analysis->name ($analysis->program $analysis->programversion, $analysis->sourcename)";
  127. }
  128. $form['analysis']['analysis_id'] = array (
  129. '#title' => t('Analysis'),
  130. '#type' => t('select'),
  131. '#description' => t("Choose the analysis to which these features are associated "),
  132. '#required' => TRUE,
  133. '#options' => $analyses,
  134. );
  135. // Advanced Options
  136. $form['advanced'] = array(
  137. '#type' => 'fieldset',
  138. '#title' => t('Advanced Options'),
  139. '#collapsed' => TRUE
  140. );
  141. $form['advanced']['re_help']= array(
  142. '#type' => 'item',
  143. '#value' => t('A regular expression is an advanced method for extracting information from a string of text.
  144. Your FASTA file may contain both a human-readable name and a unique name for each sequence.
  145. If you want to import
  146. both the name and unique name for all sequences, then you must provide regular expressions
  147. so that the loader knows how to separate them.
  148. Otherwise the name and uniquename will be the same.
  149. By default, this loader will use the first word in the definition
  150. lines of the FASTA file
  151. as the name or unique name of the feature.'),
  152. );
  153. $form['advanced']['re_name']= array(
  154. '#type' => 'textfield',
  155. '#title' => t('Regular expression for the name'),
  156. '#required' => FALSE,
  157. '#description' => t('Enter the regular expression that will extract the
  158. feature name from the FASTA definition line. For example, for a
  159. defintion line with a name and unique name separated by a bar \'|\' (>seqname|uniquename),
  160. the regular expression for the name would be, "^(.*?)\|.*$".'),
  161. );
  162. $form['advanced']['re_uname']= array(
  163. '#type' => 'textfield',
  164. '#title' => t('Regular expression for the unique name'),
  165. '#required' => FALSE,
  166. '#description' => t('Enter the regular expression that will extract the
  167. feature name from the FASTA definition line. For example, for a
  168. defintion line with a name and unique name separated by a bar \'|\' (>seqname|uniquename),
  169. the regular expression for the unique name would be "^.*?\|(.*)$").'),
  170. );
  171. // Advanced database cross-reference optoins
  172. $form['advanced']['db'] = array(
  173. '#type' => 'fieldset',
  174. '#title' => t('External Database Reference'),
  175. '#weight'=> 6,
  176. '#collapsed' => TRUE
  177. );
  178. $form['advanced']['db']['re_accession']= array(
  179. '#type' => 'textfield',
  180. '#title' => t('Regular expression for the accession'),
  181. '#required' => FALSE,
  182. '#description' => t('Enter the regular expression that will extract the accession for the external database for each feature from the FASTA definition line.'),
  183. '#weight' => 2
  184. );
  185. // get the list of databases
  186. $sql = "SELECT * FROM {db} ORDER BY name";
  187. $previous_db = tripal_db_set_active('chado'); // use chado database
  188. $db_rset = db_query($sql);
  189. tripal_db_set_active($previous_db); // now use drupal database
  190. $dbs = array();
  191. $dbs[''] = '';
  192. while($db = db_fetch_object($db_rset)){
  193. $dbs[$db->db_id] = "$db->name";
  194. }
  195. $form['advanced']['db']['db_id'] = array (
  196. '#title' => t('External Database'),
  197. '#type' => t('select'),
  198. '#description' => t("Plese choose an external database for which these sequences have a cross reference."),
  199. '#required' => FALSE,
  200. '#options' => $dbs,
  201. '#weight' => 1,
  202. );
  203. $form['advanced']['relationship'] = array(
  204. '#type' => 'fieldset',
  205. '#title' => t('Relationships'),
  206. '#weight'=> 6,
  207. '#collapsed' => TRUE
  208. );
  209. $rels = array();
  210. $rels[''] = '';
  211. $rels['part_of'] = 'part of';
  212. $rels['derives_from'] = 'produced by';
  213. // Advanced references options
  214. $form['advanced']['relationship']['rel_type']= array(
  215. '#title' => t('Relationship Type'),
  216. '#type' => t('select'),
  217. '#description' => t("Use this option to create associations, or relationships between the
  218. features of this FASTA file and existing features in the database. For
  219. example, to associate a FASTA file of peptides to existing genes or transcript sequence,
  220. select the type 'produced by'. For a CDS sequences select the type 'part of'"),
  221. '#required' => FALSE,
  222. '#options' => $rels,
  223. '#weight' => 5,
  224. );
  225. $form['advanced']['relationship']['re_subject']= array(
  226. '#type' => 'textfield',
  227. '#title' => t('Regular expression for the parent'),
  228. '#required' => FALSE,
  229. '#description' => t('Enter the regular expression that will extract the unique
  230. name needed to identify the existing sequence for which the
  231. relationship type selected above will apply.'),
  232. '#weight' => 6
  233. );
  234. $form['advanced']['relationship']['parent_type']= array(
  235. '#type' => 'textfield',
  236. '#title' => t('Parent Type'),
  237. '#required' => FALSE,
  238. '#description' => t('Please enter the Sequence Ontology term for the parent. For example
  239. if the FASTA file being loaded is a set of proteins that are
  240. products of genes, then use the SO term \'gene\' or \'transcript\' or equivalent. However,
  241. this type must match the type for already loaded features.'),
  242. '#weight' => 7
  243. );
  244. $form['button'] = array(
  245. '#type' => 'submit',
  246. '#value' => t('Import FASTA file'),
  247. '#weight' => 10,
  248. );
  249. return $form;
  250. }
  251. /**
  252. *
  253. *
  254. * @ingroup fasta_loader
  255. */
  256. function tripal_feature_fasta_load_form_validate($form, &$form_state){
  257. $fasta_file = trim($form_state['values']['fasta_file']);
  258. $organism_id = $form_state['values']['organism_id'];
  259. $type = trim($form_state['values']['seqtype']);
  260. $method = trim($form_state['values']['method']);
  261. $match_type = trim($form_state['values']['match_type']);
  262. $library_id = $form_state['values']['library_id'];
  263. $re_name = trim($form_state['values']['re_name']);
  264. $re_uname = trim($form_state['values']['re_uname']);
  265. $re_accession = trim($form_state['values']['re_accession']);
  266. $db_id = $form_state['values']['db_id'];
  267. $rel_type = $form_state['values']['rel_type'];
  268. $re_subject = trim($form_state['values']['re_subject']);
  269. $parent_type = trim($form_state['values']['parent_type']);
  270. if($method == 0){
  271. $method = 'Insert only';
  272. }
  273. if($method == 1){
  274. $method = 'Update only';
  275. }
  276. if($method == 2){
  277. $method = 'Insert and update';
  278. }
  279. if($match_type == 0){
  280. $match_type = 'Name';
  281. }
  282. if($match_type == 1){
  283. $match_type = 'Unique name';
  284. }
  285. if ($re_name and !$re_uname and strcmp($match_type,'Unique name')==0){
  286. form_set_error('re_uname',t("You must provide a regular expression to identify the sequence unique name"));
  287. }
  288. if (!$re_name and $re_uname and strcmp($match_type,'Name')==0){
  289. form_set_error('re_name',t("You must provide a regular expression to identify the sequence name"));
  290. }
  291. // check to see if the file is located local to Drupal
  292. $dfile = $_SERVER['DOCUMENT_ROOT'] . base_path() . $fasta_file;
  293. if(!file_exists($dfile)){
  294. // if not local to Drupal, the file must be someplace else, just use
  295. // the full path provided
  296. $dfile = $fasta_file;
  297. }
  298. if(!file_exists($dfile)){
  299. form_set_error('fasta_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."));
  300. }
  301. // make sure if a relationship is specified that all fields are provided.
  302. if(($rel_type or $parent_type) and !$re_subject){
  303. form_set_error('re_subject',t("Please provide a regular expression for the parent"));
  304. }
  305. if(($rel_type or $re_subject) and !$parent_type){
  306. form_set_error('parent_type',t("Please provide a SO term for the parent"));
  307. }
  308. if(($parent_type or $re_subject) and !$rel_type){
  309. form_set_error('rel_type',t("Please select a relationship type"));
  310. }
  311. // make sure if a database is specified that all fields are provided
  312. if($db_id and !$re_accession){
  313. form_set_error('re_accession',t("Please provide a regular expression for the accession"));
  314. }
  315. if($re_accession and !$db_id){
  316. form_set_error('db_id',t("Please select a database"));
  317. }
  318. // check to make sure the types exists
  319. $cvtermsql = "SELECT CVT.cvterm_id
  320. FROM {cvterm} CVT
  321. INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  322. LEFT JOIN {cvtermsynonym} CVTS on CVTS.cvterm_id = CVT.cvterm_id
  323. WHERE cv.name = '%s' and (CVT.name = '%s' or CVTS.synonym = '%s')";
  324. $cvterm = db_fetch_object(db_query($cvtermsql,'sequence',$type,$type));
  325. if(!$cvterm){
  326. form_set_error('type',t("The Sequence Ontology (SO) term selected for the sequence type is not available in the database. Please check spelling or select another."));
  327. }
  328. if($rel_type){
  329. $cvterm = db_fetch_object(db_query($cvtermsql,'sequence',$parent_type,$parent_type));
  330. if(!$cvterm){
  331. form_set_error('parent_type',t("The Sequence Ontology (SO) term selected for the parent relationship is not available in the database. Please check spelling or select another."));
  332. }
  333. }
  334. // check to make sure the 'relationship' and 'sequence' ontologies are loaded
  335. $form_state['storage']['dfile'] = $dfile;
  336. }
  337. /**
  338. *
  339. *
  340. * @ingroup fasta_loader
  341. */
  342. function tripal_feature_fasta_load_form_submit ($form, &$form_state){
  343. global $user;
  344. $dfile = $form_state['storage']['dfile'];
  345. $organism_id = $form_state['values']['organism_id'];
  346. $type = trim($form_state['values']['seqtype']);
  347. $method = trim($form_state['values']['method']);
  348. $match_type = trim($form_state['values']['match_type']);
  349. $library_id = $form_state['values']['library_id'];
  350. $re_name = trim($form_state['values']['re_name']);
  351. $re_uname = trim($form_state['values']['re_uname']);
  352. $re_accession = trim($form_state['values']['re_accession']);
  353. $db_id = $form_state['values']['db_id'];
  354. $rel_type = $form_state['values']['rel_type'];
  355. $re_subject = trim($form_state['values']['re_subject']);
  356. $parent_type = trim($form_state['values']['parent_type']);
  357. $analysis_id = $form_state['values']['analysis_id'];
  358. if($method == 0){
  359. $method = 'Insert only';
  360. }
  361. if($method == 1){
  362. $method = 'Update only';
  363. }
  364. if($method == 2){
  365. $method = 'Insert and update';
  366. }
  367. if($match_type == 0){
  368. $match_type = 'Name';
  369. }
  370. if($match_type == 1){
  371. $match_type = 'Unique name';
  372. }
  373. $args = array($dfile,$organism_id,$type,$library_id,$re_name,$re_uname,
  374. $re_accession,$db_id,$rel_type,$re_subject,$parent_type,$method,
  375. $user->uid,$analysis_id,$match_type);
  376. tripal_add_job("Import FASTA file: $dfile",'tripal_feature',
  377. 'tripal_feature_load_fasta',$args,$user->uid);
  378. }
  379. /**
  380. *
  381. *
  382. * @ingroup fasta_loader
  383. */
  384. function tripal_feature_load_fasta($dfile, $organism_id, $type,
  385. $library_id, $re_name, $re_uname, $re_accession, $db_id, $rel_type,
  386. $re_subject, $parent_type, $method, $uid, $analysis_id,
  387. $match_type,$job = NULL)
  388. {
  389. print "Opening FASTA file $dfile\n";
  390. $lines = file($dfile,FILE_SKIP_EMPTY_LINES);
  391. $i = 0;
  392. $name = '';
  393. $uname = '';
  394. $residues = '';
  395. $num_lines = sizeof($lines);
  396. $interval = intval($num_lines * 0.01);
  397. if($interval == 0){
  398. $interval = 1;
  399. }
  400. foreach ($lines as $line_num => $line) {
  401. $i++; // update the line count
  402. // update the job status every 1% features
  403. if($job and $i % $interval == 0){
  404. tripal_job_set_progress($job,intval(($i/$num_lines)*100));
  405. }
  406. // if we encounter a definition line then get the name, uniquename,
  407. // accession and relationship subject from the definition line
  408. if(preg_match('/^>/',$line)){
  409. // if we have a feature name then we are starting a new sequence
  410. // so let's handle the previous one before moving on
  411. if($name or $uname){
  412. tripal_feature_fasta_loader_handle_feature($name,$uname,$db_id,
  413. $accession,$subject,$rel_type,$parent_type,$analysis_id,$organism_id,$type,
  414. $source,$residues,$method,$re_name,$match_type);
  415. $residues = '';
  416. $name = '';
  417. $uname = '';
  418. }
  419. $line = preg_replace("/^>/",'',$line);
  420. // get the feature name
  421. if($re_name){
  422. if(!preg_match("/$re_name/",$line,$matches)){
  423. print "WARNING: Regular expression for the feature name finds nothing\n";
  424. }
  425. $name = trim($matches[1]);
  426. } else {
  427. // if the match_type is name and no regular expression was provided
  428. // then use the first word as the name, otherwise we don't set the name
  429. if(strcmp($match_type,'Name')==0){
  430. preg_match("/^\s*(.*?)[\s\|].*$/",$line,$matches);
  431. $name = trim($matches[1]);
  432. }
  433. }
  434. // get the feature unique name
  435. if($re_uname){
  436. if(!preg_match("/$re_uname/",$line,$matches)){
  437. print "WARNING: Regular expression for the feature unique name finds nothing\n";
  438. }
  439. $uname = trim($matches[1]);
  440. } else {
  441. // if the match_type is name and no regular expression was provided
  442. // then use the first word as the name, otherwise, we don't set the unqiuename
  443. if(strcmp($match_type,'Unique name')==0){
  444. preg_match("/^\s*(.*?)[\s\|].*$/",$line,$matches);
  445. $uname = trim($matches[1]);
  446. }
  447. }
  448. // get the accession
  449. preg_match("/$re_accession/",$line,$matches);
  450. $accession = trim($matches[1]);
  451. // get the relationship subject
  452. preg_match("/$re_subject/",$line,$matches);
  453. $subject = trim($matches[1]);
  454. }
  455. else {
  456. $residues .= trim($line);
  457. }
  458. }
  459. // now load the last sequence in the file
  460. tripal_feature_fasta_loader_handle_feature($name,$uname,$db_id,
  461. $accession,$subject,$rel_type,$parent_type,$analysis_id,$organism_id,$type,
  462. $source,$residues,$method,$re_name,$match_type);
  463. return '';
  464. }
  465. /**
  466. *
  467. *
  468. * @ingroup fasta_loader
  469. */
  470. function tripal_feature_fasta_loader_handle_feature($name,$uname,$db_id,$accession,
  471. $parent,$rel_type,$parent_type,$analysis_id,$organism_id,$type,
  472. $source,$residues,$method,$re_name,$match_type)
  473. {
  474. $previous_db = tripal_db_set_active('chado');
  475. // first get the type for this sequence
  476. $cvtermsql = "SELECT CVT.cvterm_id
  477. FROM {cvterm} CVT
  478. INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  479. LEFT JOIN {cvtermsynonym} CVTS on CVTS.cvterm_id = CVT.cvterm_id
  480. WHERE cv.name = '%s' and (CVT.name = '%s' or CVTS.synonym = '%s')";
  481. $cvterm = db_fetch_object(db_query($cvtermsql,'sequence',$type,$type));
  482. if(!$cvterm){
  483. print "ERROR: cannot find the term type: '$type'\n";
  484. return 0;
  485. }
  486. // check to see if this feature already exists
  487. if(strcmp($match_type,'Name')==0){
  488. $cnt_sql = "SELECT count(*) as cnt FROM {feature}
  489. WHERE organism_id = %d and name = '%s' and type_id = %d";
  490. $cnt = db_fetch_object(db_query($cnt_sql,$organism_id,$name,$cvterm->cvterm_id));
  491. if($cnt->cnt > 1){
  492. print "ERROR: multiple features exist with the name '$name' of type '$type' for the organism. skipping\n";
  493. return 0;
  494. } else {
  495. $feature_sql = "SELECT * FROM {feature}
  496. WHERE organism_id = %d and name = '%s' and type_id = %d";
  497. $feature = db_fetch_object(db_query($feature_sql,$organism_id,$name,$cvterm->cvterm_id));
  498. }
  499. }
  500. if(strcmp($match_type,'Unique name')==0){
  501. $feature_sql = "SELECT * FROM {feature}
  502. WHERE organism_id = %d and uniquename = '%s' and type_id = %d";
  503. $feature = db_fetch_object(db_query($feature_sql,$organism_id,$uname,$cvterm->cvterm_id));
  504. }
  505. if(!$feature and (strcmp($method,'Insert only')==0 or strcmp($method,'Insert and update')==0)){
  506. // if we have a unique name but not a name then set them to be teh same
  507. // and vice versa
  508. if(!$uname){
  509. $uname = $name;
  510. }
  511. elseif(!$name){
  512. $name = $uname;
  513. }
  514. // now insert the feature
  515. $sql = "INSERT INTO {feature}
  516. (organism_id, name, uniquename, residues, seqlen,
  517. md5checksum,type_id,is_analysis,is_obsolete)
  518. VALUES(%d,'%s','%s','%s',%d, '%s', %d, %s, %s)";
  519. $result = db_query($sql,$organism_id,$name,$uname,$residues,strlen($residues),
  520. md5($residues),$cvterm->cvterm_id,'false','false');
  521. if(!$result){
  522. print "ERROR: failed to insert feature '$name ($uname)'\n";
  523. return 0;
  524. } else {
  525. print "Inserted feature $name ($uname)\n";
  526. }
  527. $feature = db_fetch_object(db_query($feature_sql,$organism_id,$uname,$cvterm->cvterm_id));
  528. }
  529. if(!$feature and (strcmp($method,'Update only')==0 or strcmp($method,'Insert and update')==0)){
  530. print "WARNING: failed to find feature '$name' ('$uname') while matching on " . strtolower($match_type) . ". Skipping\n";
  531. return 0;
  532. }
  533. if($feature and (strcmp($method,'Update only')==0 or strcmp($method,'Insert and update')==0)){
  534. if(strcmp($method,'Update only')==0 or strcmp($method,'Insert and update')==0){
  535. if(strcmp($match_type,'Name')==0){
  536. // if we're matching on the name but do not have a new unique name then we
  537. // don't want to update the uniquename. If we do have a uniquename then we
  538. // should update it. We only get a uniquename if there was a regular expression
  539. // provided for pulling it out
  540. if($uname){
  541. $sql = "UPDATE {feature}
  542. SET uniquename = '%s', residues = '%s', seqlen = '%s', md5checksum = '%s'
  543. WHERE organism_id = %d and name = '%s' and type_id = %d";
  544. $result = db_query($sql,$uname,$residues,strlen($residues),md5($residues),$organism_id,$name,$cvterm->cvterm_id);
  545. } else {
  546. $sql = "UPDATE {feature}
  547. SET residues = '%s', seqlen = '%s', md5checksum = '%s'
  548. WHERE organism_id = %d and name = '%s' and type_id = %d";
  549. $result = db_query($sql,$residues,strlen($residues),md5($residues),$organism_id,$name,$cvterm->cvterm_id);
  550. }
  551. } else {
  552. // if we're matching on the unique name but do not have a new name then we
  553. // don't want to update the name. If we do have a name then we
  554. // should update it. We only get a name if there was a regular expression
  555. // provided for pulling it out
  556. if($name){
  557. $sql = "UPDATE {feature}
  558. SET name = '%s', residues = '%s', seqlen = '%s', md5checksum = '%s'
  559. WHERE organism_id = %d and uniquename = '%s' and type_id = %d";
  560. $result = db_query($sql,$name,$residues,strlen($residues),md5($residues),$organism_id,$uname,$cvterm->cvterm_id);
  561. } else {
  562. $sql = "UPDATE {feature}
  563. SET residues = '%s', seqlen = '%s', md5checksum = '%s'
  564. WHERE organism_id = %d and uniquename = '%s' and type_id = %d";
  565. $result = db_query($sql,$residues,strlen($residues),md5($residues),$organism_id,$uname,$cvterm->cvterm_id);
  566. }
  567. }
  568. if(!$result){
  569. print "ERROR: failed to update feature '$name ($uname)'\n";
  570. return 0;
  571. } else {
  572. print "Updated feature $name ($uname)\n";
  573. }
  574. } else {
  575. print "WARNING: feature already exists: '$name' ('$uname'). Skipping\n";
  576. }
  577. }
  578. // now get the feature
  579. $feature = db_fetch_object(db_query($feature_sql,$organism_id,$uname,$cvterm->cvterm_id));
  580. if(!$feature){
  581. print "Something bad has happened: $organism_id, $uname, $cvterm->cvterm_id\n";
  582. return 0;
  583. }
  584. // add in the analysis link
  585. if ($analysis_id) {
  586. $analysis_link_sql = 'SELECT * FROM analysisfeature WHERE analysis_id=%d AND feature_id=%d';
  587. $analysis_link = db_fetch_object(db_query($analysis_link_sql, $analysis_id, $feature->feature_id));
  588. if (!$analysis_link) {
  589. $sql = "INSERT INTO analysisfeature (analysis_id, feature_id) VALUES (%d, %d)";
  590. $result = db_query($sql, $analysis_id, $feature->feature_id);
  591. if(!$result){
  592. print "WARNING: could not add link between analysis: ".$analysis_id." and feature: ".$feature->uniquename."\n";
  593. }
  594. $analysis_link = db_fetch_object(db_query($analysis_link_sql, $analysis_id, $feature->feature_id));
  595. }
  596. }
  597. // now add the database cross reference
  598. if($db_id){
  599. // check to see if this accession reference exists, if not add it
  600. $dbxrefsql = "SELECT * FROM {dbxref} WHERE db_id = %s and accession = '%s'";
  601. $dbxref = db_fetch_object(db_query($dbxrefsql,$db_id,$accession));
  602. if(!$dbxref){
  603. $sql = "INSERT INTO {dbxref} (db_id,accession) VALUES (%d,'%s')";
  604. $result = db_query($sql,$db_id,$accession);
  605. if(!$result){
  606. print "WARNING: could not add external database acession: '$name accession: $accession'\n";
  607. }
  608. $dbxref = db_fetch_object(db_query($dbxrefsql,$db_id,$accession));
  609. }
  610. // check to see if the feature dbxref record exists if not, then add it
  611. $fdbxrefsql = "SELECT * FROM {feature_dbxref} WHERE feature_id = %d and dbxref_id = %d";
  612. $fdbxref = db_fetch_object(db_query($fdbxrefsql,$feature->feature_id,$dbxref->dbxref_id));
  613. if(!$fdbxref){
  614. $sql = "INSERT INTO {feature_dbxref} (feature_id,dbxref_id) VALUES (%d,%d)";
  615. $result = db_query($sql,$feature->feature_id,$dbxref->dbxref_id);
  616. if(!$result){
  617. print "WARNING: could not associate database cross reference with feature: '$name accession: $accession'\n";
  618. } else {
  619. print "Added database crossreference $name ($uname) -> $accession\n";
  620. }
  621. }
  622. }
  623. // now add in the relationship if one exists. First, get the parent type for the relationship
  624. // then get the parent feature
  625. if($rel_type){
  626. $parentcvterm = db_fetch_object(db_query($cvtermsql,'sequence',$parent_type,$parent_type));
  627. $relcvterm = db_fetch_object(db_query($cvtermsql,'relationship',$rel_type,$rel_type));
  628. $parent_feature = db_fetch_object(db_query($feature_sql,$organism_id,$parent,$parentcvterm->cvterm_id));
  629. if($parent_feature){
  630. // check to see if the relationship already exists
  631. $sql = "SELECT * FROM {feature_relationship} WHERE subject_id = %d and object_id = %d and type_id = %d";
  632. $rel = db_fetch_object(db_query($sql,$feature->feature_id,$parent_feature->feature_id,$relcvterm->cvterm_id));
  633. if($rel){
  634. print "WARNING: relationship already exists, skipping '$uname' ($type) $rel_type '$parent' ($parent_type)\n";
  635. } else {
  636. $sql = "INSERT INTO {feature_relationship} (subject_id,object_id,type_id)
  637. VALUES (%d,%d,%d)";
  638. $result = db_query($sql,$feature->feature_id,$parent_feature->feature_id,$relcvterm->cvterm_id);
  639. if(!$result){
  640. print "WARNING: failed to insert feature relationship '$uname' ($type) $rel_type '$parent' ($parent_type)\n";
  641. } else {
  642. print "Inserted relationship relationship: '$uname' ($type) $rel_type '$parent' ($parent_type)\n";
  643. }
  644. }
  645. }
  646. else {
  647. print "WARNING: cannot establish relationship '$uname' ($type) $rel_type '$parent' ($parent_type): Cannot find the parent\n";
  648. }
  649. }
  650. tripal_db_set_active($previous_db);
  651. }