Skip to content

Commit 5d98061

Browse files
committed
readfq func
1 parent 15f3b0f commit 5d98061

2 files changed

Lines changed: 33 additions & 43 deletions

File tree

scripts/delly2bnd.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#! /usr/bin/env python
22

33
from __future__ import print_function
4-
from .readfq import readfq
54
import argparse
65
import sys
76
import cyvcf2
@@ -17,6 +16,39 @@ def argument_parsing():
1716
parser.add_argument('-o', '--out', metavar='out.vcf', required=True, dest='out', help='output VCF file (required)')
1817
return parser.parse_args()
1918

19+
"""Heng Li's FASTA/FASTQ parser
20+
"""
21+
def readfq(fp): # this is a generator function
22+
last = None # this is a buffer keeping the last unprocessed line
23+
while True: # mimic closure; is it a bad idea?
24+
if not last: # the first record or a record following a fastq
25+
for l in fp: # search for the start of the next record
26+
if l[0] in '>@': # fasta/q header line
27+
last = l[:-1] # save this line
28+
break
29+
if not last: break
30+
name, seqs, last = last[1:].partition(" ")[0], [], None
31+
for l in fp: # read the sequence
32+
if l[0] in '@+>':
33+
last = l[:-1]
34+
break
35+
seqs.append(l[:-1])
36+
if not last or last[0] != '+': # this is a fasta record
37+
yield name, ''.join(seqs), None # yield a fasta record
38+
if not last: break
39+
else: # this is a fastq record
40+
seq, leng, seqs = ''.join(seqs), 0, []
41+
for l in fp: # read the quality
42+
seqs.append(l[:-1])
43+
leng += len(l) - 1
44+
if leng >= len(seq): # have read enough quality
45+
last = None
46+
yield name, seq, ''.join(seqs); # yield a fastq record
47+
break
48+
if last: # reach EOF before reading enough quality
49+
yield name, seq, None # yield a fasta record instead
50+
break
51+
2052
def delly2bnd(args):
2153
# Fetch breakpoint positions
2254
bndPos = collections.defaultdict(dict)

scripts/readfq.py

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)