annotate fasta2rdf/fastatordf.py @ 6:ec73c34af97b

FASTA2RDF
author jjkoehorst <jasperkoehorst@gmail.com>
date Sat, 21 Feb 2015 15:19:42 +0100
parents
children 3f4f1cd22a6a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python3.4
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
2 # Author: Jasper Jan Koehorst
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
3 # Date created: Jan 22 2015
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
4 # Function: generation of a RDF file from a genome fasta file
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
5
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
6 def delete_galaxy():
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
7 import sys
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
8 for index, path in enumerate(sys.path):
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
9 if "galaxy-dist/" in path:
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
10 sys.path[index] = ''
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
11
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
12 #Some modules that are required by RDFLIB are also in galaxy, this messes up the RDF import function.
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
13 delete_galaxy()
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
14
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
15 # from io import StringIO
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
16 from rdflib import Graph, URIRef, Literal,Namespace, RDF,RDFS,OWL, plugin
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
17 # import rdflib
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
18 from rdflib.store import Store
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
19 import sys
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
20
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
21 store = plugin.get('IOMemory', Store)()
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
22
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
23 global URI
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
24 URI = "http://csb.wur.nl/genome/"
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
25 global seeAlso
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
26 seeAlso = "rdfs:seeAlso"
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
27 global coreURI
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
28 coreURI = Namespace(URI)
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
29
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
30 def createClass(uri):
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
31 genomeGraph.add((uri,RDF.type,OWL.Class))
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
32 genomeGraph.add((uri,RDFS.subClassOf,OWL.Thing))
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
33 return uri
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
34
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
35 def fasta_parser(input_file):
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
36 createClass(coreURI["Genome"]) #Genome class
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
37 createClass(coreURI["Type"]) #Type class (Chr,Pls,Scaffold)
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
38
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
39 genomeDict = {}
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
40
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
41 #requires chromosome_1, chromosome_2, chromosome_1... #For multiple scaffolds
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
42 # regex = re.compile('\[type=(.*?)\]')
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
43 sequence = ""
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
44 genomeID = sys.argv[sys.argv.index('-idtag')+1].replace(" ","_")
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
45 if genomeID == 'None':
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
46 genomeID = sys.argv[sys.argv.index('-id_alternative')+1].replace(" ","_").replace(".","_")
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
47
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
48 genomeURI = coreURI[genomeID]
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
49 for index, element in enumerate(sys.argv):
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
50 if '-organism' == element:
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
51 genomeGraph.add((genomeURI, coreURI["organism"] , Literal(sys.argv[index+1])))
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
52 if '-ncbi_taxid' == element:
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
53 genomeGraph.add((genomeURI, coreURI["taxonomy"] , Literal(sys.argv[index+1])))
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
54 if '-idtag' == element:
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
55 genomeGraph.add((genomeURI, coreURI["id_tag"] , Literal(sys.argv[index+1])))
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
56 if '-ids' == element:
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
57 genomeGraph.add((genomeURI, coreURI["id_tag"] , Literal(sys.argv[index+1])))
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
58
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
59 genomeDict[genomeID] = {}
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
60 # typDict = {"plasmid":0,"scaffold":0,"chromosome":0}
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
61
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
62 #Generating genome dictionary
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
63 data = open(input_file).readlines()
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
64 fastadict = {}
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
65 key = ""
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
66 for index, line in enumerate(data):
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
67 if ">" == line[0]:
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
68 key = line.strip(">").strip()
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
69 fastadict[key] = ""
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
70 else:
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
71 fastadict[key] += line.strip()
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
72
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
73 # for line in fastadict:
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
74 # typ = regex.findall(line)
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
75 # value = 0
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
76 #If something is found
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
77 # if len(typ) > 0:
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
78 # typ = typ[0]
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
79 #If something is not found
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
80 # elif typ == []:
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
81 # typ = "scaffold"
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
82 #If something is found but does not contain a value
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
83 # elif "_" in typ:
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
84 # value = typ.split("_")[-1]
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
85 # try:
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
86 # value = int(value)
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
87 # except:
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
88 # value = 1
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
89 #Not a integer
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
90
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
91 #If a value is not given it is automatically assigned as the first one
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
92 #If a value is given...
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
93 # if value > -1:
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
94 #If a second scaffold of a chromosome_1 is found
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
95 # if typ in genomeDict[genome]:
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
96 #Retrieve how many
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
97 # value = len(genomeDict[genome][typ]) + 1
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
98 # genomeDict[genome][typ]["scaffold_"+str(value)] = {"contig":fastadict[line]}
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
99 # else:
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
100 # genomeDict[genome][typ] = {}
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
101 # genomeDict[genome][typ]["scaffold_1"] = {"contig":fastadict[line]}
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
102
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
103 #Genome dictionary to TTL
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
104 genomeClass = createClass(coreURI["Genome"])
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
105 typeClass = createClass(coreURI["DnaObject"])
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
106 for index, genome in enumerate(fastadict):
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
107 # for typ in genomeDict[genome]:
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
108 # for scaf in genomeDict[genome][typ]:
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
109 # for con in genomeDict[genome][typ][scaf]:
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
110 #A note is required here...
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
111 #Due to RDF performances we are reducing the amount of triples needed from a genome to a contig.
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
112 #Previously it was
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
113 # Genome > Class > Scaffold > Contig
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
114 #Now it will be
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
115 # Genome > Class/Scaffold/Contig
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
116 #typeURI = coreURI[genome + "/" + typ]
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
117 #scaffoldURI = coreURI[genome + "/" + typ + "/" + scaf]
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
118 #Was contigURI
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
119 typeURI = coreURI[genomeID + "/dnaobject_" + str(index)] # + "/" + scaf + "/" + con]
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
120 # sequence = genomeDict[genome][typ][scaf][con]
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
121 sequence = fastadict[genome]
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
122 genomeGraph.add((genomeURI, coreURI["dnaobject"] , typeURI))
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
123 genomeGraph.add((genomeURI, coreURI["sourcedb"], Literal(sys.argv[sys.argv.index("-sourcedb")+1])))
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
124 genomeGraph.add((typeURI, coreURI["sequence"] , Literal(sequence)))
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
125 genomeGraph.add((typeURI, coreURI["header"], Literal(genome)))
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
126 genomeGraph.add((typeURI, coreURI["sourcedb"], Literal(sys.argv[sys.argv.index("-sourcedb")+1])))
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
127 genomeGraph.add((genomeURI, RDF.type,genomeClass))
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
128 genomeGraph.add((typeURI, RDF.type,typeClass))
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
129
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
130 def save():
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
131 data = genomeGraph.serialize(format='turtle')
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
132 open(sys.argv[sys.argv.index("-output")+1],"wb").write(data)
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
133
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
134 def main():
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
135 store = plugin.get('IOMemory', Store)()
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
136 global genomeGraph
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
137 genomeGraph = Graph(store,URIRef(URI))
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
138 genomeGraph.bind("ssb",coreURI)
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
139 input_file = sys.argv[sys.argv.index("-input")+1]
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
140 fasta_parser(input_file)
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
141 save()
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
142
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
143 if __name__ == '__main__':
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
144 main()
ec73c34af97b FASTA2RDF
jjkoehorst <jasperkoehorst@gmail.com>
parents:
diff changeset
145