Skip to content

Instantly share code, notes, and snippets.

@pgsin
Created July 17, 2018 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pgsin/89cfc2832f21c67657dd34b7964ea32d to your computer and use it in GitHub Desktop.
Save pgsin/89cfc2832f21c67657dd34b7964ea32d to your computer and use it in GitHub Desktop.
Mapping database identifiers
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