Trim Adapter Sequences

Foundational Python Foundations Python String Methods Slicing Read Trimming
Significance:

Sequencing adapters are short synthetic sequences ligated to both ends of a DNA
fragment. When the fragment is shorter than the read length, the sequencer reads straight through into
the adapter, and those bases must be removed before alignment or they cause soft-clipping and mapping
errors. Adapter trimming is what cutadapt and Trimmomatic exist to do, and the core logic is simple
string searching.

Statement

You are given an adapter sequence and a set of reads.

For each read, if the adapter appears anywhere in it, remove the adapter and everything after it.
If the adapter does not appear, leave the read unchanged.

Print each processed read on its own line, in input order. If trimming leaves a read empty, print an
empty line for it.

On the final line, print how many reads were trimmed.

Input — read from standard input
Variable Type Description
adapter
line 1
str The adapter sequence to search for
1 <= len(adapter) <= 50, uppercase A, C, G, T only
n
line 2
int The number of reads that follow
1 <= n <= 200
reads
line 3..n+2
str One read per line
1 <= len(read) <= 500, uppercase A, C, G, T only

These variables are already read for you in the starter code on the right.

Output

str processed reads one per line in input order, then a final line with the count of trimmed reads

Sample Cases
Sample 1
Input
AGATCGGAAGAG
3
ACGTACGTAGATCGGAAGAGTTTT
ACGTACGTACGT
AGATCGGAAGAGACGT
Expected Output
ACGTACGT
ACGTACGTACGT

2
The first read is trimmed mid-sequence, the second is untouched, the third becomes empty.
Sample 2
Input
TTTT
2
ACGTTTTACGT
ACGACG
Expected Output
ACG
ACGACG
1
One read contains the adapter and one does not.

Submit also runs your code against 3 hidden test cases. Hidden inputs are never shown — if one fails you'll get its number and a description of the mismatch, not the data.

Constraints
  • Only the first occurrence of the adapter matters; everything from it onward is removed
  • Reads with no adapter occurrence pass through unchanged
  • A read that begins with the adapter becomes empty and prints as an empty line
  • 1 <= n <= 200
Further Reading
  • str.find() returns -1 when the substring is absent, which makes the two cases easy to separate.
  • Slice with read[:index] to keep only the part before the adapter.
  • Count a read as trimmed only when the adapter was actually found.

My Notes
Log in to save personal notes.
Console output will appear here when you click Run Code or Submit...
Expected: adapter (str), n (int), reads (remaining lines)
Next Problem
Sequence Statistics with Comprehensions