Overview

Namespaces

  • cli_db
    • propel
      • map
      • om
  • cli_import
  • LoggedPDO
  • None
  • PHP
  • webservices
    • cart
    • combisearch
    • details
      • annotations
        • feature
    • graphs
      • barplot
      • genome
    • listing
    • queue

Classes

  • AbstractImporter
  • Importer_Annotations_Dbxref
  • Importer_Annotations_Description
  • Importer_Annotations_EC
  • Importer_Annotations_GO
  • Importer_Annotations_Interpro
  • Importer_Annotations_MapMan
  • Importer_Annotations_Repeatmasker
  • Importer_Differential_Expressions
  • Importer_Expressions
  • Importer_Sequence_Ids
  • Importer_Sequences_FASTA

Interfaces

  • Importer
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: namespace cli_import;
  4: 
  5: require_once SHARED . 'classes/CLI_Command.php';
  6: 
  7: interface Importer {
  8: 
  9:     /**
 10:      * executes import
 11:      * @global \PDO $db
 12:      * @param Array $options user-provided command-line options
 13:      * @return Array results
 14:      * @throws \Exception on error
 15:      * @throws \ErrorException on error
 16:      */
 17:     static function import($options);
 18: }
 19: 
 20: define('ERR_ILLEGAL_FILE_FORMAT', 'Unsupported file format. Please recheck');
 21: 
 22: /**
 23:  * abstract class as parent for tbro-import commands.
 24:  * implements standard behavior for command-line interaction. final importers only need to implement the import method
 25:  */
 26: abstract class AbstractImporter implements \CLI_Command, Importer {
 27: 
 28:     /**
 29:      * adds command line options.
 30:      * --help
 31:      * --organism_id
 32:      * --release
 33:      * argument "files"
 34:      * @param \Console_CommandLine $parser
 35:      * @return \Console_CommandLine_Command
 36:      */
 37:     public static function CLI_getCommand(\Console_CommandLine $parser) {
 38:         $command = $parser->addCommand(call_user_func(array(get_called_class(), 'CLI_commandName')), array(
 39:             'description' => call_user_func(array(get_called_class(), 'CLI_commandDescription'))
 40:         ));
 41: 
 42:         $command->add_help_option = false;
 43: 
 44:         $command->addOption('help', array(
 45:             'short_name' => '-h',
 46:             'long_name' => '--help',
 47:             'action' => 'Help',
 48:             'action_params' => array('class' => get_called_class()),
 49:             'description' => 'show this help message and exit'
 50:         ));
 51: 
 52:         $command->addOption('organism_id', array(
 53:             'short_name' => '-o',
 54:             'long_name' => '--organism_id',
 55:             'description' => 'id of the organism this import is for'
 56:         ));
 57:         $command->addOption('release', array(
 58:             'short_name' => '-r',
 59:             'long_name' => '--release',
 60:             'description' => 'this will be used as prefix for all uniquenames and displayed in the "dataset" dropdown'
 61:         ));
 62: 
 63:         $command->addArgument('files', array(
 64:             'multiple' => true,
 65:             'description' => 'files to be imported'
 66:         ));
 67: 
 68:         return $command;
 69:     }
 70: 
 71:     /**
 72:      * check if all required options have been set
 73:      * @param \Console_CommandLine_Result $command
 74:      * @throws \Exception on missing argument
 75:      */
 76:     public static function CLI_checkRequiredOpts(\Console_CommandLine_Result $command) {
 77:         $options = $command->options;
 78: 
 79:         self::dieOnMissingArg($options, 'organism_id');
 80:         self::dieOnMissingArg($options, 'release');
 81:     }
 82: 
 83:     /**
 84:      * execute command. sets constants and calls self::import($options)
 85:      * @global \PDO $db
 86:      * @param \Console_CommandLine_Result $command
 87:      * @param \Console_CommandLine $parser
 88:      */
 89:     static function CLI_execute(\Console_CommandLine_Result $command, \Console_CommandLine $parser) {
 90:         //set constants
 91:         define('LINES_IMPORTED', 'datasets_imported');
 92:         define('DB_ORGANISM_ID', $command->options['organism_id']);
 93:         define('IMPORT_PREFIX', $command->options['release']);
 94:         //get some values for quick access
 95:         $command_name = call_user_func(array(get_called_class(), 'CLI_commandName'));
 96:         $command_options = $command->options;
 97:         $command_args = $command->args;
 98: 
 99:         //for each file argument
100:         foreach ($command_args['files'] as $filename) {
101:             //print header
102:             printf("importing %s as %s\n", $filename, $command_name);
103:             //call self::import
104:             $ret_table = call_user_func(array(get_called_class(), 'import'), array_merge($command_options, array('file' => $filename)));
105:             //output results from import
106:             $tbl = new Console_Table();
107:             foreach ($ret_table as $key => $value)
108:                 $tbl->addRow(array($key, $value));
109:             echo $tbl->getTable();
110:         }
111:         //update materialized views for statistics etc.
112:         echo "\nupdating materialized views...";
113:         global $db;
114:         $db->query('SELECT update_materialized_views()');
115:         echo " done!\n";
116:     }
117: 
118:     static function preCommitMsg() {
119:         echo "\ncommiting changes to database. this may take a moment.\n";
120:     }
121: 
122:     /**
123:      * Log instance for output
124:      * @var \Log 
125:      */
126:     public static $log;
127: 
128:     /**
129:      * progress bar instance
130:      * @var \Console_ProgressBar
131:      */
132:     private static $bar;
133: 
134:     /**
135:      * progress bar will be updated every $announce_steps imported lines
136:      * @var int 
137:      */
138:     private static $announce_steps = 100;
139: 
140:     /**
141:      * progress bar style
142:      * @var String 
143:      */
144:     private static $barstr = '[%bar%] %fraction%(%percent%), elapsed: %elapsed% , remaining est.: %estimate%';
145: 
146:     /**
147:      * updates progress bar target count
148:      * @param int $count
149:      */
150:     protected static function setLineCount($count) {
151:         $width_exec = exec('tput cols 2>&1');
152:         $width = is_int($width_exec) && $width_exec > 0 ? $width_exec : 120;
153: 
154:         if (self::$bar == null) {
155:             self::$bar = new \Console_ProgressBar(self::$barstr, '=>', ' ', $width, $count);
156:         } else {
157:             self::$bar->reset(self::$barstr, '=>', ' ', $width, $count);
158:         }
159:     }
160: 
161:     /**
162:      * updates bar progress
163:      * @param int $current_count
164:      * @return nothing
165:      */
166:     protected static function updateProgress($current_count) {
167:         if ($current_count % self::$announce_steps != 0)
168:             return;
169:         self::$bar->update($current_count);
170:     }
171: 
172:     /**
173:      * throws exception if required option is not set
174:      * @param Array $options user-specified command-line options
175:      * @param String $argname name of option
176:      * @throws \Exception
177:      */
178:     protected static function dieOnMissingArg($options, $argname) {
179:         if (!isset($options[$argname]))
180:             throw new \Exception(sprintf('option --%s has to be set', $argname));
181:     }
182: 
183: }
184: 
185: //set importer Log instance
186: AbstractImporter::$log = \Log::factory('console', '', 'Importer');
187: ?>
188: 
tbro API documentation generated by ApiGen 2.8.0