Skip to content

Commit 2244108

Browse files
committed
Fix flair-quantify error when -output_bam is not specified (fixes #433)
1 parent 6c99e87 commit 2244108

File tree

4 files changed

+36
-17
lines changed

4 files changed

+36
-17
lines changed

src/flair/count_sam_transcripts.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def check_splicesites(coveredpos, exonpos, tstart, tend):
124124
def get_matchvals(args, md):
125125
matchvals = []
126126
if args.stringent or args.check_splice:
127-
mdblocks = re.findall('\d+|\D+', md) # ['531', '^CCAGGTGAGCCGCCCGCG', '50', 'G', '2031']
127+
mdblocks = re.findall(r'\d+|\D+', md) # ['531', '^CCAGGTGAGCCGCCCGCG', '50', 'G', '2031']
128128
for b in mdblocks:
129129
if b[0] != '^':
130130
if b.isnumeric():
@@ -234,17 +234,20 @@ def parsesam(args, transcripttoexons):
234234
##for transcriptome alignment, always take rightmost side on transcript
235235
if args.remove_internal_priming:
236236
notinternalpriming = remove_internal_priming.removeinternalpriming(read.reference_name,
237-
read.reference_start,
238-
read.reference_end, False,
239-
genome, None, transcripttoexons,
240-
args.intprimingthreshold,
241-
args.intprimingfracAs)
237+
read.reference_start,
238+
read.reference_end, False,
239+
genome, None, transcripttoexons,
240+
args.intprimingthreshold,
241+
args.intprimingfracAs)
242242
else: notinternalpriming = True
243243
if notinternalpriming:
244244
pos = read.reference_start
245-
alignscore = read.get_tag('AS')
245+
try:
246+
alignscore = read.get_tag('AS')
247+
mdtag = read.get_tag('MD')
248+
except KeyError as ex:
249+
raise Exception(f"Missing AS or MD tag in alignment of '{read.query_name}' in '{args.sam.name}'") from ex
246250
cigar = read.cigartuples
247-
mdtag = read.get_tag('MD')
248251
tlen = samfile.get_reference_length(transcript)
249252
if lastread and readname != lastread:
250253
assignedt = getbesttranscript(curr_transcripts, args, transcripttoexons)

src/flair/flair_quantify.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#! /usr/bin/env python3
22

3+
import os
34
import sys
45
import re
56
import argparse
@@ -88,7 +89,7 @@ def quantify(isoform_sequences=''):
8889
readFileRoot = tempfile.NamedTemporaryFile().name
8990
if args.temp_dir != '':
9091
if not os.path.isdir(args.temp_dir):
91-
if subprocess.call(['mkdir', args.temp_dir]):
92+
if subprocess.call(['mkdir', '-p', args.temp_dir]):
9293
sys.stderr.write('Could not make temporary directory {}\n'.format(args.temp_dir))
9394
return 1
9495
readFileRoot = args.temp_dir + '/' + readFileRoot[readFileRoot.rfind('/')+1:]
@@ -101,9 +102,7 @@ def quantify(isoform_sequences=''):
101102

102103
for num, sample in enumerate(samData, 0):
103104
sys.stderr.write('Step 1/3. Aligning sample %s_%s, %s/%s \n' % (sample[0], sample[2], num+1, len(samData)))
104-
if args.output_bam: mm2_command = ['minimap2', '--MD', '-a', '-N', '4', '-t', str(args.t), args.i, sample[-2]]
105-
else: mm2_command = ['minimap2', '-a', '-N', '4', '-t', str(args.t), args.i, sample[-2]]
106-
# mm2_command = ['minimap2', '-a', '-N', '4', '-t', str(args.t), args.i, sample[-2]]
105+
mm2_command = ['minimap2', '--MD', '-a', '-N', '4', '-t', str(args.t), args.i, sample[-2]]
107106

108107
# TODO: Replace this with proper try/except Exception as ex
109108
try:
@@ -140,8 +139,7 @@ def quantify(isoform_sequences=''):
140139
if args.generate_map or args.output_bam:
141140
count_cmd += ['--generate_map', args.o+'.'+sample+'.'+group+'.isoform.read.map.txt']
142141

143-
if subprocess.call(count_cmd):
144-
return 1
142+
subprocess.check_call(count_cmd)
145143
for line in open(samOut+'.counts.txt'):
146144
line = line.rstrip().split('\t')
147145
iso, numreads = line[0], line[1]
@@ -206,5 +204,6 @@ def quantify(isoform_sequences=''):
206204
return args.o+'.counts.tsv'
207205

208206
if __name__ == '__main__':
209-
quantify()
210-
207+
# FIXME: need proper error handling
208+
status = quantify()
209+
os.exit(status)

test/Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ test-collapse-support : mkdirs ${GENOME_FA}
146146
###
147147
# FLAIR QUANTIFY
148148
###
149-
quantify-tests: test-quantify test-quantify-help
149+
quantify-tests: test-quantify test-quantify-nobam test-quantify-help
150150

151151
test-quantify : mkdirs
152152
rm -rf $O/$@.tmp
@@ -155,6 +155,12 @@ test-quantify : mkdirs
155155
diff $E/$@.tpm.tsv $O/$@.tpm.tsv
156156
diff $E/$@.A1.A.flair.aligned.readtotranscript.txt $O/$@.A1.A.flair.aligned.readtotranscript.txt
157157

158+
# without map or bam output
159+
test-quantify-nobam : mkdirs
160+
rm -rf $O/$@.tmp
161+
flair quantify -r $(READS_MANIFEST) -i $(ISOFORMS_FA) --isoform_bed $(ISOFORMS_BED) --temp_dir $O/$@.tmp --tpm --sample_id_only -o $O/$@
162+
diff $E/$@.tpm.tsv $O/$@.tpm.tsv
163+
158164
test-quantify-help: mkdirs
159165
flair quantify --help >&$O/$@.out
160166
diff $E/$@.out $O/$@.out
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
ID A1 A2 A3 B1 B2 B3
2+
ENST00000225792.10_ENSG00000108654.15 395061.7283950617 364161.84971098264 345679.012345679 263157.8947368421 315217.39130434784 376470.5882352941
3+
ENST00000256078.9_ENSG00000133703.12 0.0 5780.346820809248 6172.839506172839 47846.889952153106 27173.91304347826 5882.35294117647
4+
ENST00000311936.8_ENSG00000133703.12 111111.11111111111 92485.54913294797 98765.43209876542 114832.53588516745 103260.86956521739 94117.64705882352
5+
ENST00000348547.6_ENSG00000125991.19 111111.11111111111 104046.24277456648 123456.79012345678 172248.8038277512 135869.5652173913 117647.0588235294
6+
ENST00000357394.8_ENSG00000125991.19 37037.03703703704 23121.387283236993 55555.555555555555 33492.82296650718 43478.260869565216 52941.17647058823
7+
ENST00000451605.1_ENSG00000125991.19 74074.07407407407 167630.0578034682 98765.43209876542 138755.98086124402 146739.13043478262 94117.64705882352
8+
ENST00000557334.5_ENSG00000133703.12 18518.51851851852 0.0 24691.358024691355 43062.2009569378 86956.52173913043 23529.41176470588
9+
HISEQ:1287:HKCG7BCX3:1:1101:12283:18224-0_ENSG00000125991.19 6172.839506172839 0.0 6172.839506172839 0.0 0.0 5882.35294117647
10+
HISEQ:1287:HKCG7BCX3:1:1103:5649:96402-0_ENSG00000108654.15 135802.46913580247 86705.20231213873 141975.3086419753 76555.02392344497 48913.04347826087 135294.11764705883
11+
HISEQ:1287:HKCG7BCX3:1:1106:15192:70510-0_ENSG00000108654.15 111111.11111111111 156069.36416184972 98765.43209876542 110047.84688995215 92391.30434782608 94117.64705882352

0 commit comments

Comments
 (0)