Cutting the sequence into several sequences with the information of a dataframe

I have a fasta file with several 120-concatenation protein sequences. I also have a data frame with 120 names of proteins in column 1 and their length in column 2. Using this dataframe I want to create a new fasta file per sequence in which I get each protein independent with the name of the protein and the bacteria.

Here my fasta file.

Bacteria_1
Seq('GRRKAAVARAYMTAGKGNVTVNKKDYKDFFPVPTLQYKITQPFAITETLGQYDV...IEE', SingleLetterAlphabet())

Bacteria_2
Seq('GRRKDSVARVYLRPGKGEIVVNKRPVEDYFGRETLKMVLRQPLELTESLDKFDI...YK-', SingleLetterAlphabet())



Here my dataframe

      Names  LEN
0     Ribosomal_S9:  121
1     Ribosomal_S8:  129
2    Ribosomal_L10:  100
3             GrpE:  166
4           DUF150:  141
..              ...  ...
115      TIGR03632:  117
116      TIGR03654:  175
117      TIGR03723:  314
118      TIGR03725:  212
119      TIGR03953:  188

[120 rows x 2 columns]


The name of the genes and their length is the same for all my bacteria so this df is applicable for all sequences in my fasta file.

I am trying to get new fasta files like this:

>Bacteria_1 Ribosomal_S9:
GRRKAAVARAYMTAGKGNVTVNKKDYKDFFPVPTLQYKITQPFAITETLGQYDV...

>Bacteria_1 Ribosomal_S8:
KDYKDFFPVPTLAAVARAYMTAGKGNLGQYDVGRRKAAVARAYMTAGKGN...
...

>Bacteria_1 TIGR03953:


The second fasta file

>Bacteria_2 Ribosomal_S9:
GRRKAAVARAYMTAGKGNVTVNKKDYKDFFPVPTLQYKITQPFAITETLGQYDV...

>Bacteria_2 Ribosomal_S8:
KDYKDFFPVPTLAAVARAYMTAGKGNLGQYDVGRRKAAVARAYMTAGKGN...
...

>Bacteria_2 TIGR03953:


We wouldn't write the code for you from scratch (thats your job), but its an easy problem because in Python any string is an "array" or "dictionary". Thus

bacteria1 = 'GRRKAAVARAYMTAGKGNVTVNKKDYKDFFPVPTLQYKITQPFAITETLGQYDVIEEXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
print (bacteria1[0:120])


This will define the first gene in your list, remembering the array starts at 0 whilst your gene starts at position 1 (so you need to subtract 1). You simply parse the input list so the gene lengths form a dictionary and loop through the dictionary of gene lengths ensuring the last length is added to the current one and then issuing a write command.

You don't really need biopython in this instance, unless it will parse the gene length file you need to open (I'm not aware it can).

BTW the format you supplied isn't fasta it looks like a Biopython statement, again in this problem I wouldn't bother because Python is perfectly capable of splicing a string.

• Thanks for you help Michael. Mar 15 '20 at 11:32