annotate conversion/fasta2rdf/fastatordf.py @ 29:dd59731d50b5

Biopython and python3.4 inclusion test.
author Jasper Koehorst <jasperkoehorst@gmail.com>
date Wed, 25 Feb 2015 08:25:32 +0100
parents 74b8ba5e2d5b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python3.4
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
2 # Author: Jasper Jan Koehorst
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
3 # Date created: Jan 22 2015
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
4 # Function: generation of a RDF file from a genome fasta file
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
5
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
6
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
7 # from io import StringIO
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
8 from rdflib import Graph, URIRef, Literal,Namespace, RDF,RDFS,OWL, plugin
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
9 # import rdflib
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
10 from rdflib.store import Store
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
11 import sys
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
12
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
13 store = plugin.get('IOMemory', Store)()
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
14
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
15 global URI
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
16 URI = "http://csb.wur.nl/genome/"
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
17 global seeAlso
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
18 seeAlso = "rdfs:seeAlso"
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
19 global coreURI
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
20 coreURI = Namespace(URI)
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
21 global genomeGraph
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
22 store = plugin.get('IOMemory', Store)()
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
23 genomeGraph = Graph(store,URIRef(URI))
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
24 genomeGraph.bind("ssb",coreURI)
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
25
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
26 def delete_galaxy():
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
27 for index, path in enumerate(sys.path):
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
28 if "galaxy-dist/" in path:
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
29 sys.path[index] = ''
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
30
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
31 def createClass(uri):
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
32 genomeGraph.add((uri,RDF.type,OWL.Class))
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
33 genomeGraph.add((uri,RDFS.subClassOf,OWL.Thing))
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
34 return uri
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
35
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
36 def fasta_parser(input_file):
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
37 createClass(coreURI["Genome"]) #Genome class
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
38 createClass(coreURI["Type"]) #Type class (Chr,Pls,Scaffold)
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
39
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
40 genomeDict = {}
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
41
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
42 sequence = ""
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
43 genomeID = sys.argv[sys.argv.index('-idtag')+1].replace(" ","_")
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
44 if genomeID == 'None':
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
45 genomeID = sys.argv[sys.argv.index('-id_alternative')+1].replace(" ","_").replace(".","_")
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
46
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
47 genomeURI = coreURI[genomeID]
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
48 for index, element in enumerate(sys.argv):
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
49 if '-organism' == element:
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
50 genomeGraph.add((genomeURI, coreURI["organism"] , Literal(sys.argv[index+1])))
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
51 if '-ncbi_taxid' == element:
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
52 genomeGraph.add((genomeURI, coreURI["taxonomy"] , Literal(sys.argv[index+1])))
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
53 if '-idtag' == element:
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
54 genomeGraph.add((genomeURI, coreURI["id_tag"] , Literal(sys.argv[index+1])))
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
55 if '-ids' == element:
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
56 genomeGraph.add((genomeURI, coreURI["id_tag"] , Literal(sys.argv[index+1])))
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
57
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
58 genomeDict[genomeID] = {}
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
59
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
60 #Generating genome dictionary
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
61 data = open(input_file).readlines()
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
62 fastadict = {}
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
63 key = ""
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
64 for index, line in enumerate(data):
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
65 if ">" == line[0]:
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
66 key = line.strip(">").strip()
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
67 fastadict[key] = ""
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
68 else:
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
69 fastadict[key] += line.strip()
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
70
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
71 genomeClass = createClass(coreURI["Genome"])
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
72 typeClass = createClass(coreURI["DnaObject"])
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
73 for index, genome in enumerate(fastadict):
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
74 typeURI = coreURI[genomeID + "/dnaobject_" + str(index)]
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
75 sequence = fastadict[genome]
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
76 genomeGraph.add((genomeURI, coreURI["dnaobject"] , typeURI))
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
77 genomeGraph.add((genomeURI, coreURI["sourcedb"], Literal(sys.argv[sys.argv.index("-sourcedb")+1])))
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
78 genomeGraph.add((typeURI, coreURI["sequence"] , Literal(sequence)))
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
79 genomeGraph.add((typeURI, coreURI["header"], Literal(genome)))
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
80 genomeGraph.add((typeURI, coreURI["sourcedb"], Literal(sys.argv[sys.argv.index("-sourcedb")+1])))
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
81 genomeGraph.add((genomeURI, RDF.type,genomeClass))
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
82 genomeGraph.add((typeURI, RDF.type,typeClass))
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
83
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
84 def save():
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
85 data = genomeGraph.serialize(format='turtle')
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
86 open(sys.argv[sys.argv.index("-output")+1],"wb").write(data)
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
87
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
88 def main():
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
89 input_file = sys.argv[sys.argv.index("-input")+1]
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
90 fasta_parser(input_file)
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
91 save()
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
92
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
93 if __name__ == '__main__':
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
94 #Some modules that are required by RDFLIB are also in galaxy, this messes up the RDF import function.
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
95 delete_galaxy()
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
96 main()
74b8ba5e2d5b aragorn addition
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
97