3

I've got a dataset that looks like this:

>TRINITY_DN37_c0_g1_i1 len=333 path=[361:0-43 362:44-332] [-1, 361, 362, -2]
GCCGCCATCATGGATGCCAGCCGTGTGCAGCCCATCAAGCTTGCCAGAGTCACCAAGGTT
>TRINITY_DN37_c0_g2_i1 len=356 path=[359:0-66 360:67-355] [-1, 359, 360, -2]
ACGTGACCCCCTTTCTGTCTCAAGCCGCCATCATGGATGCCAGTCGTGTGCAGCCCATCA
>TRINITY_DN15_c1_g1_i1 len=394 path=[372:0-393] [-1, 372, -2]
GCACTTACCATGCATGGAAGGCAAATGCCATCGGAAGGTCTGCAAAGACTGTTAGGGAGT

I would need to replace the string 'len=XXX', which is in the same position across thousands of lines, with a series of numbers in order to tag uniquely each sequence. Ideally I was thinking of something like:

>TRINITY_DN37_c0_g1_i1 1 path=[361:0-43 362:44-332] [-1, 361, 362, -2]
GCCGCCATCATGGATGCCAGCCGTGTGCAGCCCATCAAGCTTGCCAGAGTCACCAAGGTT
>TRINITY_DN37_c0_g2_i1 2 path=[359:0-66 360:67-355] [-1, 359, 360, -2]
ACGTGACCCCCTTTCTGTCTCAAGCCGCCATCATGGATGCCAGTCGTGTGCAGCCCATCA
>TRINITY_DN15_c1_g1_i1 3 path=[372:0-393] [-1, 372, -2]
GCACTTACCATGCATGGAAGGCAAATGCCATCGGAAGGTCTGCAAAGACTGTTAGGGAGT

I am using OSX.

Any ideas?

  • Your sequences are already tagged uniquely, that's what the seq id is. – terdon Apr 7 '17 at 10:21
6
$ cat ip.txt 
>TRINITY_DN37_c0_g1_i1 len=333 path=[361:0-43 362:44-332] [-1, 361, 362, -2]
GCCGCCATCATGGATGCCAGCCGTGTGCAGCCCATCAAGCTTGCCAGAGTCACCAAGGTT
>TRINITY_DN37_c0_g2_i1 len=356 path=[359:0-66 360:67-355] [-1, 359, 360, -2]
ACGTGACCCCCTTTCTGTCTCAAGCCGCCATCATGGATGCCAGTCGTGTGCAGCCCATCA
>TRINITY_DN15_c1_g1_i1 len=394 path=[372:0-393] [-1, 372, -2]
GCACTTACCATGCATGGAAGGCAAATGCCATCGGAAGGTCTGCAAAGACTGTTAGGGAGT

$ awk '/len=/{sub(/len=[0-9]+/,++c)} 1' ip.txt 
>TRINITY_DN37_c0_g1_i1 1 path=[361:0-43 362:44-332] [-1, 361, 362, -2]
GCCGCCATCATGGATGCCAGCCGTGTGCAGCCCATCAAGCTTGCCAGAGTCACCAAGGTT
>TRINITY_DN37_c0_g2_i1 2 path=[359:0-66 360:67-355] [-1, 359, 360, -2]
ACGTGACCCCCTTTCTGTCTCAAGCCGCCATCATGGATGCCAGTCGTGTGCAGCCCATCA
>TRINITY_DN15_c1_g1_i1 3 path=[372:0-393] [-1, 372, -2]
GCACTTACCATGCATGGAAGGCAAATGCCATCGGAAGGTCTGCAAAGACTGTTAGGGAGT
  • /len=/ lines matching this pattern
  • sub(/len=[0-9]+/,++c) replace first occurrence of len=[0-9]+ pattern of matched line with incremented value of c (default value is 0)


Or with perl

perl -i -pe 's/len=\d+/++$c/e' ip.txt

the -i option is for inplace editing

  • Thank you!! It was exactly what I needed in order to run a program that tag sequences at that position! – Luca Apr 10 '17 at 10:04
4

Another solution different from Sundeep's one using another awk mechanism.

awk '
$2 ~ /len=/ { $2=++ctr}
{ print }
' < file     

$2 ~ /len=/ matches only lines where second field contains len= and in those lines, the second field is replaced by a counter.

Then, all lines are printed, independently of whether they matched the first rule or not.

If you want to start the counter from a given initial value (if your input is separated in several file for instance), you can do:

awk '
BEGIN { ctr=12345 }
$2 ~ /len=/ { $2=ctr++ }
{ print }
' < file

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.