QByteArray FSTReader::writeMapping(const QVariantHash& mapping) { static const QStringList PREFERED_ORDER = QStringList() << NAME_FIELD << TYPE_FIELD << SCALE_FIELD << FILENAME_FIELD << TEXDIR_FIELD << JOINT_FIELD << FREE_JOINT_FIELD << BLENDSHAPE_FIELD << JOINT_INDEX_FIELD; QBuffer buffer; buffer.open(QIODevice::WriteOnly); for (auto key : PREFERED_ORDER) { auto it = mapping.find(key); if (it != mapping.constEnd()) { if (key == FREE_JOINT_FIELD) { // writeVariant does not handle strings added using insertMulti. for (auto multi : mapping.values(key)) { buffer.write(key.toUtf8()); buffer.write(" = "); buffer.write(multi.toByteArray()); buffer.write("\n"); } } else { writeVariant(buffer, it); } } } for (auto it = mapping.constBegin(); it != mapping.constEnd(); it++) { if (!PREFERED_ORDER.contains(it.key())) { writeVariant(buffer, it); } } return buffer.data(); }
void XmlWriter::writeVariant( QXmlStreamWriter &stream, const QVariant &value ) { ASSERT_LIMITED_VARIANT( value ); if( value.isNull() || value.type() == QVariant::Invalid ) { stream.writeEmptyElement( "nil" ); } else if( value.type() == QVariant::Bool ) { stream.writeTextElement( "boolean", value.toBool() ? "true" : "false" ); } else if( value.type() == QVariant::ByteArray ) { stream.writeTextElement( "base64", value.toByteArray().toBase64() ); } else if( value.type() == QVariant::Color ) { writeColor( stream, value.value<QColor>() ); } else if( value.type() == QVariant::Date ) { stream.writeTextElement( "date", value.toDate().toString( Qt::ISODate ) ); } else if( value.type() == QVariant::DateTime ) { stream.writeTextElement( "datetime", value.toDateTime().toString( Qt::ISODate ) ); } else if( value.type() == QVariant::Double ) { stream.writeTextElement( "double", QString::number( value.toDouble() ) ); } else if( value.type() == QVariant::Hash ) { QHash<QString, QVariant> hash = value.toHash(); QHashIterator<QString, QVariant> it( hash ); stream.writeStartElement( "map" ); stream.writeAttribute( "type", "hash" ); while( it.hasNext() ) { it.next(); stream.writeStartElement( "item" ); stream.writeTextElement( "key", it.key() ); stream.writeStartElement( "value" ); writeVariant( stream, it.value() ); stream.writeEndElement(); // value stream.writeEndElement(); // item } stream.writeEndElement(); } else if( value.type() == QVariant::Image ) { QByteArray ba; QBuffer buffer( &ba ); buffer.open( QIODevice::WriteOnly ); value.value<QImage>().save( &buffer, "PNG" ); buffer.close(); stream.writeTextElement( "image", ba.toBase64() ); } else if( value.type() == QVariant::Int ) { stream.writeTextElement( "int", QString::number( value.toInt() ) ); } else if( value.type() == QVariant::List ) { stream.writeStartElement( "list" ); const QVariantList list = value.toList(); foreach( const QVariant & var, list ) { writeVariant( stream, var ); }
void XmlWriter::writeObjectData( QXmlStreamWriter &stream, const Object *object ) { Q_ASSERT( object ); // Flags const QSet<QByteArray> flags = object->flags(); for( QSet<QByteArray>::const_iterator it = flags.constBegin(); it != flags.constEnd(); ++it ) { stream.writeTextElement( "flag", ( *it ) ); } // Attributes const QHash<QByteArray, QVariant> attributes = object->attributes(); for( QHash<QByteArray, QVariant>::const_iterator it = attributes.constBegin(); it != attributes.constEnd(); ++it ) { stream.writeStartElement( "attribute" ); stream.writeTextElement( "key", it.key() ); stream.writeStartElement( "value" ); writeVariant( stream, it.value() ); stream.writeEndElement(); // value stream.writeEndElement(); // attribute } // Attachments for( int i = 0; i < object->countAttachments(); ++i ) { writeAttachment( stream, object->attachment( i ) ); } }
QByteArray FSTReader::writeMapping(const QVariantHash& mapping) { static const QStringList PREFERED_ORDER = QStringList() << NAME_FIELD << TYPE_FIELD << SCALE_FIELD << FILENAME_FIELD << TEXDIR_FIELD << JOINT_FIELD << FREE_JOINT_FIELD << BLENDSHAPE_FIELD << JOINT_INDEX_FIELD; QBuffer buffer; buffer.open(QIODevice::WriteOnly); for (auto key : PREFERED_ORDER) { auto it = mapping.find(key); if (it != mapping.constEnd()) { writeVariant(buffer, it); } } for (auto it = mapping.constBegin(); it != mapping.constEnd(); it++) { if (!PREFERED_ORDER.contains(it.key())) { writeVariant(buffer, it); } } return buffer.data(); }
int main(int argc, char const *argv[]) { if (argc != 5) { std::cout << "USAGE: splitMultiAllelicVariants <variants> <output_prefix> <min_allele_posterior> <min_genotype_posterior>" << std::endl; return 1; } cout << "\n[" << Utils::getLocalTime() << "] Running BayesTyperUtils (" << BTU_VERSION << ") splitMultiAllelicVariants script ...\n" << endl; GenotypedVcfFileReader vcf_reader(string(argv[1]), true); Auxiliaries::removeNonRelevantFormatDescriptors(&(vcf_reader.metaData()), {"GT", "GPP"}); auto output_meta_data = vcf_reader.metaData(); output_meta_data.filterDescriptors().clear(); output_meta_data.infoDescriptors().erase("ACP"); output_meta_data.infoDescriptors().erase("AsmVar_ASQR"); output_meta_data.infoDescriptors().erase("DNE"); output_meta_data.formatDescriptors().erase("GPP"); VcfFileWriter output_vcf(string(argv[2]) + ".vcf", output_meta_data, true); float min_allele_posterior = stof(argv[3]); float min_genotype_posterior = stof(argv[4]); Variant * cur_var; auto sample_ids = output_meta_data.sampleIds(); uint num_variants = 0; uint num_alt_alleles = 0; uint num_missing_alleles = 0; uint num_called_split_variants = 0; uint num_filtered_alleles = 0; uint num_filtered_samples = 0; while (vcf_reader.getNextVariant(&cur_var)) { num_variants++; num_alt_alleles += cur_var->numAlts(); vector<string> complete_sample_ids; complete_sample_ids.reserve(sample_ids.size()); for (auto & sample_id: sample_ids) { Sample & cur_sample = cur_var->getSample(sample_id); if (cur_sample.callStatus() != Sample::CallStatus::Missing) { assert(cur_sample.callStatus() == Sample::CallStatus::Complete); auto genotype_gpp = Auxiliaries::getGenotypePosterior(cur_sample); assert(genotype_gpp == Auxiliaries::getMaxGenotypePosterior(cur_sample)); if (genotype_gpp.second) { if (genotype_gpp.first < min_genotype_posterior) { cur_sample.clearGenotypeEstimate(); assert(cur_sample.callStatus() == Sample::CallStatus::Missing); num_filtered_samples++; } else { complete_sample_ids.push_back(sample_id); } } else { assert(cur_sample.ploidy() == Sample::Ploidy::Zeroploid); } } } while (cur_var->numAlts() > 1) { assert(!(cur_var->alt(0).isMissing())); auto acp_value = cur_var->alt(0).info().getValue<float>("ACP"); assert(acp_value.second); if (acp_value.first < min_allele_posterior) { num_filtered_alleles++; } else { Variant * new_var = new Variant(*cur_var); vector<uint> alts_remove(new_var->numAlts() - 1); iota(alts_remove.begin(), alts_remove.end(), 1); new_var->removeAlts(alts_remove); assert(new_var->numAlls() == 2); assert(new_var->numAlts() == 1); num_called_split_variants++; updateGenotypes(new_var, complete_sample_ids); writeVariant(&output_vcf, new_var); } cur_var->removeAlts({0}); } assert(cur_var->numAlls() == 2); assert(cur_var->numAlts() == 1); auto acp_value = cur_var->alt(0).info().getValue<float>("ACP"); assert(acp_value.second); if (cur_var->alt(0).isMissing()) { num_missing_alleles++; } else if (acp_value.first < min_allele_posterior) { num_filtered_alleles++; } else { num_called_split_variants++; updateGenotypes(cur_var, complete_sample_ids); writeVariant(&output_vcf, cur_var); } if ((num_variants % 100000) == 0) { std::cout << "[" << Utils::getLocalTime() << "] Parsed " << num_variants << " variants" << endl; } delete cur_var; } cout << "\n[" << Utils::getLocalTime() << "] Parsed " << num_variants << " variants and " << num_alt_alleles << " alternative alleles (" << num_missing_alleles << " excluded missing alleles)." << endl; cout << "\n[" << Utils::getLocalTime() << "] Filtered " << num_filtered_alleles << " alternative alleles with an allele posterior less than " << min_allele_posterior << "." << endl; cout << "[" << Utils::getLocalTime() << "] Filtered " << num_filtered_samples << " samples with a genotype posterior less than " << min_genotype_posterior << "." << endl; cout << "\n[" << Utils::getLocalTime() << "] Wrote " << num_called_split_variants << " called bi-allelic variants." << endl; cout << endl; return 0; }
QDomElement QgsXmlUtils::writeVariant( const QVariant &value, QDomDocument &doc ) { QDomElement element = doc.createElement( QStringLiteral( "Option" ) ); switch ( value.type() ) { case QVariant::Invalid: { element.setAttribute( QStringLiteral( "type" ), QStringLiteral( "invalid" ) ); break; } case QVariant::Map: { QVariantMap map = value.toMap(); for ( auto option = map.constBegin(); option != map.constEnd(); ++option ) { QDomElement optionElement = writeVariant( option.value(), doc ); optionElement.setAttribute( QStringLiteral( "name" ), option.key() ); element.appendChild( optionElement ); element.setAttribute( QStringLiteral( "type" ), QStringLiteral( "Map" ) ); } break; } case QVariant::List: { QVariantList list = value.toList(); const auto constList = list; for ( const QVariant &value : constList ) { QDomElement valueElement = writeVariant( value, doc ); element.appendChild( valueElement ); element.setAttribute( QStringLiteral( "type" ), QStringLiteral( "List" ) ); } break; } case QVariant::StringList: { QStringList list = value.toStringList(); const auto constList = list; for ( const QString &value : constList ) { QDomElement valueElement = writeVariant( value, doc ); element.appendChild( valueElement ); element.setAttribute( QStringLiteral( "type" ), QStringLiteral( "StringList" ) ); } break; } case QVariant::Int: case QVariant::UInt: case QVariant::Bool: case QVariant::Double: case QVariant::LongLong: case QVariant::ULongLong: case QVariant::String: element.setAttribute( QStringLiteral( "type" ), QVariant::typeToName( value.type() ) ); element.setAttribute( QStringLiteral( "value" ), value.toString() ); break; case QVariant::UserType: { if ( value.canConvert< QgsProperty >() ) { element.setAttribute( QStringLiteral( "type" ), QStringLiteral( "QgsProperty" ) ); const QDomElement propertyElem = QgsXmlUtils::writeVariant( value.value< QgsProperty >().toVariant(), doc ); element.appendChild( propertyElem ); break; } else if ( value.canConvert< QgsCoordinateReferenceSystem >() ) { element.setAttribute( QStringLiteral( "type" ), QStringLiteral( "QgsCoordinateReferenceSystem" ) ); const QgsCoordinateReferenceSystem crs = value.value< QgsCoordinateReferenceSystem >(); crs.writeXml( element, doc ); break; } else if ( value.canConvert< QgsGeometry >() ) { element.setAttribute( QStringLiteral( "type" ), QStringLiteral( "QgsGeometry" ) ); const QgsGeometry geom = value.value< QgsGeometry >(); element.setAttribute( QStringLiteral( "value" ), geom.asWkt() ); break; } FALLTHROUGH } default: Q_ASSERT_X( false, "QgsXmlUtils::writeVariant", QStringLiteral( "unsupported variant type %1" ).arg( QVariant::typeToName( value.type() ) ).toLocal8Bit() ); break; } return element; }