I have a text file called dna.txt which contains:
>A
ACG
>B
CCG
>C
CCG
>D
TCA
I want to create a program using Python that will compare all the lines of the text file after the first sequence (ACG) to the first sequence (ACG), and print out "conserved" if the sequences are a match, and "not conserved" if the sequences are a mismatch. I did it using an extremely inefficient way that only goes up to 30 sequences in the file, and I was wondering how maybe a loop could be utilized to simplify this block of code. This is just a short sample of the inefficient method I used:
f = open("dna.txt")
sequence_1 = linecache.getline('dna.txt', 2)
sequence_2 = linecache.getline('dna.txt', 4)
sequence_3 = linecache.getline('dna.txt', 6)
sequence_x = linecache.getline('dna.txt', 2x)
f.close()
if sequence_2 == sequence_1:
print("Conserved")
else:
print("Not Conserved")
if sequence_3 == sequence_1:
print("Conserved")
else:
print("Not Conserved")
if sequence_x == sequence_1
print("Conserved")
else:
print("Not Conserved")
As you can obviously tell, this is probably the worst way of trying to accomplish what I'm trying to do. Help would be much appreciated, thanks!