1: <?php
2:
3: namespace cli_import;
4:
5: require_once ROOT . 'classes/AbstractImporter.php';
6:
7: class Importer_Expressions extends AbstractImporter {
8:
9: 10: 11: 12: 13:
14: static function import($options) {
15:
16: $filename = $options['file'];
17: $quantification_id = $options['quantification_id'];
18: $analysis_id = $options['analysis_id'];
19:
20: $lines_total = trim(`wc -l $filename | cut -d' ' -f1`) - 1;
21: self::setLineCount($lines_total);
22:
23: global $db;
24:
25: $lines_imported = 0;
26: $inserts_executed = 0;
27:
28: try {
29:
30: $db->beginTransaction();
31:
32:
33: $param_feature_id = null;
34: $param_value = null;
35: $param_biomaterial_id = null;
36:
37:
38: $statement_insert_quant = $db->prepare('INSERT INTO expressionresult (feature_id, quantification_id, biomaterial_id, analysis_id, value) '
39: . 'VALUES (:feature_id, :quantification_id, :biomaterial_id, :analysis_id, :value)');
40: $statement_insert_quant->bindParam('feature_id', $param_feature_id, PDO::PARAM_STR);
41: $statement_insert_quant->bindParam('value', $param_value, PDO::PARAM_STR);
42: $statement_insert_quant->bindParam('biomaterial_id', $param_biomaterial_id, PDO::PARAM_INT);
43: $statement_insert_quant->bindValue('quantification_id', $quantification_id, PDO::PARAM_INT);
44: $statement_insert_quant->bindValue('analysis_id', $analysis_id, PDO::PARAM_INT);
45:
46:
47: $statement_get_biomaterial_id = $db->prepare('SELECT biomaterial_id FROM biomaterial WHERE name=? LIMIT 1');
48:
49:
50: $statement_get_feature_id = $db->prepare('SELECT feature_id FROM feature WHERE uniquename=? AND organism_id=? LIMIT 1');
51:
52: $file = fopen($filename, 'r');
53: if (feof($file))
54: return;
55:
56: $biomaterial_names = fgetcsv($file, 0, "\t");
57: $biomaterial_ids = array();
58: for ($i = 1; $i < count($biomaterial_names); $i++) {
59: $statement_get_biomaterial_id->execute(array($biomaterial_names[$i]));
60: $biomaterial_ids[$i] = $statement_get_biomaterial_id->fetchColumn();
61: if (!$biomaterial_ids[$i]) {
62: throw new ErrorException('Biomaterial with this name not defined');
63: }
64: }
65:
66:
67: while (($line = fgetcsv($file, 0, "\t")) !== false) {
68: if (count($line) == 0)
69: continue;
70:
71: $feature_uniquename = IMPORT_PREFIX . "_" . $line[0];
72: $statement_get_feature_id->execute(array($feature_uniquename, DB_ORGANISM_ID));
73: $param_feature_id = $statement_get_feature_id->fetchColumn();
74:
75:
76: for ($i = 1; $i < count($line); $i++) {
77: $param_value = $line[$i];
78: $param_biomaterial_id = $biomaterial_ids[$i];
79: $statement_insert_quant->execute();
80: $inserts_executed++;
81: }
82:
83: self::updateProgress(++$lines_imported);
84: }
85:
86: self::preCommitMsg();
87: if (!$db->commit()) {
88: $err = $db->errorInfo();
89: throw new ErrorException($err[2], ERRCODE_TRANSACTION_NOT_COMPLETED, 1);
90: }
91: } catch (\Exception $error) {
92: $db->rollback();
93: throw $error;
94: }
95: return array(LINES_IMPORTED => $lines_imported, 'inserts executed' => $inserts_executed);
96: }
97:
98: 99: 100:
101: public static function CLI_getCommand(\Console_CommandLine $parser) {
102: $command = parent::CLI_getCommand($parser);
103: $command->addOption('quantification_id', array(
104: 'short_name' => '-q',
105: 'long_name' => '--quantification_id',
106: 'description' => 'quantification id'
107: ));
108:
109: $command->addOption('analysis_id', array(
110: 'short_name' => '-a',
111: 'long_name' => '--analysis_id',
112: 'description' => 'analysis_id.'
113: ));
114: return $command;
115: }
116:
117: 118: 119:
120: public static function CLI_checkRequiredOpts(\Console_CommandLine_Result $command) {
121: parent::CLI_checkRequiredOpts($command);
122: $options = $command->options;
123: AbstractImporter::dieOnMissingArg($options, 'quantification_id');
124: AbstractImporter::dieOnMissingArg($options, 'analysis_id');
125: }
126:
127: 128: 129:
130: public static function CLI_commandDescription() {
131: return "Importer for expression result \"count matrix\" files";
132: }
133:
134: 135: 136:
137: public static function CLI_commandName() {
138: return 'expressions';
139: }
140:
141: 142: 143:
144: public static function CLI_longHelp() {
145: return <<<EOF
146:
147: Imports output files from Anna-Lena Keller's "aggregator_CountMat.pl" script.
148:
149: File format looks like:
150: ID flower_L1 flower_L2 flower_L3 coronatin_L1 coronatin_L2 coronatin_L3
151: comp234711_c0_seq9 21.93 11.26 8.83 2.40 0.00 2.25
152:
153: The labels in the header have to match the condition sample names in the database
154: \033[0;31mThis import requires a successful Sequence ID Import!\033[0m
155: EOF;
156: }
157:
158: }
159:
160: ?>
161: