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

#Obtain dataSet id REF: -> `1kg_metadata_service`
dataset = c.search_datasets().next() 

"""
Explanation: GA4GH 1000 Genomes Biometadata Example
This example illustrates the methods used to access the bio_metadata_service, including sample and individual data.
The Biometadata protocol presents messages that let's one determine the sample used to create some genomic data, such as a read group, or call set. Individuals can have multiple biosamples, and in this way the GA4GH is able to represent having samples from multiple sites of the same individual.
Initialize client
In this step we create a client object which will be used to communicate with the server. It is initialized using the 1kg URL.
End of explanation
"""


counter = 0
for individual in c.search_individuals(dataset_id=dataset.id):
    if counter > 5:
        break
    counter += 1
    print "Individual: {}".format(individual.name)
    print " id: {}".format(individual.id)
    print " dataset_id: {}".format(individual.dataset_id)
    print " description: {}".format(individual.description)
    print " species.term: {}".format(individual.species.term)
    print " species.id: {}".format(individual.species.id)
    print " sex.term: {}".format(individual.sex.term)
    print " sex.id: {}\n".format(individual.sex.id)

"""
Explanation: Search individuals method
This instance returns a list of individuals which are represented in a dataset. Note that we use dataset_id obtained from the 1kg_metadata_service notebook.
End of explanation
"""


single_individual = c.get_individual(individual_id=individual.id)
print "Individual: {}".format(single_individual.name)
print " info['Family ID']: {}".format(single_individual.info['Family ID'].values[0].string_value)

"""
Explanation: Note: Only parameters that are potentiallly required by other methods are shown. Accessing attributes in the info field is addressed below.
Get Individual by id method
This method obtains an single individual by it's unique identifier. This id was chosen arbitrarily from the retuend results.
Although the GA4GH constrains the named fields for representing an individual, the info field is able to interchange other data that might be useful. We will get an individual message and then inspect some values in the info field.
End of explanation
"""



counter = 0
for biosample in c.search_biosamples(dataset_id=dataset.id):
    if counter > 5:
        break
    counter += 1
    print "BioSample: {}".format(biosample.name)
    print " id: {}".format(biosample.id)
    print " dataset_id: {}\n".format(biosample.dataset_id)

"""
Explanation: In this case, the Family ID can be exchanged through the protocol, although the named field is not present in the Individual message itself.
Search BioSamples
We can list all of the biosamples available in a dataset similar in fashion to how we did so for individuals.
End of explanation
"""


single_biosample = c.get_biosample(biosample.id)
print"\nName: {}".format(single_biosample.name)
print" Id: {},".format(single_biosample.id)
print" Dataset Id: {},".format(single_biosample.dataset_id)
print" Desciption: {},".format(single_biosample.description)
print" Individual Id: {},".format(single_biosample.individual_id)
print" Disease: {},".format(single_biosample.disease)
print" Sample Created: {},".format(single_biosample.created)
print" Sample Updated: {}".format(single_biosample.updated)
for info in single_biosample.info:
        print" {}: \t{}".format(info, single_biosample.info[info].values[0].string_value)

"""
Explanation: Only five sets of the bio samples data response were displayed for illustration purposes, but the response returns all of the samples hosted by the provided dataset. This all of the data contained in each response, illustrated in a organized fashion.
Get bio samples by id method
This method can return an individual set of data, by simply provding its identifier,
the data returned is the one displayed with the exception of the json wrapper
End of explanation
"""


