Browse code

Implement first function and unit testing

Katharina Waury authored on 23/11/2023 15:52:05
Showing 5 changed files

... ...
@@ -9,3 +9,9 @@ Description: More about what it does (maybe more than one line)
9 9
 License: Apache License (>= 2)
10 10
 Encoding: UTF-8
11 11
 LazyData: true
12
+Suggests: 
13
+    testthat (>= 3.0.0)
14
+Config/testthat/edition: 3
15
+Imports: 
16
+    httr,
17
+    jsonlite
12 18
new file mode 100644
... ...
@@ -0,0 +1,57 @@
1
+getProteinFeatures <- function(uniprot) {
2
+  print("Hello, world!")
3
+  return(NULL)
4
+}
5
+
6
+
7
+accessUniprot <- function(uniprot) {
8
+
9
+  if (is.character(uniprot) == FALSE) {
10
+    warning("Please provide a valid UniProt ID.")
11
+    return(NULL)
12
+  }
13
+
14
+  # create protein-specific UniProt URL
15
+  url <- paste0("https://www.uniprot.org/uniprot/", uniprot, ".json")
16
+
17
+  # make the GET request
18
+  response <- httr::GET(url = url)
19
+
20
+  print(response$status_code == 200)
21
+
22
+  # check if request was successful
23
+  if (response$status_code == 200) {
24
+
25
+    # parse JSON response
26
+    result <- jsonlite::fromJSON(httr::content(response, "text", encoding="UTF-8"))
27
+    return(result)
28
+
29
+  } else {
30
+
31
+    warning("Error fetching data from UniProt.")
32
+    return(NULL)
33
+  }
34
+}
35
+
36
+
37
+# accessPredictProtein <- function(uniprot) {
38
+#
39
+#   # create protein-specific UniProt URL
40
+#   url <- paste0("https://api.predictprotein.org/v1/results/", uniprot)
41
+#
42
+#   # Make the GET request
43
+#   response <- http::GET(url = url)
44
+#
45
+#   # Check if the request was successful
46
+#   if (http_status(response)$category == "Success") {
47
+#
48
+#     # Parse JSON response
49
+#     result <- fromJSON(content(response, "text"))
50
+#     return(result)
51
+#
52
+#   } else {
53
+#
54
+#     warning("Error fetching data from UniProt.")
55
+#     return(NULL)
56
+#   }
57
+# }
0 58
deleted file mode 100644
... ...
@@ -1,18 +0,0 @@
1
-# Hello, world!
2
-#
3
-# This is an example function named 'hello' 
4
-# which prints 'Hello, world!'.
5
-#
6
-# You can learn more about package authoring with RStudio at:
7
-#
8
-#   http://r-pkgs.had.co.nz/
9
-#
10
-# Some useful keyboard shortcuts for package authoring:
11
-#
12
-#   Install Package:           'Ctrl + Shift + B'
13
-#   Check Package:             'Ctrl + Shift + E'
14
-#   Test Package:              'Ctrl + Shift + T'
15
-
16
-hello <- function() {
17
-  print("Hello, world!")
18
-}
19 0
new file mode 100644
... ...
@@ -0,0 +1,12 @@
1
+# This file is part of the standard setup for testthat.
2
+# It is recommended that you do not modify it.
3
+#
4
+# Where should you do additional test configuration?
5
+# Learn more about the roles of various files in:
6
+# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
7
+# * https://testthat.r-lib.org/articles/special-files.html
8
+
9
+library(testthat)
10
+library(immunogenViewer)
11
+
12
+test_check("immunogenViewer")
0 13
new file mode 100644
... ...
@@ -0,0 +1,10 @@
1
+test_that("Only strings are accepted for UniProt IDs", {
2
+  expect_null(accessUniprot(NULL))
3
+  expect_null(accessUniprot(4))
4
+  expect_null(accessUniprot(3.14))
5
+})
6
+
7
+test_that("Warning is raised if UniProt ID is incorrect", {
8
+  expect_null(accessUniprot("Hello"))
9
+  expect_null(accessUniprot("A00000"))
10
+})