<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Search for a reference genome
Homo sapiens's reference genome sequence
We would need two reference genomes. One as a fasta file with each chromosome, and one that we will use exclusively for the mapping that would contain all contigs.
The use of contigs in the reference genome increases the mapping specificity.
Step1:  Download from the NCBI the list of chromosome/contigs
Step2:  For each contig/chromosome download the corresponding FASTA file from NCBI
Step3:  Concatenate all contigs/chromosomes into a single file
Step4:  Remove all the other files (with single chromosome/contig)
Step5:  Creation of an index file for GEM mapper
<ASSISTANT_TASK:>
Python Code:
species = 'Homo sapiens'
taxid   = '9606'
genome  = 'GRCh38.p10'
refseq, dir1, dir2, dir3 = 'GCF', '000', '001', '405' 
genbank = 'GCF_000001405.36'
sumurl = 'ftp://ftp.ncbi.nlm.nih.gov/genomes/all/{0}/{1}/{2}/{3}/{4}_{5}/{4}_{5}_assembly_report.txt'.format(
    refseq, dir1, dir2, dir3, genbank, genome)
crmurl = 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=nuccore&id=%s&rettype=fasta&retmode=text'
Explanation: Search for a reference genome
Homo sapiens's reference genome sequence
We would need two reference genomes. One as a fasta file with each chromosome, and one that we will use exclusively for the mapping that would contain all contigs.
The use of contigs in the reference genome increases the mapping specificity.
End of explanation
! wget -q $sumurl -O chromosome_list.txt
! head chromosome_list.txt
dirname = 'genome/'
! mkdir -p $dirname
Explanation: Download from the NCBI the list of chromosome/contigs
End of explanation
contig = []
for line in open('chromosome_list.txt'):
    if line.startswith('#'):
        continue
    seq_name, seq_role, assigned_molecule, _, genbank, _, refseq, _ = line.split(None, 7)
    if seq_role == 'assembled-molecule':
        name = 'chr%s.fasta' % assigned_molecule
    else:
        name = 'chr%s_%s.fasta' % (assigned_molecule, seq_name.replace('/', '-'))
    contig.append(name)
    outfile = os.path.join(dirname, name)
    if os.path.exists(outfile) and os.path.getsize(outfile) > 10:
        continue
    error_code = os.system('wget "%s" --no-check-certificate -O %s' % (crmurl % (genbank), outfile))
    if error_code:
        error_code = os.system('wget "%s" --no-check-certificate -O %s' % (crmurl % (refseq), outfile))
    if error_code:
        print genbank
Explanation: For each contig/chromosome download the corresponding FASTA file from NCBI
End of explanation
contig_file = open('genome/Homo_sapiens_contigs.fa','w')
for molecule in contig:
    for line in open('genome/' + molecule):
        # replace the header of the sequence in the fasta file
        if line == '\n':
            continue
        if line.startswith('>'):
            line = '>' + molecule[3:].replace('.fasta', '') + '\n'
        contig_file.write(line)
contig_file.close()
Explanation: Concatenate all contigs/chromosomes into a single file
End of explanation
! rm -f genome/*.fasta
Explanation: Remove all the other files (with single chromosome/contig)
End of explanation
! gem-indexer -t 8 -i genome/Homo_sapiens_contigs.fa -o genome/Homo_sapiens_contigs
Explanation: Creation of an index file for GEM mapper
End of explanation
<END_TASK>