Estimate the strains by running an EQL site response.

        This step was recommended in Section 8.3.1 of Zalachoris (2014).
547316
def _estimate_strains(self):
        
        eql = EquivalentLinearCalculator()
        eql(self._motion, self._profile, self._loc_input)
1075193
def get_strain_label(entry, viral=False):
    
    def get_strain(entry):
        strain = entry['infraspecific_name']
        if strain != '':
            strain = strain.split('=')[-1]
            return strain

        strain = entry['isolate']
        if strain != '':
            return strain

        if len(entry['organism_name'].split(' ')) > 2 and not viral:
            strain = ' '.join(entry['organism_name'].split(' ')[2:])
            return strain

        return entry['assembly_accession']

    def cleanup(strain):
        strain = strain.strip()
        strain = strain.replace(' ', '_')
        strain = strain.replace(';', '_')
        strain = strain.replace('/', '_')
        strain = strain.replace('\\', '_')
        return strain

    return cleanup(get_strain(entry))
39877
def sequence_prep(self):
        
        # Create a sorted list of all the FASTA files in the sequence path
        strains = sorted(glob(os.path.join(self.fastapath, '*.fa*'.format(self.fastapath))))
        for sample in strains:
            # Create the object
            metadata = MetadataObject()
            # Set the sample name to be the file name of the sequence by removing the path and file extension
            sample_name = os.path.splitext(os.path.basename(sample))[0]
            if sample_name in self.strainset:
                # Extract the OLNID from the dictionary using the SEQID
                samplename = self.straindict[sample_name]
                # samplename = sample_name
                # Set and create the output directory
                outputdir = os.path.join(self.path, samplename)
                make_path(outputdir)
                # Set the name of the JSON file
                json_metadata = os.path.join(outputdir, '{name}.json'.format(name=samplename))
                if not os.path.isfile(json_metadata):
                    # Create the name and output directory attributes
                    metadata.name = samplename
                    metadata.seqid = sample_name
                    metadata.outputdir = outputdir
                    metadata.jsonfile = json_metadata
                    # Set the name of the FASTA file to use in the analyses
                    metadata.bestassemblyfile = os.path.join(metadata.outputdir,
                                                             '{name}.fasta'.format(name=metadata.name))
                    # Symlink the original file to the output directory
                    relative_symlink(sample, outputdir, '{sn}.fasta'.format(sn=metadata.name))
                    # Associate the corresponding FASTQ files with the assembly
                    metadata.fastqfiles = sorted(glob(os.path.join(self.fastqpath,
                                                                   '{name}*.gz'.format(name=metadata.name))))
                    metadata.forward_fastq, metadata.reverse_fastq = metadata.fastqfiles
                    # Write the object to file
                    self.write_json(metadata)
                else:
                    metadata = self.read_json(json_metadata)
                # Add the metadata object to the list of objects
                self.metadata.append(metadata)
956157
def assessOligo(seq):
    '''
    

    '''
    hairpin_out = calcHairpin(seq)
    homodimer_out = calcHomodimer(seq)
    return (hairpin_out, homodimer_out)
1300738
def dataframe_setup(self):
        
        # Initialise a dictionary to store the sanitized headers and strings
        genesippr_dict = dict()
        # Try to open all the reports - use pandas to extract the results from any report that exists
        try:
            sippr_matrix = pd.read_csv(os.path.join(self.reportpath, 'genesippr.csv'),
                                       delimiter=',', index_col=0).T.to_dict()
        except FileNotFoundError:
            sippr_matrix = dict()
        try:
            conf_matrix = pd.read_csv(os.path.join(self.reportpath, 'confindr_report.csv'),
                                      delimiter=',', index_col=0).T.to_dict()
        except FileNotFoundError:
            conf_matrix = dict()
        try:
            gdcs_matrix = pd.read_csv(os.path.join(self.reportpath, 'GDCS.csv'),
                                      delimiter=',', index_col=0).T.to_dict()
        except FileNotFoundError:
            gdcs_matrix = dict()
        # Populate the header:sanitized string dictionary with results from all strains
        for sample in self.metadata:
            genesippr_dict[sample.name] = dict()
            try:
                genesippr_dict[sample.name]['eae'] = self.data_sanitise(sippr_matrix[sample.name]['eae'])
            except KeyError:
                genesippr_dict[sample.name]['eae'] = 0
            try:
                genesippr_dict[sample.name]['hlyAEc'] = self.data_sanitise(sippr_matrix[sample.name]['hlyAEc'])
            except KeyError:
                genesippr_dict[sample.name]['hlyAEc'] = 0
            try:
                genesippr_dict[sample.name]['VT1'] = self.data_sanitise(sippr_matrix[sample.name]['VT1'])
            except KeyError:
                genesippr_dict[sample.name]['VT1'] = 0
            try:
                genesippr_dict[sample.name]['VT2'] = self.data_sanitise(sippr_matrix[sample.name]['VT2'])
            except KeyError:
                genesippr_dict[sample.name]['VT2'] = 0
            try:
                genesippr_dict[sample.name]['hlyALm'] = self.data_sanitise(sippr_matrix[sample.name]['hlyALm'])
            except KeyError:
                genesippr_dict[sample.name]['hlyALm'] = 0
            try:
                genesippr_dict[sample.name]['IGS'] = self.data_sanitise(sippr_matrix[sample.name]['IGS'])
            except KeyError:
                genesippr_dict[sample.name]['IGS'] = 0
            try:
                genesippr_dict[sample.name]['inlJ'] = self.data_sanitise(sippr_matrix[sample.name]['inlJ'])
            except KeyError:
                genesippr_dict[sample.name]['inlJ'] = 0
            try:
                genesippr_dict[sample.name]['invA'] = self.data_sanitise(sippr_matrix[sample.name]['invA'])
            except KeyError:
                genesippr_dict[sample.name]['invA'] = 0
            try:
                genesippr_dict[sample.name]['stn'] = self.data_sanitise(sippr_matrix[sample.name]['stn'])
            except KeyError:
                genesippr_dict[sample.name]['stn'] = 0
            try:
                genesippr_dict[sample.name]['GDCS'] = self.data_sanitise(gdcs_matrix[sample.name]['Pass/Fail'],
                                                                         header='Pass/Fail')
            except KeyError:
                genesippr_dict[sample.name]['GDCS'] = 0
            try:
                genesippr_dict[sample.name]['Contamination'] = self.data_sanitise(
                    conf_matrix[sample.name]['ContamStatus'], header='ContamStatus')
            except KeyError:
                genesippr_dict[sample.name]['Contamination'] = 0
            try:
                genesippr_dict[sample.name]['Coverage'] = self.data_sanitise(
                    gdcs_matrix[sample.name]['MeanCoverage'], header='MeanCoverage')
            except KeyError:
                genesippr_dict[sample.name]['Coverage'] = 0
        # Create a report from the header: sanitized string dictionary to be used in the creation of the report image
        with open(self.image_report, 'w') as csv:
            data = '{}\n'.format(','.join(self.header_list))
            for strain in sorted(genesippr_dict):
                data += '{str},'.format(str=strain)
                for header in self.header_list[1:]:
                    data += '{value},'.format(value=genesippr_dict[strain][header])

                data = data.rstrip(',')
                data += '\n'
            csv.write(data)
683277
def build_strain_specific_models(self, save_models=False):
        

        if len(self.df_orthology_matrix) == 0:
            raise RuntimeError('Empty orthology matrix')

        # Create an emptied copy of the reference GEM-PRO
        for strain_gempro in tqdm(self.strains):
            log.debug('{}: building strain specific model'.format(strain_gempro.id))

            # For each genome, load the metabolic model or genes from the reference GEM-PRO
            logging.disable(logging.WARNING)
            if self._empty_reference_gempro.model:
                strain_gempro.load_cobra_model(self._empty_reference_gempro.model)
            elif self._empty_reference_gempro.genes:
                strain_gempro.genes = [x.id for x in self._empty_reference_gempro.genes]
            logging.disable(logging.NOTSET)

            # Get a list of genes which do not have orthology in the strain
            not_in_strain = self.df_orthology_matrix[pd.isnull(self.df_orthology_matrix[strain_gempro.id])][strain_gempro.id].index.tolist()

            # Mark genes non-functional
            self._pare_down_model(strain_gempro=strain_gempro, genes_to_remove=not_in_strain)

            # Load sequences into the base and strain models
            self._load_strain_sequences(strain_gempro=strain_gempro)

            if save_models:
                cobra.io.save_json_model(model=strain_gempro.model,
                                         filename=op.join(self.model_dir, '{}.json'.format(strain_gempro.id)))
                strain_gempro.save_pickle(op.join(self.model_dir, '{}_gp.pckl'.format(strain_gempro.id)))


        log.info('Created {} new strain-specific models and loaded in sequences'.format(len(self.strains)))
630720
def linker(self):
        

        strainprofile = os.path.join(self.profilelocation, 'strainprofiles.txt')
        if not os.path.isfile(strainprofile):
            header = 'Strain,SequenceType\n'
            data = ''
            # Sort the profiles based on sequence type
            sortedprofiles = sorted(self.profiles.items(), key=operator.itemgetter(1))
            # Associate the sequence type with each strain
            for strain, seqtype in sortedprofiles:
                for sample in self.runmetadata.samples:
                    if sample.name == strain:
                        sample.general.coretype = seqtype
                        data += '{},{}\n'.format(strain, seqtype)
            # Write the results to file
            with open(strainprofile, 'w') as profile:
                profile.write(header)
                profile.write(data)
1265393
def write_strain_functional_genes(self, force_rerun=False):
        """"""
        if len(self.df_orthology_matrix) == 0:
            raise RuntimeError('Empty orthology matrix, please calculate first!')
        ref_functional_genes = [g.id for g in self.reference_gempro.functional_genes]
        log.info('Building strain specific models...')
        result = []
        for s in tqdm(self.strain_ids):
            result.append(self._write_strain_functional_genes(s, ref_functional_genes, self.df_orthology_matrix, force_rerun=force_rerun))

        for strain_id, functional_genes in result:
            self.strain_infodict[strain_id]['functional_genes'] = functional_genes
941192
private List<Metric> analyzeResponse(final ICommandLine opt, final String response, final int elapsed) throws MetricGatheringException {
        List<Metric> metrics = new ArrayList<Metric>();
        metrics.add(new Metric("time", "", new BigDecimal(elapsed), null, null));

        if (!opt.hasOption("certificate")) {
            if (opt.hasOption("string")) {
                boolean found = false;
                String string = opt.getOptionValue("string");
                found = response.contains(string);
                metrics.add(new Metric("string", "", new BigDecimal(Utils.getIntValue(found)), null, null));
            }
            if (opt.hasOption("expect")) {
                int count = 0;
                String[] values = opt.getOptionValue("expect").split(",");
                for (String value : values) {
                    if (response.contains(value)) {
                        count++;
                    }
                }
                metrics.add(new Metric("expect", String.valueOf(count) + " times. ", new BigDecimal(count), null, null));
            }
            if (opt.hasOption("regex")) {
                String regex = opt.getOptionValue("regex");
                Pattern p = null;
                int flags = 0;
                if (opt.hasOption("eregi")) {
                    flags = Pattern.CASE_INSENSITIVE;
                }
                if (opt.hasOption("linespan")) {
                    flags = flags | Pattern.MULTILINE;
                }
                p = Pattern.compile(regex, flags);
                boolean found = p.matcher(response).find();
                if (opt.hasOption("invert-regex")) {
                    metrics.add(new Metric("invert-regex", String.valueOf(found), new BigDecimal(Utils.getIntValue(found)), null, null));
                } else {
                    metrics.add(new Metric("regex", String.valueOf(found), new BigDecimal(Utils.getIntValue(found)), null, null));
                }
            }
        }
        return metrics;
    }
791011
def _evaluate(self):
        """"""

        retrieved_records = SortedDict()

        for record_id, record in six.iteritems(self._elements):
            if record is self._field._unset:
                # Record has not yet been retrieved, get it
                try:
                    record = self.target_app.records.get(id=record_id)
                except SwimlaneHTTP400Error:
                    # Record appears to be orphaned, don't include in set of elements
                    logger.debug("Received 400 response retrieving record '{}', ignoring assumed orphaned record")
                    continue

            retrieved_records[record_id] = record

        self._elements = retrieved_records

        return self._elements.values()
68954
def _update_estimate_and_sampler(self, ell, ell_hat, weight, extra_info,
                                     **kwargs):
        """"""
        stratum_idx = extra_info['stratum']
        self._BB_TP.update(ell*ell_hat, stratum_idx)
        self._BB_PP.update(ell_hat, stratum_idx)
        self._BB_P.update(ell, stratum_idx)

        # Update model covariance matrix for stratum_idx
        self._update_cov_model(strata_to_update = [stratum_idx])

        # Update F-measure estimate, estimator variance, exp. variance decrease
        self._update_estimates()
1299051
def reporter(self):
        
        # Create the path in which the reports are stored
        make_path(self.reportpath)
        header = 'Strain,Gene,PercentIdentity,Genus,FoldCoverage\n'
        data = ''
        with open(os.path.join(self.reportpath, self.analysistype + '.csv'), 'w') as report:
            for sample in self.runmetadata.samples:
                data += sample.name + ','
                if sample[self.analysistype].results:
                    if not sample[self.analysistype].multiple:
                        for name, identity in sample[self.analysistype].results.items():
                            if name == sample[self.analysistype].besthit[0]:
                                data += '{},{},{},{}\n'.format(name, identity, sample[self.analysistype].genera[name],
                                                               sample[self.analysistype].avgdepth[name])
                    else:
                        data += '{},{},{},{}\n'.format('multiple', 'NA', ';'.join(sample[self.analysistype]
                                                                                  .classification), 'NA')
                else:
                    data += '\n'
            report.write(header)
            report.write(data)
910624
def measure
      before = take_time
      yield
      after = take_time
      @repeat += 1
      @times[:repeat] << @repeat
      @times[:scatter] << @scatter
      bs = self.case.batch_size.abs
      if bs and bs > 1
        TIMES.each_with_index { |t, i| @times[t] << (after[i] - before[i]) / bs }
      else
        TIMES.each_with_index { |t, i| @times[t] << after[i] - before[i] }
      end
      @analysis = nil
    end
268770
def _load(self, scale=0.001):
        

        ncf = Dataset(self.path, 'r')

        bandnum = OLCI_BAND_NAMES.index(self.bandname)
        # cam = 0
        # view = 0
        # resp = ncf.variables[
        #     'spectral_response_function'][bandnum, cam, view, :]
        # wvl = ncf.variables[
        #     'spectral_response_function_wavelength'][bandnum, cam, view, :] * scale
        resp = ncf.variables[
            'mean_spectral_response_function'][bandnum, :]
        wvl = ncf.variables[
            'mean_spectral_response_function_wavelength'][bandnum, :] * scale

        self.rsr = {'wavelength': wvl, 'response': resp}
81559
def main():
    """"""

    parser = argparse.ArgumentParser(description='Run the chaid algorithm on a'
                                     ' csv/sav file.')
    parser.add_argument('file')
    parser.add_argument('dependent_variable', nargs=1)
    parser.add_argument('--dependent-variable-type', type=str)

    var = parser.add_argument_group('Independent Variable Specification')
    var.add_argument('nominal_variables', nargs='*', help='The names of '
                     'independent variables to use that have no intrinsic '
                     'order to them')
    var.add_argument('--ordinal-variables', type=str, nargs='*',
                     help='The names of independent variables to use that '
                     'have an intrinsic order but a finite amount of states')
    parser.add_argument('--weights', type=str, help='Name of weight column')

    parser.add_argument('--max-depth', type=int, help='Max depth of generated '
                        'tree')
    parser.add_argument('--min-parent-node-size', type=int, help='Minimum number of '
                        'samples required to split the parent node')
    parser.add_argument('--min-child-node-size', type=int, help='Minimum number of '
                        'samples required to split the child node')
    parser.add_argument('--alpha-merge', type=float, help='Alpha Merge')
    group = parser.add_mutually_exclusive_group(required=False)
    group.add_argument('--classify', action='store_true', help='Add column to'
                       ' input with the node id of the node that that '
                       'respondent has been placed into')
    group.add_argument('--predict', action='store_true', help='Add column to '
                       'input with the value of the  dependent variable that '
                       'the majority of respondents in that node selected')
    group.add_argument('--rules', action='store_true')
    group.add_argument('--export', action='store_true', help='Whether to export the chart to pdf/dot')
    group.add_argument('--export-path', type=str, help='Path to store chart output')


    nspace = parser.parse_args()

    if nspace.file[-4:] == '.csv':
        data = pd.read_csv(nspace.file)
    elif nspace.file[-4:] == '.sav':
        import savReaderWriter as spss
        raw_data = spss.SavReader(nspace.file, returnHeader=True)
        raw_data_list = list(raw_data)
        data = pd.DataFrame(raw_data_list)
        data = data.rename(columns=data.loc[0]).iloc[1:]
    else:
        print('Unknown file type')
        exit(1)

    config = {}
    if nspace.max_depth:
        config['max_depth'] = nspace.max_depth
    if nspace.alpha_merge:
        config['alpha_merge'] = nspace.alpha_merge
    if nspace.min_parent_node_size:
        config['min_parent_node_size'] = nspace.min_parent_node_size
    if nspace.min_child_node_size:
        config['min_child_node_size'] = nspace.min_child_node_size
    if nspace.weights:
        config['weight'] = nspace.weights
    if nspace.dependent_variable_type:
        config['dep_variable_type'] = nspace.dependent_variable_type


    ordinal = nspace.ordinal_variables or []
    nominal = nspace.nominal_variables or []
    independent_variables = nominal + ordinal
    types = dict(zip(nominal + ordinal, ['nominal'] * len(nominal) + ['ordinal'] * len(ordinal)))
    if len(independent_variables) == 0:
        print('Need to provide at least one independent variable')
        exit(1)
    tree = Tree.from_pandas_df(data, types, nspace.dependent_variable[0],
                               **config)

    if nspace.export or nspace.export_path:
        tree.render(nspace.export_path, True)

    if nspace.classify:
        predictions = pd.Series(tree.node_predictions())
        predictions.name = 'node_id'
        data = pd.concat([data, predictions], axis=1)
        print(data.to_csv())
    elif nspace.predict:
        predictions = pd.Series(tree.model_predictions())
        predictions.name = 'predicted'
        data = pd.concat([data, predictions], axis=1)
        print(data.to_csv())
    elif nspace.rules:
        print('\n'.join(str(x) for x in tree.classification_rules()))
    else:
        tree.print_tree()
        print('Accuracy: ', tree.accuracy())
562212
def get_observed_strains_and_df(observation, observation_dict):
    
    observed_df = pd.DataFrame.from_records(observation_dict[observation], columns=['strain','phenotype']).set_index('strain')
    observed_strains = observed_df.index.tolist()
    return observed_strains, observed_df
1356314
def process_response(self, request, response):
        
        try:
            log_dict = create_log_dict(request, response)

            # add the request time to the log_dict; if no start time is
            # available, use -1 as NA value
            request_time = (
                time.time() - self.start_time if hasattr(self, 'start_time')
                and self.start_time else -1)
            log_dict.update({'request_time': request_time})

            is_request_time_too_high = (
                request_time > float(settings.LOGUTILS_REQUEST_TIME_THRESHOLD))
            use_sql_info = settings.DEBUG or is_request_time_too_high

            log_msg = create_log_message(log_dict, use_sql_info, fmt=False)

            if is_request_time_too_high:
                logger.warning(log_msg, log_dict, extra=log_dict)
            else:
                logger.info(log_msg, log_dict, extra=log_dict)
        except Exception as e:
            logger.exception(e)

        return response