Entering edit mode
4.7 years ago
yoshikal
•
0
I have 2000 fasta sequence in a file, like this:
>T1
AQSFDRATVFEARRAGYQRESRVALGKSTGVLEWHVFHVWAPRETTILVETLSQIECSGKGIADRRQENPLTI------ATAI--TSQLLELVDIVIMDFKAITQFFL
>T484
AQSFDRATVFEARRAGYQREARVALGKSTGKLEWQVFHVWAPRETTILVETLSQLECSGKGIADRRQENPLKI------ATAI--TSQLLELVDIVIMDFKAITQFFL
>T582
AQSFDRATVFEKRRAGYQREARVALGKSTGKLEWQVFHVWAPRETTILVETLSQLECSGKGIADRRQENPLKI------ATAI--TSQLLELVDIVIMDFKAITQFFL
>T1424
AQSFDRATVFEKRRAGYQLEARVALGKSTGKLEWQVFHVWAPRETTILVETLSQLECSGKGIANRRQENPLKI------ATAI--TSQLLELVDILIMDFKAITQFFL
>T1552
AQSFDRATIFEKRRAGYQIEARVALGKSTGKLEWQVYHAWAPRETTILVETLSQLENAGKGVANRRHENPLKI------ATAI--TSQLLELVDILMMDFKAITQFFL
.
.
.
I randomly draw f (f = 2) sequence from N (N = 2000).
For example if I have drawn 120 and 1500, I get the Fasta sequence:
> T1424
AQSFDRATVFEKRRAGYQLEARVALGKSTGKLEWQVFHVWAPRETTILVETLSQLECSGKGIANRRQENPLKI ------ ATAI - TSQLLELVDILIMDFKAITQFFL
> T1552
AQSFDRATIFEKRRAGYQIEARVALGKSTGKLEWQVYHAWAPRETTILVETLSQLENAGKGVANRRHENPLKI ------ ATAI - TSQLLELVDILMMDFKAITQFFL
What I want to do is replace these sequences with gaps of length 55:
> T1424
-----------------------------------------------------------------------------------------------------------------------------
> T1552
-----------------------------------------------------------------------------------------------------------------------------
I tried with this code:
from Bio import SeqIO
from random import *
records = list(SeqIO.parse("fichier1.txt", "fasta"))
#print(records[0].id) # first record
#print(records[0].seq)
N=len(records) #2000
f=2
l=[]
for i in range(f):
x=randint(1, N)
l.append(x)
d={}
for i2 in l:
print(records[i2].id,records[i2].seq)
d[str(records[i2].id)]=str(records[i2].seq)
with open("fichier1.txt") as fichier, open("newfile.txt", "w") as newfile:
texte = fichier.read()
new_text += texte.replace(str(records[i2].seq), "---------------------------------------")
nouveaufichier.write(new_text)
print(d)
What does not work because sometimes there can be the same sequence in the file but with a different identifier.
From the ID and the corresponding sequence, I would like to change the sequence to introduce gaps.
Switch to BioPython andreplace the sequence based on the identifier. This seems to be a very specific "solve my code problem" question. What is the purpose of this task?EDIT: Just saw that you're already using BioPython.