Skip to content

Commit ab34336

Browse files
committed
lint fix
1 parent ec77331 commit ab34336

File tree

5 files changed

+77
-131
lines changed

5 files changed

+77
-131
lines changed

BRB/ET.py

Lines changed: 35 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import subprocess
33
import os
44
import glob
5-
import csv
65
import json
76
import BRB.misc
87
from BRB.logger import log
@@ -32,54 +31,34 @@ def getNReads(d):
3231
return (int(res) / 4), 0.
3332

3433

35-
def getOffSpeciesRate(d, organism = None):
34+
def getOffSpeciesRate(d, organism = None) -> float:
3635
"""
37-
Get the percentage of off-species reads from a directory
38-
This is copied from the bcl2fastq pipeline
36+
Parses
3937
"""
40-
fname = glob.glob("{}/*_screen.txt".format(d))[0]
38+
fname = glob.glob("{}/*rep".format(d))[0]
4139
if not os.path.exists(fname):
42-
return 0,0
43-
total = 0
44-
species=[]
45-
ohol=[]
46-
mhol=[]
47-
i = 0
48-
maxi = 0
49-
rRNA = ""
50-
rRNA_rate = 0
51-
if organism in ['human', 'mouse']:
52-
rRNA = "{}rRNA".format(organism)
53-
elif organism == 'drosophila':
54-
rRNA = "flyrRNA"
55-
for line in csv.reader(open(fname, "r"), dialect="excel-tab") :
56-
if(len(line) == 0) :
57-
break
58-
if(line[0].startswith("#")) :
59-
print("Hit a #")
60-
continue
61-
if(line[0].startswith("Library")) :
62-
continue
63-
if(line[0].startswith("Genome")) :
64-
continue
65-
if(line[0].startswith("PhiX") or line[0].startswith("Adapters") or line[0].startswith("Vectors") or line[0].startswith("rRNA")):
66-
continue
67-
if((len(rRNA)!=0) and (line[0].startswith(rRNA))):
68-
rRNA_rate = float(line[5]) + float(line[7])
69-
continue
70-
species.append(line[0])
71-
ohol.append(float(line[5]))
72-
73-
if(ohol[maxi] < ohol[i]) :
74-
maxi = i
75-
i += 1
76-
77-
off = 0
78-
for i in range(len(ohol)) :
79-
if(i != maxi) :
80-
off += ohol[i]
81-
print("off is {}, rRNA_rate is {}".format(off, rRNA_rate))
82-
return off, rRNA_rate
40+
return 0
41+
# match parkour org to kraken db organism/group
42+
org_map = {
43+
'Human (GRCh38)': 'humangrp',
44+
'Human (GRCh37 / hg19)': 'humangrp',
45+
'Mouse (GRCm38 / mm10)': 'mousegrp',
46+
'Mouse (GRCm39)': 'mousegrp',
47+
'Escherichia phage Lambda':'lambdaphage',
48+
'Caenorhabditis_elegans': 'c-elegans',
49+
'lamprey': 'sea-lamprey',
50+
'medaka': 'japanese-medaka',
51+
'zebrafish': 'zebrafish',
52+
'drosophila': 'flygrp',
53+
}
54+
if organism not in org_map:
55+
return 0
56+
with open(fname) as f:
57+
for line in f:
58+
if org_map[organism] in line:
59+
off = float(line.strip().split()[0])/100
60+
log.info(f"confident reads for {fname} = {off}")
61+
return off
8362

8463

8564
def getBaseStatistics(config, outputDir, samples_id, organism = None):
@@ -103,12 +82,8 @@ def getBaseStatistics(config, outputDir, samples_id, organism = None):
10382
sampleName = glob.glob("{}/*_R1_fastqc.zip".format(d))[0]
10483
sampleName = os.path.split(sampleName)[1][:-14]
10584
nReads, optDupes = getNReads(d) # opt. dup.
106-
try:
107-
offRate, rRNA_rate = getOffSpeciesRate(d,organism)
108-
except:
109-
offRate = 0
110-
rRNA_rate = 0
111-
baseDict[libName] = [sampleName, nReads, offRate, optDupes, rRNA_rate]
85+
offRate = getOffSpeciesRate(d,organism)
86+
baseDict[libName] = [sampleName, nReads, offRate, optDupes]
11287
s2l[sampleName] = libName
11388
return baseDict, s2l
11489

@@ -126,10 +101,7 @@ def DNA(config, outputDir, baseDict, sample2lib):
126101
sampleName = os.path.basename(fname).split(".Bowtie2_summary")[0]
127102
lastLine = open(fname).read().split("\n")[-2]
128103
mappedPercent = lastLine.split("%")[0]
129-
try:
130-
baseDict[sample2lib[sampleName]].append(float(mappedPercent))
131-
except:
132-
continue
104+
baseDict[sample2lib[sampleName]].append(float(mappedPercent))
133105
# % Duplicated
134106
dup_info = glob.glob("{}/multiQC/multiqc_data/multiqc_samtools_flagstat.txt".format(outputDir))[0]
135107
dup_df = pd.read_csv(dup_info, sep ="\t", usecols=["Sample", "total_passed", "duplicates_passed"])
@@ -155,8 +127,7 @@ def DNA(config, outputDir, baseDict, sample2lib):
155127
'optical_duplicates': v[3],
156128
'dupped_reads': v[6],
157129
'mapped_reads': v[5],
158-
'insert_size': v[7],
159-
'rRNA_rate': v[4]})
130+
'insert_size': v[7]})
160131
return m
161132

162133

@@ -214,22 +185,19 @@ def RNA(config, outputDir, baseDict, sample2lib):
214185
'uniq_mapped': v[6],
215186
'multi_mapped': v[7],
216187
'dupped_reads': v[8],
217-
'assigned_reads': v[9],
218-
'rRNA_rate': v[4]})
188+
'assigned_reads': v[9]})
219189
return m
220190

221191

222192
def sendToParkour(config, msg):
223-
FCID = config.get("Options", "runID").split("_")[3][1:] # C605HACXX from 150416_SN7001180_0196_BC605HACXX
193+
FCID = config.get("Options", "runID").split("_")[3][1:]
224194
if '-' in FCID:
225195
FCID = FCID.split('-')[-1]
226196
d = {'flowcell_id': FCID}
227197
d['sequences'] = json.dumps(msg)
228-
log.info("sendToParkour: Sending {} to Parkour".format(d))
229-
#print("Sending:")
230-
#print("{}".format(d))
231-
#print("To parkour")
198+
log.info(f"sendToParkour: Sending {d} to Parkour")
232199
res = requests.post(config.get("Parkour", "ResultsURL"), auth=(config.get("Parkour", "user"), config.get("Parkour", "password")), data=d, verify=config.get("Parkour", "cert"))
200+
log.info(f"sendToParkour return {res}")
233201

234202

235203
def phoneHome(config, outputDir, pipeline, samples_tuples, organism):
@@ -252,8 +220,7 @@ def phoneHome(config, outputDir, pipeline, samples_tuples, organism):
252220
m.append({'barcode': k,
253221
'reads_pf_sequenced': v[1],
254222
'confident_reads': v[2],
255-
'optical_duplicates': v[3],
256-
'rRNA_rate': v[4]})
223+
'optical_duplicates': v[3]})
257224
msg = m
258225

259226
if msg is not None:
@@ -283,7 +250,6 @@ def telegraphHome(config, group, project, skipList, organism=None):
283250
m.append({'barcode': k,
284251
'reads_pf_sequenced': v[1],
285252
'confident_reads': v[2],
286-
'optical_duplicates': v[3],
287-
'rRNA_rate': v[4]})
253+
'optical_duplicates': v[3]})
288254
sendToParkour(config, m)
289255
return "Sent information on {} libraries from project {} from the {} group to Parkour\n".format(len(skipList), project, group)

BRB/PushButton.py

Lines changed: 35 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
def createPath(config, group, project, organism, libraryType, tuples):
1212
"""Ensures that the output path exists, creates it otherwise, and return where it is"""
13-
if tuples[0][3] == True: # if external data
13+
if tuples[0][3]:
1414
baseDir = "{}/{}/Analysis_{}".format(config.get('Paths', 'baseData'),
1515
config.get('Options', 'runID'),
1616
BRB.misc.pacifier(project))
@@ -29,7 +29,7 @@ def createPath(config, group, project, organism, libraryType, tuples):
2929

3030
def linkFiles(config, group, project, odir, tuples):
3131
"""Create symlinks in odir to fastq files in {project}. Return 1 if paired-end, 0 otherwise."""
32-
if tuples[0][3] == True: # if external data
32+
if tuples[0][3]:
3333
baseDir = "{}/{}/Project_{}".format(config.get('Paths', 'baseData'),
3434
config.get('Options', 'runID'),
3535
BRB.misc.pacifier(project))
@@ -57,20 +57,15 @@ def linkFiles(config, group, project, odir, tuples):
5757

5858
def removeLinkFiles(d):
5959
"""Remove symlinks created by linkFiles()"""
60-
try:
61-
files = glob.glob("{}/originalFASTQ/*_R?.fastq.gz".format(d))
60+
files = glob.glob("{}/originalFASTQ/*_R?.fastq.gz".format(d))
61+
if files:
6262
for fname in files:
6363
os.unlink(fname)
64-
except:
65-
print("check if originalFASTQ exists!")
66-
log.warning("removeLinkeFiles: check if originalFASTQ exists!")
6764
files = glob.glob("{}/*_R?.fastq.gz".format(d))
6865
for fname in files:
6966
os.unlink(fname)
7067

7168

72-
73-
7469
def relinkFiles(config, group, project, organism, libraryType, tuples):
7570
"""
7671
Generate symlinks under the snakepipes originalFASTQ folder directly from the project folder.
@@ -86,10 +81,7 @@ def relinkFiles(config, group, project, organism, libraryType, tuples):
8681
log.info(f"Multiqc report found for {group} project {project}.")
8782
of = Path(config.get('Paths', 'bioinfoCoreDir')) / 'Analysis' + project + '_multiqc.html'
8883
log.info(f"Trying to copy mqc report to {of}.")
89-
try:
90-
shutil.copyfile(mqcf, of)
91-
except:
92-
log.warning(f"Copying {mqcf} to {of} failed.")
84+
shutil.copyfile(mqcf, of)
9385
else:
9486
log.info(f"no multiqc report under {mqcf}.")
9587

@@ -136,15 +128,10 @@ def copyCellRanger(config, d):
136128
short_fid = str(os.path.basename(lane_dir)).split('_')[2] + '_'
137129
bioinfoCoreDirPath = Path(config.get('Paths', 'bioinfoCoreDir')) / Path(short_fid + nname)
138130
nname = seqfac_lane_dir / nname
139-
try:
140-
shutil.copyfile(fname, nname)
141-
except:
142-
log.warning("copyCellRanger from {} to {} failed.".format(fname, nname))
131+
shutil.copyfile(fname, nname)
143132
# to bioinfocore dir
144-
try:
145-
shutil.copyfile(fname, bioinfoCoreDirPath)
146-
except:
147-
log.warning("copyCellRanger from {} to {} failed.".format(fname, bioinfoCoreDirPath))
133+
shutil.copyfile(fname, bioinfoCoreDirPath)
134+
log.warning("copyCellRanger from {} to {} failed.".format(fname, bioinfoCoreDirPath))
148135

149136

150137
def copyRELACS(config, d):
@@ -176,49 +163,41 @@ def copyRELACS(config, d):
176163
seqfac_lane_dir = Path(config.get('Paths', 'seqFacDir')) / year_postfix / lane_dir
177164
os.makedirs(seqfac_lane_dir, exist_ok=True)
178165
nname = seqfac_lane_dir / nname
179-
try:
180-
shutil.copyfile(fname, nname)
181-
except:
182-
log.warning("copyCellRanger from {} to {} failed.".format(fname, nname))
183-
166+
shutil.copyfile(fname, nname)
184167

185168
def tidyUpABit(d):
186169
"""
187170
Reduce the number of files in the analysis folder.
188171
"""
189-
try:
190-
shutil.rmtree(os.path.join(d, 'cluster_logs'))
191-
os.unlink(os.path.join(d, 'config.yaml'))
192-
shutil.rmtree(os.path.join(d, '.snakemake'))
193-
# multiqc data
194-
mqc_log = os.path.join(d, 'multiQC', 'multiqc_data', 'multiqc.log')
195-
mqc_out = os.path.join(d, 'multiQC', 'multiQC.out')
196-
mqc_err = os.path.join(d, 'multiQC', 'multiQC.err')
197-
if os.path.exists(mqc_log):
198-
os.unlink(mqc_log)
199-
if os.path.exists(mqc_out):
200-
os.unlink(mqc_out)
201-
if os.path.exists(mqc_err):
202-
os.unlink(mqc_err)
203-
204-
for f in glob.glob(os.path.join(d, '*.log')):
205-
os.unlink(f)
206-
207-
for d2 in glob.glob(os.path.join(d, 'FASTQ*')):
208-
shutil.rmtree(d2)
209-
except:
210-
pass
172+
shutil.rmtree(os.path.join(d, 'cluster_logs'))
173+
os.unlink(os.path.join(d, 'config.yaml'))
174+
shutil.rmtree(os.path.join(d, '.snakemake'))
175+
# multiqc data
176+
mqc_log = os.path.join(d, 'multiQC', 'multiqc_data', 'multiqc.log')
177+
mqc_out = os.path.join(d, 'multiQC', 'multiQC.out')
178+
mqc_err = os.path.join(d, 'multiQC', 'multiQC.err')
179+
if os.path.exists(mqc_log):
180+
os.unlink(mqc_log)
181+
if os.path.exists(mqc_out):
182+
os.unlink(mqc_out)
183+
if os.path.exists(mqc_err):
184+
os.unlink(mqc_err)
185+
186+
for f in glob.glob(os.path.join(d, '*.log')):
187+
os.unlink(f)
188+
189+
for d2 in glob.glob(os.path.join(d, 'FASTQ*')):
190+
shutil.rmtree(d2)
191+
211192

212193
def stripRights(d):
213194
# Strip rights.
214-
try:
215-
for r, dirs, files in os.walk(d):
216-
for d in dirs:
217-
os.chmod(os.path.join(r, d), stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP)
218-
for f in files:
219-
os.chmod(os.path.join(r, f), stat.S_IRWXU | stat.S_IRGRP)
220-
except:
221-
pass
195+
for r, dirs, files in os.walk(d):
196+
for d in dirs:
197+
os.chmod(os.path.join(r, d), stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP)
198+
for f in files:
199+
os.chmod(os.path.join(r, f), stat.S_IRWXU | stat.S_IRGRP)
200+
222201

223202
def touchDone(outputDir, fname="analysis.done"):
224203
open(os.path.join(outputDir, fname), "w").close()

BRB/demultiplex_relacs.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import glob
66
from multiprocessing import Pool
77
import subprocess
8-
import gzip
98
import matplotlib.pyplot as plt
109
import numpy as np
1110
import editdistance as ed

BRB/run.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
#!/usr/bin/env python
22
import sys
33
import os
4-
import datetime
5-
import time
6-
import syslog
7-
import argparse
84
import BRB.getConfig
95
import BRB.findFinishedFlowCells
106
import BRB.PushButton
117
import BRB.email
128
import BRB.ET
139
import BRB.misc
1410
from BRB.logger import setLog, log
15-
import importlib
1611
import rich_click as click
1712
from time import sleep
1813

pyproject.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,10 @@ dependencies = [
3232
[project.scripts]
3333
BigRedButton = "BRB.run:run_brb"
3434
demultiplex_relacs = "BRB.demultiplex_relacs:main"
35+
36+
[tool.ruff]
37+
ignore = [
38+
"E722",
39+
"F841",
40+
"E712"
41+
]

0 commit comments

Comments
 (0)