1: <?php
2:
3: namespace cli_import;
4:
5: require_once ROOT . 'classes/AbstractImporter.php';
6:
7: 8: 9:
10: class Importer_Sequence_Ids extends AbstractImporter {
11:
12: 13: 14: 15: 16: 17: 18:
19: public static function get_import_dbxref() {
20: global $db;
21: $stm_get_import_prefix_id = $db->prepare('SELECT get_or_insert_dbxref(?, ?)');
22: $stm_get_import_prefix_id->execute(array(DB_NAME_IMPORTS, IMPORT_PREFIX));
23: $import_prefix_id = $stm_get_import_prefix_id->fetchColumn();
24: unset($stm_get_import_prefix_id);
25:
26: return $import_prefix_id;
27: }
28:
29: 30: 31:
32: static function import($options) {
33: $filename = $options['file'];
34: $file_type = $options['file_type'];
35:
36: $lines_total = trim(`wc -l $filename | cut -d' ' -f1`);
37: self::setLineCount($lines_total);
38:
39: global $db;
40: if (false)
41: $db = new PDO();
42:
43: $lines_imported = 0;
44: $isoforms_added = 0;
45: $unigenes_added = 0;
46:
47:
48: $param_unigene_name = null;
49: $param_unigene_uniq = null;
50: $param_isoform_name = null;
51: $param_isoform_uniq = null;
52: $param_unigene_lastid = null;
53:
54: try {
55: $db->beginTransaction();
56: $import_prefix_id = self::get_import_dbxref();
57:
58:
59: $stm_link_organism_import_dbxref = $db->prepare("SELECT * FROM organism_dbxref WHERE organism_id=? AND dbxref_id=?");
60: $stm_link_organism_import_dbxref->execute(array(DB_ORGANISM_ID, $import_prefix_id));
61: if ($stm_link_organism_import_dbxref->rowCount() == 0)
62: $db->prepare("INSERT INTO organism_dbxref (organism_id, dbxref_id) VALUES (?, ?)")->execute(array(DB_ORGANISM_ID, $import_prefix_id));
63: unset($stm_link_organism_import_dbxref);
64:
65:
66: $stm_sel_unigene_byUniquename = $db->prepare('SELECT feature_id FROM feature WHERE uniquename=:uniquename AND organism_id=:organism_id');
67: $stm_sel_unigene_byUniquename->bindValue('organism_id', DB_ORGANISM_ID, PDO::PARAM_INT);
68: $stm_sel_unigene_byUniquename->bindParam('uniquename', $param_unigene_uniq);
69:
70:
71:
72: $stm_ins_unigene = $db->prepare('INSERT INTO feature (name, uniquename, type_id, organism_id, dbxref_id) VALUES (:name, :uniquename, :type_id, :organism_id, :dbxref_id) RETURNING feature_id');
73: $stm_ins_unigene->bindValue('type_id', CV_UNIGENE, PDO::PARAM_INT);
74: $stm_ins_unigene->bindValue('organism_id', DB_ORGANISM_ID, PDO::PARAM_INT);
75: $stm_ins_unigene->bindParam('name', $param_unigene_name, PDO::PARAM_STR);
76: $stm_ins_unigene->bindParam('uniquename', $param_unigene_uniq);
77: $stm_ins_unigene->bindValue('dbxref_id', $import_prefix_id, PDO::PARAM_INT);
78:
79:
80: $stm_ins_isoform = $db->prepare('INSERT INTO feature (name, uniquename, type_id, organism_id, dbxref_id) VALUES (:name, :uniquename, :type_id, :organism_id, :dbxref_id)');
81: $stm_ins_isoform->bindValue('type_id', CV_ISOFORM, PDO::PARAM_INT);
82: $stm_ins_isoform->bindValue('organism_id', DB_ORGANISM_ID, PDO::PARAM_INT);
83: $stm_ins_isoform->bindParam('name', $param_isoform_name, PDO::PARAM_STR);
84: $stm_ins_isoform->bindParam('uniquename', $param_isoform_uniq, PDO::PARAM_STR);
85: $stm_ins_isoform->bindValue('dbxref_id', $import_prefix_id, PDO::PARAM_INT);
86:
87:
88: $stm_ins_feature_rel = $db->prepare('INSERT INTO feature_relationship (subject_id, type_id, object_id) VALUES (currval(\'feature_feature_id_seq\'), :type_id, :parent)');
89: $stm_ins_feature_rel->bindValue('type_id', CV_RELATIONSHIP_UNIGENE_ISOFORM, PDO::PARAM_INT);
90: $stm_ins_feature_rel->bindParam('parent', $param_unigene_lastid, PDO::PARAM_INT);
91:
92:
93: $file = fopen($filename, 'r');
94:
95: $last_unigene = "";
96: while (($line = fgetcsv($file, 0, "\t")) !== false) {
97: switch ($file_type) {
98: case 'only_isoforms':
99: $param_isoform_name = $line[0];
100: $param_isoform_uniq = IMPORT_PREFIX . "_" . $param_isoform_name;
101: $stm_ins_isoform->execute();
102: $isoforms_added++;
103: break;
104:
105: case 'only_unigenes':
106: $param_unigene_name = $line[0];
107: $param_unigene_uniq = IMPORT_PREFIX . "_" . $param_unigene_name;
108: $stm_ins_unigene->execute();
109: $unigenes_added++;
110: break;
111:
112: case 'map':
113:
114: list($param_unigene_name, $param_isoform_name) = $line;
115:
116:
117: if ($last_unigene != $param_unigene_name) {
118: $param_unigene_uniq = IMPORT_PREFIX . "_" . $param_unigene_name;
119:
120: $stm_sel_unigene_byUniquename->execute();
121: $param_unigene_lastid = $stm_sel_unigene_byUniquename->fetchColumn();
122:
123: if (!$param_unigene_lastid) {
124: $stm_ins_unigene->execute();
125: $unigenes_added++;
126:
127: $param_unigene_lastid = $stm_ins_unigene->fetchColumn();
128: }
129:
130: $last_unigene = $param_unigene_name;
131: }
132:
133: if (!empty($param_isoform_name)) {
134:
135: $param_isoform_uniq = IMPORT_PREFIX . "_" . $param_isoform_name;
136: $stm_ins_isoform->execute();
137: $isoforms_added++;
138:
139:
140: $stm_ins_feature_rel->execute();
141: }
142: break;
143: }
144:
145: self::updateProgress(++$lines_imported);
146: }
147: self::preCommitMsg();
148: if (!$db->commit()) {
149: $err = $db->errorInfo();
150: throw new ErrorException($err[2], ERRCODE_TRANSACTION_NOT_COMPLETED, 1);
151: }
152: } catch (\Exception $error) {
153: $db->rollback();
154: throw $error;
155: }
156: return array(LINES_IMPORTED => $lines_imported, 'unigenes_added' => $unigenes_added, 'isoforms_added' => $isoforms_added);
157: }
158:
159: 160: 161:
162: public static function CLI_getCommand(\Console_CommandLine $parser) {
163: $command = parent::CLI_getCommand($parser);
164: $command->addOption('file_type', array(
165: 'short_name' => '-t',
166: 'long_name' => '--file_type',
167: 'description' => "file format description. one of ('map', 'only_isoforms', 'only_unigenes'). defaults to 'map'",
168: 'choices' => array('map', 'only_isoforms', 'only_unigenes'),
169: 'default' => 'map'
170: ));
171: return $command;
172: }
173:
174: 175: 176:
177: public static function CLI_commandName() {
178: return 'sequence_ids';
179: }
180:
181: 182: 183:
184: public static function CLI_commandDescription() {
185: return "Sequence ID Importer";
186: }
187:
188: 189: 190:
191: public static function CLI_longHelp() {
192: return <<<EOF
193:
194: This command creates the database features all other commands work with.
195: You can either import a line-separated file with just the sequence ids you use throughout the files that will be imported or you can import a map, which maps isoforms on unigenes.
196:
197: File format "only_isoforms":
198: isoform1
199: isoform2
200: isoform3
201:
202: File format "only_unigenes":
203: unigene1
204: unigene2
205: unigene3
206:
207: File Format "map" (tab-separated):
208: unigene1 isoform1
209: unigene1 isoform2
210: unigene2 isoform3
211: unigene3 isoform4
212: EOF;
213: }
214:
215: }
216:
217: ?>
218: