fasta_loader.inc 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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. $org_rset = chado_query($sql);
  30. $organisms = array();
  31. $organisms[''] = '';
  32. while ($organism = db_fetch_object($org_rset)) {
  33. $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  34. }
  35. $form['organism_id'] = array(
  36. '#title' => t('Organism'),
  37. '#type' => t('select'),
  38. '#description' => t("Choose the organism to which these sequences are associated"),
  39. '#required' => TRUE,
  40. '#options' => $organisms,
  41. );
  42. $form['seqtype']= array(
  43. '#type' => 'textfield',
  44. '#title' => t('Sequence Type'),
  45. '#required' => TRUE,
  46. '#description' => t('Please enter the Sequence Ontology (SO) term name that describes the sequences in the FASTA file (e.g. gene, mRNA, protein, etc...)'),
  47. );
  48. // get the list of organisms
  49. $sql = "SELECT L.library_id, L.name, CVT.name as type
  50. FROM {library} L
  51. INNER JOIN {cvterm} CVT ON L.type_id = CVT.cvterm_id
  52. ORDER BY name";
  53. $lib_rset = chado_query($sql);
  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('Used for "updates only" or "insert and update" methods. Not required if method type is "insert".
  93. Feature data is stored in Chado with both a human-readable
  94. name and a unique name. If the features in your FASTA file are uniquely identified using
  95. a human-readable name then select the "Name" button. If your features are
  96. uniquely identified using the unique name then select the "Unique name" button. If you
  97. loaded your features first using the GFF loader then the unique name of each
  98. features were indicated by the "ID=" attribute and the name by the "Name=" attribute.
  99. By default, the FASTA loader will use the first word (character string
  100. before the first space) as the name for your feature. If
  101. this does not uniquely identify your feature consider specifying a regular expression in the advanced section below.
  102. Additionally, you may import both a name and a unique name for each sequence using the advanced options.'),
  103. '#default_value' => 1,
  104. );
  105. $form['analysis'] = array(
  106. '#type' => 'fieldset',
  107. '#title' => t('Analysis Used to Derive Features'),
  108. '#collapsed' => TRUE
  109. );
  110. $form['analysis']['desc'] = array(
  111. '#type' => 'markup',
  112. '#value' => t("Why specify an analysis for a data load? All data comes
  113. from some place, even if downloaded from Genbank. By specifying
  114. analysis details for all data uploads, it allows an end user to reproduce the
  115. data set, but at least indicates the source of the data."),
  116. );
  117. // get the list of organisms
  118. $sql = "SELECT * FROM {analysis} ORDER BY name";
  119. $org_rset = chado_query($sql);
  120. $analyses = array();
  121. $analyses[''] = '';
  122. while ($analysis = db_fetch_object($org_rset)) {
  123. $analyses[$analysis->analysis_id] = "$analysis->name ($analysis->program $analysis->programversion, $analysis->sourcename)";
  124. }
  125. $form['analysis']['analysis_id'] = array(
  126. '#title' => t('Analysis'),
  127. '#type' => t('select'),
  128. '#description' => t("Choose the analysis to which these features are associated"),
  129. '#required' => TRUE,
  130. '#options' => $analyses,
  131. );
  132. // Advanced Options
  133. $form['advanced'] = array(
  134. '#type' => 'fieldset',
  135. '#title' => t('Advanced Options'),
  136. '#collapsible' => TRUE,
  137. '#collapsed' => TRUE
  138. );
  139. $form['advanced']['re_help']= array(
  140. '#type' => 'item',
  141. '#value' => t('A regular expression is an advanced method for extracting information from a string of text.
  142. Your FASTA file may contain both a human-readable name and a unique name for each sequence.
  143. If you want to import
  144. both the name and unique name for all sequences, then you must provide regular expressions
  145. so that the loader knows how to separate them.
  146. Otherwise the name and uniquename will be the same.
  147. By default, this loader will use the first word in the definition
  148. lines of the FASTA file
  149. as the name or unique name of the feature.'),
  150. );
  151. $form['advanced']['re_name']= array(
  152. '#type' => 'textfield',
  153. '#title' => t('Regular expression for the name'),
  154. '#required' => FALSE,
  155. '#description' => t('Enter the regular expression that will extract the
  156. feature name from the FASTA definition line. For example, for a
  157. defintion line with a name and unique name separated by a bar \'|\' (>seqname|uniquename),
  158. the regular expression for the name would be, "^(.*?)\|.*$".'),
  159. );
  160. $form['advanced']['re_uname']= array(
  161. '#type' => 'textfield',
  162. '#title' => t('Regular expression for the unique name'),
  163. '#required' => FALSE,
  164. '#description' => t('Enter the regular expression that will extract the
  165. feature name from the FASTA definition line. For example, for a
  166. defintion line with a name and unique name separated by a bar \'|\' (>seqname|uniquename),
  167. the regular expression for the unique name would be "^.*?\|(.*)$").'),
  168. );
  169. // Advanced database cross-reference optoins
  170. $form['advanced']['db'] = array(
  171. '#type' => 'fieldset',
  172. '#title' => t('External Database Reference'),
  173. '#weight' => 6,
  174. '#collapsed' => TRUE
  175. );
  176. $form['advanced']['db']['re_accession']= array(
  177. '#type' => 'textfield',
  178. '#title' => t('Regular expression for the accession'),
  179. '#required' => FALSE,
  180. '#description' => t('Enter the regular expression that will extract the accession for the external database for each feature from the FASTA definition line.'),
  181. '#weight' => 2
  182. );
  183. // get the list of databases
  184. $sql = "SELECT * FROM {db} ORDER BY name";
  185. $db_rset = chado_query($sql);
  186. $dbs = array();
  187. $dbs[''] = '';
  188. while ($db = db_fetch_object($db_rset)) {
  189. $dbs[$db->db_id] = "$db->name";
  190. }
  191. $form['advanced']['db']['db_id'] = array(
  192. '#title' => t('External Database'),
  193. '#type' => t('select'),
  194. '#description' => t("Plese choose an external database for which these sequences have a cross reference."),
  195. '#required' => FALSE,
  196. '#options' => $dbs,
  197. '#weight' => 1,
  198. );
  199. $form['advanced']['relationship'] = array(
  200. '#type' => 'fieldset',
  201. '#title' => t('Relationships'),
  202. '#weight' => 6,
  203. '#collapsed' => TRUE
  204. );
  205. $rels = array();
  206. $rels[''] = '';
  207. $rels['part_of'] = 'part of';
  208. $rels['derives_from'] = 'produced by';
  209. // Advanced references options
  210. $form['advanced']['relationship']['rel_type']= array(
  211. '#title' => t('Relationship Type'),
  212. '#type' => t('select'),
  213. '#description' => t("Use this option to create associations, or relationships between the
  214. features of this FASTA file and existing features in the database. For
  215. example, to associate a FASTA file of peptides to existing genes or transcript sequence,
  216. select the type 'produced by'. For a CDS sequences select the type 'part of'"),
  217. '#required' => FALSE,
  218. '#options' => $rels,
  219. '#weight' => 5,
  220. );
  221. $form['advanced']['relationship']['re_subject']= array(
  222. '#type' => 'textfield',
  223. '#title' => t('Regular expression for the parent'),
  224. '#required' => FALSE,
  225. '#description' => t('Enter the regular expression that will extract the unique
  226. name needed to identify the existing sequence for which the
  227. relationship type selected above will apply.'),
  228. '#weight' => 6
  229. );
  230. $form['advanced']['relationship']['parent_type']= array(
  231. '#type' => 'textfield',
  232. '#title' => t('Parent Type'),
  233. '#required' => FALSE,
  234. '#description' => t('Please enter the Sequence Ontology term for the parent. For example
  235. if the FASTA file being loaded is a set of proteins that are
  236. products of genes, then use the SO term \'gene\' or \'transcript\' or equivalent. However,
  237. this type must match the type for already loaded features.'),
  238. '#weight' => 7
  239. );
  240. $form['button'] = array(
  241. '#type' => 'submit',
  242. '#value' => t('Import FASTA file'),
  243. '#weight' => 10,
  244. );
  245. return $form;
  246. }
  247. /**
  248. *
  249. *
  250. * @ingroup fasta_loader
  251. */
  252. function tripal_feature_fasta_load_form_validate($form, &$form_state) {
  253. $fasta_file = trim($form_state['values']['fasta_file']);
  254. $organism_id = $form_state['values']['organism_id'];
  255. $type = trim($form_state['values']['seqtype']);
  256. $method = trim($form_state['values']['method']);
  257. $match_type = trim($form_state['values']['match_type']);
  258. $library_id = $form_state['values']['library_id'];
  259. $re_name = trim($form_state['values']['re_name']);
  260. $re_uname = trim($form_state['values']['re_uname']);
  261. $re_accession = trim($form_state['values']['re_accession']);
  262. $db_id = $form_state['values']['db_id'];
  263. $rel_type = $form_state['values']['rel_type'];
  264. $re_subject = trim($form_state['values']['re_subject']);
  265. $parent_type = trim($form_state['values']['parent_type']);
  266. if ($method == 0) {
  267. $method = 'Insert only';
  268. }
  269. if ($method == 1) {
  270. $method = 'Update only';
  271. }
  272. if ($method == 2) {
  273. $method = 'Insert and update';
  274. }
  275. if ($match_type == 0) {
  276. $match_type = 'Name';
  277. }
  278. if ($match_type == 1) {
  279. $match_type = 'Unique name';
  280. }
  281. if ($re_name and !$re_uname and strcmp($match_type, 'Unique name')==0) {
  282. form_set_error('re_uname', t("You must provide a regular expression to identify the sequence unique name"));
  283. }
  284. if (!$re_name and $re_uname and strcmp($match_type, 'Name')==0) {
  285. form_set_error('re_name', t("You must provide a regular expression to identify the sequence name"));
  286. }
  287. // check to see if the file is located local to Drupal
  288. $fasta_file = trim($fasta_file);
  289. $dfile = $_SERVER['DOCUMENT_ROOT'] . base_path() . $fasta_file;
  290. if (!file_exists($dfile)) {
  291. // if not local to Drupal, the file must be someplace else, just use
  292. // the full path provided
  293. $dfile = $fasta_file;
  294. }
  295. if (!file_exists($dfile)) {
  296. 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."));
  297. }
  298. // make sure if a relationship is specified that all fields are provided.
  299. if (($rel_type or $parent_type) and !$re_subject) {
  300. form_set_error('re_subject', t("Please provide a regular expression for the parent"));
  301. }
  302. if (($rel_type or $re_subject) and !$parent_type) {
  303. form_set_error('parent_type', t("Please provide a SO term for the parent"));
  304. }
  305. if (($parent_type or $re_subject) and !$rel_type) {
  306. form_set_error('rel_type', t("Please select a relationship type"));
  307. }
  308. // make sure if a database is specified that all fields are provided
  309. if ($db_id and !$re_accession) {
  310. form_set_error('re_accession', t("Please provide a regular expression for the accession"));
  311. }
  312. if ($re_accession and !$db_id) {
  313. form_set_error('db_id', t("Please select a database"));
  314. }
  315. // check to make sure the types exists
  316. $cvtermsql = "SELECT CVT.cvterm_id
  317. FROM {cvterm} CVT
  318. INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  319. LEFT JOIN {cvtermsynonym} CVTS on CVTS.cvterm_id = CVT.cvterm_id
  320. WHERE cv.name = '%s' and (CVT.name = '%s' or CVTS.synonym = '%s')";
  321. $cvterm = db_fetch_object(chado_query($cvtermsql, 'sequence', $type, $type));
  322. if (!$cvterm) {
  323. 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."));
  324. }
  325. if ($rel_type) {
  326. $cvterm = db_fetch_object(chado_query($cvtermsql, 'sequence', $parent_type, $parent_type));
  327. if (!$cvterm) {
  328. 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."));
  329. }
  330. }
  331. // check to make sure the 'relationship' and 'sequence' ontologies are loaded
  332. $form_state['storage']['dfile'] = $dfile;
  333. }
  334. /**
  335. *
  336. *
  337. * @ingroup fasta_loader
  338. */
  339. function tripal_feature_fasta_load_form_submit($form, &$form_state) {
  340. global $user;
  341. $dfile = $form_state['storage']['dfile'];
  342. $organism_id = $form_state['values']['organism_id'];
  343. $type = trim($form_state['values']['seqtype']);
  344. $method = trim($form_state['values']['method']);
  345. $match_type = trim($form_state['values']['match_type']);
  346. $library_id = $form_state['values']['library_id'];
  347. $re_name = trim($form_state['values']['re_name']);
  348. $re_uname = trim($form_state['values']['re_uname']);
  349. $re_accession = trim($form_state['values']['re_accession']);
  350. $db_id = $form_state['values']['db_id'];
  351. $rel_type = $form_state['values']['rel_type'];
  352. $re_subject = trim($form_state['values']['re_subject']);
  353. $parent_type = trim($form_state['values']['parent_type']);
  354. $analysis_id = $form_state['values']['analysis_id'];
  355. if ($method == 0) {
  356. $method = 'Insert only';
  357. }
  358. if ($method == 1) {
  359. $method = 'Update only';
  360. }
  361. if ($method == 2) {
  362. $method = 'Insert and update';
  363. }
  364. if ($match_type == 0) {
  365. $match_type = 'Name';
  366. }
  367. if ($match_type == 1) {
  368. $match_type = 'Unique name';
  369. }
  370. $args = array($dfile, $organism_id, $type, $library_id, $re_name, $re_uname,
  371. $re_accession, $db_id, $rel_type, $re_subject, $parent_type, $method,
  372. $user->uid, $analysis_id, $match_type);
  373. $fname = preg_replace("/.*\/(.*)/", "$1", $dfile);
  374. tripal_add_job("Import FASTA file: $fname", 'tripal_feature',
  375. 'tripal_feature_load_fasta', $args, $user->uid);
  376. }
  377. /**
  378. *
  379. *
  380. * @ingroup fasta_loader
  381. */
  382. function tripal_feature_load_fasta($dfile, $organism_id, $type,
  383. $library_id, $re_name, $re_uname, $re_accession, $db_id, $rel_type,
  384. $re_subject, $parent_type, $method, $uid, $analysis_id,
  385. $match_type, $job = NULL) {
  386. // begin the transaction
  387. $connection = tripal_db_start_transaction();
  388. // if we cannot get a connection then let the user know the loading will be slow
  389. if (!$connection) {
  390. print "A persistant connection was not obtained. Loading will be slow\n";
  391. }
  392. else {
  393. print "\nNOTE: Loading of this FASTA file is performed using a database transaction. \n" .
  394. "If the load fails or is terminated prematurely then the entire set of \n" .
  395. "insertions/updates is rolled back and will not be found in the database\n\n";
  396. }
  397. // first get the type for this sequence
  398. $cvtermsql = "SELECT CVT.cvterm_id
  399. FROM {cvterm} CVT
  400. INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  401. LEFT JOIN {cvtermsynonym} CVTS on CVTS.cvterm_id = CVT.cvterm_id
  402. WHERE cv.name = '%s' and (CVT.name = '%s' or CVTS.synonym = '%s')";
  403. $cvterm = db_fetch_object(chado_query($cvtermsql, 'sequence', $type, $type));
  404. if (!$cvterm) {
  405. watchdog("T_fasta_loader", "Cannot find the term type: '%type'", array('%type' => $type), WATCHDOG_ERROR);
  406. return 0;
  407. }
  408. if ($parent_type) {
  409. $parentcvterm = db_fetch_object(chado_query($cvtermsql, 'sequence', $parent_type, $parent_type));
  410. if (!$parentcvterm) {
  411. watchdog("T_fasta_loader", "Cannot find the paretne term type: '%type'", array('%type' => $parentcvterm), WATCHDOG_ERROR);
  412. return 0;
  413. }
  414. }
  415. if ($rel_type) {
  416. $relcvterm = db_fetch_object(chado_query($cvtermsql, 'relationship', $rel_type, $rel_type));
  417. if (!$relcvterm) {
  418. watchdog("T_fasta_loader", "Cannot find the relationship term type: '%type'", array('%type' => $relcvterm), WATCHDOG_ERROR);
  419. return 0;
  420. }
  421. }
  422. print "Opening FASTA file $dfile\n";
  423. //$lines = file($dfile, FILE_SKIP_EMPTY_LINES);
  424. $fh = fopen($dfile, 'r');
  425. if (!$fh) {
  426. watchdog('T_fasta_loader', "cannot open file: %dfile", array('%dfile' => $dfile), WATCHDOG_ERROR);
  427. return 0;
  428. }
  429. $filesize = filesize($dfile);
  430. $i = 0;
  431. $name = '';
  432. $uname = '';
  433. $residues = '';
  434. $interval = intval($filesize * 0.01);
  435. if ($interval < 1) {
  436. $interval = 1;
  437. }
  438. $inv_read = 0;
  439. // we need to get the table schema to make sure we don't overrun the
  440. // size of fields with what our regular expressions retrieve
  441. $feature_tbl = tripal_core_get_chado_table_schema('feature');
  442. $dbxref_tbl = tripal_core_get_chado_table_schema('dbxref');
  443. //foreach ($lines as $line_num => $line) {
  444. while ($line = fgets($fh)) {
  445. $i++; // update the line count
  446. $num_read += drupal_strlen($line);
  447. $intv_read += drupal_strlen($line);
  448. // if we encounter a definition line then get the name, uniquename,
  449. // accession and relationship subject from the definition line
  450. if (preg_match('/^>/', $line)) {
  451. // if we have a feature name then we are starting a new sequence
  452. // so lets handle the previous one before moving on
  453. if ($name or $uname) {
  454. tripal_feature_fasta_loader_handle_feature($name, $uname, $db_id,
  455. $accession, $subject, $rel_type, $parent_type, $analysis_id, $organism_id, $cvterm,
  456. $source, $residues, $method, $re_name, $match_type, $parentcvterm, $relcvterm);
  457. $residues = '';
  458. $name = '';
  459. $uname = '';
  460. }
  461. $line = preg_replace("/^>/", '', $line); // remove the > symbol from the defline
  462. // get the feature name
  463. if ($re_name) {
  464. if (!preg_match("/$re_name/", $line, $matches)) {
  465. watchdog('trp-fasta', "ERROR: Regular expression for the feature name finds nothing. Line %line.", array('%line' => $i), 'error');
  466. }
  467. elseif (strlen($matches[1]) > $feature_tbl['fields']['name']['length']) {
  468. watchdog('trp-fasta', "WARNING: Regular expression retrieves a value too long for the feature name. Line %line.", array('%line' => $i), 'error');
  469. }
  470. else {
  471. $name = trim($matches[1]);
  472. }
  473. }
  474. else {
  475. // if the match_type is name and no regular expression was provided
  476. // then use the first word as the name, otherwise we don't set the name
  477. if (strcmp($match_type, 'Name')==0) {
  478. if(preg_match("/^\s*(.*?)[\s\|].*$/", $line, $matches)){
  479. if (strlen($matches[1]) > $feature_tbl['fields']['name']['length']) {
  480. watchdog('trp-fasta', "WARNING: Regular expression retrieves a feature name too long for the feature name. Line %line.", array('%line' => $i), 'error');
  481. }
  482. else {
  483. $name = trim($matches[1]);
  484. }
  485. }
  486. else {
  487. watchdog('trp-fasta', "ERROR: Cannot find a feature name. Line %line.", array('%line' => $i), 'error');
  488. }
  489. }
  490. }
  491. // get the feature unique name
  492. if ($re_uname) {
  493. if (!preg_match("/$re_uname/", $line, $matches)) {
  494. watchdog('trp-fasta', "ERROR: Regular expression for the feature unique name finds nothing. Line %line.", array('%line' => $i), 'error');
  495. }
  496. $uname = trim($matches[1]);
  497. }
  498. else {
  499. // if the match_type is name and no regular expression was provided
  500. // then use the first word as the name, otherwise, we don't set the unqiuename
  501. if (strcmp($match_type, 'Unique name')==0) {
  502. if(preg_match("/^\s*(.*?)[\s\|].*$/", $line, $matches)){
  503. $uname = trim($matches[1]);
  504. }
  505. else {
  506. watchdog('trp-fasta', "ERROR: Cannot find a feature unique name. Line %line.", array('%line' => $i), 'error');
  507. }
  508. }
  509. }
  510. // get the accession
  511. preg_match("/$re_accession/", $line, $matches);
  512. if (strlen($matches[1]) > $dbxref_tbl['fields']['accession']['length']) {
  513. watchdog('trp-fasta', "WARNING: Regular expression retrieves an accession too long for the feature name. Cannot add cross reference. Line %line.", array('%line' => $i), 'warning');
  514. }
  515. else {
  516. $accession = trim($matches[1]);
  517. }
  518. // get the relationship subject
  519. preg_match("/$re_subject/", $line, $matches);
  520. $subject = trim($matches[1]);
  521. }
  522. else {
  523. $residues .= trim($line);
  524. // update the job status every % features
  525. if ($job and $intv_read >= $interval) {
  526. $intv_read = 0;
  527. $percent = sprintf("%.2f", ($num_read / $filesize) * 100);
  528. if ($name) {
  529. print "Parsing Line $i (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes. Current feature: $name\r";
  530. }
  531. else {
  532. print "Parsing Line $i (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes. Current feature: $uname\r";
  533. }
  534. tripal_job_set_progress($job, intval(($num_read / $filesize) * 100));
  535. }
  536. }
  537. }
  538. // now load the last sequence in the file
  539. tripal_feature_fasta_loader_handle_feature($name, $uname, $db_id,
  540. $accession, $subject, $rel_type, $parent_type, $analysis_id, $organism_id, $cvterm,
  541. $source, $residues, $method, $re_name, $match_type, $parentcvterm, $relcvterm);
  542. // commit the transaction
  543. tripal_db_commit_transaction();
  544. print "\nDone\n";
  545. }
  546. /**
  547. *
  548. *
  549. * @ingroup fasta_loader
  550. */
  551. function tripal_feature_fasta_loader_handle_feature($name, $uname, $db_id, $accession,
  552. $parent, $rel_type, $parent_type, $analysis_id, $organism_id, $cvterm,
  553. $source, $residues, $method, $re_name, $match_type, $parentcvterm, $relcvterm) {
  554. // check to see if this feature already exists if the match_type is 'Name'
  555. if (strcmp($match_type, 'Name')==0) {
  556. $values = array(
  557. 'organism_id' => $organism_id,
  558. 'name' => $name,
  559. 'type_id' => $cvterm->cvterm_id,
  560. );
  561. $options = array('statement_name' => 'sel_feature_ornaty');
  562. $results = tripal_core_chado_select('feature', array('feature_id'), $values, $options);
  563. if (count($results) > 1) {
  564. watchdog('T_fasta_loader', "Multiple features exist with the name '%name' of type
  565. '%type' for the organism. skipping", array('%name' => $name, '%type' => $type));
  566. return 0;
  567. }
  568. if (count($results) == 1) {
  569. $feature = $results[0];
  570. }
  571. }
  572. // check to see if this feature already exists if the match_type is 'Unique Name'
  573. if (strcmp($match_type, 'Unique name')==0) {
  574. $values = array(
  575. 'organism_id' => $organism_id,
  576. 'uniquename' => $uname,
  577. 'type_id' => $cvterm->cvterm_id,
  578. );
  579. $options = array('statement_name' => 'sel_feature_oruqty');
  580. $results = tripal_core_chado_select('feature', array('feature_id'), $values, $options);
  581. if (count($results) > 1) {
  582. watchdog('T_fasta_loader', "Multiple features exist with the name '%name' of type
  583. '%type' for the organism. skipping", array('%name' => $name, '%type' => $type));
  584. return 0;
  585. }
  586. if (count($results) == 1) {
  587. $feature = $results[0];
  588. }
  589. // if the feature exists but this is an "insert only" method then skip this feature
  590. if ($feature and (strcmp($method, 'Insert only')==0)) {
  591. watchdog('T_fasta_loader', "Feature already exists '%name' ('%uname') while matching on %type. Skipping insert.",
  592. array('%name' => $name, '%uname' => $uname, '%type' => drupal_strtolower($match_type)), WATCHDOG_WARNING);
  593. return 0;
  594. }
  595. }
  596. // if we don't have a feature and we're doing an insert then do the insert
  597. $inserted = 0;
  598. if (!$feature and (strcmp($method, 'Insert only')==0 or strcmp($method, 'Insert and update')==0)) {
  599. // if we have a unique name but not a name then set them to be the same and vice versa
  600. if (!$uname) {
  601. $uname = $name;
  602. }
  603. elseif (!$name) {
  604. $name = $uname;
  605. }
  606. // insert the feature
  607. $values = array(
  608. 'organism_id' => $organism_id,
  609. 'name' => $name,
  610. 'uniquename' => $uname,
  611. 'residues' => $residues,
  612. 'seqlen' => drupal_strlen($residues),
  613. 'md5checksum' => md5($residues),
  614. 'type_id' => $cvterm->cvterm_id,
  615. 'is_analysis' => 'FALSE',
  616. 'is_obsolete' => 'FALSE',
  617. );
  618. $options = array('statement_name' => 'ins_feature_all');
  619. $success = tripal_core_chado_insert('feature', $values, $options);
  620. if (!$success) {
  621. watchdog('T_fasta_loader', "Failed to insert feature '%name (%uname)'",
  622. array('%name' => $name, '%uname' => $numane), WATCHDOG_ERROR);
  623. return 0;
  624. }
  625. // now get the feature we just inserted
  626. $values = array(
  627. 'organism_id' => $organism_id,
  628. 'uniquename' => $uname,
  629. 'type_id' => $cvterm->cvterm_id,
  630. );
  631. $options = array('statement_name' => 'sel_feature_oruqty');
  632. $results = tripal_core_chado_select('feature', array('feature_id'), $values, $options);
  633. if (count($results) == 1) {
  634. $inserted = 1;
  635. $feature = $results[0];
  636. }
  637. else {
  638. watchdog('T_fasta_loader', "Failed to retreive newly inserted feature '%name (%uname)'",
  639. array('%name' => $name, '%uname' => $numane), WATCHDOG_ERROR);
  640. return 0;
  641. }
  642. }
  643. // if we don't have a feature and the user wants to do an update then fail
  644. if (!$feature and (strcmp($method, 'Update only')==0 or drupal_strcmp($method, 'Insert and update')==0)) {
  645. watchdog('T_fasta_loader', "Failed to find feature '%name' ('%uname') while matching on " .
  646. drupal_strtolower($match_type), array('%name' => $name, '%uname' => $uname), WATCHDOG_ERROR);
  647. return 0;
  648. }
  649. // if we do have a feature and this is an update then proceed with the update
  650. if ($feature and !$inserted and (strcmp($method, 'Update only')==0 or strcmp($method, 'Insert and update')==0)) {
  651. // if the user wants to match on the Name field
  652. if (strcmp($match_type, 'Name')==0) {
  653. // if we're matching on the name but do not have a unique name then we don't want to update the uniquename.
  654. $values = array();
  655. if ($uname) {
  656. // first check to make sure that by changing the unique name of this feature that we won't conflict with
  657. // another existing feature of the same name
  658. $values = array(
  659. 'organism_id' => $organism_id,
  660. 'uniquename' => $uname,
  661. 'type_id' => $cvterm->cvterm_id,
  662. );
  663. $options = array('statement_name' => 'sel_feature_oruqty');
  664. $results = tripal_core_chado_select('feature', array('feature_id'), $values, $options);
  665. if (count($results) > 0) {
  666. watchdog('T_fasta_loader', "Cannot update the feature '%name' with a uniquename of '%uname' and type of '%type' as it
  667. conflicts with an existing feature with the same uniquename and type.",
  668. array('%name' => $name, '%uname' => $uname, '%type' => $type));
  669. return 0;
  670. }
  671. // the changes to the uniquename don't conflict so proceed with the update
  672. $values = array(
  673. 'uniquename' => $uname,
  674. 'residues' => $residues,
  675. 'seqlen' => drupal_strlen($residues),
  676. 'md5checksum' => md5($residues),
  677. 'is_analysis' => 'false',
  678. 'is_obsolete' => 'false',
  679. );
  680. $match = array(
  681. 'name' => $name,
  682. 'organism_id' => $organism_id,
  683. 'type_id' => $cvterm->cvterm_id,
  684. );
  685. $options = array('statement_name' => 'upd_feature_resemdisis_naorty_un');
  686. }
  687. // if we do not have a new unique name then don't change the existing uniquename field
  688. else {
  689. $values = array(
  690. 'residues' => $residues,
  691. 'seqlen' => drupal_strlen($residues),
  692. 'md5checksum' => md5($residues),
  693. 'is_analysis' => 'false',
  694. 'is_obsolete' => 'false',
  695. );
  696. $match = array(
  697. 'name' => $name,
  698. 'organism_id' => $organism_id,
  699. 'type_id' => $cvterm->cvterm_id,
  700. );
  701. $options = array('statement_name' => 'upd_feature_unresemdisis_naorty');
  702. }
  703. // perform the update
  704. $success = tripal_core_chado_update('feature', $match, $values, $options);
  705. if (!$success) {
  706. watchdog('T_fasta_loader', "Failed to update feature '%name' ('%name')",
  707. array('%name' => $name, '%uiname' => $uname), WATCHDOG_ERROR);
  708. return 0;
  709. }
  710. }
  711. if (strcmp($match_type, 'Unique name')==0) {
  712. // if we're matching on the uniquename but do not have a new name then we don't want to update the name.
  713. $values = array();
  714. if ($name) {
  715. $values = array(
  716. 'name' => $name,
  717. 'residues' => $residues,
  718. 'seqlen' => drupal_strlen($residues),
  719. 'md5checksum' => md5($residues),
  720. 'is_analysis' => 'false',
  721. 'is_obsolete' => 'false',
  722. );
  723. $match = array(
  724. 'uniquename' => $uname,
  725. 'organism_id' => $organism_id,
  726. 'type_id' => $cvterm->cvterm_id,
  727. );
  728. $options = array('statement_name' => 'upd_feature_resemdisis_unorty_na');
  729. }
  730. // if we have a unique name then update it after matching by the name
  731. else {
  732. $values = array(
  733. 'residues' => $residues,
  734. 'seqlen' => drupal_strlen($residues),
  735. 'md5checksum' => md5($residues),
  736. 'is_analysis' => 'false',
  737. 'is_obsolete' => 'false',
  738. );
  739. $match = array(
  740. 'uniquename' => $uname,
  741. 'organism_id' => $organism_id,
  742. 'type_id' => $cvterm->cvterm_id,
  743. );
  744. $options = array('statement_name' => 'upd_feature_naresemdisis_unorty');
  745. }
  746. $success = tripal_core_chado_update('feature', $match, $values, $options);
  747. if (!$success) {
  748. watchdog('T_fasta_loader', "Failed to update feature '%name' ('%name')",
  749. array('%name' => $name, '%uiname' => $uname), WATCHDOG_ERROR);
  750. return 0;
  751. }
  752. }
  753. }
  754. // add in the analysis link
  755. if ($analysis_id) {
  756. // if the association doens't alredy exist then add one
  757. $values = array(
  758. 'analysis_id' => $analysis_id,
  759. 'feature_id' => $feature->feature_id,
  760. );
  761. $sel_options = array('statement_name' => 'sel_analysisfeature_anfe');
  762. $results = tripal_core_chado_select('analysisfeature', array('analysisfeature_id'), $values, $sel_options);
  763. if (count($results) == 0) {
  764. $ins_options = array('statement_name' => 'ins_analysisfeature_anfe');
  765. $success = tripal_core_chado_insert('analysisfeature', $values, $ins_options);
  766. if (!$success) {
  767. watchdog('T_fasta_loader', "Failed to associate analysis and feature '%name' ('%name')",
  768. array('%name' => $name, '%uname' => $uname), WATCHDOG_ERROR);
  769. return 0;
  770. }
  771. }
  772. }
  773. // now add the database cross reference
  774. if ($db_id) {
  775. // check to see if this accession reference exists, if not add it
  776. $values = array(
  777. 'db_id' => $db_id,
  778. 'accession' => $accession
  779. );
  780. $sel_options = array('statement_name' => 'sel_dbxref_dbac');
  781. $results = tripal_core_chado_select('dbxref', array('dbxref_id'), $values, $sel_options);
  782. // if the accession doesn't exist then add it
  783. if (count($results) == 0) {
  784. $ins_options = array('statement_name' => 'ins_dbxref_dbac');
  785. $results = tripal_core_chado_insert('dbxref', $values, $ins_options);
  786. if (!$results) {
  787. watchdog('T_fasta_loader', "Failed to add database accession '%accession'",
  788. array('%accession' => $accession), WATCHDOG_ERROR);
  789. return 0;
  790. }
  791. $results = tripal_core_chado_select('dbxref', array('dbxref_id'), $values, $sel_options);
  792. if (count($results) == 1) {
  793. $dbxref = $results[0];
  794. }
  795. else {
  796. watchdog('T_fasta_loader', "Failed to retreive newly inserted dbxref '%name (%uname)'",
  797. array('%name' => $name, '%uname' => $numane), WATCHDOG_ERROR);
  798. return 0;
  799. }
  800. }
  801. else {
  802. $dbxref = $results[0];
  803. }
  804. // check to see if the feature dbxref record exists if not, then add it
  805. $values = array(
  806. 'feature_id' => $feature->feature_id,
  807. 'dbxref_id' => $dbxref->dbxref_id
  808. );
  809. $sel_options = array('statement_name' => 'sel_featuredbxref_fedb');
  810. $results = tripal_core_chado_select('feature_dbxref', array('feature_dbxref_id'), $values, $sel_options);
  811. if (count($results) == 0) {
  812. $ins_options = array('statement_name' => 'ins_featuredbxref_fedb');
  813. $success = tripal_core_chado_insert('feature_dbxref', $values, $ins_options);
  814. if (!$success) {
  815. watchdog('T_fasta_loader', "Failed to add associate database accession '%accession' with feature",
  816. array('%accession' => $accession), WATCHDOG_ERROR);
  817. return 0;
  818. }
  819. }
  820. }
  821. // now add in the relationship if one exists. If not, then add it
  822. if ($rel_type) {
  823. $values = array(
  824. 'organism_id' => $organism_id,
  825. 'uniquename' => $parent,
  826. 'type_id' => $parentcvterm->cvterm_id,
  827. );
  828. $options = array('statement_name' => 'sel_feature_oruqty');
  829. $results = tripal_core_chado_select('feature', array('feature_id'), $values, $options);
  830. if (count($results) != 1) {
  831. watchdog('T_fasta_loader', "Cannot find a unique fature for the parent '%parent' of type
  832. '%type' for the feature.", array('%parent' => $parent, '%type' => $parent_type));
  833. return 0;
  834. }
  835. $parent_feature = $results[0];
  836. // check to see if the relationship already exists if not then add it
  837. $values = array(
  838. 'subject_id' => $feature->feature_id,
  839. 'object_id' => $parent_feature->feature_id,
  840. 'type_id' => $relcvterm->cvterm_id,
  841. );
  842. $sel_options = array('statement_name' => 'sel_featurerelationship_suojty');
  843. $results = tripal_core_chado_select('feature_relationship', array('feature_relationship_id'), $values, $sel_options);
  844. if (count($results) == 0) {
  845. $ins_options = array('statement_name' => 'ins_featurerelationship_suojty');
  846. $success = tripal_core_chado_insert('feature_relationship', $values, $ins_options);
  847. if (!$success) {
  848. watchdog('T_fasta_loader', "Failed to add associate database accession '%accession' with feature",
  849. array('%accession' => $accession), WATCHDOG_ERROR);
  850. return 0;
  851. }
  852. }
  853. }
  854. }