Intermine-Python: Tutorial 9: Creating Lists

This tutorial will talk about how you can create and save lists in your account. Like the previous tutorial, you will need to provide your login information while creating a service object.

You can either provide a list of identifiers (for example, gene symbols) and the Intermine Server will try to resolve them into objects in the database or you provide a query that specifies exactly what you want.

In [1]:
from intermine.webservice import Service

Enter your login information, uncomment the line of code and then run it.

In [2]:
service=Service("https://www.flymine.org/flymine/service",username="[email protected]",password="demo")

We will look at three methods in this tutorial. Let's say you want to upload a list of strings (Gene Symbols). We begin by declaring a list, which I've called symbols.

In [3]:
symbols=["eve","zen","rudimentary"]

Note that you can use their secondary identifiers or DB identifiers instead if you're more comfortable with that. So if you changed symbols to ["CG2328","zen","rudimentary"] or ["eve","FBgn0004053","rudimentary] it would not be an issue as Intermine would be able to resolve it for you.

To create a list, you need to declare a list manager object. We then use the create_list method as shown below. Remember to define a name for the list, otherwise the list will be lost once the session will be terminated.

In [4]:
lm=service.list_manager()
In [5]:
lm.delete_lists(["my list"])
lm.create_list(content=symbols,list_type="Gene",name="my list")
Out[5]:
<intermine.lists.list.List at 0x7f28805744a8>

We will now look at how you can save a list from a query. Suppose that you want to extract only the information regarding the gene symbol "eve" stored in the previous list that we uploaded. You can do it as shown below.

In [6]:
query=service.new_query("Gene")
In [7]:
query.add_constraint("Gene","IN","my list")
Out[7]:
<ListConstraint: Gene IN my list>
In [8]:
query.add_constraint("symbol","=","eve")
Out[8]:
<BinaryConstraint: Gene.symbol = eve>
In [9]:
lm.delete_lists(["my list 2"])
lm.create_list(query,name="my list 2")
Out[9]:
<intermine.lists.list.List at 0x7f28804c63c8>

Finally, let's say that you have a file with a list of gene identifiers stored on your machine, which you want to upload as a list. Change the string stored to match the path location of your file. Uncomment the lines of code and run it.

In [10]:
#file="Enter/File/Path.tsv"
#lm.create_list(file,list_type="Gene",name="my list 3")

If you want to view the names of all the lists available, use the following:

In [11]:
lm.get_all_list_names()
Out[11]:
dict_keys(['Gene List (Fri Aug 23 2019 15:39:10 GMT+0100 (British Summer Time))', 'Gene List (Mon Jun 03 2019 14:12:11 GMT+0100 (BST))', 'Gene list for D. melanogaster 03 Jun 2019 13:10:42', 'H. sapiens orthologues of FlyTF_site_specific_1', 'PL FlyAtlas_brain_top', 'PL FlyAtlas_head_top', 'PL FlyAtlas_hindgut_top', 'PL FlyAtlas_maleglands_top', 'PL FlyAtlas_ovary_top', 'PL FlyAtlas_testis_top', 'PL FlyAtlas_tubules_top', 'PL FlyTF_PWM_TFs', 'PL FlyTF_REDfly_TFs', 'PL FlyTF_putativeTFs', 'PL FlyTF_site_specific_TFs', 'PL FlyTF_trusted_TFs', 'PL classI', 'PL classII', 'PL classIII', 'PL classIIIa', 'PL classIIIb', 'PL classIIIc', 'PL classIIa', 'PL classIIb', 'PL classIIc', 'PL classIa', 'PL classIb', 'PL dme_immunity_genes', 'Testing stuff', 'UseCase1_transcripts_ERstress', 'UseCase1_transcripts_oxidativeStress', 'UseCase5_ScreenHits', 'UseCase5_backgroundGeneList', 'bobo', 'bobo_1', 'combination-1', 'combination-2', 'esyN demo list', 'esyN demo list_1', 'my list', 'my list 2', 'my_list_1'])