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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
|
# Copyright 2019 Damien Goutte-Gattat. All rights reserved.
#
# This file is part of the Biopython distribution and governed by your
# choice of the "Biopython License Agreement" or the "BSD 3-Clause License".
# Please see the LICENSE file that should have been included as part of this
# package.
"""Tests for the SeqIO Gck module."""
from io import BytesIO
import os.path
import shutil
import unittest
from zipfile import ZipFile
from Bio import SeqIO, MissingExternalDependencyError
from urllib.request import urlopen, HTTPError
import requires_internet
class TestGckWithDGVC(unittest.TestCase):
sample_data = {
"pACW": {
"file": "Drosophila Gateway Vectors GCK/pACW",
"name": "Construct:",
"id": "Construct:",
"description": "Construct: pACTIN-RW-SV",
"length": 7957,
"topology": "circular",
"features": [
{
"type": "CDS",
"start": 6155,
"end": 7013,
"strand": 1,
"label": "ampR",
},
{
"type": "misc_feature",
"start": 5216,
"end": 6071,
"strand": 1,
"label": "SV40 sti/polyA",
},
{
"type": "misc_feature",
"start": 89,
"end": 2662,
"strand": 1,
"label": "actin5C promoter",
},
{
"type": "CDS",
"start": 3722,
"end": 4400,
"strand": 1,
"label": "chlR",
},
{
"type": "CDS",
"start": 4722,
"end": 5025,
"strand": 1,
"label": "ccdB",
},
{
"type": "misc_feature",
"start": 3489,
"end": 3507,
"strand": 1,
"label": "attR1",
},
{
"type": "misc_feature",
"start": 5175,
"end": 5193,
"strand": -1,
"label": "attR2",
},
{
"type": "misc_feature",
"start": 3489,
"end": 5193,
"strand": 1,
"label": "Gateway cassette",
},
{
"type": "misc_feature",
"start": 5192,
"end": 5205,
"strand": 1,
"label": "triple STOP",
},
{
"type": "CDS",
"start": 2763,
"end": 3480,
"strand": 1,
"label": "ECFP",
},
{
"type": "misc_feature",
"start": 2755,
"end": 3482,
"strand": 1,
"label": "pACTIN-SV",
},
{
"type": "misc_feature",
"start": 2755,
"end": 3482,
"strand": 1,
"label": "Construct: pACTIN-RW-SV",
},
],
},
"pPWF": {
"file": "Drosophila Gateway Vectors GCK/pPWG",
"name": "Construct:",
"id": "Construct:",
"description": "Construct: pPWF",
"length": 12320,
"topology": "circular",
"features": [
{
"type": "misc_feature",
"start": 0,
"end": 587,
"strand": 1,
"label": "P 5' end",
},
{
"type": "misc_feature",
"start": 9327,
"end": 9560,
"strand": -1,
"label": "P 3' end",
},
{
"type": "misc_feature",
"start": 1363,
"end": 4244,
"strand": -1,
"label": "mini-white",
},
{
"type": "CDS",
"start": 10466,
"end": 11324,
"strand": 1,
"label": "ampR",
},
{
"type": "misc_feature",
"start": 7930,
"end": 9314,
"strand": 1,
"label": "K10 terminator",
},
{
"type": "misc_feature",
"start": 4762,
"end": 4829,
"strand": 1,
"label": "GAGA repeats",
},
{
"type": "misc_feature",
"start": 4855,
"end": 5177,
"strand": 1,
"label": "GAL4 sites",
},
{
"type": "misc_feature",
"start": 5279,
"end": 5415,
"strand": 1,
"label": "P intron",
},
{
"type": "misc_feature",
"start": 5184,
"end": 5279,
"strand": 1,
"label": "P promoter",
},
{
"type": "misc_feature",
"start": 4762,
"end": 5416,
"strand": 1,
"label": "UASp promoter",
},
{
"type": "misc_feature",
"start": 10060,
"end": 12092,
"strand": 1,
"label": "pUC8",
},
{
"type": "misc_feature",
"start": 7106,
"end": 7124,
"strand": -1,
"label": "attR2",
},
{
"type": "CDS",
"start": 6653,
"end": 6956,
"strand": 1,
"label": "ccdB",
},
{
"type": "CDS",
"start": 5653,
"end": 6331,
"strand": 1,
"label": "chlR",
},
{
"type": "misc_feature",
"start": 5420,
"end": 7124,
"strand": 1,
"label": "Gateway Cassette",
},
{
"type": "misc_feature",
"start": 5420,
"end": 5438,
"strand": 1,
"label": "attR1",
},
{
"type": "CDS",
"start": 7137,
"end": 7854,
"strand": 1,
"label": "EGFP",
},
{
"type": "misc_feature",
"start": 7129,
"end": 7856,
"strand": 1,
"label": "pACTIN-SV",
},
{
"type": "misc_feature",
"start": 7129,
"end": 7856,
"strand": 1,
"label": "Construct: pACTIN-WC-SV",
},
{
"type": "misc_feature",
"start": 5416,
"end": 7875,
"strand": 1,
"label": "Construct: pPWF",
},
],
},
}
def setUp(self):
# We are using the files of the Drosophila Gateway Vector Collection
# (<https://emb.carnegiescience.edu/drosophila-gateway-vector-collection>)
# as sample Gck files. We cannot redistribute those files along with
# Biopython, so we need to download them now for the tests to run.
if not os.path.exists("Gck/DGVC_GCK.zip"):
try:
requires_internet.check()
except MissingExternalDependencyError:
self.skipTest("Sample files missing and no Internet access")
return
try:
with urlopen(
"https://emb.carnegiescience.edu/sites/default/files/DGVC_GCK.zip"
) as src, open("Gck/DGVC_GCK.zip", "wb") as dst:
shutil.copyfileobj(src, dst)
except HTTPError:
self.skipTest("Cannot download the sample files")
return
self.zipdata = ZipFile("Gck/DGVC_GCK.zip")
def tearDown(self):
self.zipdata.close()
def test_read(self):
"""Read sample files."""
for sample in self.sample_data.values():
with self.zipdata.open(sample["file"]) as f:
record = SeqIO.read(f, "gck")
self.assertEqual(sample["name"], record.name)
self.assertEqual(sample["id"], record.id)
self.assertEqual(sample["description"], record.description)
self.assertEqual(sample["length"], len(record))
self.assertEqual(sample["topology"], record.annotations["topology"])
self.assertEqual(len(sample["features"]), len(record.features))
for i, exp_feat in enumerate(sample["features"]):
read_feat = record.features[i]
self.assertEqual(exp_feat["type"], read_feat.type)
self.assertEqual(exp_feat["start"], read_feat.location.start)
self.assertEqual(exp_feat["end"], read_feat.location.end)
self.assertEqual(exp_feat["strand"], read_feat.location.strand)
self.assertEqual(exp_feat["label"], read_feat.qualifiers["label"][0])
class TestGckWithArtificialData(unittest.TestCase):
def setUp(self):
with open("Gck/artificial.gck", "rb") as f:
self.buffer = f.read()
def test_read(self):
"""Read an artificial sample file."""
h = BytesIO(self.buffer)
record = SeqIO.read(h, "gck")
self.assertEqual("ACGTACGTACGT", record.seq)
self.assertEqual("Sample construct", record.description)
self.assertEqual("linear", record.annotations["topology"])
self.assertEqual(2, len(record.features))
self.assertEqual(2, record.features[0].location.start)
self.assertEqual(6, record.features[0].location.end)
self.assertEqual(1, record.features[0].location.strand)
self.assertEqual("misc_feature", record.features[0].type)
self.assertEqual("FeatureA", record.features[0].qualifiers["label"][0])
self.assertEqual(7, record.features[1].location.start)
self.assertEqual(11, record.features[1].location.end)
self.assertEqual(-1, record.features[1].location.strand)
self.assertEqual("CDS", record.features[1].type)
self.assertEqual("FeatureB", record.features[1].qualifiers["label"][0])
h.close()
def munge_buffer(self, position, value):
mod_buffer = bytearray(self.buffer)
if isinstance(value, list):
mod_buffer[position : position + len(value) - 1] = value
else:
mod_buffer[position] = value
return BytesIO(mod_buffer)
def test_conflicting_lengths(self):
"""Read a file with incorrect length."""
# Change the sequence length as indicated in the sequence packet
h = self.munge_buffer(0x1C, [0x00, 0x00, 0x20, 0x15])
with self.assertRaisesRegex(ValueError, "Conflicting sequence length values"):
SeqIO.read(h, "gck")
h.close()
# Change the sequence length as indicated in the features packet
h = self.munge_buffer(0x36, [0x00, 0x00, 0x20, 0x15])
with self.assertRaisesRegex(ValueError, "Conflicting sequence length values"):
SeqIO.read(h, "gck")
h.close()
# Change the number of features
h = self.munge_buffer(0x3B, 0x30)
with self.assertRaisesRegex(
ValueError, "Features packet size inconsistent with number of features"
):
SeqIO.read(h, "gck")
h.close()
# Change the number of restriction sites
h = self.munge_buffer(0x137, 0x30)
with self.assertRaisesRegex(
ValueError, "Sites packet size inconsistent with number of sites"
):
SeqIO.read(h, "gck")
h.close()
class TestGckWithImproperHeader(unittest.TestCase):
def test_read(self):
"""Read a file with an incomplete header."""
handle = BytesIO(b"tiny")
with self.assertRaisesRegex(
ValueError, "Improper header, cannot read 24 bytes from handle"
):
SeqIO.read(handle, "gck")
handle.close()
if __name__ == "__main__":
runner = unittest.TextTestRunner(verbosity=2)
unittest.main(testRunner=runner)
|