fasta_loader.inc 29 KB

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