Skip to content

Commit ce06417

Browse files
authored
Add fuzzy regex search (#24)
* check for min consecutive repeats of motif in clipped segments * Use fuzzy motifs and format arg names correctly * create and search with regex motifs * search with min repeats of regex patterns * Update args and use fuzzy * Pythonic arg names.
1 parent 769e77b commit ce06417

7 files changed

Lines changed: 320 additions & 334 deletions

File tree

README.md

Lines changed: 67 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ In most eukaryotic species, chromosomes terminate in repetitive [telomeric](http
4040

4141
Teloclip is designed to recover long-reads that can be used to extend draft contigs and resolve missing telomeres (short-read alignments may also be processed with teloclip). It does this by searching alignments of raw long-read data (i.e. Pacbio or ONT reads mapped with Minimap2) for 'clipped' alignments that occur at the ends of draft contigs. A 'clipped' alignment is produced where the *end* of a read is not part of its best alignment. This can occur when a read extends past the end of an assembled contig.
4242

43-
Information about segments of a read that were aligned or clipped are stored in [SAM formatted](https://en.wikipedia.org/wiki/SAM_(file_format)) alignments as a [CIGAR string](https://www.drive5.com/usearch/manual/cigar.html). Teloclip parses these strings to determine if a read has been clipped at one or both ends of a contig.
43+
Information about segments of a read that were aligned or clipped are stored in [SAM formatted](https://en.wikipedia.org/wiki/SAM_(file_format)) alignments as a [CIGAR string](https://www.drive5.com/usearch/manual/cigar.html). Teloclip parses these strings to determine if a read has been clipped at one or both ends of a contig.
4444

4545
Optionally, teloclip can screen overhanging reads for telomere-associated motifs (i.e. 'TTAGGG' / 'CCCTAA') and report only those containing a match.
4646

@@ -54,7 +54,7 @@ Teloclip requires Python >= 3.8.
5454

5555
There are 4 options available for installing Teloclip locally:
5656

57-
1) Install from PyPi.
57+
1) Install from PyPi.
5858
This or Bioconda will get you the latest stable release.
5959

6060
```bash
@@ -94,10 +94,6 @@ teloclip --version
9494
teloclip --help
9595
```
9696

97-
### Run with Gitpod
98-
99-
Alternatively, [launch a Gitpod Workspace](https://gitpod.io/#https://github.com/adamtaranto/teloclip) with `teloclip`, `samtools`, and `minimap2` pre-installed.
100-
10197
## Example Usage
10298

10399
Basic use case:
@@ -117,10 +113,10 @@ samtools faidx ref.fa
117113

118114
```bash
119115
# Read alignment input from sam file and write overhang-reads to stout
120-
teloclip --ref ref.fa.fai in.sam
116+
teloclip --ref-idx ref.fa.fai in.sam
121117

122118
# Read alignment input from stdin and write stdout to file
123-
teloclip --ref ref.fa.fai < in.sam > out.sam
119+
teloclip --ref-idx ref.fa.fai < in.sam > out.sam
124120
```
125121

126122
**Reading and writing BAM alignments**
@@ -130,7 +126,7 @@ You can use bam files with teloclip like this:
130126

131127
```bash
132128
# Read alignments from bam file, pipe sam lines to teloclip, sort overhang-read alignments and wite to bam file
133-
samtools view -h in.bam | teloclip --ref ref.fa.fai | samtools sort > out.bam
129+
samtools view -h in.bam | teloclip --ref-idx ref.fa.fai | samtools sort > out.bam
134130
```
135131

136132
**Streaming SAM records from aligner**
@@ -139,43 +135,40 @@ samtools view -h in.bam | teloclip --ref ref.fa.fai | samtools sort > out.bam
139135
# Map PacBio long-reads to ref assembly,
140136
# return alignments clipped at contig ends,
141137
# write to sorted bam.
142-
minimap2 -ax map-pb ref.fa pacbio_reads.fq.gz | teloclip --ref ref.fa.fai | samtools sort > out.bam
138+
minimap2 -ax map-pb ref.fa pacbio_reads.fq.gz | teloclip --ref-idx ref.fa.fai | samtools sort > out.bam
143139

144140
# Map reads to reference,
145141
# Exclude non-primary alignments.
146142
# Return alignments clipped at contig ends,
147143
# write to sorted bam.
148-
minimap2 -ax map-pb ref.fa pacbio_reads.fq.gz | samtools view -h -F 0x100 | teloclip --ref ref.fa.fai | samtools sort > out.bam
144+
minimap2 -ax map-pb ref.fa pacbio_reads.fq.gz | samtools view -h -F 0x100 | teloclip --ref-idx ref.fa.fai | samtools sort > out.bam
149145
```
150146

151147
**Report clipped alignments containing target motifs**
152148

153149
```bash
154150
# Report alignments which are clipped at a contig end
155151
# AND contain >=1 copy of the telomeric repeat "TTAGGG" (or its reverse complement "CCCTAA") in the clipped region.
156-
samtools view -h in.bam | teloclip --ref ref.fa.fai --motifs TTAGGG | samtools sort > out.bam
152+
samtools view -h in.bam | teloclip --ref-idx ref.fa.fai --motifs TTAGGG | samtools sort > out.bam
157153

158154
# Report alignments which are clipped at a contig end
159155
# AND contain >=1 copy of the telomeric repeat "TTAGGG" (or its reverse complement "CCCTAA") ANYWHERE in the read.
160-
samtools view -h in.bam | teloclip --ref ref.fa.fai --motifs TTAGGG --matchAny | samtools sort > out.bam
156+
samtools view -h in.bam | teloclip --ref-idx ref.fa.fai --motifs TTAGGG --match-anywhere | samtools sort > out.bam
161157

162-
# To change the minimum number of consecutive repeats required for a match, simply extend the search motif.
163-
# In this example 3 TTAGGG are required for a positive match.
164-
samtools view -h in.bam | teloclip --ref ref.fa.fai --motifs TTAGGGTTAGGGTTAGGG | samtools sort > out.bam
158+
# To change the minimum number of consecutive motif repeats required for a match, set "--min-repeats"
159+
samtools view -h in.bam | teloclip --ref-idx ref.fa.fai --motifs TTAGGG --min-repeats 4 | samtools sort > out.bam
165160

166161
```
167162

168163
**Matching noisy target motifs**
169164

170-
Raw long-reads can contain errors in the length of homopolymer tracks. If the `--fuzzy` option is set, motifs will be converted to regex patterns that allow the number of repeated bases to vary by +/- 1.
165+
Raw long-reads can contain errors in the length of homopolymer tracks. If the `--fuzzy` option is set, motifs will be converted to regex patterns that allow the number of repeated bases to vary by +/- 1.
171166
i.e. "TTAGGG" -> "T{1,3}AG{2,4}". This pattern will match TTAGG TTAGGGG TAGG TTTAGGG etc.
172167

173-
To reduce off target matching you can increase to minimum required number of motif matches with "--min_repeats".
168+
To reduce off target matching you can increase the minimum required number of sequential motif matches with "--min-repeats".
174169

175170
```bash
176-
# Compress homopolymers in query motifs and clipped regions to compensate for errors in raw PacBio or ONP data.
177-
# i.e. The motif 'TTAGGGTTAGGG' becomes 'TAGTAG' and will match 'TTTTTAAAGGTTTAAGGG'.
178-
samtools view -h in.bam | teloclip --ref ref.fa.fai --noPoly --motifs TTAGGGTTAGGG | samtools sort > out.bam
171+
samtools view -h in.bam | teloclip --ref-idx ref.fa.fai --fuzzy --motifs TTAGGG --min-repeats 4 | samtools sort > out.bam
179172
```
180173

181174
**Extract clipped reads**
@@ -186,7 +179,7 @@ Collections of reads that overhang a contig end can be assembled with `miniasm`
186179

187180
```bash
188181
# Find clipped alignments containing motif 'TTAGGG' and write reads to separate fasta files for each reference contig end.
189-
samtools view -h in.bam | teloclip --ref ref.fa.fai --motifs TTAGGG | teloclip-extract --refIdx ref.fa.fai --extractReads --extractDir SplitOverhangs
182+
samtools view -h in.bam | teloclip --ref-idx ref.fa.fai --motifs TTAGGG | teloclip-extract --ref-idx ref.fa.fai --extract-reads --extract-dir split_overhangs_by_contig
190183
```
191184

192185
### Optional Quality Control
@@ -199,7 +192,7 @@ In some cases it may be also be useful to prioritise primary alignments. This ca
199192

200193
```bash
201194
# Exclude secondary alignments.
202-
samtools view -h -F 0x100 in.sam | teloclip --ref ref.fa.fai > noSA.sam
195+
samtools view -h -F 0x100 in.sam | teloclip --ref-idx ref.fa.fai > noSA.sam
203196
```
204197

205198
**Pre-corrected Data**
@@ -208,118 +201,88 @@ Some assembly tools, such as [Canu](https://github.com/marbl/canu), preform pre-
208201

209202
This trimming step can result in loss of distal telomeric sequences and so these reads should **NOT** be used with Teloclip.
210203

211-
212204
However, long-reads that have been error-corrected using Illumina data with tools such as [LoRDEC](https://github.com/lanl001/halc) or [HALC](http://www.atgc-montpellier.fr/lordec/) should be fine.
213205

214206
Generally speaking, raw long-reads will be fine for extending your contigs. Any errors in the extended region can be corrected with a round of polishing with short-read data using [Pilon](https://github.com/broadinstitute/pilon).
215207

216208
### Extending contigs
217209

218-
Before using terminal alignments identified by Teloclip to extend contigs you should inspect the alignments in a genome browser that displays information about clipped reads, such as [IGV](https://github.com/igvteam/igv).
210+
Before using terminal alignments identified by Teloclip to extend contigs you should inspect the alignments in a genome browser that displays information about clipped reads, such as [IGV](https://github.com/igvteam/igv).
219211

220212
Check for conflicting soft-clipped sequences. These indicate non-specific read alignments. You may need to tighten your alignment criteria or manually remove low-confidence alignments.
221213

222214
After manually extending contigs the revised assembly should be re-polished using available long and short read data to correct indels present in the raw long-reads.
223215

224216
Finally, validate the updated assembly by re-mapping long-read data and checking for alignments that extend into revised contig ends.
225217

226-
227-
### Alternative use cases
228-
229-
**Illumina data**
230-
231-
Teloclip will also work fine with aligned short read data, which has a far lower error rate than single-molecule long-read data.
232-
233-
However, there are obvious limits to the distance that a contig may be extended with shorter reads.
234-
235-
Teloclip does not use information from paired-reads.
236-
237-
**Merging existing assemblies**
238-
239-
You may have assemblies for your genome generated with different assemblers/configurations or data types (i.e. Illumina, PacBio, ONT) which vary in their success in assembling individual telomeres.
240-
241-
These alternative assemblies can be treated as pseudo-long-reads and aligned to a reference using [Minimap2](https://github.com/lh3/minimap2).
242-
243-
Teloclip can identify aligned contigs that can be used to extend those in the reference set.
244-
245-
Be cautious of short contigs that may align to may repetative sub-telomeric regions and result non-specific extension of contigs.
246-
247-
Also beware of low-complexity telomeric regions on different chromosomes aligning to each other and resulting in end-to-end fusions.
248-
249-
250-
```bash
251-
# Align alternative assembly contigs to reference and report overhang alignments. Ignore secondary alignments.
252-
minimap2 -ax asm5 ref.fa asm.fa | samtools view -h -F 0x100 | teloclip --ref ref.fa.fai | samtools sort > asm2ref.bam
253-
```
254-
255-
**Circularising Mitochondrial / Bacterial genomes**
256-
257-
Using default settings, teloclip will report alignments with clipped regions extending past linear contig ends.
258-
259-
Reads can be extracted from these alignments using [circlator's bam2reads](https://github.com/sanger-pathogens/circlator/wiki/Task%3A-bam2reads) and re-aligned to an assembly graph in [Bandage](https://github.com/rrwick/Bandage) to help identify uncircularised contigs.
260-
261218
## Options
262219

263220
### Teloclip Options
264221

265222
Run `teloclip --help` to view the programs' most commonly used options:
266223

267-
```
268-
Usage: teloclip [-h] [--version] --refIdx REFIDX [--minClip MINCLIP] [--maxBreak MAXBREAK]
269-
[--motifs MOTIFS] [--noRev NOREV] [--noPoly NOPOLY] [--matchAny MATCHANY]
224+
```code
225+
usage: teloclip [-h] --ref-idx REF_IDX [--min-clip MIN_CLIP] [--max-break MAX_BREAK]
226+
[--motifs MOTIFS] [--no-rev] [--fuzzy] [-r MIN_REPEATS]
227+
[--min-anchor MIN_ANCHOR] [--match-anywhere] [-v]
270228
[samfile]
271229
272-
Required:
273-
--refIdx REFIDX Path to fai index for reference fasta. Index fasta using `samtools faidx FASTA`
274-
275-
Positional arguments:
276-
samfile Input SAM can be added as the first positional argument after flagged options.
277-
If not set teloclip will read from stdin.
278-
279-
Optional:
280-
--minClip Require clip to extend past ref contig end by at least N bases.
281-
Default: 1
282-
--maxBreak Tolerate max N unaligned bases at contig ends.
283-
Default: 50
284-
--motifs If set keep only reads containing given motif/s from a comma delimited list
285-
of strings. By default also search for reverse complement of motifs.
286-
i.e. TTAGGG,TTAAGGG will also match CCCTAA,CCCTTAA
287-
Default: None
288-
--noRev If set do NOT search for reverse complement of specified motifs.
289-
Default: Find motifs on both strands.
290-
--noPoly If set collapse homopolymer tracks within motifs before searching overhangs.
291-
i.e. "TTAGGGTTAGGGTTAGGGTTAGGGTTAGGG" -> "TAGTAGTAGTAGTAG".
292-
Useful for PacBio or ONP long reads homopolymer length errors. Defaut: Off.
293-
--matchAny If set motif match may occur in unclipped region of alignment.
294-
Defaut: False
295-
--version Show program's version number and exit.
230+
Filter SAM file for clipped alignments containing unassembled telomeric repeats.
231+
232+
positional arguments:
233+
samfile
234+
235+
options:
236+
-h, --help show this help message and exit
237+
--ref-idx REF_IDX Path to fai index for reference fasta. Index fasta using `samtools
238+
faidx FASTA`
239+
--min-clip MIN_CLIP Require clip to extend past ref contig end by at least N bases.
240+
--max-break MAX_BREAK
241+
Tolerate max N unaligned bases before contig end.
242+
--motifs MOTIFS If set keep only reads containing given motif/s from comma
243+
delimited list of strings. By default also search for reverse
244+
complement of motifs. i.e. TTAGGG,TTAAGGG will also match
245+
CCCTAA,CCCTTAA
246+
--no-rev If set do NOT search for reverse complement of specified motifs.
247+
--fuzzy If set, tolerate +/- 1 variation in motif homopolymer runs i.e.
248+
TTAGGG -> T{1,3}AG{2,4}. Default: Off
249+
-r MIN_REPEATS, --min-repeats MIN_REPEATS
250+
Minimum number of sequential pattern matches required for a hit to
251+
be reported. Default: 3
252+
--min-anchor MIN_ANCHOR
253+
Minimum number of aligned bases (anchor) required on the non-
254+
clipped portion of the read. Default: 500
255+
--match-anywhere If set, motif match may occur in unclipped region of reads.
256+
-v, --version show program's version number and exit
296257
```
297258

298259
### Teloclip-extract Options
299260

300261
Run `teloclip-extract --help` to view the programs' most commonly used options:
301262

302-
```
303-
Usage: teloclip-extract [-h] --refIdx REFIDX [--prefix PREFIX]
304-
[--extractReads] [--extractDir EXTRACTDIR]
305-
[--minClip MINCLIP] [--maxBreak MAXBREAK] [--version]
263+
```code
264+
usage: teloclip-extract [-h] --ref-idx REF_IDX [--prefix PREFIX] [--extract-reads]
265+
[--extract-dir EXTRACT_DIR] [--min-clip MIN_CLIP]
266+
[--max-break MAX_BREAK] [-v]
306267
[samfile]
307268
269+
Extract overhanging reads for each end of each reference contig. Write to fasta.
270+
308271
positional arguments:
309-
samfile If not set, will read sam from stdin.
310-
311-
optional arguments:
312-
-h, --help Show this help message and exit
313-
--refIdx Path to fai index for reference fasta. Index fasta
314-
using `samtools faidx FASTA`
315-
--prefix Use this prefix for output files. Default: None.
316-
--extractReads If set, write overhang reads to fasta by contig.
317-
--extractDir
272+
samfile
273+
274+
options:
275+
-h, --help show this help message and exit
276+
--ref-idx REF_IDX Path to fai index for reference fasta. Index fasta using `samtools
277+
faidx FASTA`
278+
--prefix PREFIX Use this prefix for output files. Default: None.
279+
--extract-reads If set, write overhang reads to fasta by contig.
280+
--extract-dir EXTRACT_DIR
318281
Write extracted reads to this directory. Default: cwd.
319-
--minClip Require clip to extend past ref contig end by at least
320-
N bases.
321-
--maxBreak Tolerate max N unaligned bases at contig ends.
322-
--version Show program's version number and exit
282+
--min-clip MIN_CLIP Require clip to extend past ref contig end by at least N bases.
283+
--max-break MAX_BREAK
284+
Tolerate max N unaligned bases before contig end.
285+
-v, --version show program's version number and exit
323286
```
324287

325288
## Citing Teloclip
@@ -354,7 +317,6 @@ Xu, Z., Wang, G., Zhu, X. et al. Genome assembly of two allotetraploid cotton ge
354317

355318
Yang, H.P., Wenzel, M., Hauser, D.A., Nelson, J.M., Xu, X., Eliáš, M. and Li, F.W., 2021. Monodopsis and Vischeria genomes shed new light on the biology of eustigmatophyte algae. Genome biology and evolution, 13(11), p.evab233.
356319

357-
358320
## Issues
359321

360322
Submit feedback to the [Issue Tracker](https://github.com/Adamtaranto/teloclip/issues)

0 commit comments

Comments
 (0)