Skip to content

Commit

Permalink
Mccalluc/unnecessary evals (#1935)
Browse files Browse the repository at this point in the history
* Replace eval with globals
In many of these we are taking a string from the url. There is a tight regex match on the url, but this is dangerous.

* Get rid of one more eval

* Useless function call

* Combine normal named params and splat

* Get rid of the last eval
  • Loading branch information
mccalluc committed Jul 24, 2017
1 parent 857bfe6 commit ab24871
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,9 @@ def parse_gff_attribute(attr):
return ret_dict


def parse_db_string(attr, sym):
def parse_db_params(attr, sym):
"""Helper function for entering gff, gtf, files into models"""
ret_string = ''
for var in sym:
if var in attr:
if ret_string:
ret_string += ', %s="%s"' % (var, attr[var])
else:
ret_string += '%s="%s"' % (var, attr[var])
return ret_string
return {k: attr[k] for k in sym}


def getFileHandle(file_in):
Expand Down Expand Up @@ -171,7 +164,7 @@ def addWigAnnotation(self, wig_file, db_model, annot_type):
"""General function for adding Wig files into the annotation_server
specifically for Conservation and GC content
"""
current_table = eval(db_model)
current_table = globals()[db_model]
current_table.objects.all().delete()
handle = getFileHandle(wig_file)
for line in handle:
Expand All @@ -196,14 +189,13 @@ def addWigAnnotation(self, wig_file, db_model, annot_type):
ret_attr = parse_wig_attribute(line)
table_vals = ['name', 'altColor', 'color', 'visibility',
'priority', 'type', 'description']
db_string = "WigDescription(genome_build='%s', " \
"annotation_type='%s', %s)"
db_string = db_string % (
self.GENOME_BUILD, annot_type, parse_db_string(
ret_attr, table_vals)
params = parse_db_params(
ret_attr, table_vals
)
# saving to wigDescription model
item = eval(db_string)
item = WigDescription(
genome_build=self.GENOME_BUILD,
annotation_type=annot_type,
**params)
item.save()

elif t1[0] == 'fixedStep':
Expand Down Expand Up @@ -296,22 +288,19 @@ def addGeneAnnotation(self, gff_file, db_model, table_vals):
"""Function for adding additional gene annotation files
i.e hg19 gencode, dm3 flybase, ce10 wormbase
"""
current_table = eval(db_model)
current_table = globals()[db_model]
current_table.objects.all().delete()
handle = getFileHandle(gff_file)
for line in handle:
# TODO: what to do with additional description fields
if line[0] != '#':
t1 = line.strip().split('\t')
attrib = parse_gff_attribute(t1[8])
db_string = (
'current_table(chrom=t1[0], source=t1[1], feature=t1[2], '
'start=t1[3], end=t1[4], score=t1[5], strand=t1[6], '
'frame=t1[7], attribute=t1[8], %s)'
)
parse_db_string(attrib, table_vals)
db_string = db_string % (parse_db_string(attrib, table_vals))
item = eval(db_string)
params = parse_db_params(attrib, table_vals)
item = current_table(
chrom=t1[0], source=t1[1], feature=t1[2],
start=t1[3], end=t1[4], score=t1[5], strand=t1[6],
frame=t1[7], attribute=t1[8], **params)
item.save()

def addGapRegions(self, bed_file, db_model):
Expand All @@ -322,7 +311,7 @@ def addGapRegions(self, bed_file, db_model):
"genome: %s, file: %s table: %s",
self.GENOME_BUILD, bed_file, db_model)

current_table = eval(db_model)
current_table = globals()[db_model]
current_table.objects.all().delete()
handle = getFileHandle(bed_file)
for line in handle:
Expand All @@ -341,7 +330,7 @@ def addBedAnnotation(self, bed_file, db_model):
"genome: %s, file: %s table: %s",
self.GENOME_BUILD, bed_file, db_model)

current_table = eval(db_model)
current_table = globals()[db_model]
current_table.objects.all().delete()
handle = getFileHandle(bed_file)
for line in handle:
Expand Down
20 changes: 10 additions & 10 deletions refinery/annotation_server/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def search_genes(request, genome, search_string):
genome, search_string)

if genome in SUPPORTED_GENOMES:
current_table = eval(genome + "_EnsGene")
current_table = globals()[genome + "_EnsGene"]
curr_vals = current_table.objects.filter(
Q(name__contains=search_string) | Q(name2__contains=search_string)
).values('name', 'chrom', 'strand', 'txStart', 'txEnd', 'cdsStart',
Expand Down Expand Up @@ -81,7 +81,7 @@ def get_length(request, genome):
logger.debug("annotation_server.get_length called for genome: %s", genome)

if genome in SUPPORTED_GENOMES:
current_table = eval(genome + "_ChromInfo")
current_table = globals()[genome + "_ChromInfo"]
data = ValuesQuerySetToDict(
current_table.objects.values('chrom', 'size'))
return HttpResponse(data, 'application/json')
Expand All @@ -95,7 +95,7 @@ def get_chrom_length(request, genome, chrom):
"%s chromosome: %s", genome, chrom)

if genome in SUPPORTED_GENOMES:
current_table = eval(genome + "_ChromInfo")
current_table = globals()[genome + "_ChromInfo"]
curr_vals = current_table.objects.filter(chrom__iexact=chrom).values(
'chrom', 'size')
data = ValuesQuerySetToDict(curr_vals)
Expand All @@ -112,7 +112,7 @@ def get_cytoband(request, genome, chrom):
"%s chromosome: %s", genome, chrom)

if genome in SUPPORTED_GENOMES:
current_table = eval(genome + "_CytoBand")
current_table = globals()[genome + "_CytoBand"]
curr_vals = current_table.objects.filter(chrom__iexact=chrom).values(
'chrom', 'chromStart', 'chromEnd', 'name', 'gieStain')
data = ValuesQuerySetToDict(curr_vals)
Expand All @@ -127,7 +127,7 @@ def get_genes(request, genome, chrom, start, end):
"%s chromosome: %s", genome, chrom)

if genome in SUPPORTED_GENOMES:
current_table = eval(genome + "_EnsGene")
current_table = globals()[genome + "_EnsGene"]
curr_vals = current_table.objects.filter(
Q(chrom__iexact=chrom),
Q(cdsStart__range=(start, end)) | Q(cdsEnd__range=(start, end))
Expand All @@ -145,7 +145,7 @@ def get_gc(request, genome, chrom, start, end):
"%s chromosome: %s:%s-%s", genome, chrom, start, end)

if genome in SUPPORTED_GENOMES:
current_table = eval(genome + "_GC")
current_table = globals()[genome + "_GC"]
curr_vals = current_table.objects.filter(
Q(chrom__iexact=chrom),
Q(position__range=(start, end)),
Expand All @@ -164,7 +164,7 @@ def get_maptheo(request, genome, chrom, start, end):
"%s chromosome: %s:%s-%s", genome, chrom, start, end)

if genome in MAPPABILITY_THEORETICAL:
current_table = eval(genome + "_MappabilityTheoretical")
current_table = globals()[genome + "_MappabilityTheoretical"]
curr_vals = current_table.objects.filter(
Q(chrom__iexact=chrom),
Q(chromStart__range=(start, end)) | Q(chromEnd__range=(start, end))
Expand All @@ -182,7 +182,7 @@ def get_mapemp(request, genome, chrom, start, end):
"%s chromosome: %s:%s-%s", genome, chrom, start, end)

if genome in SUPPORTED_GENOMES:
current_table = eval(genome + "_MappabilityEmpirial")
current_table = globals()[genome + "_MappabilityEmpirial"]
curr_vals = current_table.objects.filter(
Q(chrom__iexact=chrom),
Q(chromStart__range=(start, end)) | Q(chromEnd__range=(start, end))
Expand All @@ -199,7 +199,7 @@ def get_conservation(request, genome, chrom, start, end):
"%s chromosome: %s:%s-%s", genome, chrom, start, end)

if genome in SUPPORTED_GENOMES:
current_table = eval(genome + "_Conservation")
current_table = globals()[genome + "_Conservation"]
curr_vals = current_table.objects.filter(
Q(chrom__iexact=chrom),
Q(position__range=(start, end)),
Expand All @@ -217,7 +217,7 @@ def get_gapregion(request, genome, chrom, start, end):
"%s chromosome: %s:%s-%s", genome, chrom, start, end)

if genome in GAP_REGIONS:
current_table = eval(genome + "_GapRegions")
current_table = globals()[genome + "_GapRegions"]
curr_vals = current_table.objects.filter(
Q(chrom__iexact=chrom),
Q(chromStart__gte=start),
Expand Down

0 comments on commit ab24871

Please sign in to comment.