0

I am rather new to bioinformatics (this is my first post!) and I would appreciate some help on task that has me stuck.

I have a Tab-delimited data table with three columns:

AATTCTTGCA 4 [A/T]
AATTCCTTCG 7 [C/T]
AATTCAACAA 2 [T/C]

I would like to replace the character in the first column at the position indicated by the second column with the string in the third column so that the output is:

AAT[A/T]CTTGCA
AATTCC[C/T]TCG
A[T/C]TTCAACAA

I am working through various tutorials now and will update my post when I have some (failed) commands with sed/awk.

Thanks in advance!

0

2 Answers 2

5

The following awk command should do the task:

awk -F"\t" '{printf "%s%s%s%s",substr($1,1,$2-1),$3,substr($1,$2+1),ORS}' input.txt

The option -F sets the field separator to TAB. The program will then print (using the printf() function) for every line

  • the substring of field 1 from the beginning up to (but excluding) the character position indicated in field 2
  • the string contained in field 3
  • the remainder of field 1, starting one past the character position indicated in field 2
  • the "output record separator", which defaults to new-line

thereby effectively replacing the indicated character with the content of field 3.

Note that in hindsight this amount of explicit formatting control is actually not necessary, and the program can be abbreviated to

awk -F"\t" '{print substr($1,1,$2-1) $3 substr($1,$2+1)}' input.txt

Caveat: The program assumes that the character position in field 2 is always reasonable, i.e. greater than 0 and less or equal to the total length of field 1. If the file can be corrupt, more error-checking is needed.

0
0
#!/usr/bin/python
k=open('jk.txt','r')
for i in k:
    fg=i.split(' ')
    secondcol=int(fg[1])
    thirdcol=str(fg[2])
    firscol=list(fg[0])
    firscol[secondcol-1]=thirdcol.strip()
    print "".join(firscol)

output

AAT[A/T]CTTGCA
AATTCC[C/T]TCG
A[T/C]TTCAACAA

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.