Created
July 17, 2018 23:43
-
-
Save pgsin/1a3f091999db9d77b8b823921d8854f4 to your computer and use it in GitHub Desktop.
Mapping database identifiers https://www.uniprot.org/help/api_idmapping
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Net; | |
using System.Text; | |
public class MappingExample | |
{ | |
private const string UniprotServer = "https://www.uniprot.org/"; | |
static void Main() | |
{ | |
Run("uploadlists", new[] { | |
new KeyValuePair<string, string>("from", "ACC"), | |
new KeyValuePair<string, string>("to", "P_REFSEQ_AC"), | |
new KeyValuePair<string, string>("format", "tab"), | |
new KeyValuePair<string, string>("query", "P13368 P20806 Q9UM73 P97793 Q17192") | |
}); | |
} | |
private static void Run(string tool, KeyValuePair<string, string>[] parameters) | |
{ | |
StringBuilder locationBuilder = new StringBuilder(UniprotServer + tool + "/?"); | |
for (int i = 0; i < parameters.Length; i++) | |
{ | |
if (i > 0) locationBuilder.Append('&'); | |
locationBuilder.Append(parameters[i].Key).Append('=').Append(parameters[i].Value); | |
} | |
string location = locationBuilder.ToString(); | |
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(location); | |
// Set some reasonable limits on resources used by this request | |
request.MaximumAutomaticRedirections = 4; | |
request.MaximumResponseHeadersLength = 4; | |
// Set credentials to use for this request. Highly not recommended by Uniprot | |
request.Credentials = CredentialCache.DefaultCredentials; | |
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); | |
if (response.StatusCode == HttpStatusCode.OK) | |
{ | |
Console.WriteLine("Content length is {0}", response.ContentLength); | |
Console.WriteLine("Content type is {0}", response.ContentType); | |
Stream receiveStream = response.GetResponseStream(); | |
if (receiveStream == null) | |
{ | |
Console.Error.WriteLine("The Response Stream is empty"); | |
return; | |
} | |
// Pipes the stream to a higher level stream reader with the required encoding format. | |
using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8)) | |
{ | |
Console.WriteLine("Response stream received."); | |
Console.WriteLine(readStream.ReadToEnd()); | |
} | |
} | |
else | |
{ | |
Console.Error.WriteLine($"Status code is not ok: {response.StatusCode}"); | |
return; | |
} | |
response.Close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment