I've been looking for a way to do it for over a day but could not manage finding what I exactly need. I have 2 files. FIRST file has NAMES in one column POSITIONS in second and LETTERS in the third column. In the SECOND file I have NAMES in first column, and STRINGS in the second column. I have a loop that goes through each line in the FIRST file, matches the NAMES to the SECOND file on each line, and goes to the POSITIONS and changes the STRING using the LETTERS. Loop works perfectly, but I can not keep the changes for the next LETTER.
It's like
FIRST FILE
NAME1 2 X
NAME1 5 Z
NAME1 7 J
NAME2 3 P
NAME2 6 D
SECOND FILE
NAME1 AAAAAAAAA
NAME2 BBBBBBB
I use STRING as input and create a NEWSTRING during the loop with the changed LETTER and when I print it inside the loop, I get after the first loop:
AXAAAAAAA
And after the second:
AAAAZAAAA
What I am looking for is kind of a magical one liner that lets doing it inside the loop,something like STRING=NEWSTRING so that my input in the next loop will be NEWSTRING
AXAAAAAAA
and so it will generate
AXAAZAAAA
in the second loop
I have tried append, add, list, and a few more things, but none worked.
with open ("FILE1.txt")as f:
POS=f.readlines()
for line in POS:
columns=line.split()
query=columns[0]
locate=(int(columns[1])-1)
newnuc=columns[2]
oldnuc=columns[3]
with open ("FILE2.txt")as f:
Sequo=f.readlines()
for linex in Sequo:
columnos=linex.split()
querios=columnos[0]
sequence=columnos[1]
if query == querios:
newseqons= sequence [:locate] + newnuc + sequence [locate + 1:]
print(newseqons)
HERE IS MY NEW CODE, PATRICK
with open (r'C:\Users\Administrator\Desktop\Sequorro.txt') as f2:
Sequo=f2.readlines()
for linex in Sequo:
columnos=linex.split()
querios=columnos[0]
sequence=columnos[1]
d={}
d.update({querios: sequence})
print(d)
{'CRUP_004407-RA': 'AAAAAAAAA'}
{'CRUP_004416-RA': 'GGGGGGGGG'}
with open (r'C:\Users\Administrator\Desktop\POS.txt') as f1:
POS=f1.readlines()
for line in POS:
columns=line.split()
query=columns[0]
locate=(int(columns[1]))
newnuc=columns[2]
oldnuc=columns[3]
oldstr=d[querios]
d[querios]=oldstr[:locate-1] +newnuc +oldstr[locate:]
print(d)
{'CRUP_004416-RA': 'GCGGGGGGG'}
{'CRUP_004416-RA': 'GCGGGGGGG'}
{'CRUP_004416-RA': 'GCGGGTGGG'}
{'CRUP_004416-RA': 'GCCGGTGGG'}
{'CRUP_004416-RA': 'GCCAGTGGG'}
{'CRUP_004416-RA': 'GCCAGTTGG'}
with open (r'C:\Users\Administrator\Desktop\Sequorooo.txt','w') as f2:
for querios, sequence in sorted(d.items()):
f2.write('{}{}'.format(querios, sequence))
f2.close()
CRUP_004416-RAGCCAGTTGG