annotate imgt_loader/imgt_loader.r @ 43:2325074a8461 draft

Uploaded
author davidvanzessen
date Thu, 26 Oct 2017 09:58:05 -0400
parents 106275b54470
children 4d2a8f98a502
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
1 args <- commandArgs(trailingOnly = TRUE)
40
106275b54470 Uploaded
davidvanzessen
parents: 39
diff changeset
2 options(show.error.locations = TRUE)
0
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
3
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
4 summ.file = args[1]
20
9185c3dfc679 Uploaded
davidvanzessen
parents: 0
diff changeset
5 sequences.file = args[2]
9185c3dfc679 Uploaded
davidvanzessen
parents: 0
diff changeset
6 aa.file = args[3]
9185c3dfc679 Uploaded
davidvanzessen
parents: 0
diff changeset
7 junction.file = args[4]
28
798b62942b4b Uploaded
davidvanzessen
parents: 20
diff changeset
8 gapped.aa.file = args[5]
798b62942b4b Uploaded
davidvanzessen
parents: 20
diff changeset
9 out.file = args[6]
0
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
10
40
106275b54470 Uploaded
davidvanzessen
parents: 39
diff changeset
11 print(summ.file)
106275b54470 Uploaded
davidvanzessen
parents: 39
diff changeset
12 print(out.file)
106275b54470 Uploaded
davidvanzessen
parents: 39
diff changeset
13
0
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
14 summ = read.table(summ.file, sep="\t", header=T, quote="", fill=T)
20
9185c3dfc679 Uploaded
davidvanzessen
parents: 0
diff changeset
15 sequences = read.table(sequences.file, sep="\t", header=T, quote="", fill=T)
0
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
16 aa = read.table(aa.file, sep="\t", header=T, quote="", fill=T)
28
798b62942b4b Uploaded
davidvanzessen
parents: 20
diff changeset
17 gapped.aa = read.table(gapped.aa.file, sep="\t", header=T, quote="", fill=T)
0
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
18 junction = read.table(junction.file, sep="\t", header=T, quote="", fill=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
19
40
106275b54470 Uploaded
davidvanzessen
parents: 39
diff changeset
20 print(paste("nrow(summ)", nrow(summ)))
106275b54470 Uploaded
davidvanzessen
parents: 39
diff changeset
21
106275b54470 Uploaded
davidvanzessen
parents: 39
diff changeset
22 write.table(summ, out.file, sep="\t", quote=F, row.names=F, col.names=T)
106275b54470 Uploaded
davidvanzessen
parents: 39
diff changeset
23
39
bad6a9a53ae7 Uploaded
davidvanzessen
parents: 31
diff changeset
24 fix_column_names = function(df){
bad6a9a53ae7 Uploaded
davidvanzessen
parents: 31
diff changeset
25 if("V.DOMAIN.Functionality" %in% names(df)){
bad6a9a53ae7 Uploaded
davidvanzessen
parents: 31
diff changeset
26 names(df)[names(df) == "V.DOMAIN.Functionality"] = "Functionality"
bad6a9a53ae7 Uploaded
davidvanzessen
parents: 31
diff changeset
27 print("found V.DOMAIN.Functionality, changed")
bad6a9a53ae7 Uploaded
davidvanzessen
parents: 31
diff changeset
28 }
bad6a9a53ae7 Uploaded
davidvanzessen
parents: 31
diff changeset
29 if("V.DOMAIN.Functionality.comment" %in% names(df)){
bad6a9a53ae7 Uploaded
davidvanzessen
parents: 31
diff changeset
30 names(df)[names(df) == "V.DOMAIN.Functionality.comment"] = "Functionality.comment"
bad6a9a53ae7 Uploaded
davidvanzessen
parents: 31
diff changeset
31 print("found V.DOMAIN.Functionality.comment, changed")
bad6a9a53ae7 Uploaded
davidvanzessen
parents: 31
diff changeset
32 }
bad6a9a53ae7 Uploaded
davidvanzessen
parents: 31
diff changeset
33 return(df)
bad6a9a53ae7 Uploaded
davidvanzessen
parents: 31
diff changeset
34 }
bad6a9a53ae7 Uploaded
davidvanzessen
parents: 31
diff changeset
35
bad6a9a53ae7 Uploaded
davidvanzessen
parents: 31
diff changeset
36 summ = fix_column_names(summ)
bad6a9a53ae7 Uploaded
davidvanzessen
parents: 31
diff changeset
37 sequences = fix_column_names(sequences)
bad6a9a53ae7 Uploaded
davidvanzessen
parents: 31
diff changeset
38 aa = fix_column_names(aa)
bad6a9a53ae7 Uploaded
davidvanzessen
parents: 31
diff changeset
39 gapped.aa = fix_column_names(gapped.aa)
bad6a9a53ae7 Uploaded
davidvanzessen
parents: 31
diff changeset
40 junction = fix_column_names(junction)
bad6a9a53ae7 Uploaded
davidvanzessen
parents: 31
diff changeset
41
40
106275b54470 Uploaded
davidvanzessen
parents: 39
diff changeset
42 print(paste("nrow(summ)", nrow(summ)))
106275b54470 Uploaded
davidvanzessen
parents: 39
diff changeset
43
0
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
44 old_summary_columns=c('Sequence.ID','JUNCTION.frame','V.GENE.and.allele','D.GENE.and.allele','J.GENE.and.allele','CDR1.IMGT.length','CDR2.IMGT.length','CDR3.IMGT.length','Orientation')
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
45 old_sequence_columns=c('CDR1.IMGT','CDR2.IMGT','CDR3.IMGT')
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
46 old_junction_columns=c('JUNCTION')
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
47
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
48 added_summary_columns=c('Functionality','V.REGION.identity..','V.REGION.identity.nt','D.REGION.reading.frame','AA.JUNCTION','Functionality.comment','Sequence')
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
49 added_sequence_columns=c('FR1.IMGT','FR2.IMGT','FR3.IMGT','CDR3.IMGT','JUNCTION','J.REGION','FR4.IMGT')
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
50
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
51 added_junction_columns=c('P3.V.nt.nb','N.REGION.nt.nb','N1.REGION.nt.nb','P5.D.nt.nb','P3.D.nt.nb','N2.REGION.nt.nb','P5.J.nt.nb','X3.V.REGION.trimmed.nt.nb','X5.D.REGION.trimmed.nt.nb','X3.D.REGION.trimmed.nt.nb','X5.J.REGION.trimmed.nt.nb','N.REGION','N1.REGION','N2.REGION')
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
52 added_junction_columns=c(added_junction_columns, 'P5.D1.nt.nb', 'P3.D1.nt.nb', 'N2.REGION.nt.nb', 'P5.D2.nt.nb', 'P3.D2.nt.nb', 'N3.REGION.nt.nb', 'P5.D3.nt.nb', 'P3.D2.nt.nb', 'N4.REGION.nt.nb', 'X5.D1.REGION.trimmed.nt.nb', 'X3.D1.REGION.trimmed.nt.nb', 'X5.D2.REGION.trimmed.nt.nb', 'X3.D2.REGION.trimmed.nt.nb', 'X5.D3.REGION.trimmed.nt.nb', 'X3.D3.REGION.trimmed.nt.nb', 'D.REGION.nt.nb', 'D1.REGION.nt.nb', 'D2.REGION.nt.nb', 'D3.REGION.nt.nb')
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
53
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
54 out=summ[,c("Sequence.ID","JUNCTION.frame","V.GENE.and.allele","D.GENE.and.allele","J.GENE.and.allele")]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
55
40
106275b54470 Uploaded
davidvanzessen
parents: 39
diff changeset
56 print(paste("nrow(summ)", nrow(summ)))
106275b54470 Uploaded
davidvanzessen
parents: 39
diff changeset
57 print(paste("nrow(aa)", nrow(aa)))
106275b54470 Uploaded
davidvanzessen
parents: 39
diff changeset
58
0
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
59 out[,"CDR1.Seq"] = aa[,"CDR1.IMGT"]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
60 out[,"CDR1.Length"] = summ[,"CDR1.IMGT.length"]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
61
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
62 out[,"CDR2.Seq"] = aa[,"CDR2.IMGT"]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
63 out[,"CDR2.Length"] = summ[,"CDR2.IMGT.length"]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
64
31
3a76faa53c59 Uploaded
davidvanzessen
parents: 28
diff changeset
65 out[,"CDR3.Seq"] = gapped.aa[,"CDR3.IMGT"]
0
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
66 out[,"CDR3.Length"] = summ[,"CDR3.IMGT.length"]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
67
31
3a76faa53c59 Uploaded
davidvanzessen
parents: 28
diff changeset
68 out[,"CDR3.IMGT"] = out[,"CDR3.Seq"]
3a76faa53c59 Uploaded
davidvanzessen
parents: 28
diff changeset
69
3a76faa53c59 Uploaded
davidvanzessen
parents: 28
diff changeset
70 out[,"CDR3.Seq.DNA"] = sequences[,"CDR3.IMGT"]
28
798b62942b4b Uploaded
davidvanzessen
parents: 20
diff changeset
71 out[,"CDR3.Length.DNA"] = nchar(as.character(out[,"CDR3.Seq.DNA"]))
0
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
72 out[,"Strand"] = summ[,"Orientation"]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
73 out[,"CDR3.Found.How"] = "a"
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
74
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
75 out[,added_summary_columns] = summ[,added_summary_columns]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
76
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
77 out[,added_sequence_columns] = aa[,added_sequence_columns]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
78
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
79 out[,added_junction_columns] = junction[,added_junction_columns]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
80
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
81 out[,"Top V Gene"] = gsub(".* ", "", gsub("\\*.*", "", summ[,"V.GENE.and.allele"]))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
82 out[,"Top D Gene"] = gsub(".* ", "", gsub("\\*.*", "", summ[,"D.GENE.and.allele"]))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
83 out[,"Top J Gene"] = gsub(".* ", "", gsub("\\*.*", "", summ[,"J.GENE.and.allele"]))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
84
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
85 out = out[!grepl("Less than", summ[,"V.GENE.and.allele"]),]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
86 out = out[!grepl("Less than", summ[,"D.GENE.and.allele"]),]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
87 out = out[!grepl("Less than", summ[,"J.GENE.and.allele"]),]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
88
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
89 out = out[,c('Sequence.ID','JUNCTION.frame','Top V Gene','Top D Gene','Top J Gene','CDR1.Seq','CDR1.Length','CDR2.Seq','CDR2.Length','CDR3.Seq','CDR3.Length','CDR3.Seq.DNA','CDR3.Length.DNA','Strand','CDR3.Found.How','Functionality','V.REGION.identity..','V.REGION.identity.nt','D.REGION.reading.frame','AA.JUNCTION','Functionality.comment','Sequence','FR1.IMGT','FR2.IMGT','FR3.IMGT','CDR3.IMGT','JUNCTION','J.REGION','FR4.IMGT','P3.V.nt.nb','N.REGION.nt.nb','N1.REGION.nt.nb','P5.D.nt.nb','P3.D.nt.nb','N2.REGION.nt.nb','P5.J.nt.nb','X3.V.REGION.trimmed.nt.nb','X5.D.REGION.trimmed.nt.nb','X3.D.REGION.trimmed.nt.nb','X5.J.REGION.trimmed.nt.nb','N.REGION','N1.REGION','N2.REGION', 'P5.D1.nt.nb', 'P3.D1.nt.nb', 'N2.REGION.nt.nb', 'P5.D2.nt.nb', 'P3.D2.nt.nb', 'N3.REGION.nt.nb', 'P5.D3.nt.nb', 'P3.D2.nt.nb', 'N4.REGION.nt.nb', 'X5.D1.REGION.trimmed.nt.nb', 'X3.D1.REGION.trimmed.nt.nb', 'X5.D2.REGION.trimmed.nt.nb', 'X3.D2.REGION.trimmed.nt.nb', 'X5.D3.REGION.trimmed.nt.nb', 'X3.D3.REGION.trimmed.nt.nb', 'D.REGION.nt.nb', 'D1.REGION.nt.nb', 'D2.REGION.nt.nb', 'D3.REGION.nt.nb')]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
90
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
91 names(out) = c('ID','VDJ Frame','Top V Gene','Top D Gene','Top J Gene','CDR1 Seq','CDR1 Length','CDR2 Seq','CDR2 Length','CDR3 Seq','CDR3 Length','CDR3 Seq DNA','CDR3 Length DNA','Strand','CDR3 Found How','Functionality','V-REGION identity %','V-REGION identity nt','D-REGION reading frame','AA JUNCTION','Functionality comment','Sequence','FR1-IMGT','FR2-IMGT','FR3-IMGT','CDR3-IMGT','JUNCTION','J-REGION','FR4-IMGT','P3V-nt nb','N-REGION-nt nb','N1-REGION-nt nb','P5D-nt nb','P3D-nt nb','N2-REGION-nt nb','P5J-nt nb','3V-REGION trimmed-nt nb','5D-REGION trimmed-nt nb','3D-REGION trimmed-nt nb','5J-REGION trimmed-nt nb','N-REGION','N1-REGION','N2-REGION', 'P5.D1.nt.nb', 'P3.D1.nt.nb', 'N2.REGION.nt.nb', 'P5.D2.nt.nb', 'P3.D2.nt.nb', 'N3.REGION.nt.nb', 'P5.D3.nt.nb', 'P3.D2.nt.nb', 'N4.REGION.nt.nb', 'X5.D1.REGION.trimmed.nt.nb', 'X3.D1.REGION.trimmed.nt.nb', 'X5.D2.REGION.trimmed.nt.nb', 'X3.D2.REGION.trimmed.nt.nb', 'X5.D3.REGION.trimmed.nt.nb', 'X3.D3.REGION.trimmed.nt.nb', 'D.REGION.nt.nb', 'D1.REGION.nt.nb', 'D2.REGION.nt.nb', 'D3.REGION.nt.nb')
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
92
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
93 out[,"VDJ Frame"] = as.character(out[,"VDJ Frame"])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
94 fltr = out[,"VDJ Frame"] == "in-frame"
40
106275b54470 Uploaded
davidvanzessen
parents: 39
diff changeset
95 if(any(fltr, na.rm=T)){
0
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
96 out[fltr, "VDJ Frame"] = "In-frame"
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
97 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
98 fltr = out[,"VDJ Frame"] == "null"
40
106275b54470 Uploaded
davidvanzessen
parents: 39
diff changeset
99 if(any(fltr, na.rm = T)){
0
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
100 out[fltr, "VDJ Frame"] = "Out-of-frame"
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
101 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
102 fltr = out[,"VDJ Frame"] == "out-of-frame"
40
106275b54470 Uploaded
davidvanzessen
parents: 39
diff changeset
103 if(any(fltr, na.rm = T)){
0
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
104 out[fltr, "VDJ Frame"] = "Out-of-frame"
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
105 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
106 fltr = out[,"VDJ Frame"] == ""
40
106275b54470 Uploaded
davidvanzessen
parents: 39
diff changeset
107 if(any(fltr, na.rm = T)){
0
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
108 out[fltr, "VDJ Frame"] = "Out-of-frame"
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
109 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
110
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
111 for(col in c('Top V Gene','Top D Gene','Top J Gene')){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
112 out[,col] = as.character(out[,col])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
113 fltr = out[,col] == ""
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
114 fltr[is.na(fltr)] = T
40
106275b54470 Uploaded
davidvanzessen
parents: 39
diff changeset
115 if(any(fltr, na.rm = T)){
0
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
116 out[fltr,col] = "NA"
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
117 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
118 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
119
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
120 write.table(out, out.file, sep="\t", quote=F, row.names=F, col.names=T)