Make database for gffutils. 

    Parameters
    ----------
    gtf : str
        Path to Gencode gtf file.

    db : str
        Path to save database to. 

    Returns
    -------
    out_db : gffutils.FeatureDB
        gffutils feature database.
480751
def make_gffutils_db(gtf, db):
    
    import gffutils
    out_db = gffutils.create_db(gtf,
                                db,
                                keep_order=True,
                                infer_gene_extent=False)
    return out_db
462041
def attach_db(self, db):
        
        if db is not None:
            if isinstance(db, basestring):
                db = gffutils.FeatureDB(db)
            if not isinstance(db, gffutils.FeatureDB):
                raise ValueError(
                    "`db` must be a filename or a gffutils.FeatureDB")
        self._kwargs['db'] = db
        self.db = db
932710
def _dbsetup(self):
        
        self._dbconn = sqlite3.connect(self._db_file)
        # Create table for multiplicons
        sql = '''CREATE TABLE multiplicons
                 (id, genome_x, list_x, parent, genome_y, list_y, level,
                  number_of_anchorpoints, profile_length, begin_x, end_x,
                  begin_y, end_y, is_redundant)'''
        self._dbconn.execute(sql)
        # Create table for multiplicons ('order' appears to be reserved)
        sql = '''CREATE TABLE segments
                 (id, multiplicon, genome, list, first, last, ord)'''
        self._dbconn.execute(sql)
        self._dbconn.commit()
528458
def makeblastdb(self):
        
        # Iterate through the samples to set the bait file.
        for sample in self.runmetadata.samples:
            if sample.general.bestassemblyfile != 'NA':
                # Remove the file extension
                db = os.path.splitext(sample[self.analysistype].baitfile)[0]
                # Add '.nhr' for searching below
                nhr = '{}.nhr'.format(db)
                # Check for already existing database files
                if not os.path.isfile(str(nhr)):
                    # Create the databases
                    command = 'makeblastdb -in {} -parse_seqids -max_file_sz 2GB -dbtype nucl -out {}'\
                        .format(sample[self.analysistype].baitfile, db)
                    out, err = run_subprocess(command)
                    write_to_logfile(command,
                                     command,
                                     self.logfile, sample.general.logout, sample.general.logerr,
                                     sample[self.analysistype].logout, sample[self.analysistype].logerr)
                    write_to_logfile(out,
                                     err,
                                     self.logfile, sample.general.logout, sample.general.logerr,
                                     sample[self.analysistype].logout, sample[self.analysistype].logerr)
498675
def initDatabase(self, db_file):
        
        try:
            self.__f = io.open(db_file, "rb")
        except IOError, e:
            print "[Error]: ", e
            sys.exit()
602553
function _db(dbConfig, dataPath) {
  if (dbConfig.dialect === 'sqlite') {
    /* eslint-disable-next-line no-param-reassign */
    dbConfig.storage = resolve(dataPath, 'vapid.sqlite');
  }

  return new Database(dbConfig);
}
948389
def gtf_to_bed(gtf, alt_out_dir=None):
    
    out_file = os.path.splitext(gtf)[0] + '.bed'
    if file_exists(out_file):
        return out_file
    if not os.access(os.path.dirname(out_file), os.W_OK | os.X_OK):
        if not alt_out_dir:
            raise IOError('Cannot write transcript BED output file %s' % out_file)
        else:
            out_file = os.path.join(alt_out_dir, os.path.basename(out_file))
    with open(out_file, "w") as out_handle:
        db = get_gtf_db(gtf)
        for feature in db.features_of_type('transcript', order_by=("seqid", "start", "end")):
            chrom = feature.chrom
            start = feature.start
            end = feature.end
            attributes = feature.attributes.keys()
            strand = feature.strand
            name = (feature['gene_name'][0] if 'gene_name' in attributes else
                    feature['gene_id'][0])
            line = "\t".join([str(x) for x in [chrom, start, end, name, ".",
                                               strand]])
            out_handle.write(line + "\n")
    return out_file
157671
def get_gene_source_set(gtf):
    
    gene_to_source = {}
    db = get_gtf_db(gtf)
    for feature in complete_features(db):
        gene_id = feature['gene_id'][0]
        sources = gene_to_source.get(gene_id, set([])).union(set([feature.source]))
        gene_to_source[gene_id] = sources
    return gene_to_source
751401
def _run_genotype_gvcfs_genomicsdb(genomics_db, region, out_file, data):
    
    if not utils.file_exists(out_file):
        with file_transaction(data, out_file) as tx_out_file:
            broad_runner = broad.runner_from_config(data["config"])
            params = ["-T", "GenotypeGVCFs",
                      "--variant", "gendb://%s" % genomics_db,
                      "-R", dd.get_ref_file(data),
                      "--output", tx_out_file,
                      "-L", bamprep.region_to_gatk(region)]
            params += ["-ploidy", str(ploidy.get_ploidy([data], region))]
            # Avoid slow genotyping runtimes with improved quality score calculation in GATK4
            # https://gatkforums.broadinstitute.org/gatk/discussion/11471/performance-troubleshooting-tips-for-genotypegvcfs/p1
            params += ["--use-new-qual-calculator"]
            resources = config_utils.get_resources("gatk", data["config"])
            params += [str(x) for x in resources.get("options", [])]
            cores = dd.get_cores(data)
            memscale = {"magnitude": 0.9 * cores, "direction": "increase"} if cores > 1 else None
            broad_runner.run_gatk(params, memscale=memscale)
    return vcfutils.bgzip_and_index(out_file, data["config"])
833600
def database(self):
        
        import sqlite3
        try:
            os.remove('{}/metadatabase.sqlite'.format(self.reportpath))
        except OSError:
            pass
        # Set the name of the database
        db = sqlite3.connect('{}/metadatabase.sqlite'.format(self.reportpath))
        # Create a cursor to allow access to the database
        cursor = db.cursor()
        # Set up the db
        cursor.execute('''
          CREATE TABLE IF NOT EXISTS Samples (
            id     INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
            name   TEXT UNIQUE
          )
        ''')
        # Create a variable to store the names of the header values for each individual table
        # This will store a set of all the headers from all the strains, as there can be some variability present, as
        # not all analyses are available for all taxonomic groups
        columns = dict()
        for sample in self.metadata:
            # Create a metadata object to store the new tables
            data = MetadataObject()
            data.name = sample.name
            # Insert each strain name into the Samples table
            cursor.execute('''
              INSERT OR IGNORE INTO Samples (name)
              VALUES ( ? )
            ''', (sample.name, ))
            # Each header in the .json file represents a major category e.g. ARMI, GeneSeekr, commands, etc. and
            # will be made into a separate table
            for header in sample.datastore.items():
                # Allow for certain analyses, such as core genome, not being performed on all strains
                try:

                    # Key and value: data description and data value e.g. targets present: 1012, etc.
                    for key, value in sorted(header[1].datastore.items()):
                        # Only the values consisting of dictionaries are of interest
                        if type(value) == dict:
                            # Clean the column names so there are no issues entering names into the database
                            cleanedcolumn = self.columnclean(key)
                            # Set the table name
                            tablename = '{}_{}'.format(header[0].replace('.', '_'), cleanedcolumn)
                            # Create the table (if it doesn't already exist)
                            cursor.execute('''
                                          CREATE TABLE IF NOT EXISTS {} (
                                            sample_id INTEGER
                                          )
                                          '''.format(tablename))
                            # Add the attributes with the dictionaries (values) to the metadata object
                            setattr(data, tablename, GenObject(value))
                            for gene, result in sorted(value.items()):
                                # Add the data header to the dictionary
                                try:
                                    columns[tablename].add(gene)
                                # Initialise the dictionary the first time a table name is encountered
                                except KeyError:
                                    columns[tablename] = set()
                                    columns[tablename].add(str(gene))
                except (AttributeError, IndexError):
                    pass
            self.tabledata.append(data)
        # Iterate through the dictionary containing all the data headers
        for table, setofheaders in sorted(columns.items()):
            # Each header will be used as a column in the appropriate table
            for cleanedcolumn in sorted(setofheaders):
                # Alter the table by adding each header as a column
                cursor.execute('''
                  ALTER TABLE {}
                  ADD COLUMN {} TEXT
                '''.format(table, cleanedcolumn))
            # Iterate through the samples and pull out the data for each table/column
            # for sample in self.metadata:
            for sample in self.tabledata:
                # Find the id associated with each sample in the Sample table
                cursor.execute('''
                  SELECT id from Samples WHERE name=?
                ''', (sample.name,))
                sampleid = cursor.fetchone()[0]
                # Add the sample_id to the table
                cursor.execute('''
                  INSERT OR IGNORE INTO {}
                  (sample_id) VALUES ("{}")
                  '''.format(table, sampleid))
                # Add the data to the table
                try:
                    # Find the data for each table/column
                    for item in sorted(sample[table].datastore.items()):
                        # Clean the names
                        cleanedcolumn = self.columnclean(str(item[0]))
                        # Add the data to the column of the appropriate table,
                        # where the sample_id matches the current strain
                        cursor.execute('''
                          UPDATE {}
                          SET {} = ?
                          WHERE sample_id = {}
                          '''.format(table, cleanedcolumn, sampleid), (str(item[1]), ))
                except KeyError:
                    pass
        # Commit the changes to the database
        db.commit()
918707
def database_create(self, database, **params):
        
        binds = {}
        dbname = database
        for key, engine in self.keys_engines():
            if hasattr(database, '__call__'):
                dbname = database(engine)
            assert dbname, "Cannot create a database, no db name given"
            key = key if key else 'default'
            binds[key] = self._database_create(engine, dbname)
        return self.copy(binds)
834854
def createDatabase(self, name, **dbArgs) :
        ""
        dbArgs['name'] = name
        payload = json.dumps(dbArgs, default=str)
        url = self.URL + "/database"
        r = self.session.post(url, data = payload)
        data = r.json()
        if r.status_code == 201 and not data["error"] :
            db = Database(self, name)
            self.databases[name] = db
            return self.databases[name]
        else :
            raise CreationError(data["errorMessage"], r.content)
1138896
def tx2genefile(gtf, out_file=None, data=None, tsv=True, keep_version=False):
    
    if tsv:
        extension = ".tsv"
        sep = "\t"
    else:
        extension = ".csv"
        sep = ","
    if file_exists(out_file):
        return out_file
    with file_transaction(data, out_file) as tx_out_file:
        with open(tx_out_file, "w") as out_handle:
            for k, v in tx2genedict(gtf, keep_version).items():
                out_handle.write(sep.join([k, v]) + "\n")
    logger.info("tx2gene file %s created from %s." % (out_file, gtf))
    return out_file
265194
def split_gtf(gtf, sample_size=None, out_dir=None):
    
    if out_dir:
        part1_fn = os.path.basename(os.path.splitext(gtf)[0]) + ".part1.gtf"
        part2_fn = os.path.basename(os.path.splitext(gtf)[0]) + ".part2.gtf"
        part1 = os.path.join(out_dir, part1_fn)
        part2 = os.path.join(out_dir, part2_fn)
        if file_exists(part1) and file_exists(part2):
            return part1, part2
    else:
        part1 = tempfile.NamedTemporaryFile(delete=False, suffix=".part1.gtf").name
        part2 = tempfile.NamedTemporaryFile(delete=False, suffix=".part2.gtf").name

    db = get_gtf_db(gtf)
    gene_ids = set([x['gene_id'][0] for x in db.all_features()])
    if not sample_size or (sample_size and sample_size > len(gene_ids)):
        sample_size = len(gene_ids)
    gene_ids = set(random.sample(gene_ids, sample_size))
    part1_ids = set(random.sample(gene_ids, sample_size / 2))
    part2_ids = gene_ids.difference(part1_ids)
    with open(part1, "w") as part1_handle:
        for gene in part1_ids:
            for feature in db.children(gene):
                part1_handle.write(str(feature) + "\n")
    with open(part2, "w") as part2_handle:
        for gene in part2_ids:
            for feature in db.children(gene):
                part2_handle.write(str(feature) + "\n")
    return part1, part2
133294
def to_db(self, table, dtypes=None):
        
        if self._check_db() is False:
            return
        if table not in self.db.tables:
            self.db.create_table(table)
            self.info("Created table ", table)
        self.start("Saving data to database table " + table + " ...")
        recs = self.to_records_()
        self.insert(table, recs, dtypes)
        self.end("Data inserted in table", table)
414380
def univec(self, databasepath):
        
        logging.info('Downloading univec database')
        databasepath = self.create_database_folder(databasepath, 'univec')
        # Set the name of the output file
        outputfile = os.path.join(databasepath, 'UniVec_core.tfa')
        target_url = 'ftp://ftp.ncbi.nlm.nih.gov/pub/UniVec/UniVec_Core'
        self.database_download(output_file=outputfile,
                               target_url=target_url,
                               database_path=databasepath)
        # Create a copy of the file with a .fasta extension
        if os.path.isfile(outputfile):
            renamed = os.path.splitext(outputfile)[0] + '.fasta'
            shutil.copy(outputfile, renamed)
147895
def _create_memory_database_interface(self) -> GraphDatabaseInterface:
        
        Base = declarative_base()
        engine = sqlalchemy.create_engine("sqlite://", poolclass=StaticPool)
        Session = sessionmaker(bind=engine)

        dbi: GraphDatabaseInterface = create_graph_database_interface(
            sqlalchemy, Session(), Base, sqlalchemy.orm.relationship
        )

        Base.metadata.drop_all(engine)
        Base.metadata.create_all(engine)

        return dbi