import ga4gh_client.client as client
c = client.HttpClient("http://1kgenomes.ga4gh.org")

"""
Explanation: GA4GH 1000 Genome Variant Service Example
This example illustrates how to access the GA4GH variant service.
The GA4GH variant service interchanges variant data similar to that found in VCF. It includes metadata related to variant entries and calls, which are the observed genetic variations pertaining to a given sample.
Initialize the client
In this step we create a client object which will be used to communicate with the server. It is initialized using the URL.
End of explanation
"""


for variant_set in c.search_variant_sets(dataset_id="WyIxa2dlbm9tZXMiXQ"):
    print "Variant Set: {}".format(variant_set.name)
    print " id: {}".format(variant_set.id)
    print " dataset_id: {}".format(variant_set.dataset_id)
    print " reference_set_id: {}\n".format(variant_set.reference_set_id)

"""
Explanation: Search variant sets method
This call returns variant sets hosted by the API. Observe that we are using the dataset id obtained from the metadata service example.
End of explanation
"""


variant_set = c.get_variant_set(variant_set_id="WyIxa2dlbm9tZXMiLCJ2cyIsInBoYXNlMy1yZWxlYXNlIl0")
print "name: {}".format(variant_set.name)
print "dataset_id: {}".format(variant_set.dataset_id)
print "reference_set_id: {}".format(variant_set.reference_set_id)
for metadata in variant_set.metadata[0:3]:
    print metadata

"""
Explanation: Note:
In the previous call, not all the elements returned are printed to the screen.
Below we will look at the metadata descriptions in a variant set.

Get Variant Set by ID method
The following request returns a Variant Set by id. This identifier is unique to the server instance.
End of explanation
"""


counter = 0
for variant in c.search_variants(variant_set_id="WyIxa2dlbm9tZXMiLCJ2cyIsInBoYXNlMy1yZWxlYXNlIl0", reference_name="1", start=10176, end= 40176):
    if counter > 5:
        break
    counter += 1
    print "Variant id: {}...".format(variant.id[0:10])
    print "Variant Set Id: {}".format(variant.variant_set_id)
    print "Names: {}".format(variant.names)
    print "Reference Chromosome: {}".format(variant.reference_name)
    print "Start: {}, End: {}".format(variant.start, variant.end)
    print "Reference Bases: {}".format(variant.reference_bases)
    print "Alternate Bases: {}\n".format(variant.alternate_bases)

"""
Explanation: The metadata fields of a Variant Set message can be used to interpret fields annotated about a variant, which will be addressed later in this document.
Search Variants method
Using the variant_set_id returned in one of the previous calls we constrain our variant search to a single variant set.
End of explanation
"""


single_variant = c.get_variant(variant_id=variant.id)
print "idd: {}".format(single_variant.id)
print "Variant Set Id: {}".format(single_variant.variant_set_id)
print "Names: {}".format(single_variant.names)
print "Reference Name: {}".format(single_variant.reference_name)
print "Start: {}, End: {}".format(single_variant.start, single_variant.end)
print "Reference Bases: {}".format(single_variant.reference_bases)
print "Alternate Bases: {}\n".format(single_variant.alternate_bases)
for info in single_variant.info:
    print "Key: {},\tValues: {}".format(info, single_variant.info[info].values[0].string_value)

"""
Explanation: The fields shown above are selected named fields in the data model. Note that the data returned is richer, and will be illiustrated in the following example by examining the info field.
Get Variant by ID method
We can get a variant record, if we know its unique identifier, by performing a get variant call. We use one of the variant identifiers obtained above. We will then list all of the data available in the info field.
End of explanation
"""


metadata_dictionary = {}
for metadata in variant_set.metadata:
    metadata_dictionary[metadata.key] = metadata # Load the metadata elements into a dictionary
for key in single_variant.info:
    metadata_entry = metadata_dictionary["INFO." + key]
    print "\nKey: {}     Value: {}     Type: {}".format(
        key,
        single_variant.info[key].values[0].string_value,
        metadata_entry.type)
    print " " + metadata_entry.description

"""
Explanation: Using the variant_set.metadata message we can interpret these annotations.
End of explanation
"""


counter = 0
list_of_callset_ids = [] # Will use this list near the end to make a search variants query
for call_set in c.search_call_sets(variant_set_id=single_variant.variant_set_id):
    if counter > 3:
        break
    else:
        counter += 1
        list_of_callset_ids.append(call_set.id)
        print "Call Set Name: {}".format(call_set.name)
        print "  id: {}".format(call_set.name)
        print "  bio_sample_id: {}".format(call_set.name)
        print "  variant_set_ids: {}\n".format(call_set.variant_set_ids)

"""
Explanation: Search Call Sets method
Call sets represent a sample's column in a VCF, or the collection of observed sequences from a particular variation analysis. The Thousand Genomes data presents the variation data of 2504 individuals through 2504 callsets. These Call Sets ca be used to construct queries for individual genomic variations.
End of explanation
"""


call_set = c.get_call_set(call_set_id=call_set.id)
print call_set

"""
Explanation: Get Call Set by ID method
Using one of the identifiers obtained in the previous call we can get a single Call Set element. This identifier is unique to the server instance.
End of explanation
"""


for variant_with_calls in  c.search_variants(call_set_ids=list_of_callset_ids, variant_set_id="WyIxa2dlbm9tZXMiLCJ2cyIsInBoYXNlMy1yZWxlYXNlIl0", reference_name="1", start=10176, end= 10502):
    print "Variant Id: {}".format(variant_with_calls.id)
    print "Variant Set Id: {}".format(variant_with_calls.variant_set_id)
    print "Names:{}".format(variant_with_calls.names)
    print "Reference Chromosome: {}".format(variant_with_calls.reference_name)
    print "Start: {}, End: {}".format(variant_with_calls.start, variant_with_calls.end)
    print "Reference Bases: {}".format(variant_with_calls.reference_bases)
    print "Alternate Bases: {}\n".format(variant_with_calls.alternate_bases)
    for call in variant_with_calls.calls:
        print "  Call Set Name: {}".format(call.call_set_name)
        print "  Genotype: {}".format(call.genotype)
        print "  Phase set: {}".format(call.phaseset)
        print "  Call Set Id: {}\n".format(call.call_set_id)

"""
Explanation: Making a Search Variants Request with Call Set IDs
By passing in a list of call_set_ids to the variants search method, we can get in return
a set of variants with the list of calls that belong to that variant. The genotype field of a Call states whether the variation was observed for a given sample.
End of explanation
"""


