1: <?php
2:
3: namespace cli_import;
4:
5: require_once ROOT . 'classes/Importer_Annotations_Dbxref.php';
6:
7: class Importer_Annotations_MapMan extends Importer_Annotations_Dbxref {
8:
9: 10: 11:
12: public static function CLI_commandName() {
13: return "annotation_mapman";
14: }
15:
16: 17: 18:
19: public static function CLI_commandDescription() {
20: return "import MapMan annotations";
21: }
22:
23: 24: 25: 26: 27: 28:
29: public static function get_or_create_DB($dbname) {
30: global $db;
31: $stm = $db->prepare(<<<EOF
32: WITH new_row AS (
33: INSERT INTO db (name) SELECT ? WHERE NOT EXISTS (SELECT 1 FROM db WHERE name = ?) RETURNING db_id
34: )
35: SELECT db_id FROM new_row
36: UNION
37: SELECT db_id FROM db WHERE name = ?;
38: EOF
39: );
40: $stm->execute(array($dbname, $dbname, $dbname));
41: return $stm->fetchColumn();
42: }
43:
44: /**
45: * imported Mapman BINs will be linked as dbxref to a DB with this name
46: * @var String
47: */
48: static $db_name = 'MapMan';
49:
50: /**
51: * @inheritDoc
52: */
53: public static function import($options) {
54: $filename = $options['file'];
55: $lines_total = trim(`wc -l $filename | cut -d' ' -f1`);
56: self::setLineCount($lines_total);
57: $lines_imported = 0;
58: $lines_skipped = 0;
59:
60: global $db;
61: try {
62: $db->beginTransaction();
63: $import_prefix_id = Importer_Sequence_Ids::get_import_dbxref();
64:
65: self::get_or_create_DB(self::$db_name);
66:
67:
68: /**
69: * get parent feature id
70: * parameters: :object_name, :organism_id, :dbxref_id
71: */
72: $stm_get_parentfeature = $db->prepare('SELECT feature_id FROM feature WHERE name=:object_name AND organism_id=:organism_id AND dbxref_id=:dbxref_id');
73:
74: /**
75: * insert new feature for hit
76: * parameters: :name, :uniquename, :type_id, :organism_id, :dbxref_id
77: * returns: feature_id
78: */
79: $stm_insert_feature = $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");
80:
81: /**
82: * link new feature with parent feature
83: * parameters: :subject_id, :type_id, :object_id
84: */
85: $stm_link_feature = $db->prepare("INSERT INTO feature_relationship (subject_id, type_id, object_id) VALUES (:subject_id, :type_id, :object_id)");
86:
87: /**
88: * add textual annoation
89: * parameters: feature_id, type_id, value
90: */
91: $stm_insert_featureprop = $db->prepare("INSERT INTO featureprop (feature_id, type_id, value) VALUES (?,?,?)");
92:
93: /**
94: * link new feature to dbxref
95: * parameters: feature_id, cvterm_id
96: */
97: $stm_link_dbxref = $db->prepare('INSERT INTO feature_dbxref (feature_id, dbxref_id) VALUES (?,?)');
98:
99: /**
100: * get dbxref id. if non-existant, create
101: * parameters: :dbname, :accession
102: * returns: dbxref_id
103: */
104: $stm_try_insert_dbxref_id = $db->prepare("SELECT * FROM get_or_insert_dbxref(:dbname, :accession)");
105:
106: /**
107: * get cvterm_id. if non-existant, create
108: * parameters: name, definition, dbxref_id dbxref_id, dbxref_id
109: * returns: cvterm_id
110: */
111: $stm_try_insert_cvterm = $db->prepare(<<<EOF
112: WITH new_row AS (
113: INSERT INTO cvterm (name, definition, cv_id, dbxref_id) SELECT ?,?,(SELECT cv_id FROM cv WHERE name='local'),? WHERE NOT EXISTS (SELECT 1 FROM cvterm WHERE dbxref_id = ?) RETURNING cvterm_id
114: )
115: SELECT cvterm_id FROM new_row
116: UNION
117: SELECT cvterm_id FROM cvterm WHERE dbxref_id = ?;
118: EOF
119: );
120: /**
121: * insert cvtermprop if non-existant with these values
122: * parameters: cvterm_id, type_id, value, cvterm_id, type_id, cvterm_id, type_id, value
123: */
124: $stm_try_insert_cvtermprop = $db->prepare(
125: <<<EOF
126: INSERT INTO cvtermprop (cvterm_id, type_id, value, rank) SELECT ?,?,?, COALESCE((SELECT rank+1 FROM cvtermprop WHERE cvterm_id=? AND type_id=? ORDER BY rank DESC LIMIT 1), 0)
127: WHERE NOT EXISTS (SELECT 1 FROM cvtermprop WHERE cvterm_id = ? AND type_id=? AND value=? )
128:
129: EOF
130: );
131: $dbxrefs = array();
132: $cvterms = array();
133:
134: $file = fopen($filename, 'r');
135: //skip header line
136: fgets($file);
137: while (($line = fgetcsv($file, 0, "\t")) != false) {
138: //if..elseif..else: check which section we are in
139: // header, looks like <BINCODE>\t<H_DESC>
140: if (count($line) == 2) {
141: $stm_try_insert_dbxref_id->execute(array(
142: // parameters: :dbname, :accession
143: // returns: dbxref_id
144: self::$db_name,
145: $line[0]
146: ));
147: $dbxref_id = $stm_try_insert_dbxref_id->fetchColumn();
148: $dbxrefs[$line[0]] = $dbxref_id;
149: $stm_try_insert_cvterm->execute(array(
150: // parameters: name, definition, dbxref_id dbxref_id, dbxref_id
151: // returns: cvterm_id
152: $line[0],
153: $line[1],
154: $dbxref_id,
155: $dbxref_id,
156: $dbxref_id
157: ));
158: $cvterms[$line[0]] = $stm_try_insert_cvterm->fetchColumn();
159: } else if (count($line) == 5) {
160: //mapping, looks like <BINCODE>, <H_DESC>, <srcfeature_name>, <feature_description>, "T"
161: if ($line[4] == 'T') {
162: $stm_get_parentfeature->execute(array(
163: ':object_name' => $line[2],
164: ':organism_id' => DB_ORGANISM_ID,
165: ':dbxref_id' => $import_prefix_id
166: ));
167: if ($stm_get_parentfeature->rowCount() == 0) {
168: self::updateProgress($lines_imported + (++$lines_skipped));
169: continue;
170: }
171: $parent_id = $stm_get_parentfeature->fetchColumn();
172: $stm_insert_feature->execute(array(
173: ':name' => $line[2] . '_MapMan_' . $line[0],
174: ':uniquename' => IMPORT_PREFIX . '_' . $line[2] . '_MapMan_' . $line[0],
175: ':type_id' => CV_ANNOTATION_MAPMAN_FEATURE,
176: ':organism_id' => DB_ORGANISM_ID,
177: ':dbxref_id' => $import_prefix_id
178: ));
179: $feature_id = $stm_insert_feature->fetchColumn();
180: $stm_link_feature->execute(array(
181: ':subject_id' => $feature_id,
182: ':type_id' => CV_ANNOTATION_MAPMAN_RELATIONSHIP,
183: ':object_id' => $parent_id
184: ));
185: $stm_insert_featureprop->execute(array(
186: //parameters: feature_id, type_id, value
187: $feature_id,
188: CV_ANNOTATION_MAPMAN_PROP,
189: $line[3]
190: ));
191: $stm_link_dbxref->execute(array(
192: // parameters: feature_id, cvterm_id
193: $feature_id,
194: $dbxrefs[$line[0]]
195: ));
196: } else
197: //footer, looks like: <BINCODE>, <H_DESC>, <CHEM>, <C_DESC>, "M"
198: if ($line[4] == 'M') {
199: $val = sprintf("%s\t%s", $line[2], $line[3]);
200: $cvterm_id = $cvterms[$line[0]];
201: $stm_try_insert_cvtermprop->execute(array(
202: //cvterm_id, type_id, value, cvterm_id, type_id, cvterm_id, type_id, value
203: $cvterm_id,
204: CV_ANNOTATION_MAPMAN_PROP,
205: $val,
206: $cvterm_id,
207: CV_ANNOTATION_MAPMAN_PROP,
208: $cvterm_id,
209: CV_ANNOTATION_MAPMAN_PROP,
210: $val
211: ));
212: }
213: }
214: self::updateProgress((++$lines_imported) + $lines_skipped);
215: }
216:
217: self::preCommitMsg();
218: if (!$db->commit()) {
219: $err = $db->errorInfo();
220: throw new ErrorException($err[2], ERRCODE_TRANSACTION_NOT_COMPLETED, 1);
221: }
222: } catch (\Exception $error) {
223: $db->rollback();
224: throw $error;
225: }
226: return array(LINES_IMPORTED => $lines_imported);
227: }
228:
229: /**
230: * @inheritDoc
231: */
232: public static function CLI_longHelp() {
233: return <<<EOF
234: \033[0;31mThis import requires a successful Sequence ID Import!\033[0m
235: EOF;
236: }
237:
238: }
239:
240: ?>
241: