-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRead_Entry_Function.R
executable file
·192 lines (123 loc) · 4.94 KB
/
Read_Entry_Function.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# Function for reading one entry of the Main Data
# Input: One entry of the table + additional info for the study
# Output: DataFrame to be rbind in the main dataset
# If the entry is from the macroscopic column or the organ weight column,target is not NA
read.entry <- function(entry, RO, StudyId, Species,type, Duration, Dose, Dose_interv, target=NA) {
# Define all possible options for every category
Reversibility_Values=c("r", "tr", "nr", "ap_r", "na","r_tr")
Severity_Values=c("minimal", "mild", "slight", "moderate","marked","severe")
Second_level_info_Values=c("immunogenicity", "secondary", "stress")
#Create the empty dataFrame
if (any(is.na(target))) {
df=data.frame(identifier=character(),
study_id=character(),
species=character(),
type=character(),
duration=character(),
dose=character(),
dose_interval=character(),
severity=character(),
description=character(),
adversity=character(),
reversibility=character(),
extra_info=character(),
stringsAsFactors = FALSE)
}else {
df=data.frame(identifier=character(),
study_id=character(),
species=character(),
type=character(),
duration=character(),
dose=character(),
dose_interval=character(),
severity=character(),
description=character(),
target=character(),
adversity=character(),
reversibility=character(),
extra_info=character(),
stringsAsFactors = FALSE)
}
#Split the enrty into the different findings and delete uneccesary gaps in the beginning and the end of each finding
findings_vector=unlist(strsplit(entry, ","))
findings_vector=as.character(sapply(findings_vector,trimws))
# If target is not NA, split the targets and delete uneccesary gaps in the beginning and the end of each target organ
if (!any(is.na(target))) {
target_organs=unlist(strsplit(target,","))
target_organs=as.character(sapply(target_organs,trimws))
}
#Define number of findings
numb_find=length(findings_vector)
#Start breaking each of the findings
for (i in 1:numb_find) {
#Define one specific finding
finding=findings_vector[i]
#Split the entries in the finding and delete uneccessary spaces
finding=unlist(strsplit(finding,"[-,/]"))
finding=as.character(sapply(finding,trimws))
#Start the row-entry in the dataframe
df_row=c(RO, StudyId, Species,type, Duration, Dose, Dose_interv)
#Define Adversity
index=1
if (finding[index]=="ayes") {
adversity="yes"
index=index+1
}else if (finding[index]=="yes") {
adversity="no"
index=index+1
} else {
print(paste("Error in adversity at study", StudyId,"in the dose", Dose ))
}
#Define Severity
if (finding[index] %in% Severity_Values) {
severity=finding[index]
index=index+1
}else {
severity=NA
}
#Define Description
description=finding[index]
index=index+1
#Define Reversibility
if (finding[index] %in% Reversibility_Values){
if (finding[index]=="r") {
reversibility="Reversible"
} else if (finding[index]=="nr") {
reversibility="Not_Reversible"
} else if (finding[index]=="ap_r") {
reversibility="Appeared_in_Recovery"
} else if (finding[index]=="tr") {
reversibility="Trend_Recovery"
} else if (finding[index]=="na") {
reversibility=NA
} else if (finding[index]=="r_tr") {
reversibility="Trend_Recovery_or_Recovery"
}
index=index+1
} else {
print(paste("Error in reversibility at study", StudyId,"in the dose", Dose ))
}
#Define extra_info
if (index==length(finding)) {
extra_info=finding[index]
}else{
extra_info=NA
}
# Define target if exist
if (!any(is.na(target))) {
finding_target=target_organs[i]
}
# Combine everything into a new row of the df
if (any(is.na(target))) {
df_row= append(df_row,c(severity,description,adversity,reversibility,extra_info))
}else {
df_row= append(df_row,c(severity,description,finding_target,adversity,reversibility,extra_info))
}
#Add it to the df dataframe
df_row=data.frame(t(df_row), stringsAsFactors = FALSE)
names(df_row)=names(df)
df=rbind(df,df_row)
rm(df_row)
}
return(df)
}