How to fragment genomes into non-overlapping sequences of differing sizes? I have bacterial chromosomes/plasmids that I have downloaded off refseq in a multi-fasta that I would like to fragment into non-overlapping fragments of between 1 kb and 15 kb.
I have tried googling for related tools but to no avail. Does anyone know how I would achieve this? Something very similar is done in this paper:
https://link.springer.com/article/10.1007/s40484-019-0187-4
Thanks in advance!
You could use numpy.random.randint to sample integers from a uniform distribution, from 1000 to 15000 nt, extracting subsequences of that length from a linearized multi-FASTA file.
Here's some code:
#!/usr/bin/env python

import sys
import numpy as np

low = int(sys.argv[1])
high = int(sys.argv[2])

def process_record(header, sequence, low, high):
    next = 0
    fragment = 1
    while next &lt; len(sequence):
        new_next = np.random.randint(low, high) + next  
        if new_next &gt; len(sequence) - high:
            new_next = len(sequence)
        sys.stdout.write('{}:{}\n{}\n'.format(header, fragment, sequence[next:new_next]))
        next = new_next
        fragment += 1

header = &quot;&quot;
sequence = &quot;&quot;
for line in sys.stdin:
    if line.startswith('&gt;'):
        if len(sequence) &gt; 0:
            process_record(header, sequence, low, high)
            sequence = &quot;&quot;
        header = line.rstrip()
    else:
        sequence += line.rstrip()

if len(sequence) &gt; 0:
    process_record(header, sequence, low, high)

To use it:
$ ./13879.py 1000 15000 &lt; in.fa &gt; out.fa

Fragments will be disjoint (non-overlapping, contiguous) subsequences.
I add a check to determine if the last fragment of a FASTA record would be shorter than low (1kb) in length, and, if so, append that fragment to the final subsequence. It is possible for that last fragment to be up to 16 kb - 1 nt in length, given your parameters:
if new_next &gt; len(sequence) - high:
    new_next = len(sequence)

You could change this logic to discard the last fragment (i.e. break), or simply allow fragments less than 1kb in size, depending on what you need.
EDIT
Here's a slight modification that should allow to you preserve fragment sizes within your bounds.
A cartoon may help illustrate the problem. Let's set up a FASTA record, which has been broken up into n fragments:
   1       2           n-2      n-1      n
|-----|---------|...|-------|---------|-----|

We may get unlucky and sample a random integer for fragment n-1 (frag(n-1)) which makes the length of frag(n) less than low (1 kb in your example).
In my first suggested solution, frag(n-1) and frag(n) are appended to one another. This is why you get some fragments longer than high (15 kb).
Another option is to discard frag(n), which is of length less than low (1 kb). Here's some code:
#!/usr/bin/env python

import sys
import numpy as np
import timeit

low = int(sys.argv[1])
high = int(sys.argv[2])
in_fn = sys.argv[3]

&quot;&quot;&quot;
Globals
&quot;&quot;&quot;
fragment_lengths = []

&quot;&quot;&quot;
Process a record
&quot;&quot;&quot;
def process_record(header, sequence, low, high):
    next = 0
    last = 0
    fragment = 1
    fragment_sequence = &quot;&quot;
    while last &lt; len(sequence) - high:
        next = np.random.randint(low, high + 1) + last
        fragment_sequence += sequence[last:next]
        # Comment the sys.stdout.write and uncomment the fragment_lengths line, if measuring fragment length distributions 
        sys.stdout.write('{}:{}-{}:{}\n{}\n'.format(header, last, next, fragment, sequence[last:next]))
        #fragment_lengths.append(next - last)                                                                                                                                                                                                                                                                                                                                                                                          
        last = next
        fragment += 1
    &quot;&quot;&quot;
    Uncomment to compare input sequence with fragments
    &quot;&quot;&quot;
    #sys.stderr.write('In &gt; {}\nOut &gt; {}\n'.format(sequence, fragment_sequence))
    
&quot;&quot;&quot;
Parse records
&quot;&quot;&quot;
def parse_records(in_fn, low, high):
    header = &quot;&quot;
    sequence = &quot;&quot;
    with open(in_fn, 'r') as ifh:
        for line in ifh:
            if line.startswith('&gt;'):
                if len(sequence) &gt; 0:
                    process_record(header, sequence, low, high)
                    sequence = &quot;&quot;
                header = line.rstrip()
            else:
                sequence += line.rstrip()
        if len(sequence) &gt; 0:
            process_record(header, sequence, low, high)

def count_elements(seq):
    hist = {}
    for i in seq:
        hist[i] = hist.get(i, 0) + 1
    return hist

def wrapper(func, *args, **kwargs):
    def wrapped():
        return func(*args, **kwargs)
    return wrapped

def main(in_fn, low, high):
    &quot;&quot;&quot;
    Comment below if testing the distribution of fragments
    &quot;&quot;&quot;
    parse_records(in_fn, low, high)
    &quot;&quot;&quot;
    Uncomment to test the distribution of fragments from 
    running fns on same input 100k times
    &quot;&quot;&quot;
    #wrapped = wrapper(parse_records, in_fn, low, high)
    #timeit.timeit(wrapped, number=100000)
    #print(count_elements(fragment_lengths))

if __name__ == &quot;__main__&quot;:
    main(in_fn, low, high)

If frag(n) is needed, and you need to preserve the uniform distribution (or whatever fragment length distribution you want to model), then rejection sampling might be an option.
With rejection sampling, you keep fragmenting your input sequence, until one of the distributions of lengths gets you a set of fragments that entirely cover the input sequence.
If you need that, follow up and I'll take another look.