Question: Delete ID only if not presnt in next line (lines are in pair)
0
gravatar for waqasnayab
17 months ago by
waqasnayab180
Pakistan
waqasnayab180 wrote:

Dear Community,

My this question is the follow-up / extension of C: Copy Sample ID from VCF file to ID column

I have a file like:

rsid1:sample1,sample2,sample3
rsid2:sample1,sample5
rsid3:sample4, sample6
rsid4:sample6

Each line is paired with the second line, that is , rsid1 and rsid2 are paired, rsid3 and rsid4 are paired and so on. I want to print only those samples that are present in a pair, so the desired output would be:

rsid1:sample1
rsid2:sample1
rsids3:sample6
rsid4:sample6

or is it possible to get the direct output like this from a VCF file (answered by PIERRE in the above link, but last time I did not ask this pair feature).

Any help appreciated.

Thanks,

Waqas.

sequencing snp • 503 views
ADD COMMENTlink modified 17 months ago by cpad011211k • written 17 months ago by waqasnayab180
6
gravatar for Pierre Lindenbaum
17 months ago by
France/Nantes/Institut du Thorax - INSERM UMR1087
Pierre Lindenbaum117k wrote:

using awk and sort:

cat input.txt |\
awk -F ':' '{n=split($2,a,/[, ]+/);for(i=1;i<=n;i++) {printf("%d %s %s\n",int((NR-1)/2),$1,a[i]);}}' |\
sort -t ' ' -k1,1 -k3,3   |\
awk '{if(NR>1 && i1==$1 && i3==$3 ) printf("%s:%s\n%s:%s\n",i2,$3,$2,$3);i1=$1;i2=$2;i3=$3;}'


rsid1:sample1
rsid2:sample1
rsid3:sample6
rsid4:sample6
ADD COMMENTlink written 17 months ago by Pierre Lindenbaum117k
1

Pierre or the man who always has awk-one-liners for your case! :D

ADD REPLYlink written 17 months ago by glihm590

Yes, it worked really very well, in fact perfect..,,,,,!!!!

Thanks Pierre

ADD REPLYlink written 17 months ago by waqasnayab180
0
gravatar for cpad0112
17 months ago by
cpad011211k
India
cpad011211k wrote:
$ awk -F: '{split($2,a,",");for(i in a)print $1"\t"a[i]}' test | sort -k2  | uniq -D -f1 | sed 's/\t/:/g'



 rsid1:sample1
 rsid2:sample1
 rsid3:sample6
 rsid4:sample6

input (test):

rsid1:sample1,sample2,sample3
rsid2:sample1,sample5
rsid3:sample4,sample6
rsid4:sample6
ADD COMMENTlink modified 17 months ago • written 17 months ago by cpad011211k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1266 users visited in the last hour