Skip to content

Commit cd33e23

Browse files
committed
Pylint fixes
1 parent 4b8bf25 commit cd33e23

File tree

1 file changed

+53
-44
lines changed

1 file changed

+53
-44
lines changed

r2dt.py

Lines changed: 53 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import click
2323
from colorhash import ColorHash
2424

25-
from utils import crw, rfam, ribovision, gtrnadb, config, generate_model_info, shared
25+
from utils import crw, rfam, ribovision, gtrnadb, config, shared
2626
from utils import generate_model_info as gmi
2727
from utils import list_models as lm
2828
from utils import generate_cm_library as gcl
@@ -38,21 +38,21 @@ def get_ribotyper_output(fasta_input, output_folder, cm_library, skip_ribovore_f
3838
output_folder, os.path.basename(output_folder) + ".ribotyper.long.out"
3939
)
4040
if not os.path.exists(ribotyper_long_out):
41-
cmd = "ribotyper.pl --skipval -i {cm_library}/modelinfo.txt -f {fasta_input} {output_folder}".format(
42-
cm_library=cm_library, fasta_input=fasta_input, output_folder=output_folder
43-
)
41+
cmd = f"ribotyper.pl --skipval -i {cm_library}/modelinfo.txt -f {fasta_input} {output_folder}"
4442
print(cmd)
4543
os.system(cmd)
4644
f_out = os.path.join(output_folder, "hits.txt")
4745
if not skip_ribovore_filters:
4846
cmd = (
49-
"cat %s | grep -v '^#' | grep -v MultipleHits | grep PASS | awk -v OFS='\t' '{print $2, $8, $3}' > %s"
50-
% (ribotyper_long_out, f_out)
47+
f"cat {ribotyper_long_out} | grep -v '^#' | "
48+
f"grep -v MultipleHits | grep PASS | "
49+
f"awk -v OFS='\t' '{{print $2, $8, $3}}' > {f_out}"
5150
)
5251
else:
5352
cmd = (
54-
"cat %s | grep -v '^#' | grep -v NoHits | awk -v OFS='\t' '{print $2, $8, $3}' > %s"
55-
% (ribotyper_long_out, f_out)
53+
f"cat {ribotyper_long_out} | grep -v '^#' "
54+
f"| grep -v NoHits | "
55+
f"awk -v OFS='\t' '{{print $2, $8, $3}}' > {f_out}"
5656
)
5757
os.system(cmd)
5858
return f_out
@@ -121,7 +121,7 @@ def get_seq_ids(input_fasta):
121121
Get a list of sequence ids from a fasta file.
122122
"""
123123
seq_ids = set()
124-
with open(input_fasta, "r") as f_in:
124+
with open(input_fasta, "r", encoding="utf-8") as f_in:
125125
for line in f_in:
126126
if line.startswith(">"):
127127
match = re.search(r">(.*?)\s", line)
@@ -138,7 +138,7 @@ def get_hits(folder):
138138
hits_file = os.path.join(folder, "hits.txt")
139139
if not os.path.exists(hits_file):
140140
return hits
141-
with open(hits_file, "r") as f_in:
141+
with open(hits_file, "r", encoding="utf-8") as f_in:
142142
for line in f_in:
143143
hits.add(line.split("\t")[0])
144144
return hits
@@ -150,9 +150,9 @@ def get_subset_fasta(fasta_input, output_filename, seq_ids):
150150
from <fasta_input>.
151151
"""
152152
index_filename = output_filename + ".txt"
153-
with open(index_filename, "w") as f_out:
153+
with open(index_filename, "w", encoding="utf-8") as f_out:
154154
for seq_id in seq_ids:
155-
f_out.write(seq_id + "\n")
155+
f_out.write(f"{seq_id}\n")
156156
cmd = f"esl-sfetch -o {output_filename} -f {fasta_input} {index_filename}"
157157
os.system(cmd)
158158
os.system("esl-sfetch --index " + output_filename)
@@ -233,8 +233,9 @@ def draw(
233233
skip_ribovore_filters,
234234
),
235235
"r",
236-
) as f:
237-
for line in f.readlines():
236+
encoding="utf-8",
237+
) as f_ribotyper:
238+
for line in f_ribotyper.readlines():
238239
rnacentral_id, model_id, _ = line.split("\t")
239240
ribovision.visualise(
240241
"rfam",
@@ -346,7 +347,6 @@ def draw(
346347
"RF00005",
347348
output_folder,
348349
subset_fasta,
349-
False,
350350
constraint,
351351
exclusion,
352352
fold_type,
@@ -371,6 +371,7 @@ def draw(
371371

372372

373373
def organise_results(results_folder, output_folder):
374+
"""Move files to the final folder structure."""
374375
destination = os.path.join(output_folder, "results")
375376
svg_folder = os.path.join(destination, "svg")
376377
thumbnail_folder = os.path.join(destination, "thumbnail")
@@ -386,11 +387,12 @@ def organise_results(results_folder, output_folder):
386387
os.system(f"mkdir -p {folder}")
387388

388389
svgs = glob.glob(os.path.join(results_folder, "*.colored.svg"))
389-
if len(svgs):
390+
if svgs:
390391
for svg in svgs:
391-
with open(svg, "r") as f_svg:
392+
with open(svg, "r", encoding="utf-8") as f_svg:
392393
thumbnail = generate_thumbnail(f_svg.read(), svg)
393-
with open(svg.replace(".colored.", ".thumbnail."), "w") as f_thumbnail:
394+
thumbnail_filename = svg.replace(".colored.", ".thumbnail.")
395+
with open(thumbnail_filename, "w", encoding="utf-8") as f_thumbnail:
394396
f_thumbnail.write(thumbnail)
395397
os.system(f"mv {results_folder}/*.colored.svg {svg_folder}")
396398
os.system(f"mv {results_folder}/*.thumbnail.svg {thumbnail_folder}")
@@ -403,7 +405,6 @@ def gtrnadb_group():
403405
"""
404406
Use tRNA templates for structure visualisation.
405407
"""
406-
pass
407408

408409

409410
@gtrnadb_group.command("setup")
@@ -486,7 +487,6 @@ def rnasep_group():
486487
"""
487488
Use RNAse P templates for structure visualisation.
488489
"""
489-
pass
490490

491491

492492
@rnasep_group.command("draw")
@@ -513,8 +513,9 @@ def rnasep_draw(
513513
fasta_input, output_folder, config.RNASEP_CM_LIBRARY, skip_ribovore_filters
514514
),
515515
"r",
516-
) as f:
517-
for line in f.readlines():
516+
encoding="utf-8",
517+
) as f_ribotyper:
518+
for line in f_ribotyper.readlines():
518519
rnacentral_id, model_id, _ = line.split("\t")
519520
ribovision.visualise(
520521
"rnasep",
@@ -533,7 +534,6 @@ def crw_group():
533534
"""
534535
Use CRW templates for structure visualisation.
535536
"""
536-
pass
537537

538538

539539
@crw_group.command("draw")
@@ -560,8 +560,9 @@ def rrna_draw(
560560
fasta_input, output_folder, config.CRW_CM_LIBRARY, skip_ribovore_filters
561561
),
562562
"r",
563-
) as f:
564-
for line in f.readlines():
563+
encoding="utf-8",
564+
) as f_ribotyper:
565+
for line in f_ribotyper.readlines():
565566
rnacentral_id, model_id, _ = line.split("\t")
566567
ribovision.visualise(
567568
"crw",
@@ -580,7 +581,6 @@ def ribovision_group():
580581
"""
581582
Use RiboVision templates for structure visualisation.
582583
"""
583-
pass
584584

585585

586586
@ribovision_group.command("draw_lsu")
@@ -610,8 +610,9 @@ def ribovision_draw_lsu(
610610
skip_ribovore_filters,
611611
),
612612
"r",
613-
) as f:
614-
for line in f.readlines():
613+
encoding="utf-8",
614+
) as f_ribotyper:
615+
for line in f_ribotyper.readlines():
615616
rnacentral_id, model_id, _ = line.split("\t")
616617
ribovision.visualise(
617618
"lsu",
@@ -652,8 +653,9 @@ def ribovision_draw_ssu(
652653
skip_ribovore_filters,
653654
),
654655
"r",
655-
) as f:
656-
for line in f.readlines():
656+
encoding="utf-8",
657+
) as f_ribotyper:
658+
for line in f_ribotyper.readlines():
657659
rnacentral_id, model_id, _ = line.split("\t")
658660
ribovision.visualise(
659661
"ssu",
@@ -672,7 +674,6 @@ def rfam_group():
672674
"""
673675
Use Rfam templates for structure visualisation.
674676
"""
675-
pass
676677

677678

678679
@rfam_group.command("blacklisted")
@@ -732,26 +733,27 @@ def rfam_validate(rfam_accession, output):
732733
be output to the given file, otherwise it will not.
733734
"""
734735
if rfam_accession not in rfam.blacklisted():
735-
output.write(rfam_accession + "\n")
736+
output.write(f"{rfam_accession}\n")
736737

737738

738739
def generate_thumbnail(image, description):
740+
"""Generate a thumbnail SVG as an outline of the 2D diagram."""
739741
move_to_start_position = None
740742
color = ColorHash(description).hex
741743
points = []
742-
for i, line in enumerate(image.split("\n")):
744+
for _, line in enumerate(image.split("\n")):
743745
if "width" in line and not "stroke-width" in line:
744746
width = re.findall(r'width="(\d+(\.\d+)?)"', line)
745747
if "height" in line:
746748
height = re.findall(r'height="(\d+(\.\d+)?)"', line)
747-
for nt in re.finditer(
748-
'<text x="(\d+)(\.\d+)?" y="(\d+)(\.\d+)?".*?</text>', line
749+
for nt_block in re.finditer(
750+
r'<text x="(\d+)(\.\d+)?" y="(\d+)(\.\d+)?".*?</text>', line
749751
):
750-
if "numbering-label" in nt.group(0):
752+
if "numbering-label" in nt_block.group(0):
751753
continue
752754
if not move_to_start_position:
753-
move_to_start_position = f"M{nt.group(1)} {nt.group(3)} "
754-
points.append(f"L{nt.group(1)} {nt.group(3)}")
755+
move_to_start_position = f"M{nt_block.group(1)} {nt_block.group(3)} "
756+
points.append(f"L{nt_block.group(1)} {nt_block.group(3)}")
755757
if len(points) < 200:
756758
stroke_width = "3"
757759
elif len(points) < 500:
@@ -760,8 +762,11 @@ def generate_thumbnail(image, description):
760762
stroke_width = "4"
761763
else:
762764
stroke_width = "2"
763-
thumbnail = '<svg xmlns="http://www.w3.org/2000/svg" width="{}" height="{}"><path style="stroke:{};stroke-width:{}px;fill:none;" d="'.format(
764-
width[0][0], height[0][0], color, stroke_width
765+
thumbnail = (
766+
f'<svg xmlns="http://www.w3.org/2000/svg" '
767+
f'width="{width[0][0]}" height="{height[0][0]}">'
768+
f'<path style="stroke:{color};stroke-width:{stroke_width}px;'
769+
f'fill:none;" d="'
765770
)
766771
thumbnail += move_to_start_position
767772
thumbnail += " ".join(points)
@@ -775,12 +780,12 @@ def organise_metadata(output_folder, result_folders):
775780
"""
776781
tsv_folder = os.path.join(output_folder, "results", "tsv")
777782
os.system(f"mkdir -p {tsv_folder}")
778-
with open(os.path.join(tsv_folder, "metadata.tsv"), "w") as f_out:
783+
with open(os.path.join(tsv_folder, "metadata.tsv"), "w", encoding="utf-8") as f_out:
779784
for folder in result_folders:
780785
hits = os.path.join(folder, "hits.txt")
781786
if not os.path.exists(hits):
782787
continue
783-
with open(hits, "r") as f_hits:
788+
with open(hits, "r", encoding="utf-8") as f_hits:
784789
for line in f_hits.readlines():
785790
if "gtrnadb" in folder:
786791
line = line.replace("PASS", "GtRNAdb")
@@ -902,7 +907,9 @@ def force_draw(
902907
"ribovision_lsu": "RiboVision",
903908
"rnasep": "RNAse P database",
904909
}
905-
with open(os.path.join(metadata_folder, "metadata.tsv"), "a") as f_out:
910+
with open(
911+
os.path.join(metadata_folder, "metadata.tsv"), "a", encoding="utf-8"
912+
) as f_out:
906913
line = f"{seq_id}\t{model_id}\t{label_mapping[model_type]}\n"
907914
f_out.write(line)
908915

@@ -917,7 +924,9 @@ def list_models():
917924
for item in data:
918925
print(item["description"])
919926
lm.check_unique_descriptions(data)
920-
with open(os.path.join(config.DATA, "models.json"), "w") as models_file:
927+
with open(
928+
os.path.join(config.DATA, "models.json"), "w", encoding="utf-8"
929+
) as models_file:
921930
json.dump(data, models_file)
922931

923932

0 commit comments

Comments
 (0)