0

I have a space separated file like this:(it has 1775 lines)

head output.fam
0 ALIKE_g_1LTX827_BI_SNP_F01_33250.CEL 0 0 0 -9
0 BURRY_g_3KYJ479_BI_SNP_A12_40182.CEL 0 0 0 -9
0 ABAFT_g_4RWG569_BI_SNP_E12_35136.CEL 0 0 0 -9
0 MILLE_g_5AVC089_BI_SNP_F02_35746.CEL 0 0 0 -9
0 PEDAL_g_8WWR250_BI_SNP_B06_37732.CEL 0 0 0 -9
...

and a comma separated file phg000008.individualinfo (that has 1838 lines):

#Phen_Sample_ID - individual sample name associated with phenotypes
#Geno_Sample_ID - sample name associates with genotypes
#Ind_id - unique individual name which can be used to match duplicates (in this case same as Phen_Sample_ID)
#Ped_id - Pedigree ID
#Fa_id - Father individual ID
#Ma_id - Mother individual ID
#Sex - coded 1 for Male, 2 for Female
#Ind_QC_flag - value "ALL" indicates released in both Quality Filtered and Complete set
#Genotyping_Plate
#Sample_plate_well_string - This string corresponds to the file within the CEL files distribution
#Genotype_Clustering_Set
#Study-id - dbGaP assigned study id
 #Phen_ID,Geno_Sample_ID,Ind_id,Ped_id,Fa_id,Ma_id,Sex,Ind_QC_flag,Genotyping_Plate,Sample_plate_well_string,Genotyping_Clustering_Set,Study_id
G1000,G1000,G1000,fam1000-,0,0,2,ALL,7FDZ321,POSED_g_7FDZ321_BI_SNP_B02_36506,set05,phs000018
G1001,G1001,G1001,fam1001-,G4243,G4205,1,ALL,3KYJ479,BURRY_g_3KYJ479_BI_SNP_H04_40068,set02,phs000018       
G2208,G2208,G2208,fam2208-,G3119,G3120,2,ALL,1LTX827,ALIKE_g_1LTX827_BI_SNP_F01_33250,set01,phs000018
G1676,G1676,G1676,fam1676-,G1675,G1674,1,ALL,3KYJ479,BURRY_g_3KYJ479_BI_SNP_A12_40182,set02,phs000018
...

I would like to change my output.fam by looking if I could find value from the 2nd column in output.fam, say ALIKE_g_1LTX827_BI_SNP_F01_33250.CEL in phg000008.individualinfo (disregarding .CEL suffix) and is there is a row with that entry replace that entry in output.fam with the value in the first column of phg000008.individualinfo and also for the same line replace the value of the first column of output.fam with the value in the 4th column of phg000008.individualinfo (excluding - suffix)

So for example for two lines, output.fam would look like this:

fam2208 G2208 0 0 0 -9
fam1676 G1676 0 0 0 -9
0

Try

awk '
FNR == NR       {sub (/-/, "", $4)              # get rid of "-" in $4
                 T[$10 ".CEL"] = $4 " " $1      # save file2 in temp array
                 next
                }
$2 in T         {$1 = T[$2]                     # check if $2 is relevant; replace 
                 $2 = ""                        # $1 with temp array value; delete $2
                 print  
                }
' FS=, file2 FS=" " file1
2
  • thanks can you please tell me what is file1 and file2?
    – anamaria
    Mar 25 '20 at 22:03
  • Your input files in order of appearance: file1 = output.fam, file2 = phg000008.individualinfo.
    – RudiC
    Mar 26 '20 at 8:52
0

You can generate a sed script from the phg file and use it to modify the fam file:

grep -v ^# phg000008.individualinfo \
| cut -d, -f3,4,10 \
| sed -E 's=(.*),(.*)-,(.*)=s/[^ ]+ \3\\.CEL/\2 \1/=' \
| grep s/ \
| sed -Ef- output.fam 

The generated script looks like this:

s/[^ ]+ POSED_g_7FDZ321_BI_SNP_B02_36506\.CEL/fam1000 G1000/
s/[^ ]+ BURRY_g_3KYJ479_BI_SNP_H04_40068\.CEL/fam1001 G1001/
s/[^ ]+ ALIKE_g_1LTX827_BI_SNP_F01_33250\.CEL/fam2208 G2208/
s/[^ ]+ BURRY_g_3KYJ479_BI_SNP_A12_40182\.CEL/fam1676 G1676/

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.