<p>I don't believe this is possible. So I'm answering this to close it, since I don't have enough reputation to close it myself.</p>

CC BY-SA 4.0
2021-11-11T10:23:06.013Z
2021-11-11T10:23:06.013Z
Glubbdrubb
<p>This <a href="https://biopython.org/docs/1.75/api/Bio.Align.html" rel="nofollow noreferrer">BioPython document</a> should get you started.</p>
<p>A specific example is given <a href="https://stackoverflow.com/questions/45581807/muscle-multiple-sequence-alignment-with-biopython">here</a>.</p>

CC BY-SA 4.0
2021-11-08T12:27:03.400Z
All the examples in documentation you linked require the files to be ready to be aligned, where each file represents only one gene and multiple samples. That is not what I have here, and I don't want to write intermediate gene files. I want to do it all in memory until the final alignments are ready.
Glubbdrubb
CC BY-SA 4.0
2021-11-09T13:31:21.217Z
Strange requirement. No idea how to align in python without calling another application by file.
Dan Bolser
CC BY-SA 4.0
2021-11-09T13:45:27.027Z
I learnt how to create a MultipleSeqAlignment object based on that dict, but I can't see how to pass that object to a muscle subprocess...
I guess I'd need a muscle python library.
Glubbdrubb
CC BY-SA 4.0
2021-11-11T10:13:19.393Z
I don't think muscle can work 'file free'... may be wrong.
Dan Bolser
CC BY-SA 4.0
2021-11-11T10:21:02.343Z
Yeah, I've given up on this. Biopython supports this using pairwise alignments, but that's obviously not going to work here. Thanks for your help.
Glubbdrubb
CC BY-SA 4.0
2021-11-12T14:28:20.427Z
Good, what you're trying is very weird.
Dan Bolser
CC BY-SA 4.0
2021-11-08T12:19:23.333Z
2021-11-08T12:19:23.333Z
Dan Bolser
<p>I'd like to perform multiple alignments, where a gene from each sample was read in from fasta files. The fasta file represented one sample and had multiple genes. I have read in each sample fasta file and now have a dictionary of genes and their samples and sequences.
Here is a small example.</p>
<p>The main key is the gene ID for another dictionary as the value. In the dictionary 'R??' is the sample name and key,
followed by the annotation and sequence in a list.</p>
<pre><code>{'PRELSG_MIT_v1:5894-6032': {'R47': ['not annotated',
                                     Seq('ATAAAAATAGTTACCATAGCTGTTGATGGATGCTTCGATCATAAAATATGTTTG...GGA')],
                             'R48': ['not annotated',
                                     Seq('ATAAAAATAGTTACCATAGCTGTTGATGGATGCTTCGATCATAAAATATGTTTG...GGA')],
                             'R49': ['not annotated',
                                     Seq('ATAAAAATAGTTACCATAGCTGTTGATGGATGCTTCGATCATAAAATATGTTTG...GGA')],
                             'R50': ['not annotated',
                                     Seq('ATAAAAATAGTTACCATAGCTGTTGATGGATGCTTCGATCATAAAATATGTTTG...GGA')],
                             'R51': ['not annotated',
                                     Seq('ATAAAAATAGTTACCATAGCTGTTGATGGATGCTTCGATCATAAAATATGTTTG...GGA')],
                             'R52': ['not annotated',
                                     Seq('ATAAAAATAGTTACCATAGCTGTTGATGGATGCTTCGATCATAAAATATGTTTG...GGA')]},
 'exon_PRELSG_MIT00300.1-E1': {'R47': ['cytochrome b',
                                       Seq('TCCAGATAATGCTATTACAGTAGATAGATATGCTACTCCTTTACATATTGTTCC...TTC')],
                               'R48': ['cytochrome b',
                                       Seq('TCCAGATAATGCTATTACAGTAGATAGATATGCTACTCCTTTACATATTGTTCC...TTC')],
                               'R49': ['cytochrome b',
                                       Seq('TCCAGATAATGCTATTACAGTAGATAGATATGCTACTCCTTTACATATTGTTCC...TTC')],
                               'R50': ['cytochrome b',
                                       Seq('TCCAGATAATGCTATTACAGTAGATAGATATGCTACTCCTTTACATATTGTTCC...TTC')],
                               'R51': ['cytochrome b',
                                       Seq('TCCAGATAATGCTATTACAGTAGATAGATATGCTACTCCTTTACATATTGTTCC...TTC')],
                               'R52': ['cytochrome b',
                                       Seq('TCCAGATAATGCTATTACAGTAGATAGATATGCTACTCCTTTACATATTGTTCC...TTC')]}}
</code></pre>
<p>I'd like to align each gene and write out those alignments to file without any intermediate files written to disk.
How do I do that?</p>

CC BY-SA 4.0
2021-11-11T13:54:17.453Z
do you want to align each of those `Seq('ATAAAAATAGTT......` on top of each other ?
moth
CC BY-SA 4.0
2021-11-12T14:29:43.977Z
Yes, what you said moth. I want one alignment for each gene.
Glubbdrubb
<python><multiple-sequence-alignment>
Use biopython to align SeqRecords stored in dict
# Use biopython to align SeqRecords stored in dict

(Asked by: username_0 on 2021-11-08)

I'd like to perform multiple alignments, where a gene from each sample was read in from fasta files. The fasta file represented one sample and had multiple genes. I have read in each sample fasta file and now have a dictionary of genes and their samples and sequences.
Here is a small example.
The main key is the gene ID for another dictionary as the value. In the dictionary 'R??' is the sample name and key,
followed by the annotation and sequence in a list.
{'PRELSG_MIT_v1:5894-6032': {'R47': ['not annotated',
                                     Seq('ATAAAAATAGTTACCATAGCTGTTGATGGATGCTTCGATCATAAAATATGTTTG...GGA')],
                             'R48': ['not annotated',
                                     Seq('ATAAAAATAGTTACCATAGCTGTTGATGGATGCTTCGATCATAAAATATGTTTG...GGA')],
                             'R49': ['not annotated',
                                     Seq('ATAAAAATAGTTACCATAGCTGTTGATGGATGCTTCGATCATAAAATATGTTTG...GGA')],
                             'R50': ['not annotated',
                                     Seq('ATAAAAATAGTTACCATAGCTGTTGATGGATGCTTCGATCATAAAATATGTTTG...GGA')],
                             'R51': ['not annotated',
                                     Seq('ATAAAAATAGTTACCATAGCTGTTGATGGATGCTTCGATCATAAAATATGTTTG...GGA')],
                             'R52': ['not annotated',
                                     Seq('ATAAAAATAGTTACCATAGCTGTTGATGGATGCTTCGATCATAAAATATGTTTG...GGA')]},
 'exon_PRELSG_MIT00300.1-E1': {'R47': ['cytochrome b',
                                       Seq('TCCAGATAATGCTATTACAGTAGATAGATATGCTACTCCTTTACATATTGTTCC...TTC')],
                               'R48': ['cytochrome b',
                                       Seq('TCCAGATAATGCTATTACAGTAGATAGATATGCTACTCCTTTACATATTGTTCC...TTC')],
                               'R49': ['cytochrome b',
                                       Seq('TCCAGATAATGCTATTACAGTAGATAGATATGCTACTCCTTTACATATTGTTCC...TTC')],
                               'R50': ['cytochrome b',
                                       Seq('TCCAGATAATGCTATTACAGTAGATAGATATGCTACTCCTTTACATATTGTTCC...TTC')],
                               'R51': ['cytochrome b',
                                       Seq('TCCAGATAATGCTATTACAGTAGATAGATATGCTACTCCTTTACATATTGTTCC...TTC')],
                               'R52': ['cytochrome b',
                                       Seq('TCCAGATAATGCTATTACAGTAGATAGATATGCTACTCCTTTACATATTGTTCC...TTC')]}}

I'd like to align each gene and write out those alignments to file without any intermediate files written to disk.
How do I do that?

**Tags:** python, multiple-sequence-alignment
> **Comments:**
> **username_1**: do you want to align each of those `Seq('ATAAAAATAGTT......` on top of each other ?  
> **username_0**: Yes, what you said moth. I want one alignment for each gene.  

---

## 2 answers

### Answer by username_0 on 2021-11-11. Score: 0 | Accepted answer

I don't believe this is possible. So I'm answering this to close it, since I don't have enough reputation to close it myself.


---

### Answer by username_2 on 2021-11-08. Score: 0

This [BioPython document](https://biopython.org/docs/1.75/api/Bio.Align.html) should get you started.

A specific example is given [here](https://stackoverflow.com/questions/45581807/muscle-multiple-sequence-alignment-with-biopython).

> **Comments:**
> **username_0**: All the examples in documentation you linked require the files to be ready to be aligned, where each file represents only one gene and multiple samples. That is not what I have here, and I don't want to write intermediate gene files. I want to do it all in memory until the final alignments are ready.  
> **username_2**: Strange requirement. No idea how to align in python without calling another application by file.  
> **username_0**: I learnt how to create a MultipleSeqAlignment object based on that dict, but I can't see how to pass that object to a muscle subprocess... I guess I'd need a muscle python library.  
> **username_2**: I don't think muscle can work 'file free'... may be wrong.  
> **username_0**: Yeah, I've given up on this. Biopython supports this using pairwise alignments, but that's obviously not going to work here. Thanks for your help.  
> **username_2**: Good, what you're trying is very weird.  

---

