def run(self, ubiquityThreshold, singleCopyThreshold, minGenomes, mostSpecificRank, minMarkers, completenessThreshold, contaminationThreshold): print 'Ubiquity threshold: ' + str(ubiquityThreshold) print 'Single-copy threshold: ' + str(singleCopyThreshold) print 'Min. genomes: ' + str(minGenomes) print 'Most specific taxonomic rank: ' + str(mostSpecificRank) print 'Min markers: ' + str(minMarkers) print 'Completeness threshold: ' + str(completenessThreshold) print 'Contamination threshold: ' + str(contaminationThreshold) img = IMG() markerset = MarkerSet() lineages = img.lineagesByCriteria(minGenomes, mostSpecificRank) degenerateGenomes = {} for lineage in lineages: genomeIds = img.genomeIdsByTaxonomy(lineage, 'Final') print '' print 'Lineage ' + lineage + ' contains ' + str(len(genomeIds)) + ' genomes.' # get table of PFAMs and do some initial filtering to remove PFAMs that are # clearly not going to pass the ubiquity and single-copy thresholds countTable = img.countTable(genomeIds) countTable = img.filterTable(genomeIds, countTable, ubiquityThreshold*0.9, singleCopyThreshold*0.9) markerGenes = markerset.markerGenes(genomeIds, countTable, ubiquityThreshold*len(genomeIds), singleCopyThreshold*len(genomeIds)) if len(markerGenes) < minMarkers: continue geneDistTable = img.geneDistTable(genomeIds, markerGenes, spacingBetweenContigs=1e6) colocatedGenes = markerset.colocatedGenes(geneDistTable) colocatedSets = markerset.colocatedSets(colocatedGenes, markerGenes) for genomeId in genomeIds: completeness, contamination = markerset.genomeCheck(colocatedSets, genomeId, countTable) if completeness < completenessThreshold or contamination > contaminationThreshold: degenerateGenomes[genomeId] = degenerateGenomes.get(genomeId, []) + [[lineage.split(';')[-1].strip(), len(genomeIds), len(colocatedSets), completeness, contamination]] # write out degenerate genomes metadata = img.genomeMetadata('Final') fout = open('./data/degenerate_genomes.tsv', 'w') fout.write('Genome Id\tTaxonomy\tGenome Size (Gbps)\tScaffolds\tBiotic Relationships\tStatus\tLineage\t# genomes\tMarker set size\tCompleteness\tContamination\n') for genomeId, data in degenerateGenomes.iteritems(): fout.write(genomeId + '\t' + '; '.join(metadata[genomeId]['taxonomy']) + '\t%.2f' % (float(metadata[genomeId]['genome size']) / 1e6) + '\t' + str(metadata[genomeId]['scaffold count'])) fout.write('\t' + metadata[genomeId]['biotic relationships'] + '\t' + metadata[genomeId]['status']) for d in data: fout.write('\t' + d[0] + '\t' + str(d[1]) + '\t' + str(d[2]) + '\t%.3f\t%.3f' % (d[3], d[4])) fout.write('\n') fout.close()
def run(self, ubiquityThreshold, singleCopyThreshold, minGenomes, minMarkers, mostSpecificRank, percentGenomes, numReplicates): img = IMG() lineages = img.lineagesByCriteria(minGenomes, mostSpecificRank) fout = open('./data/lineage_evaluation.tsv', 'w') fout.write('Lineage\t# genomes\t# markers\tpercentage\tnum replicates\tmean\tstd\tmean %\tmean + std%\tmean + 2*std %\n') for lineage in lineages: genomeIds = img.genomeIdsByTaxonomy(lineage, 'Final') if len(genomeIds) < minGenomes: continue countTable = img.countTable(genomeIds) countTable = img.filterTable(genomeIds, countTable, ubiquityThreshold*0.9, singleCopyThreshold*0.9) # calculate marker set for all genomes markerGenes = img.markerGenes(genomeIds, countTable, ubiquityThreshold*len(genomeIds), singleCopyThreshold*len(genomeIds)) if len(markerGenes) < minMarkers: continue print '\nLineage ' + lineage + ' contains ' + str(len(genomeIds)) + ' genomes.' print ' Marker genes: ' + str(len(markerGenes)) fout.write(lineage + '\t' + str(len(genomeIds)) + '\t' + str(len(markerGenes)) + '\t%.2f' % percentGenomes + '\t' + str(numReplicates)) # withhold select percentage of genomes and calculate new marker set changeMarkerSetSize = [] for _ in xrange(0, numReplicates): subsetGenomeIds = random.sample(genomeIds, int((1.0-percentGenomes)*len(genomeIds) + 0.5)) newMarkerGenes = img.markerGenes(subsetGenomeIds, countTable, ubiquityThreshold*len(subsetGenomeIds), singleCopyThreshold*len(subsetGenomeIds)) changeMarkerSetSize.append(len(newMarkerGenes.symmetric_difference(markerGenes))) m = mean(changeMarkerSetSize) s = std(changeMarkerSetSize) print ' Mean: %.2f, Std: %.2f, Per: %.2f' % (m, s, (m+ 2*s) * 100 / len(markerGenes)) fout.write('\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f' % (m, s, m * 100 / len(markerGenes), (m + s) * 100 / len(markerGenes), (m + 2*s) * 100 / len(markerGenes)) + '\n') fout.close()
def run(self, ubiquityThreshold, singleCopyThreshold, minGenomes, mostSpecificRank, minMarkers, completenessThreshold, contaminationThreshold): print 'Ubiquity threshold: ' + str(ubiquityThreshold) print 'Single-copy threshold: ' + str(singleCopyThreshold) print 'Min. genomes: ' + str(minGenomes) print 'Most specific taxonomic rank: ' + str(mostSpecificRank) print 'Min markers: ' + str(minMarkers) print 'Completeness threshold: ' + str(completenessThreshold) print 'Contamination threshold: ' + str(contaminationThreshold) img = IMG() markerset = MarkerSet() lineages = img.lineagesByCriteria(minGenomes, mostSpecificRank) degenerateGenomes = {} for lineage in lineages: genomeIds = img.genomeIdsByTaxonomy(lineage, 'Final') print '' print 'Lineage ' + lineage + ' contains ' + str( len(genomeIds)) + ' genomes.' # get table of PFAMs and do some initial filtering to remove PFAMs that are # clearly not going to pass the ubiquity and single-copy thresholds countTable = img.countTable(genomeIds) countTable = img.filterTable(genomeIds, countTable, ubiquityThreshold * 0.9, singleCopyThreshold * 0.9) markerGenes = markerset.markerGenes( genomeIds, countTable, ubiquityThreshold * len(genomeIds), singleCopyThreshold * len(genomeIds)) if len(markerGenes) < minMarkers: continue geneDistTable = img.geneDistTable(genomeIds, markerGenes, spacingBetweenContigs=1e6) colocatedGenes = markerset.colocatedGenes(geneDistTable) colocatedSets = markerset.colocatedSets(colocatedGenes, markerGenes) for genomeId in genomeIds: completeness, contamination = markerset.genomeCheck( colocatedSets, genomeId, countTable) if completeness < completenessThreshold or contamination > contaminationThreshold: degenerateGenomes[genomeId] = degenerateGenomes.get( genomeId, []) + [[ lineage.split(';')[-1].strip(), len(genomeIds), len(colocatedSets), completeness, contamination ]] # write out degenerate genomes metadata = img.genomeMetadata('Final') fout = open('./data/degenerate_genomes.tsv', 'w') fout.write( 'Genome Id\tTaxonomy\tGenome Size (Gbps)\tScaffolds\tBiotic Relationships\tStatus\tLineage\t# genomes\tMarker set size\tCompleteness\tContamination\n' ) for genomeId, data in degenerateGenomes.iteritems(): fout.write(genomeId + '\t' + '; '.join(metadata[genomeId]['taxonomy']) + '\t%.2f' % (float(metadata[genomeId]['genome size']) / 1e6) + '\t' + str(metadata[genomeId]['scaffold count'])) fout.write('\t' + metadata[genomeId]['biotic relationships'] + '\t' + metadata[genomeId]['status']) for d in data: fout.write('\t' + d[0] + '\t' + str(d[1]) + '\t' + str(d[2]) + '\t%.3f\t%.3f' % (d[3], d[4])) fout.write('\n') fout.close()
def run(self, taxonomyStr, ubiquityThreshold, singleCopyThreshold, numBins, numRndGenomes): img = IMG() markerSet = MarkerSet() metadata = img.genomeMetadata() lineageGenomeIds = img.genomeIdsByTaxonomy(taxonomyStr, metadata) # build marker set from finished prokaryotic genomes genomeIds = [] for genomeId in lineageGenomeIds: if metadata[genomeId]['status'] == 'Finished' and ( metadata[genomeId]['taxonomy'][0] == 'Bacteria' or metadata[genomeId]['taxonomy'][0] == 'Archaea'): genomeIds.append(genomeId) genomeIds = set(genomeIds) - img.genomesWithMissingData(genomeIds) print 'Lineage ' + taxonomyStr + ' contains ' + str( len(genomeIds)) + ' genomes.' # get marker set countTable = img.countTable(genomeIds) countTable = img.filterTable(genomeIds, countTable, 0.9 * ubiquityThreshold, 0.9 * singleCopyThreshold) markerGenes = markerSet.markerGenes( genomeIds, countTable, ubiquityThreshold * len(genomeIds), singleCopyThreshold * len(genomeIds)) tigrToRemove = img.identifyRedundantTIGRFAMs(markerGenes) markerGenes = markerGenes - tigrToRemove geneDistTable = img.geneDistTable(genomeIds, markerGenes, spacingBetweenContigs=1e6) print 'Number of marker genes: ' + str(len(markerGenes)) # randomly set genomes to plot if numRndGenomes != -1: genomeIds = random.sample(list(genomeIds), numRndGenomes) genomeIds = set(genomeIds) # plot distribution of marker genes filename = 'geneDistribution.' + taxonomyStr.replace( ';', '_') + '.' + str(ubiquityThreshold) + '-' + str( singleCopyThreshold) + '.tsv' fout = open(filename, 'w') fout.write( 'Genome ID\tLineage\tNumber of Genes\tUniformity\tDistribution\n') matrix = [] rowLabels = [] for genomeId in genomeIds: binSize = float(metadata[genomeId]['genome size']) / numBins binCounts = [0] * numBins pts = [] for _, data in geneDistTable[genomeId].iteritems(): for genePos in data: binNum = int(genePos[1] / binSize) binCounts[binNum] += 1 pts.append(genePos[1]) matrix.append(binCounts) u = markerSet.uniformity(metadata[genomeId]['genome size'], pts) fout.write(genomeId + '\t' + '; '.join(metadata[genomeId]['taxonomy']) + '\t' + str(len(geneDistTable[genomeId])) + '\t%.3f' % u) for b in xrange(0, numBins): fout.write('\t' + str(binCounts[b])) fout.write('\n') rowLabels.append('%.2f' % u + ', ' + str(genomeId) + ' - ' + '; '.join(metadata[genomeId]['taxonomy'][0:5])) fout.close() # plot data heatmap = Heatmap() plotFilename = 'geneDistribution.' + taxonomyStr.replace( ';', '_') + '.' + str(ubiquityThreshold) + '-' + str( singleCopyThreshold) + '.png' heatmap.plot(plotFilename, matrix, rowLabels, 0.6)
def run(self, ubiquityThreshold, singleCopyThreshold, minGenomes, minMarkers, mostSpecificRank, percentGenomes, numReplicates): img = IMG() lineages = img.lineagesByCriteria(minGenomes, mostSpecificRank) fout = open('./data/lineage_evaluation.tsv', 'w') fout.write( 'Lineage\t# genomes\t# markers\tpercentage\tnum replicates\tmean\tstd\tmean %\tmean + std%\tmean + 2*std %\n' ) for lineage in lineages: genomeIds = img.genomeIdsByTaxonomy(lineage, 'Final') if len(genomeIds) < minGenomes: continue countTable = img.countTable(genomeIds) countTable = img.filterTable(genomeIds, countTable, ubiquityThreshold * 0.9, singleCopyThreshold * 0.9) # calculate marker set for all genomes markerGenes = img.markerGenes(genomeIds, countTable, ubiquityThreshold * len(genomeIds), singleCopyThreshold * len(genomeIds)) if len(markerGenes) < minMarkers: continue print '\nLineage ' + lineage + ' contains ' + str( len(genomeIds)) + ' genomes.' print ' Marker genes: ' + str(len(markerGenes)) fout.write(lineage + '\t' + str(len(genomeIds)) + '\t' + str(len(markerGenes)) + '\t%.2f' % percentGenomes + '\t' + str(numReplicates)) # withhold select percentage of genomes and calculate new marker set changeMarkerSetSize = [] for _ in xrange(0, numReplicates): subsetGenomeIds = random.sample( genomeIds, int((1.0 - percentGenomes) * len(genomeIds) + 0.5)) newMarkerGenes = img.markerGenes( subsetGenomeIds, countTable, ubiquityThreshold * len(subsetGenomeIds), singleCopyThreshold * len(subsetGenomeIds)) changeMarkerSetSize.append( len(newMarkerGenes.symmetric_difference(markerGenes))) m = mean(changeMarkerSetSize) s = std(changeMarkerSetSize) print ' Mean: %.2f, Std: %.2f, Per: %.2f' % (m, s, (m + 2 * s) * 100 / len(markerGenes)) fout.write('\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f' % (m, s, m * 100 / len(markerGenes), (m + s) * 100 / len(markerGenes), (m + 2 * s) * 100 / len(markerGenes)) + '\n') fout.close()
def run(self, taxonomyStr, ubiquityThreshold, singleCopyThreshold, numBins, numRndGenomes): img = IMG() markerSet = MarkerSet() metadata = img.genomeMetadata() lineageGenomeIds = img.genomeIdsByTaxonomy(taxonomyStr, metadata) # build marker set from finished prokaryotic genomes genomeIds = [] for genomeId in lineageGenomeIds: if metadata[genomeId]['status'] == 'Finished' and (metadata[genomeId]['taxonomy'][0] == 'Bacteria' or metadata[genomeId]['taxonomy'][0] == 'Archaea'): genomeIds.append(genomeId) genomeIds = set(genomeIds) - img.genomesWithMissingData(genomeIds) print 'Lineage ' + taxonomyStr + ' contains ' + str(len(genomeIds)) + ' genomes.' # get marker set countTable = img.countTable(genomeIds) countTable = img.filterTable(genomeIds, countTable, 0.9*ubiquityThreshold, 0.9*singleCopyThreshold) markerGenes = markerSet.markerGenes(genomeIds, countTable, ubiquityThreshold*len(genomeIds), singleCopyThreshold*len(genomeIds)) tigrToRemove = img.identifyRedundantTIGRFAMs(markerGenes) markerGenes = markerGenes - tigrToRemove geneDistTable = img.geneDistTable(genomeIds, markerGenes, spacingBetweenContigs=1e6) print 'Number of marker genes: ' + str(len(markerGenes)) # randomly set genomes to plot if numRndGenomes != -1: genomeIds = random.sample(list(genomeIds), numRndGenomes) genomeIds = set(genomeIds) # plot distribution of marker genes filename = 'geneDistribution.' + taxonomyStr.replace(';','_') + '.' + str(ubiquityThreshold) + '-' + str(singleCopyThreshold) + '.tsv' fout = open(filename, 'w') fout.write('Genome ID\tLineage\tNumber of Genes\tUniformity\tDistribution\n') matrix = [] rowLabels = [] for genomeId in genomeIds: binSize = float(metadata[genomeId]['genome size']) / numBins binCounts = [0]*numBins pts = [] for _, data in geneDistTable[genomeId].iteritems(): for genePos in data: binNum = int(genePos[1] / binSize) binCounts[binNum] += 1 pts.append(genePos[1]) matrix.append(binCounts) u = markerSet.uniformity(metadata[genomeId]['genome size'], pts) fout.write(genomeId + '\t' + '; '.join(metadata[genomeId]['taxonomy']) + '\t' + str(len(geneDistTable[genomeId])) + '\t%.3f' % u) for b in xrange(0, numBins): fout.write('\t' + str(binCounts[b])) fout.write('\n') rowLabels.append('%.2f' % u + ', ' + str(genomeId) + ' - ' + '; '.join(metadata[genomeId]['taxonomy'][0:5])) fout.close() # plot data heatmap = Heatmap() plotFilename = 'geneDistribution.' + taxonomyStr.replace(';','_') + '.' + str(ubiquityThreshold) + '-' + str(singleCopyThreshold) + '.png' heatmap.plot(plotFilename, matrix, rowLabels, 0.6)
def run(self, ubiquityThreshold, singleCopyThreshold, trustedCompleteness, trustedContamination, genomeCompleteness, genomeContamination): img = IMG() markerset = MarkerSet() metadata = img.genomeMetadata() trustedOut = open('./data/trusted_genomes.tsv', 'w') trustedOut.write('Genome Id\tLineage\tGenome size (Mbps)\tScaffold count\tBiotic Relationship\tStatus\tCompleteness\tContamination\n') filteredOut = open('./data/filtered_genomes.tsv', 'w') filteredOut.write('Genome Id\tLineage\tGenome size (Mbps)\tScaffold count\tBiotic Relationship\tStatus\tCompleteness\tContamination\n') allGenomeIds = set() allTrustedGenomeIds = set() for lineage in ['Archaea', 'Bacteria']: # get all genomes in lineage and build gene count table print '\nBuilding gene count table.' allLineageGenomeIds = img.genomeIdsByTaxonomy(lineage, metadata, 'All') countTable = img.countTable(allLineageGenomeIds) countTable = img.filterTable(allLineageGenomeIds, countTable, 0.9*ubiquityThreshold, 0.9*singleCopyThreshold) # get all genomes from specific lineage allGenomeIds = allGenomeIds.union(allLineageGenomeIds) print 'Lineage ' + lineage + ' contains ' + str(len(allLineageGenomeIds)) + ' genomes.' # tabulate genomes from each phylum allPhylumCounts = {} for genomeId in allLineageGenomeIds: taxon = metadata[genomeId]['taxonomy'][1] allPhylumCounts[taxon] = allPhylumCounts.get(taxon, 0) + 1 # identify marker set for genomes markerGenes = markerset.markerGenes(allLineageGenomeIds, countTable, ubiquityThreshold*len(allLineageGenomeIds), singleCopyThreshold*len(allLineageGenomeIds)) print ' Marker genes: ' + str(len(markerGenes)) geneDistTable = img.geneDistTable(allLineageGenomeIds, markerGenes, spacingBetweenContigs=1e6) colocatedGenes = markerset.colocatedGenes(geneDistTable, metadata) colocatedSets = markerset.colocatedSets(colocatedGenes, markerGenes) print ' Marker set size: ' + str(len(colocatedSets)) # identifying trusted genomes (highly complete, low contamination genomes) trustedGenomeIds = set() for genomeId in allLineageGenomeIds: completeness, contamination = markerset.genomeCheck(colocatedSets, genomeId, countTable) if completeness >= trustedCompleteness and contamination <= trustedContamination: trustedGenomeIds.add(genomeId) allTrustedGenomeIds.add(genomeId) trustedOut.write(genomeId + '\t' + '; '.join(metadata[genomeId]['taxonomy'])) trustedOut.write('\t%.2f' % (float(metadata[genomeId]['genome size']) / 1e6)) trustedOut.write('\t' + str(metadata[genomeId]['scaffold count'])) trustedOut.write('\t' + metadata[genomeId]['biotic relationships']) trustedOut.write('\t' + metadata[genomeId]['status']) trustedOut.write('\t%.3f\t%.3f' % (completeness, contamination) + '\n') else: filteredOut.write(genomeId + '\t' + '; '.join(metadata[genomeId]['taxonomy'])) filteredOut.write('\t%.2f' % (float(metadata[genomeId]['genome size']) / 1e6)) filteredOut.write('\t' + str(metadata[genomeId]['scaffold count'])) filteredOut.write('\t' + metadata[genomeId]['biotic relationships']) filteredOut.write('\t' + metadata[genomeId]['status']) filteredOut.write('\t%.3f\t%.3f' % (completeness, contamination) + '\n') print ' Trusted genomes: ' + str(len(trustedGenomeIds)) # determine status of trusted genomes statusBreakdown = {} for genomeId in trustedGenomeIds: statusBreakdown[metadata[genomeId]['status']] = statusBreakdown.get(metadata[genomeId]['status'], 0) + 1 print ' Trusted genome status breakdown: ' for status, count in statusBreakdown.iteritems(): print ' ' + status + ': ' + str(count) # determine status of retained genomes proposalNameBreakdown = {} for genomeId in trustedGenomeIds: proposalNameBreakdown[metadata[genomeId]['proposal name']] = proposalNameBreakdown.get(metadata[genomeId]['proposal name'], 0) + 1 print ' Retained genome proposal name breakdown: ' for pn, count in proposalNameBreakdown.iteritems(): if 'KMG' in pn or 'GEBA' in pn or 'HMP' in pn: print ' ' + pn + ': ' + str(count) print ' Filtered genomes by phylum:' trustedPhylumCounts = {} for genomeId in trustedGenomeIds: taxon = metadata[genomeId]['taxonomy'][1] trustedPhylumCounts[taxon] = trustedPhylumCounts.get(taxon, 0) + 1 for phylum, count in allPhylumCounts.iteritems(): print phylum + ': %d of %d' % (trustedPhylumCounts.get(phylum, 0), count) trustedOut.close() filteredOut.close() # write out lineage statistics for genome distribution allStats = {} trustedStats = {} for r in xrange(0, 6): # Domain to Genus for genomeId, data in metadata.iteritems(): taxaStr = '; '.join(data['taxonomy'][0:r+1]) allStats[taxaStr] = allStats.get(taxaStr, 0) + 1 if genomeId in allTrustedGenomeIds: trustedStats[taxaStr] = trustedStats.get(taxaStr, 0) + 1 sortedLineages = img.lineagesSorted() fout = open('./data/lineage_stats.tsv', 'w') fout.write('Lineage\tGenomes with metadata\tTrusted genomes\n') for lineage in sortedLineages: fout.write(lineage + '\t' + str(allStats.get(lineage, 0))+ '\t' + str(trustedStats.get(lineage, 0))+ '\n') fout.close()
def run(self, ubiquityThreshold, singleCopyThreshold, trustedCompleteness, trustedContamination, genomeCompleteness, genomeContamination): img = IMG() markerset = MarkerSet() metadata = img.genomeMetadata() trustedOut = open('./data/trusted_genomes.tsv', 'w') trustedOut.write( 'Genome Id\tLineage\tGenome size (Mbps)\tScaffold count\tBiotic Relationship\tStatus\tCompleteness\tContamination\n' ) filteredOut = open('./data/filtered_genomes.tsv', 'w') filteredOut.write( 'Genome Id\tLineage\tGenome size (Mbps)\tScaffold count\tBiotic Relationship\tStatus\tCompleteness\tContamination\n' ) allGenomeIds = set() allTrustedGenomeIds = set() for lineage in ['Archaea', 'Bacteria']: # get all genomes in lineage and build gene count table print '\nBuilding gene count table.' allLineageGenomeIds = img.genomeIdsByTaxonomy( lineage, metadata, 'All') countTable = img.countTable(allLineageGenomeIds) countTable = img.filterTable(allLineageGenomeIds, countTable, 0.9 * ubiquityThreshold, 0.9 * singleCopyThreshold) # get all genomes from specific lineage allGenomeIds = allGenomeIds.union(allLineageGenomeIds) print 'Lineage ' + lineage + ' contains ' + str( len(allLineageGenomeIds)) + ' genomes.' # tabulate genomes from each phylum allPhylumCounts = {} for genomeId in allLineageGenomeIds: taxon = metadata[genomeId]['taxonomy'][1] allPhylumCounts[taxon] = allPhylumCounts.get(taxon, 0) + 1 # identify marker set for genomes markerGenes = markerset.markerGenes( allLineageGenomeIds, countTable, ubiquityThreshold * len(allLineageGenomeIds), singleCopyThreshold * len(allLineageGenomeIds)) print ' Marker genes: ' + str(len(markerGenes)) geneDistTable = img.geneDistTable(allLineageGenomeIds, markerGenes, spacingBetweenContigs=1e6) colocatedGenes = markerset.colocatedGenes(geneDistTable, metadata) colocatedSets = markerset.colocatedSets(colocatedGenes, markerGenes) print ' Marker set size: ' + str(len(colocatedSets)) # identifying trusted genomes (highly complete, low contamination genomes) trustedGenomeIds = set() for genomeId in allLineageGenomeIds: completeness, contamination = markerset.genomeCheck( colocatedSets, genomeId, countTable) if completeness >= trustedCompleteness and contamination <= trustedContamination: trustedGenomeIds.add(genomeId) allTrustedGenomeIds.add(genomeId) trustedOut.write(genomeId + '\t' + '; '.join(metadata[genomeId]['taxonomy'])) trustedOut.write( '\t%.2f' % (float(metadata[genomeId]['genome size']) / 1e6)) trustedOut.write('\t' + str(metadata[genomeId]['scaffold count'])) trustedOut.write( '\t' + metadata[genomeId]['biotic relationships']) trustedOut.write('\t' + metadata[genomeId]['status']) trustedOut.write('\t%.3f\t%.3f' % (completeness, contamination) + '\n') else: filteredOut.write( genomeId + '\t' + '; '.join(metadata[genomeId]['taxonomy'])) filteredOut.write( '\t%.2f' % (float(metadata[genomeId]['genome size']) / 1e6)) filteredOut.write( '\t' + str(metadata[genomeId]['scaffold count'])) filteredOut.write( '\t' + metadata[genomeId]['biotic relationships']) filteredOut.write('\t' + metadata[genomeId]['status']) filteredOut.write('\t%.3f\t%.3f' % (completeness, contamination) + '\n') print ' Trusted genomes: ' + str(len(trustedGenomeIds)) # determine status of trusted genomes statusBreakdown = {} for genomeId in trustedGenomeIds: statusBreakdown[metadata[genomeId] ['status']] = statusBreakdown.get( metadata[genomeId]['status'], 0) + 1 print ' Trusted genome status breakdown: ' for status, count in statusBreakdown.iteritems(): print ' ' + status + ': ' + str(count) # determine status of retained genomes proposalNameBreakdown = {} for genomeId in trustedGenomeIds: proposalNameBreakdown[metadata[genomeId][ 'proposal name']] = proposalNameBreakdown.get( metadata[genomeId]['proposal name'], 0) + 1 print ' Retained genome proposal name breakdown: ' for pn, count in proposalNameBreakdown.iteritems(): if 'KMG' in pn or 'GEBA' in pn or 'HMP' in pn: print ' ' + pn + ': ' + str(count) print ' Filtered genomes by phylum:' trustedPhylumCounts = {} for genomeId in trustedGenomeIds: taxon = metadata[genomeId]['taxonomy'][1] trustedPhylumCounts[taxon] = trustedPhylumCounts.get(taxon, 0) + 1 for phylum, count in allPhylumCounts.iteritems(): print phylum + ': %d of %d' % (trustedPhylumCounts.get( phylum, 0), count) trustedOut.close() filteredOut.close() # write out lineage statistics for genome distribution allStats = {} trustedStats = {} for r in xrange(0, 6): # Domain to Genus for genomeId, data in metadata.iteritems(): taxaStr = '; '.join(data['taxonomy'][0:r + 1]) allStats[taxaStr] = allStats.get(taxaStr, 0) + 1 if genomeId in allTrustedGenomeIds: trustedStats[taxaStr] = trustedStats.get(taxaStr, 0) + 1 sortedLineages = img.lineagesSorted() fout = open('./data/lineage_stats.tsv', 'w') fout.write('Lineage\tGenomes with metadata\tTrusted genomes\n') for lineage in sortedLineages: fout.write(lineage + '\t' + str(allStats.get(lineage, 0)) + '\t' + str(trustedStats.get(lineage, 0)) + '\n') fout.close()
def run(self, taxonomyStr, ubiquityThreshold, singleCopyThreshold, replicates, minGenomes, maxGenomes, stepSize): img = IMG() markergenes = MarkerGenes() genomeIds = img.genomeIdsByTaxonomy(taxonomyStr, 'Final') print('Lineage ' + taxonomyStr + ' contains ' + str(len(genomeIds)) + ' genomes.') if len(genomeIds) < minGenomes: sys.stderr.write('[Error] Insufficent number of genomes.\n') sys.exit() print('') print('Ubiquity threshold: ' + str(ubiquityThreshold)) print('Single-copy threshold: ' + str(singleCopyThreshold)) meanMarkerSetSize = [] stdMarkerSetSize = [] markerSetSizes = [] if maxGenomes == -1: maxGenomes = len(genomeIds) if maxGenomes > len(genomeIds): maxGenomes = len(genomeIds) countTable = img.countTable(genomeIds) countTable = img.filterTable(genomeIds, countTable) for numGenomes in range(minGenomes, maxGenomes, stepSize): markerSetSize = [] for _ in range(0, replicates): genomeIdSubset = random.sample(genomeIds, numGenomes) markerGenes = markergenes.identify( genomeIdSubset, countTable, ubiquityThreshold * len(genomeIdSubset), singleCopyThreshold * len(genomeIdSubset)) geneDistTable = img.geneDistTable(genomeIdSubset, markerGenes, spacingBetweenContigs=1e6) colocatedGenes = img.colocatedGenes(geneDistTable) colocatedSets = img.colocatedSets(colocatedGenes, markerGenes) markerSetSize.append(len(colocatedSets)) markerSetSizes.append(markerSetSize) m = mean(markerSetSize) meanMarkerSetSize.append(m) s = std(markerSetSize) stdMarkerSetSize.append(s) print('') print('Genomes: ' + str(numGenomes) + ', Ubiquity > ' + str(int(ubiquityThreshold * len(genomeIdSubset))) + ', Single-copy > ' + str(int(singleCopyThreshold * len(genomeIdSubset)))) print('Mean: %.2f +/- %.2f' % (m, s)) print('Min: %d, Max: %d' % (min(markerSetSize), max(markerSetSize))) # plot data errorBar = ErrorBar() plotFilename = './images/markerset.' + taxonomyStr.replace( ';', '_') + '.' + str(ubiquityThreshold) + '-' + str( singleCopyThreshold) + '.errorbar.png' title = taxonomyStr.replace( ';', '; ' ) + '\n' + 'Ubiquity = %.2f' % ubiquityThreshold + ', Single-copy = %.2f' % singleCopyThreshold errorBar.plot(plotFilename, arange(minGenomes, maxGenomes, stepSize), meanMarkerSetSize, stdMarkerSetSize, 'Number of Genomes', 'Marker Set Size', title) boxPlot = BoxPlot() plotFilename = './images/markerset.' + taxonomyStr.replace( ';', '_') + '.' + str(ubiquityThreshold) + '-' + str( singleCopyThreshold) + '.boxplot.png' boxPlot.plot(plotFilename, markerSetSizes, arange(minGenomes, maxGenomes, stepSize), 'Number of Genomes', 'Marker Set Size', True, title)
def run(self, taxonomyStr, ubiquityThreshold, singleCopyThreshold, replicates, minGenomes, maxGenomes, stepSize): img = IMG() markergenes = MarkerGenes() genomeIds = img.genomeIdsByTaxonomy(taxonomyStr, 'Final') print 'Lineage ' + taxonomyStr + ' contains ' + str(len(genomeIds)) + ' genomes.' if len(genomeIds) < minGenomes: sys.stderr.write('[Error] Insufficent number of genomes.\n') sys.exit() print '' print 'Ubiquity threshold: ' + str(ubiquityThreshold) print 'Single-copy threshold: ' + str(singleCopyThreshold) meanMarkerSetSize = [] stdMarkerSetSize = [] markerSetSizes = [] if maxGenomes == -1: maxGenomes = len(genomeIds) if maxGenomes > len(genomeIds): maxGenomes = len(genomeIds) countTable = img.countTable(genomeIds) countTable = img.filterTable(genomeIds, countTable) for numGenomes in xrange(minGenomes, maxGenomes, stepSize): markerSetSize = [] for _ in xrange(0, replicates): genomeIdSubset = random.sample(genomeIds, numGenomes) markerGenes = markergenes.identify(genomeIdSubset, countTable, ubiquityThreshold*len(genomeIdSubset), singleCopyThreshold*len(genomeIdSubset)) geneDistTable = img.geneDistTable(genomeIdSubset, markerGenes, spacingBetweenContigs=1e6) colocatedGenes = img.colocatedGenes(geneDistTable) colocatedSets = img.colocatedSets(colocatedGenes, markerGenes) markerSetSize.append(len(colocatedSets)) markerSetSizes.append(markerSetSize) m = mean(markerSetSize) meanMarkerSetSize.append(m) s = std(markerSetSize) stdMarkerSetSize.append(s) print '' print 'Genomes: ' + str(numGenomes) + ', Ubiquity > ' + str(int(ubiquityThreshold*len(genomeIdSubset))) + ', Single-copy > ' + str(int(singleCopyThreshold*len(genomeIdSubset))) print 'Mean: %.2f +/- %.2f' % (m, s) print 'Min: %d, Max: %d' %(min(markerSetSize), max(markerSetSize)) # plot data errorBar = ErrorBar() plotFilename = './images/markerset.' + taxonomyStr.replace(';','_') + '.' + str(ubiquityThreshold) + '-' + str(singleCopyThreshold) + '.errorbar.png' title = taxonomyStr.replace(';', '; ') + '\n' + 'Ubiquity = %.2f' % ubiquityThreshold + ', Single-copy = %.2f' % singleCopyThreshold errorBar.plot(plotFilename, arange(minGenomes, maxGenomes, stepSize), meanMarkerSetSize, stdMarkerSetSize, 'Number of Genomes', 'Marker Set Size', title) boxPlot = BoxPlot() plotFilename = './images/markerset.' + taxonomyStr.replace(';','_') + '.' + str(ubiquityThreshold) + '-' + str(singleCopyThreshold) + '.boxplot.png' boxPlot.plot(plotFilename, markerSetSizes, arange(minGenomes, maxGenomes, stepSize), 'Number of Genomes', 'Marker Set Size', True, title)