1

enter image description here

dna="AAGAGATGCCATTGTCCCCCGGCCTCCTGCTGCTGCTCTTAGCGGGGCCACATCGGCCACCGCTGCCCTGCCCCTGGAGGGTGGCCCCACCGGCCGTTACAGCGAGCATAC" 

So basically I was trying to select ONLY the letter "C"s in the dna variable, and simply replace it with the letter "G".

Is there a way/function I can have for this? Would be greatly appreciated if answered!

001
  • 13,291
  • 5
  • 35
  • 66
KL777
  • 43
  • 6
  • 3
    You'll need to do more than that to reverse complement your sequence ;) – Chris_Rands Feb 04 '22 at 20:10
  • 2
    If you first replace all the `C` -> `G` how will you then replace all the `G` -> `C` ? Ex: `"CGCG".replace('C', 'G')` results in `"GGGG"`. – 001 Feb 04 '22 at 20:11

4 Answers4

7

Use maketrans

Since you need: C <-> G and A <-> T

Meaning C -> G and G -> C, etc.

Example

dna = "CGATCCGG" # dna sequence

# Create translation table
x = "CGAT"       # original letters
y = "GCTA"       # letters to map too
mytable = str.maketrans(x, y)  # Create translation table
                               # dna.maketrans(x, y) also works

# Apply table to dna string
translated = dna.translate(mytable)

# Show Original vs. translated
print(f"Original:\t{dna}\nTranslate:\t{translated}")
# Output:
Original:   CGATCCGG
Translate:  GCTAGGCC
DarrylG
  • 16,732
  • 2
  • 17
  • 23
2

You can use .replace():

dna=("AAGAGATGCCATTGTCCCCCGGCCTCCTGCTGCTGCTCT"
"TAGCGGGGCCACATCGGCCACCGCTGCCCTGCCCCTGGAGGGTGGCCCCACCGGCCGTTACAGCGAGCATAC")

print(dna.replace("C", "G"))
BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33
2

If you want to replace Cs with Gs, use this:

dna.replace('C', 'G')
Asdoost
  • 316
  • 1
  • 2
  • 15
-1

for general replacement, you can use str.replace(target, replacement) but the issue in the context of the problem is that if you string replace all the C's with G's, you don't know what was originally a G to replace to a C to get the compliment.

It would be better to convert the strand into a list, then replace values one by 1 with case blocks or if/else blocks:

def dna_compliment(strand):
  listStrand = list(strand)
  for i in range(len(listStrand)):
     if listStrand[i] == 'C':
        listStrand[i] = 'G'
     elif ...
     ...
  return ''.join(listStrand)
imbes
  • 71
  • 1
  • 6