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
use serde::{Deserialize, Serialize};
use tracing::instrument;
use htsget_config::Query;
use crate::{QueryBuilder, Result};
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct PostRequest {
pub format: Option<String>,
pub class: Option<String>,
pub fields: Option<Vec<String>>,
pub tags: Option<Vec<String>>,
pub notags: Option<Vec<String>>,
pub regions: Option<Vec<Region>>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Region {
#[serde(rename = "referenceName")]
pub reference_name: String,
pub start: Option<u32>,
pub end: Option<u32>,
}
impl PostRequest {
#[instrument(level = "trace", skip_all, ret)]
pub(crate) fn get_queries(self, id: impl Into<String>) -> Result<Vec<Query>> {
if let Some(ref regions) = self.regions {
let id = id.into();
regions
.iter()
.map(|region| {
Ok(
self
.get_base_query_builder(id.clone())?
.with_reference_name(Some(region.reference_name.clone()))
.with_range_from_u32(region.start, region.end)?
.build(),
)
})
.collect::<Result<Vec<Query>>>()
} else {
Ok(vec![self.get_base_query_builder(id)?.build()])
}
}
fn get_base_query_builder(&self, id: impl Into<String>) -> Result<QueryBuilder> {
QueryBuilder::new(Some(id.into()), self.format.clone())?
.with_class(self.class.clone())?
.with_fields_from_vec(self.fields.clone())
.with_tags_from_vec(self.tags.clone(), self.notags.clone())
}
}
#[cfg(test)]
mod tests {
use htsget_config::{Class, Format};
use super::*;
#[test]
fn post_request_without_regions() {
assert_eq!(
PostRequest {
format: Some("VCF".to_string()),
class: Some("header".to_string()),
fields: None,
tags: None,
notags: None,
regions: None,
}
.get_queries("id")
.unwrap(),
vec![Query::new("id", Format::Vcf).with_class(Class::Header)]
);
}
#[test]
fn post_request_with_one_region() {
assert_eq!(
PostRequest {
format: Some("VCF".to_string()),
class: Some("header".to_string()),
fields: None,
tags: None,
notags: None,
regions: Some(vec![Region {
reference_name: "20".to_string(),
start: Some(150),
end: Some(153),
}]),
}
.get_queries("id")
.unwrap(),
vec![Query::new("id", Format::Vcf)
.with_class(Class::Header)
.with_reference_name("20".to_string())
.with_start(150)
.with_end(153)]
);
}
#[test]
fn post_request_with_regions() {
assert_eq!(
PostRequest {
format: Some("VCF".to_string()),
class: Some("header".to_string()),
fields: None,
tags: None,
notags: None,
regions: Some(vec![
Region {
reference_name: "20".to_string(),
start: Some(150),
end: Some(153),
},
Region {
reference_name: "11".to_string(),
start: Some(152),
end: Some(154),
}
]),
}
.get_queries("id")
.unwrap(),
vec![
Query::new("id", Format::Vcf)
.with_class(Class::Header)
.with_reference_name("20".to_string())
.with_start(150)
.with_end(153),
Query::new("id", Format::Vcf)
.with_class(Class::Header)
.with_reference_name("11".to_string())
.with_start(152)
.with_end(154)
]
);
}
}