Difficult to say, also you cannot claim your
RAM is sufficient for the task with evidence suggesting otherwise and lacking information about your data size ... but one wild guess:
Try moving this line outside of your outer loop:
with open('SCR0023_protein.fasta', 'a') as new_fasta_file:
Currently, the open call is made for each line in your input file, which is unnecessary. Depending on how python manages system file handles internally, you may simply end up with too many temporarily open file handles. It may not solve it but at least will reduce the number of system calls significantly.
In fact you should not need this line at all because the file was already created and opened, but then closed, in the beginning.
Another point would be to not re-use the seqrecord obtained from the indexed object, because that is a reference not a copy, changing the id's may not go down well. So, try to make a new record instead.
Try this:
from Bio import SeqIO
from Bio.SeqRecord import SeqRecord
fasta_SCR0023 = SeqIO.index('Galaxy37-[FASTA_from_pilon_on_data_35_and_data_32].fasta', 'fasta')
fasta_SCR0023_prot = open('SCR0023_protein.fasta', 'a') # you can open in append mode directly
busco = open('Galaxy38-[Busco_on_data_37__full_table].tabular', 'r')
for line in busco:
if not line.startswith('#'):
parts = line.split('\t')
# adding some safety checks, lines could be empty
if len(parts) > 0 and parts[1].strip() == 'Complete': # you have used strip for some fields, might be safe to use it for all
record = fasta_SCR0023.get(parts[2].strip()) # returns None in case the record is not there
if not record: continue
myseq = record.seq[int(parts[3])-1:int(parts[4])]
if parts[5].strip() == '-':
myseq = myseq.reverse_complement()
rec_id = 'SCR0023_' + parts[9].strip().replace(' ', '_') + '_' + parts[0].strip()
aarecord = SeqRecord(
myseq, # myseq.translate(), ## removed the translation because the result may be invalid
id=rec_id,
name=rec_id,
description=parts[9].strip()
)
print(aarecord)
SeqIO.write(aarecord, fasta_SCR0023_prot, 'fasta')
print('-------------------------------------------------')
fasta_SCR0023_prot.close()