@@ -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