In [2]:
from __future__ import print_function
from path import path
import csv, json
In [11]:
outdir = path('npactweb/npactweb/static/drawingtest/')
outdir.mkdir_p()
sample_nprofile_output_file = 'webroot/uploads/library/NC_007760.80524f06a4021199748d8779c0657200b69e1632.nprofile'
sample_extract_output_file = 'webroot/uploads/library/NC_007760.fcc471be757b9fcf724b04ec01e7caed5484e878.genes'
sample_nprofile_csv_file = outdir / 'nprofile.csv'
sample_nprofile_json_file = outdir / 'nprofile.json'
sample_extract_json_file = outdir / 'extract.json'
sample_extract_csv_file = outdir / 'extract.csv'
In [12]:
def write_csv(ofile, data, header=None):
if not hasattr(ofile, 'write'):
ofile = open(ofile, 'wb')
#CSV writer isn't working for some unknown reason
# cw = csv.writer(output, quoting=csv.QUOTE_NONE)
# if(header):
# cw.writerow(header)
# rows = 0
# cw.writerows(data)
if header:
ofile.write(','.join(header) + '\n')
for l in data:
ofile.write(','.join(map(str, l)) + '\n')
def write_json(ofile, data, keys):
if not hasattr(ofile, 'write'):
ofile = open(ofile, 'w')
data = [dict(zip(keys,d)) for d in data]
json.dump(data, ofile)
In [13]:
def parse_nprofile(ifile):
#Read the file in and split the fields on space
nprofile_lines = [l.split() for l in path(ifile).lines(retain=False)]
#convert the strings to numbers
data = [(int(c), float(x), float(y), float(z)) for (c,x,y,z) in nprofile_lines]
return data
nprofile_data = parse_nprofile(sample_nprofile_output_file)
write_csv(sample_nprofile_csv_file, nprofile_data, header=['coordinate', 'r', 'g', 'b'])
write_json(sample_nprofile_json_file, nprofile_data, ['coordinate', 'r', 'g', 'b'])
In [14]:
import re
def parse_extract(ifile):
lines = [str.split(l) for l in path(ifile).lines(retain=False)]
for l in lines:
name, coords = l
complement = 0
if coords.startswith('complement'):
coords = coords[11:-1]
complement = 1
begin,end = coords.split('..')
yield name, int(begin), int(end), complement
extract_data = list(parse_extract(sample_extract_output_file))
write_csv(sample_extract_csv_file, extract_data, header=['name', 'start', 'end', 'complement'])
write_json(sample_extract_json_file, extract_data, ['name', 'start', 'end', 'complement'])
In [15]:
[print(l) for l in path(sample_nprofile_csv_file).lines(retain=False)[0:10]]; None
In [16]:
[print(l) for l in path(sample_extract_csv_file).lines(retain=False)[0:10]]; None
In [17]:
print(json.dumps(json.load(open(sample_nprofile_json_file))[0:4], indent=3))
In [18]:
print(json.dumps(json.load(open(sample_extract_json_file))[0:4], indent=3))
In [ ]: