1: <?php
2:
3: namespace cli_import;
4:
5: require_once ROOT . 'classes/AbstractImporter.php';
6:
7: 8: 9:
10: class Importer_Differential_Expressions extends AbstractImporter {
11:
12: 13: 14: 15: 16:
17: static function convertDbl(&$value, $key) {
18: if ($value == '-Inf')
19: $value = '-Infinity';
20: else if ($value == 'Inf')
21: $value = 'Infinity';
22: else if ($value == 'NA')
23: $value = 'NaN';
24: else if (floatval($value) > 0 && floatval($value) < 1e-307) {
25: $value = 0;
26: }
27: }
28:
29: 30: 31:
32: static function import($options) {
33:
34: $filename = $options['file'];
35: $analysis_id = $options['analysis_id'];
36: $biomaterial_parentA_name = $options['conditionGroupA'];
37: $biomaterial_parentB_name = $options['conditionGroupB'];
38:
39: $lines_total = trim(`wc -l $filename | cut -d' ' -f1`);
40: self::setLineCount($lines_total);
41:
42: global $db;
43: $lines_imported = 0;
44:
45: $lines_skipped = 0;
46:
47: if (false)
48: $db = new PDO ();
49:
50: try {
51: $db->beginTransaction();
52:
53:
54: $statement_get_biomaterial_id = $db->prepare('SELECT b.biomaterial_id, bp.value AS type FROM biomaterial b JOIN biomaterialprop bp ON (b.biomaterial_id = bp.biomaterial_id) WHERE b.name=:name AND bp.type_id = ' . CV_BIOMATERIAL_TYPE . ' LIMIT 1');
55: $statement_get_biomaterial_id->bindValue('name', $biomaterial_parentA_name);
56: $statement_get_biomaterial_id->execute();
57: $rowa = $statement_get_biomaterial_id->fetch(\PDO::FETCH_ASSOC);
58: if ($statement_get_biomaterial_id->rowCount() == 0) {
59: throw new ErrorException(sprintf('Biomaterial with this name not defined (%s)', $biomaterial_parentA_name));
60: }
61:
62: if ($rowa['type'] != 'condition') {
63: throw new ErrorException(sprintf('This biomaterial is not of type condition! (%s)', $biomaterial_parentA_name));
64: }
65: $biomaterial_parentA_id = $rowa['biomaterial_id'];
66:
67:
68: $statement_get_biomaterial_id->bindValue('name', $biomaterial_parentB_name);
69: $statement_get_biomaterial_id->execute();
70: $rowb = $statement_get_biomaterial_id->fetch(\PDO::FETCH_ASSOC);
71: if ($statement_get_biomaterial_id->rowCount() == 0) {
72: throw new ErrorException(sprintf('Biomaterial with this name not defined (%s)', $biomaterial_parentB_name));
73: }
74:
75: if ($rowb['type'] != 'condition') {
76: throw new ErrorException(sprintf('This biomaterial is not of type condition! (%s)', $biomaterial_parentB_name));
77: }
78: $biomaterial_parentB_id = $rowb['biomaterial_id'];
79:
80:
81:
82: $statement_test_biomaterial_children = $db->prepare('SELECT biomaterial_relationship_id FROM biomaterial_relationship WHERE object_id=:parent LIMIT 1');
83: $statement_test_biomaterial_children->bindValue('parent', $biomaterial_parentA_id);
84: $statement_test_biomaterial_children->execute();
85: if (!($statement_test_biomaterial_children->fetchColumn())) {
86: throw new ErrorException(sprintf('Biomaterial has no children (%s)', $biomaterial_parentA_name));
87: }
88:
89:
90: $statement_test_biomaterial_children->bindValue('parent', $biomaterial_parentB_id);
91: $statement_test_biomaterial_children->execute();
92: if (!($statement_test_biomaterial_children->fetchColumn())) {
93: throw new ErrorException(sprintf('Biomaterial has no children (%s)', $biomaterial_parentB_name));
94: }
95:
96:
97: $dummy = null;
98: $feature_name = null;
99: $param_baseMean = null;
100: $param_baseMeanA = null;
101: $param_baseMeanB = null;
102: $param_foldChange = null;
103: $param_log2foldChange = null;
104: $param_pval = null;
105: $param_pvaladj = null;
106: $param_feature_uniquename = null;
107:
108:
109: $query_insert_diffexp = <<<EOF
110: INSERT INTO diffexpresult(analysis_id, feature_id, biomateriala_id, biomaterialb_id, baseMean, baseMeanA, baseMeanB, foldChange, log2foldChange, pval, pvaladj)
111: SELECT :analysis_id, feature_id, :biomaterialA_id, :biomaterialB_id, :baseMean, :baseMeanA, :baseMeanB, :foldChange, :log2foldChange, :pval, :pvaladj
112: FROM feature WHERE uniquename = :feature_uniquename AND organism_id = :organism
113: EOF;
114:
115: $statement_insert_diffexp = $db->prepare($query_insert_diffexp);
116: $statement_insert_diffexp->bindValue('analysis_id', $analysis_id, PDO::PARAM_INT);
117: $statement_insert_diffexp->bindValue('biomaterialA_id', $biomaterial_parentA_id, PDO::PARAM_INT);
118: $statement_insert_diffexp->bindValue('biomaterialB_id', $biomaterial_parentB_id, PDO::PARAM_INT);
119: $statement_insert_diffexp->bindParam('baseMean', $param_baseMean, PDO::PARAM_STR);
120: $statement_insert_diffexp->bindParam('baseMeanA', $param_baseMeanA, PDO::PARAM_STR);
121: $statement_insert_diffexp->bindParam('baseMeanB', $param_baseMeanB, PDO::PARAM_STR);
122: $statement_insert_diffexp->bindParam('foldChange', $param_foldChange, PDO::PARAM_STR);
123: $statement_insert_diffexp->bindParam('log2foldChange', $param_log2foldChange, PDO::PARAM_STR);
124: $statement_insert_diffexp->bindParam('pval', $param_pval, PDO::PARAM_STR);
125: $statement_insert_diffexp->bindParam('pvaladj', $param_pvaladj, PDO::PARAM_STR);
126:
127: $statement_insert_diffexp->bindParam('feature_uniquename', $param_feature_uniquename, PDO::PARAM_STR);
128: $statement_insert_diffexp->bindValue('organism', DB_ORGANISM_ID, PDO::PARAM_INT);
129:
130:
131: $file = fopen($filename, 'r');
132: if (feof($file))
133: return;
134:
135: fgets($file);
136:
137: while (($line = fgetcsv($file, 0, ",")) !== false) {
138: array_walk($line, array('Importer_Differential_Expressions', 'convertDbl'));
139: list($dummy, $feature_name, $param_baseMean, $param_baseMeanA, $param_baseMeanB, $param_foldChange, $param_log2foldChange, $param_pval, $param_pvaladj) = $line;
140:
141: if ($feature_name == 'NaN') {
142: $lines_skipped++;
143: continue;
144: }
145:
146:
147: $param_feature_uniquename = IMPORT_PREFIX . "_" . $feature_name;
148: $statement_insert_diffexp->execute();
149:
150: $lines_feature_skipped += ($statement_insert_diffexp->rowCount() == 0) ? 1 : 0;
151: self::updateProgress(++$lines_imported);
152: }
153: self::preCommitMsg();
154: if (!$db->commit()) {
155: $err = $db->errorInfo();
156: throw new ErrorException($err[2], ERRCODE_TRANSACTION_NOT_COMPLETED, 1);
157: }
158: } catch (\Exception $error) {
159: $db->rollback();
160: throw $error;
161: }
162: return array(LINES_IMPORTED => $lines_imported, 'lines_featurenotfound_skipped' => $lines_feature_skipped, 'lines_NA_skipped' => $lines_skipped);
163: }
164:
165: 166: 167:
168: public static function CLI_getCommand(\Console_CommandLine $parser) {
169: $command = parent::CLI_getCommand($parser);
170:
171: $command->addOption('analysis_id', array(
172: 'short_name' => '-a',
173: 'long_name' => '--analysis_id',
174: 'description' => 'analysis id'
175: ));
176: $command->addOption('conditionGroupA', array(
177: 'short_name' => '-A',
178: 'long_name' => '--conditionA',
179: 'description' => 'condition A name'
180: ));
181: $command->addOption('conditionGroupB', array(
182: 'short_name' => '-B',
183: 'long_name' => '--conditionB',
184: 'description' => 'condition B name'
185: ));
186: return $command;
187: }
188:
189: 190: 191:
192: public static function CLI_checkRequiredOpts(\Console_CommandLine_Result $command) {
193: parent::CLI_checkRequiredOpts($command);
194: $options = $command->options;
195: AbstractImporter::dieOnMissingArg($options, 'analysis_id');
196: AbstractImporter::dieOnMissingArg($options, 'conditionGroupA');
197: AbstractImporter::dieOnMissingArg($options, 'conditionGroupB');
198: }
199:
200: 201: 202:
203: public static function CLI_commandDescription() {
204: return "Importer for differential expression results";
205: }
206:
207: 208: 209:
210: public static function CLI_commandName() {
211: return 'differential_expressions';
212: }
213:
214: 215: 216:
217: public static function CLI_longHelp() {
218: return <<<EOF
219:
220: File Format looks like this (\033[0;31mFirst line will be skipped\033[0m):
221: ,id,baseMean,baseMeanA,baseMeanB,foldChange,log2FoldChange,pval,padj
222: 6,comp230079_c0,249.687338527051,206.660251316251,292.714425737851,1.41640409257952,0.502232917392478,2.32555262831702e-08,9.65409100672613e-08
223: 9,comp234683_c0,1904.88401956508,1811.60920428892,1998.15883484125,1.10297454335664,0.141399493923902,0.000466092095479145,0.00137346251047776
224:
225: \033[0;31mThis import requires a successful Sequence ID Import!\033[0m
226: EOF;
227: }
228:
229: }
230:
231: ?>
232: