Skip to content

Commit fbdd7d5

Browse files
committed
Merge branch 'release/0.5.4'
2 parents ecde422 + c91c715 commit fbdd7d5

4 files changed

Lines changed: 82 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,18 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
66

77
## [Unreleased]
88

9-
## [0.5.3] - 2019-03-19
9+
## [0.5.4] - 2019-03-19
10+
11+
### Added
12+
13+
- Downloading annotation from NCBI now implemented.
14+
- Genbank assemblies at NCBI can be searched and downloaded
1015

1116
### Fixed
1217

13-
- Fixed Ensembl downloads
18+
- Fixed #23.
19+
- Fixed #26.
20+
- Fixed Ensembl downloads (#30)
1421
- Fixed FTP tests for CI
1522

1623
## [0.5.2] - 2018-09-11

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ utilities and make sure they are in your PATH:
3939
* `genePredToGtf`
4040
* `bedToGenePred`
4141
* `gtfToGenePred`
42+
* `gff3ToGenePred`
4243

4344
You can find the binaries [here](http://hgdownload.cse.ucsc.edu/admin/exe/).
4445

genomepy/__about__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""Metadata"""
2-
__version__ = '0.5.3'
2+
__version__ = '0.5.4'
33
__author__ = "Simon van Heeringen"

genomepy/provider.py

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,17 +617,23 @@ def _get_genomes(self):
617617

618618
names = [
619619
"assembly_summary_refseq.txt",
620+
"assembly_summary_genbank.txt",
620621
"assembly_summary_refseq_historical.txt",
621622
]
622623

624+
sys.stderr.write("Downloading assembly summaries from NCBI, this will take a while...\n")
625+
seen = {}
623626
for fname in names:
624627
urlcleanup()
625628
response = urlopen(self.assembly_url + "/" + fname)
626629
lines = response.read().decode('utf-8').splitlines()
627630
header = lines[1].strip("# ").split("\t")
628631
for line in lines[2:]:
629632
vals = line.strip("# ").split("\t")
630-
genomes.append(dict(zip(header, vals)))
633+
# Don't repeat samples with the same BioSample ID
634+
if vals[2] not in seen: # BioSample ID
635+
genomes.append(dict(zip(header, vals)))
636+
seen[vals[2]] = 1
631637

632638
return genomes
633639

@@ -780,3 +786,67 @@ def _post_process_download(self, name, genome_dir, mask="soft"):
780786

781787
# Rename tmp file to real genome file
782788
shutil.move(new_fa, fa)
789+
790+
def download_annotation(self, name, genome_dir, version=None):
791+
"""
792+
Download annotation file to to a specific directory
793+
794+
Parameters
795+
----------
796+
name : str
797+
Genome / species name
798+
799+
genome_dir : str
800+
Directory to install annotation
801+
"""
802+
sys.stderr.write("Downloading annotation...\n")
803+
if not self.genomes:
804+
self.genomes = self._get_genomes()
805+
806+
for genome in self.genomes:
807+
if genome["asm_name"] == name:
808+
#ftp_path': 'ftp://ftp.ncbi.nlm.nih.gov/genomes/all/GCF/000/004/195/GCF_000004195.3_Xenopus_tropicalis_v9.1'
809+
url = genome["ftp_path"]
810+
url += "/" + url.split("/")[-1] + "_genomic.gff.gz"
811+
812+
out_dir = os.path.join(genome_dir, name)
813+
if not os.path.exists(out_dir):
814+
os.mkdir(out_dir)
815+
816+
# Download the file
817+
try:
818+
response = urlopen(url)
819+
gff_file = out_dir + "/"+ name + ".annotation.gff.gz"
820+
with open(gff_file, "wb") as f:
821+
f.write(response.read())
822+
except Exception:
823+
sys.stderr.write("WARNING: Could not download annotation from NCBI, skipping.\n")
824+
sys.stderr.write("URL: {}\n".format(url))
825+
826+
sys.stderr.write("If you think the annotation should be there, please file a bug report at:\n")
827+
sys.stderr.write("https://github.com/simonvh/genomepy/issues\n")
828+
return
829+
830+
cmd = "gff3ToGenePred {0} /dev/stdout | wc -l"
831+
out = sp.check_output(cmd.format(gff_file), shell=True)
832+
if out.strip() == b"0":
833+
sys.stderr.write("WARNING: annotation from NCBI contains no genes, skipping.\n")
834+
else:
835+
# Convert to BED file
836+
bed_file = gff_file.replace("gff.gz", "bed")
837+
cmd = "gff3ToGenePred -rnaNameAttr=gene {0} /dev/stdout | genePredToBed /dev/stdin {1} && gzip {1}"
838+
ret = sp.check_call(cmd.format(gff_file, bed_file), shell=True)
839+
840+
# Convert to GTF file
841+
gtf_file = gff_file.replace("gff.gz", "gtf")
842+
cmd = "gff3ToGenePred -geneNameAttr=gene {0} /dev/stdout | genePredToGtf file /dev/stdin {1} && gzip {1}"
843+
ret = sp.check_call(cmd.format(gff_file, gtf_file), shell=True)
844+
845+
readme = os.path.join(genome_dir, name, "README.txt")
846+
with open(readme, "a") as f:
847+
f.write("annotation url: {}\n".format(url))
848+
849+
return out_dir
850+
851+
852+

0 commit comments

Comments
 (0)