This view generates a csv output file of all animal data for use in litter analysis.
	
	The view writes to a csv table the birthdate, breeding cage and strain.
332550
def litters_csv(request):
    """"""
    animal_list = Animal.objects.all()
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename=litters.csv'
    writer = csv.writer(response)
    writer.writerow(["Born", "Breeding", "Strain"])
    for animal in animal_list:
        writer.writerow([
            animal.Born,
            animal.Breeding,
            animal.Strain
            ])
    return response
784543
def _get_result_paths(self, data):
        

        result = {}

        result['Output'] = ResultPath(
            Path=self.Parameters['--output'].Value,
            IsWritten=self.Parameters['--output'].isOn())

        result['ClusterFile'] = ResultPath(
            Path=self.Parameters['--uc'].Value,
            IsWritten=self.Parameters['--uc'].isOn())

        # uchime 3-way global alignments
        result['Output_aln'] = ResultPath(
            Path=self.Parameters['--uchimealns'].Value,
            IsWritten=self.Parameters['--uchimealns'].isOn())

        # uchime tab-separated format
        result['Output_tabular'] = ResultPath(
            Path=self.Parameters['--uchimeout'].Value,
            IsWritten=self.Parameters['--uchimeout'].isOn())

        # chimeras fasta file output
        result['Output_chimeras'] = ResultPath(
            Path=self.Parameters['--chimeras'].Value,
            IsWritten=self.Parameters['--chimeras'].isOn())

        # nonchimeras fasta file output
        result['Output_nonchimeras'] = ResultPath(
            Path=self.Parameters['--nonchimeras'].Value,
            IsWritten=self.Parameters['--nonchimeras'].isOn())

        # log file
        result['LogFile'] = ResultPath(
            Path=self.Parameters['--log'].Value,
            IsWritten=self.Parameters['--log'].isOn())

        return result
755131
def self_generate(output_filename, filename="iso3166-1.csv"):  # pragma: no cover
    
    import csv
    import re

    countries = []
    alt_codes = []
    with open(filename, "r") as csv_file:
        for row in csv.reader(csv_file):
            name = row[0].rstrip("*")
            name = re.sub(r"\(the\)", "", name)
            if name:
                countries.append((name, row[1]))
                alt_codes.append((row[1], row[2], int(row[3])))
    with open(__file__, "r") as source_file:
        contents = source_file.read()
    # Write countries.
    bits = re.match("(.*\nCOUNTRIES = \{\n)(.*?)(\n\}.*)", contents, re.DOTALL).groups()
    country_list = []
    for name, code in countries:
        name = name.replace('"', r"\"").strip()
        country_list.append('    "{code}": _("{name}"),'.format(name=name, code=code))
    content = bits[0]
    content += "\n".join(country_list)
    # Write alt codes.
    alt_bits = re.match(
        "(.*\nALT_CODES = \{\n)(.*)(\n\}.*)", bits[2], re.DOTALL
    ).groups()
    alt_list = []
    for code, code3, codenum in alt_codes:
        name = name.replace('"', r"\"").strip()
        alt_list.append(
            '    "{code}": ("{code3}", {codenum}),'.format(
                code=code, code3=code3, codenum=codenum
            )
        )
    content += alt_bits[0]
    content += "\n".join(alt_list)
    content += alt_bits[2]
    # Generate file.
    with open(output_filename, "w") as output_file:
        output_file.write(content)
    return countries
841420
def generate_graph_csv(variable_table_name):
    
    temp_csv = home_dir + "/PycharmProjects/crystal/crystal/static/temp.csv"
    conn, c = open_data_base_connection()

    # Get variable data
    c.execute("""SELECT * FROM {}""".format(variable_table_name))
    with open(temp_csv, "w", newline='') as csv_file:
        csv_writer = csv.writer(csv_file)
        csv_writer.writerow([i[0] for i in c.description])  # write headers
        csv_writer.writerows(c)
        print("File saved: {}".format(temp_csv))
        conn.close()

        return temp_csv
875432
def main(model_folder):
    
    a = yaml.load(open(utils.get_latest_in_folder(model_folder, ".json")))

    layers = []
    filenames = ["model.yml", "input_semantics.csv", "output_semantics.csv",
                 "preprocessing.yml", "features.yml"]

    # Create input_semantics.csv
    inputs = a['layers'][0]['_props']['n_visible']
    with open('input_semantics.csv', 'wb') as csvfile:
        spamwriter = csv.writer(csvfile, delimiter=';',
                                quotechar='|', quoting=csv.QUOTE_MINIMAL)
        for row in range(inputs):
            spamwriter.writerow(["inputs %i" % row])

    # Create output_semantics.csv
    outputs = a['layers'][-1]['_props']['n_hidden']
    create_output_semantics(model_folder, outputs)

    # Write layers
    for layer_index, layer in enumerate(a['layers']):
        W = _as_ndarray(layer['params']['W'])
        Wfile = h5py.File('W%i.hdf5' % layer_index, 'w')
        Wfile.create_dataset(Wfile.id.name, data=W)
        Wfile.close()

        b = _as_ndarray(layer['params']['b'])
        bfile = h5py.File('b%i.hdf5' % layer_index, 'w')
        bfile.create_dataset(bfile.id.name, data=b)
        bfile.close()

        activation = layer['_props']['activation']
        activation = activation.replace('sigmoid', 'Sigmoid')
        activation = activation.replace('softmax', 'Softmax')
        layers.append({'W': {'size': list(W.shape),
                             'filename': 'W%i.hdf5' % layer_index},
                       'b': {'size': list(b.shape),
                             'filename': 'b%i.hdf5' % layer_index},
                       'activation': activation})
        filenames.append('W%i.hdf5' % layer_index)
        filenames.append('b%i.hdf5' % layer_index)

    model = {'type': 'mlp', 'layers': layers}

    with open("model.yml", 'w') as f:
        yaml.dump(model, f, default_flow_style=False)

    logging.info("Get preprocessing.yml")
    # Get model folder
    model_description_file = os.path.join(model_folder, "info.yml")
    with open(model_description_file, 'r') as ymlfile:
        model_description = yaml.load(ymlfile)

    # Get feature folder
    feature_description_file = os.path.join(utils.get_project_root(),
                                            model_description["data-source"],
                                            "info.yml")
    with open(feature_description_file, 'r') as ymlfile:
        feature_description = yaml.load(ymlfile)

    with open("features.yml", 'w') as f:
        yaml.dump(feature_description, f, default_flow_style=False)

    # Get preprocessing folder
    preprocessing_description_file = os.path.join(utils.get_project_root(),
                                                  feature_description["data-source"],
                                                  "info.yml")
    with open(preprocessing_description_file, 'r') as ymlfile:
        preprocessing_description = yaml.load(ymlfile)

    with open("preprocessing.yml", 'w') as f:
        yaml.dump(preprocessing_description, f, default_flow_style=False)

    with tarfile.open("model.tar", "w:") as tar:
        for name in filenames:
            tar.add(name)

    # Remove temporary files which are now in tar file
    for filename in filenames:
        os.remove(filename)
1031422
def csvpretty(csvfile: csvfile=sys.stdin):
    
    shellish.tabulate(csv.reader(csvfile))
885933
public static void main(String[] args) {
        String modelFile = "";
        String outputFile = "";
        int numberOfRows = 0;
        try {
            modelFile = args[0];
            outputFile = args[1];
            numberOfRows = Integer.valueOf(args[2]);
        } catch (IndexOutOfBoundsException | NumberFormatException e) {
            System.out.println("ERROR! Invalid command line arguments, expecting: <scxml model file> "
                    + "<desired csv output file> <desired number of output rows>");
            return;
        }

        FileInputStream model = null;
        try {
            model = new FileInputStream(modelFile);
        } catch (FileNotFoundException e) {
            System.out.println("ERROR! Model file not found");
            return;
        }

        SCXMLEngine engine = new SCXMLEngine();
        engine.setModelByInputFileStream(model);
        engine.setBootstrapMin(5);

        DataConsumer consumer = new DataConsumer();
        CSVFileWriter writer;
        try {
            writer = new CSVFileWriter(outputFile);
        } catch (IOException e) {
            System.out.println("ERROR! Can not write to output csv file");
            return;
        }
        consumer.addDataWriter(writer);

        DefaultDistributor dist = new DefaultDistributor();
        dist.setThreadCount(5);
        dist.setMaxNumberOfLines(numberOfRows);
        dist.setDataConsumer(consumer);

        engine.process(dist);
        writer.closeCSVFile();

        System.out.println("COMPLETE!");
    }
264539
def table_output(data):
    ''''''
    if type(data) == DictType:
        data = data.items()
    headings = [ item[0] for item in data ]
    rows = [ item[1] for item in data ]
    columns = zip(*rows)
    if len(columns):
        widths = [ max([ len(str(y)) for y in row ]) for row in rows ]
    else:
        widths = [ 0 for c in headings ]
    for c, heading in enumerate(headings):
        widths[c] = max(widths[c], len(heading))
    column_count = range(len(rows))
    table = [ ' '.join([ headings[c].ljust(widths[c]) for c in column_count ]) ]
    table.append(' '.join([ '=' * widths[c] for c in column_count ]))
    for column in columns:
        table.append(' '.join([ str(column[c]).ljust(widths[c]) for c in column_count ]))
    return '\n'.join(table)
307750
def data(args):
    
    p = OptionParser(data.__doc__)
    p.add_option("--notsv", default=False, action="store_true",
                 help="Do not write data.tsv")
    opts, args = p.parse_args(args)

    if len(args) != 4:
        sys.exit(not p.print_help())

    databin, sampleids, strids, metafile = args
    final_columns, percentiles = read_meta(metafile)
    df, m, samples, loci = read_binfile(databin, sampleids, strids)

    # Clean the data
    m %= 1000  # Get the larger of the two alleles
    m[m == 999] = -1  # Missing data

    final = set(final_columns)
    remove = []
    for i, locus in enumerate(loci):
        if locus not in final:
            remove.append(locus)
            continue

    pf = "STRs_{}_SEARCH".format(timestamp())
    filteredstrids = "{}.STR.ids".format(pf)
    fw = open(filteredstrids, "w")
    print("\n".join(final_columns), file=fw)
    fw.close()
    logging.debug("Dropped {} columns; Retained {} columns (`{}`)".\
                    format(len(remove), len(final_columns), filteredstrids))

    # Remove low-quality columns!
    df.drop(remove, inplace=True, axis=1)
    df.columns = final_columns

    filtered_bin = "{}.data.bin".format(pf)
    if need_update(databin, filtered_bin):
        m = df.as_matrix()
        m.tofile(filtered_bin)
        logging.debug("Filtered binary matrix written to `{}`".format(filtered_bin))

    # Write data output
    filtered_tsv = "{}.data.tsv".format(pf)
    if not opts.notsv and need_update(databin, filtered_tsv):
        df.to_csv(filtered_tsv, sep="\t", index_label="SampleKey")
392264
def reporter(self):
        
        logging.info('Creating {} report'.format(self.analysistype))
        # Create the path in which the reports are stored
        make_path(self.reportpath)
        header = 'Strain,Serotype\n'
        data = ''
        with open(os.path.join(self.reportpath, '{}.csv'.format(self.analysistype)), 'w') as report:
            for sample in self.runmetadata.samples:
                if sample.general.bestassemblyfile != 'NA':
                    data += sample.name + ','
                    if sample[self.analysistype].results:
                        # Set the O-type as either the appropriate attribute, or O-untypable
                        if ';'.join(sample.serosippr.o_set) == '-':
                            otype = 'O-untypeable'
                        else:
                            otype = '{oset} ({opid})'.format(oset=';'.join(sample.serosippr.o_set),
                                                             opid=sample.serosippr.best_o_pid)
                        # Same as above, but for the H-type
                        if ';'.join(sample.serosippr.h_set) == '-':
                            htype = 'H-untypeable'

                        else:
                            htype = '{hset} ({hpid})'.format(hset=';'.join(sample.serosippr.h_set),
                                                             hpid=sample.serosippr.best_h_pid)
                        serotype = '{otype}:{htype}'.format(otype=otype,
                                                            htype=htype)
                        # Populate the data string
                        data += serotype if serotype != 'O-untypeable:H-untypeable' else 'ND'
                        data += '\n'
                    else:
                        data += '\n'
            report.write(header)
            report.write(data)
1234416
def csv(self, output):
        """"""
        import csv
        csvwriter = csv.writer(self.outfile)
        csvwriter.writerows(output)
1290441
def generate_csv(in_dir, out):
    
    writer = UnicodeWriter(open(out, 'wb'), delimiter=';')
    writer.writerow(('Reference ID', 'Created', 'Origin', 'Subject'))
    for cable in cables_from_source(in_dir):
        writer.writerow((cable.reference_id, cable.created, cable.origin, titlefy(cable.subject)))
614635
def vcf_writer(parser, keep, extract, args):
    """"""
    # The output
    output = sys.stdout if args.output == "-" else open(args.output, "w")

    try:
        # Getting the samples
        samples = np.array(parser.get_samples(), dtype=str)
        k = _get_sample_select(samples=samples, keep=keep)

        # Writing the VCF header
        output.write(_VCF_HEADER.format(
            date=datetime.today().strftime("%Y%m%d"),
            version=__version__,
            samples="\t".join(samples[k]),
        ))

        # The data generator
        generator = _get_generator(parser=parser, extract=extract, keep=k,
                                   check_maf=args.maf)

        # The number of markers extracted
        nb_extracted = 0

        for data in generator:
            # Keeping only the required genotypes
            genotypes = data.genotypes

            # Computing the alternative allele frequency
            af = np.nanmean(genotypes) / 2

            print(data.variant.chrom, data.variant.pos, data.variant.name,
                  data.reference, data.coded, ".", "PASS", "AF={}".format(af),
                  "GT:DS", sep="\t", end="", file=output)

            for geno in genotypes:
                if np.isnan(geno):
                    output.write("\t./.:.")
                else:
                    rounded_geno = int(round(geno, 0))
                    output.write("\t{}:{}".format(
                        _VCF_GT_MAP[rounded_geno], geno,
                    ))

            output.write("\n")
            nb_extracted += 1

        if nb_extracted == 0:
            logger.warning("No markers matched the extract list")

    finally:
        output.close()
416510
def generate_csv(src, out):
    
    writer = UnicodeWriter(open(out, 'wb'), delimiter=';')
    writer.writerow(('Reference ID', 'Created', 'Origin', 'Subject'))
    for cable in cables_from_source(src, predicate=pred.origin_filter(pred.origin_germany)):
        writer.writerow((cable.reference_id, cable.created, cable.origin, titlefy(cable.subject)))
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)
90365
def report(data):
    """"""
    work_dir = dd.get_work_dir(data[0][0])
    out_dir = op.join(work_dir, "report")
    safe_makedir(out_dir)
    summary_file = op.join(out_dir, "summary.csv")
    with file_transaction(summary_file) as out_tx:
        with open(out_tx, 'w') as out_handle:
            out_handle.write("sample_id,%s\n" % _guess_header(data[0][0]))
            for sample in data:
                info = sample[0]
                group = _guess_group(info)
                files = info["seqbuster"] if "seqbuster" in info else "None"
                out_handle.write(",".join([dd.get_sample_name(info),
                                           group]) + "\n")
    _modify_report(work_dir, out_dir)
    return summary_file
274104
def create_ped_file(samples, base_vcf, out_dir=None):
    
    out_file = "%s.ped" % utils.splitext_plus(base_vcf)[0]
    if out_dir:
        out_file = os.path.join(out_dir, os.path.basename(out_file))
    sample_ped_lines = {}
    header = ["#Family_ID", "Individual_ID", "Paternal_ID", "Maternal_ID", "Sex", "Phenotype", "Ethnicity"]
    for md_ped in list(set([x for x in [tz.get_in(["metadata", "ped"], data)
                                        for data in samples] if x is not None])):
        with open(md_ped) as in_handle:
            reader = csv.reader(in_handle, dialect="excel-tab")
            for parts in reader:
                if parts[0].startswith("#") and len(parts) > len(header):
                    header = header + parts[len(header):]
                else:
                    sample_ped_lines[parts[1]] = parts
    if not utils.file_exists(out_file):
        with file_transaction(samples[0], out_file) as tx_out_file:
            with open(tx_out_file, "w") as out_handle:
                want_samples = set(vcfutils.get_samples(base_vcf))
                writer = csv.writer(out_handle, dialect="excel-tab")
                writer.writerow(header)
                for data in samples:
                    ped_info = get_ped_info(data, samples)
                    sname = ped_info["individual_id"]
                    if sname in want_samples:
                        want_samples.remove(sname)
                        if sname in sample_ped_lines:
                            writer.writerow(sample_ped_lines[sname])
                        else:
                            writer.writerow([ped_info["family_id"], ped_info["individual_id"],
                                            ped_info["paternal_id"], ped_info["maternal_id"],
                                            ped_info["gender"], ped_info["affected"],
                                            ped_info["ethnicity"]])
    return out_file