<p>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 </p>

<pre><code>bacteria1 = 'GRRKAAVARAYMTAGKGNVTVNKKDYKDFFPVPTLQYKITQPFAITETLGQYDVIEEXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
print (bacteria1[0:120])
</code></pre>

<p>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. </p>

<p>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).</p>

<hr>

<p>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.</p>

CC BY-SA 4.0
2020-03-15T11:32:34.507Z
Thanks for you help Michael.
Manolo Dominguez Becerra
CC BY-SA 4.0
2020-03-15T11:22:15.693Z
2020-03-15T11:29:00.727Z
2020-03-15T11:29:00.727Z
M__
<p>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.</p>

<p>Here my fasta file.</p>

<pre><code>Bacteria_1
Seq('GRRKAAVARAYMTAGKGNVTVNKKDYKDFFPVPTLQYKITQPFAITETLGQYDV...IEE', SingleLetterAlphabet())

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


</code></pre>

<p>Here my dataframe</p>

<pre><code>      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]
</code></pre>

<p>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.</p>

<p>I am trying to get new fasta files like this:</p>

<pre><code>&gt;Bacteria_1 Ribosomal_S9:
GRRKAAVARAYMTAGKGNVTVNKKDYKDFFPVPTLQYKITQPFAITETLGQYDV...

&gt;Bacteria_1 Ribosomal_S8:
KDYKDFFPVPTLAAVARAYMTAGKGNLGQYDVGRRKAAVARAYMTAGKGN...
...

&gt;Bacteria_1 TIGR03953:
AJEFISUBEVFCVALSKDMCFPADNSCF0AISCIABNSCNAISCN...
</code></pre>

<p>The second fasta file</p>

<pre><code>&gt;Bacteria_2 Ribosomal_S9:
GRRKAAVARAYMTAGKGNVTVNKKDYKDFFPVPTLQYKITQPFAITETLGQYDV...

&gt;Bacteria_2 Ribosomal_S8:
KDYKDFFPVPTLAAVARAYMTAGKGNLGQYDVGRRKAAVARAYMTAGKGN...
...

&gt;Bacteria_2 TIGR03953:
AJEFISUBEVFCVALSKDMCFPADNSCF0AISCIABNSCNAISCN...
</code></pre>

<fasta><ngs><python>
Cutting the sequence into several sequences with the information of a dataframe
# Cutting the sequence into several sequences with the information of a dataframe

(Asked by: username_0 on 2020-03-15)

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:
AJEFISUBEVFCVALSKDMCFPADNSCF0AISCIABNSCNAISCN...

The second fasta file
>Bacteria_2 Ribosomal_S9:
GRRKAAVARAYMTAGKGNVTVNKKDYKDFFPVPTLQYKITQPFAITETLGQYDV...

>Bacteria_2 Ribosomal_S8:
KDYKDFFPVPTLAAVARAYMTAGKGNLGQYDVGRRKAAVARAYMTAGKGN...
...

>Bacteria_2 TIGR03953:
AJEFISUBEVFCVALSKDMCFPADNSCF0AISCIABNSCNAISCN...

**Tags:** fasta, ngs, python

---

## 1 answers

### Answer by username_1 on 2020-03-15. Score: 1

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.

> **Comments:**
> **username_0**: Thanks for you help Michael.  

---

