# Use biopython to align SeqRecords stored in dict

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?

• do you want to align each of those Seq('ATAAAAATAGTT...... on top of each other ?
– moth
Nov 11 '21 at 13:54
• Yes, what you said moth. I want one alignment for each gene. Nov 12 '21 at 14:29

## 2 Answers

This BioPython document should get you started.

A specific example is given here.

• 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. Nov 8 '21 at 12:27
• Strange requirement. No idea how to align in python without calling another application by file. Nov 9 '21 at 13:31
• 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. Nov 9 '21 at 13:45
• I don't think muscle can work 'file free'... may be wrong. Nov 11 '21 at 10:13
• 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. Nov 11 '21 at 10:21

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.