This file is indexed.

/usr/bin/metastudent is in metastudent 2.0.1-1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
#   metastudent - predict GO terms from sequence
#   Copyright (C) 2012-2013 Tobias Hamp <hampt@rostlab.org>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import tempfile
from getopt import getopt, GetoptError
import sys
import os
from shutil import copy2, rmtree
import random
import math
from ConfigParser import RawConfigParser
import collections
from metastudentPkg.runMethods import runBlast, runMethodA, runMethodB,\
	runMethodC
from metastudentPkg.Logger import Logger
from metastudentPkg.commons import GOConfig, GeneOntology, GOPrediction, encodeFastaHeaders, decodeFastaHeader, p, setSilent, isSilent, getPkgPath, splitBigFastaFile
import commands
import metastudentPkg
from metastudentPkg.version import VERSION
import urllib2

configMap=None
metastudentPath=""
metastudentPkgPath=""
dataPkgPath=""

def usage():
	print "metastudent -i <FASTA_FILE> -o <RESULT_FILE_PREFIX> [--silent] [--debug] [--keep-temp] [--output-blast] [--blast-only] [--all-predictions] [--no-names] [--with-images] [--ontologies=<MFO or BPO or CCO or MFO,BPO or ...>] [--blast-kickstart-databases=<BLAST_RESULT_FILE(S)>] [--temp-dir=<DIR>] [--config=<CONFIG_FILE>]\n!!! Make sure your input fasta file contains at most 500 sequences !!!"

def enrichOutputLinesWithNames(predLinesDict, geneOntology):
	for methodChar, predLines in predLinesDict.iteritems():
		newPredLines = []
		for predLine in predLines:
			targetId, goTerm, rel = predLine.split("\t")
			newPredLine = "%s\t%s\t%s\t%s" % (targetId, goTerm, rel, geneOntology.termToName[goTerm])
			newPredLines.append(newPredLine)
		newPredLinesAll.append(newPredLines)
		predLinesDict[methodChar] = newPredLines

def generateGraphImage(predLines):
	goTerms = []
	for predLine in predLines:
		targetId, goTerm = predLine.split("\t")[:2]
		goTerms.append(goTerm)
	
	chunks = []
	
	if len(goTerms) > 0:
		
		termDataTokens=[]
		for i, goTerm in enumerate(goTerms):
			if i == 0:
				header = "%22" + goTerm.replace(":", "%3A") + "%22%3A"
			else:
				header = "%2C%22" + goTerm.replace(":", "%3A") + "%22%3A"
			openBraket = "{"
			formatString = "%22fill%22%3A%22yellow%22%2C%22font%22%3A++%22black%22%2C%22border%22%3A%22red%22"
			closeBraket = "}"
			fullToken = header + openBraket + formatString + closeBraket
			termDataTokens.append(fullToken)
		termDataOpt = "term_data={" + "".join(termDataTokens) + "}%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A"
		
		baseUrl = "http://amigo1.geneontology.org/cgi-bin/amigo/visualize?"
		modeOpt = "mode=advanced"
		termDataTypeOpt = "term_data_type=json"
		formatOpt = "format=png"
		
		fullURL = baseUrl + "&".join([modeOpt,termDataOpt,termDataTypeOpt,formatOpt])

		req = urllib2.urlopen(fullURL)
		
		CHUNK = 16 * 1024
		while True:
			chunk = req.read(CHUNK)
			if not chunk: 
				break
			chunks.append(chunk)
		
	return chunks

"""
calculate coefficients for linear combinations of results
"""
def metaPredict(preds, go, onto):
	
	targetList = preds.items()[0][1].targetsSorted
	
	protToTermToGroupToScore = collections.defaultdict(dict)
	
	for methodChar, predictioni in preds.iteritems():
		targetToTermToScore = predictioni.targetToTermToScore
		for target, termToScore in targetToTermToScore.iteritems():
			for term, score in termToScore.iteritems():
				protToTermToGroupToScore[target].setdefault((term,None),{})[methodChar] = float(score)	


	weightA = float(configMap["GROUP_A_WEIGHT_%s" % (onto)])
	weightB = float(configMap["GROUP_B_WEIGHT_%s" % (onto)])
	weightC = float(configMap["GROUP_C_WEIGHT_%s" % (onto)])
	intercept = float(configMap["INTERCEPT_%s" % (onto)])

	predLines = []
	for prot in protToTermToGroupToScore.keys():
		for (term, name), groupToScore in protToTermToGroupToScore[prot].iteritems():
			groupAScore = groupToScore.get("A", 0.0)
			groupBScore = groupToScore.get("B", 0.0)
			groupCScore = groupToScore.get("C", 0.0)
			metaScore = (weightA * groupAScore) + (weightB * groupBScore) + (weightC * groupCScore) + intercept
			if metaScore > 0.0:
				if metaScore > 0.95 and onto=="MFO":
					metaScore = 0.95
				elif metaScore > 0.95 and onto=="BPO":
					metaScore = 0.95
				elif metaScore > 0.95 and onto=="CCO":
					metaScore = 0.95
				
				predLines.append( prot + "\t" + term + "\t%.2f" % metaScore )
	
	metaPred = GOPrediction(predLines, go)
	
	return metaPred	
	
def runIt(tempfile, inputFastaFilePath, outputFilePath, outputBlast, blastKickstartDatabasePaths, ontologies, blastOnly, keepTemp, allPreds, debug, noNames, withImages):
	predLinesDict = {}
	predsDict = {}
	
	try:
		p("Creating tmpDir...")
		tmpDirPath=""
		tmpDirPath=tempfile.mkdtemp(prefix=configMap["MYNAME"])
		p("\tUsing " + tmpDirPath)
		
		p("Copying input file to tmpDir...")
		fastaFilePathLocal=""
		try:
			fastaFilePathLocal = os.path.join(tmpDirPath, os.path.basename(inputFastaFilePath)) 
			copy2(inputFastaFilePath, os.path.join(tmpDirPath, fastaFilePathLocal))
			encodeFastaHeaders(os.path.join(tmpDirPath, fastaFilePathLocal))
		except (IOError, os.error), why:
			Logger.log("Error copying files " + str(why))
		
		p("Loading Gene Ontology...")
		try:
			goTreeFilePath = os.path.join(configMap["DATABASE_BASE_PATH"], "goGraph.txt")	
			closureFilePath = os.path.join(configMap["DATABASE_BASE_PATH"], "fullTransitiveClosureGO.txt")
			nameMappingFilePath = os.path.join(configMap["DATABASE_BASE_PATH"], "nameMapping.txt")
			goConfig = GOConfig()
			goConfig.setTreeFilePath(goTreeFilePath)
			goConfig.setClosureFilePath(closureFilePath)
			goConfig.setNameMappingFilePath(nameMappingFilePath)
			geneOntology = GeneOntology(goConfig)
		except (IOError, os.error), why:
			Logger.log("Error loading Gene Ontology " + str(why))
		
		for i, ontology in enumerate(ontologies):
			
			database = os.path.join(os.path.join(configMap["DATABASE_BASE_PATH"], ontology), configMap["BLAST_SRC_%s" % (ontology) ] + ".fasta")
			
			suffix="_eval%s_iters%s_src%s.%s.blast" % (configMap["BLAST_EVAL_%s" % (ontology)], configMap["BLAST_ITERS_%s" % (ontology)], configMap["BLAST_SRC_%s" % (ontology)], ontology.lower())	
			blastOutputFilePathLocal = fastaFilePathLocal+suffix
			blastKickstartDatabasePath = blastKickstartDatabasePaths[i]
			
			if blastKickstartDatabasePath == "": #no kickstart
				p("Running Blast %s" % (ontology))
				runBlast(fastaFilePathLocal, database, blastOutputFilePathLocal, tmpDirPath, configMap["BLAST_EVAL_%s" % (ontology)], configMap["BLAST_ITERS_%s" % (ontology)], configMap)
				blastKickstartDatabasePath = blastOutputFilePathLocal
			if not blastOnly:
				predLinesDict["A"] = runMethodA(blastKickstartDatabasePath, fastaFilePathLocal, tmpDirPath, configMap["GROUP_A_THRESHOLD_%s" % (ontology)], configMap["GROUP_A_K_%s" % (ontology) ], configMap["BLAST_ITERS_%s" % (ontology)], ontology, configMap)
				predLinesDict["B"] = runMethodB(blastKickstartDatabasePath, fastaFilePathLocal, tmpDirPath, configMap["GROUP_B_K_%s" % (ontology) ], ontology, configMap)
				predLinesDict["C"] = runMethodC(blastKickstartDatabasePath, fastaFilePathLocal, tmpDirPath, configMap["GROUP_C_SCORING_%s" % (ontology) ], ontology, configMap, debug)
				

				
				for methodChar, predLines in predLinesDict.iteritems():

					predsDict[methodChar] = GOPrediction(predLines, geneOntology)

					predsDict[methodChar].propagatePrediction()

					if not noNames:
						predLinesDict[methodChar] = predsDict[methodChar].toOutputLinesWithNames()
					else:

						predLinesDict[methodChar] = predsDict[methodChar].toOutputLines()

				if allPreds:
					for methodChar, predLines in predLinesDict.iteritems():

						methodOutputFile = open(outputFilePath+".%s.%s.txt" % (ontology, methodChar),'w')
						methodOutputFile.write("".join(predLinesDict[methodChar]))
						methodOutputFile.close()
						
						if withImages:
							methodImageFileChunks = generateGraphImage(predLinesDict[methodChar])
							if len(methodImageFileChunks) > 0:
								methodImageFile = open(outputFilePath+".%s.%s.png" % (ontology, methodChar),'w')
								for chunki in methodImageFileChunks:
									methodImageFile.write(chunki)
								methodImageFile.close()					

				predsMeta = metaPredict(predsDict, geneOntology, ontology)

				predsMeta.propagatePrediction()

				predLinesMeta=None
				if not noNames:
					predLinesMeta = predsMeta.toOutputLinesWithNames()
				else:
					predLinesMeta = predsMeta.toOutputLines()
					
				methodOutputFile = open(outputFilePath+".%s.txt" % (ontology),'w')
				methodOutputFile.write("".join(predLinesMeta))
				methodOutputFile.close()

				if withImages:
					methodImageFileChunks = generateGraphImage(predLinesMeta)
					if len(methodImageFileChunks) > 0:
						methodImageFile = open(outputFilePath+".%s.png" % (ontology),'w')
						for chunki in methodImageFileChunks:
							methodImageFile.write(chunki)
						methodImageFile.close()					


			if outputBlast:
				p("copying back blast output")
				try:
					copy2(blastOutputFilePathLocal, outputFilePath + suffix )
				except (IOError, os.error), why:
					Logger.log("Error copying file " + str(why))
			
	except:
		print >> sys.stderr,"Error occurred: " + str(sys.exc_info()[0].__name__)
		raise
		sys.exit(1)
	finally:
		if not keepTemp:
			p("Deleting temp directories...")
			rmtree(tmpDirPath)
		p("\n====== Metastudent finished ======\n")


def formatPath(path):
	return path.replace("<ms-path>",metastudentPath) \
		.replace("<pkg-path>", metastudentPkgPath) \
		.replace("<default-data-package-path>", dataPkgPath) \
		.replace("<python-package-path>", metastudentPkgPath)

def readConfigFile(configMap, configPath):
	config = RawConfigParser()
	try:
		config.read(configPath)
	except:
		Logger.log("Error: Could not properly read config file: " + configPath, level=1)
		Logger.log("Error type: " + str(sys.exc_info()[0].__name__),level=1)
		sys.exit(1)
	
	
	if not config.has_section("General"):
		Logger.log("Error: config file " + configPath + " does not have a [General] section")
	else:
		if config.has_option('General', 'MYNAME'):
			configMap["MYNAME"] =  config.get('General', 'MYNAME')	
		if config.has_option('General', 'TMPDIR'):
			if config.get('General', 'TMPDIR') == "":
				configMap["TMPDIR"]=tempfile.gettempdir()
			elif config.get('General', 'TMPDIR') == "base":
				tmpPath = os.path.join(os.path.join(metastudentPath, "tmp"))
				if not os.path.exists(tmpPath):
					os.mkdir(tmpPath)
				configMap["TMPDIR"] = tmpPath
			else:
				if os.path.isdir(config.get('General', 'TMPDIR')):
					configMap["TMPDIR"]=config.get('General', 'TMPDIR')
				else:
					print >> sys.stderr,"Wrong argument for option TMPDIR"
					sys.exit(1)
		if config.has_option('General', "BLASTPGP_EXE_PATH"):
			if os.path.exists(config.get('General', 'BLASTPGP_EXE_PATH')):
				configMap["BLASTPGP_EXE_PATH"]=config.get('General', 'BLASTPGP_EXE_PATH')
			else:
				print >> sys.stderr,"Wrong argument for option BLASTPGP_EXE_PATH"
				sys.exit(1)
                if config.has_option('General', "JAR_INSTALL_FOLDER_PATH"):
                        if os.path.exists(config.get('General', 'JAR_INSTALL_FOLDER_PATH')):
                                configMap["JAR_INSTALL_FOLDER_PATH"]=config.get('General', 'JAR_INSTALL_FOLDER_PATH')
                        else:   
                                print >> sys.stderr,"Wrong argument for option JAR_INSTALL_FOLDER_PATH"
                                sys.exit(1)
		if config.has_option('General', "DATABASE_BASE_PATH"):
			configMap["DATABASE_BASE_PATH"]=formatPath(config.get('General', 'DATABASE_BASE_PATH'))
		if config.has_option('General', "FASTA_SPLIT_SIZE"):
			try:
				configMap["FASTA_SPLIT_SIZE"]=int(config.get('General', 'FASTA_SPLIT_SIZE'))
			except ValueError:
				print >> sys.stderr,"Wrong argument for option FASTA_SPLIT_SIZE"
				sys.exit(1)
		if config.has_option('General', 'GROUP_A_PATH'):
			if os.path.isdir(formatPath(config.get('General', 'GROUP_A_PATH'))):
				configMap["GROUP_A_PATH"]=formatPath(config.get('General', 'GROUP_A_PATH'))
			else:
				print >> sys.stderr,"Wrong argument for option GROUP_A_PATH"
				sys.exit(1)	
		if config.has_option('General', 'GROUP_B_PATH'):
			if os.path.isdir(formatPath(config.get('General', 'GROUP_B_PATH'))):
				configMap["GROUP_B_PATH"]=formatPath(config.get('General', 'GROUP_B_PATH'))
			else:
				print >> sys.stderr,"Wrong argument for option GROUP_B_PATH"
				sys.exit(1)	
		if config.has_option('General', 'GROUP_C_PATH'):
			if os.path.isdir(formatPath(config.get('General', 'GROUP_C_PATH'))):
				configMap["GROUP_C_PATH"]=formatPath(config.get('General', 'GROUP_C_PATH'))
			else:
				print >> sys.stderr,"Wrong argument for option GROUP_C_PATH"
				sys.exit(1)	






		if config.has_option('General', "GROUP_A_WEIGHT_MFO"):
			try:
				configMap["GROUP_A_WEIGHT_MFO"]=float(config.get('General', 'GROUP_A_WEIGHT_MFO'))
			except ValueError:
				print >> sys.stderr,"Wrong argument for option GROUP_A_WEIGHT_MFO"
				sys.exit(1)
		if config.has_option('General', "GROUP_B_WEIGHT_MFO"):
			try:
				configMap["GROUP_B_WEIGHT_MFO"]=float(config.get('General', 'GROUP_B_WEIGHT_MFO'))
			except ValueError:
				print >> sys.stderr,"Wrong argument for option GROUP_B_WEIGHT_MFO"
				sys.exit(1)
		if config.has_option('General', "GROUP_C_WEIGHT_MFO"):
			try:
				configMap["GROUP_C_WEIGHT_MFO"]=float(config.get('General', 'GROUP_C_WEIGHT_MFO'))
			except ValueError:
				print >> sys.stderr,"Wrong argument for option GROUP_C_WEIGHT_MFO"
				sys.exit(1)
		if config.has_option('General', "INTERCEPT_MFO"):
			try:
				configMap["INTERCEPT_MFO"]=float(config.get('General', 'INTERCEPT_MFO'))
			except ValueError:
				print >> sys.stderr,"Wrong argument for option INTERCEPT_MFO"
				sys.exit(1)
				
		if config.has_option('General', "GROUP_A_THRESHOLD_MFO"):
			try:
				float(config.get('General', 'GROUP_A_THRESHOLD_MFO'))
			except ValueError:
				print >> sys.stderr,"Wrong argument for option GROUP_A_THRESHOLD_MFO"
				sys.exit(1)
			configMap["GROUP_A_THRESHOLD_MFO"]=config.get('General', 'GROUP_A_THRESHOLD_MFO')
		if config.has_option('General', "GROUP_A_K_MFO"):
			try:
				int(config.get('General', 'GROUP_A_K_MFO'))
			except ValueError:
				print >> sys.stderr,"Wrong argument for option GROUP_A_K_MFO"
				sys.exit(1)
			configMap["GROUP_A_K_MFO"]=config.get('General', 'GROUP_A_K_MFO')
		if config.has_option('General', "GROUP_B_K_MFO"):
			try:
				int(config.get('General', 'GROUP_B_K_MFO'))
			except ValueError:
				print >> sys.stderr,"Wrong argument for option GROUP_B_K_MFO"
				sys.exit(1)
			configMap["GROUP_B_K_MFO"]=config.get('General', 'GROUP_B_K_MFO')
		if config.has_option('General', "GROUP_C_SCORING_MFO"):
			try:
				int(config.get('General', 'GROUP_C_SCORING_MFO'))
			except ValueError:
				print >> sys.stderr,"Wrong argument for option GROUP_C_SCORING_MFO"
				sys.exit(1)
			configMap["GROUP_C_SCORING_MFO"]=config.get('General', 'GROUP_C_SCORING_MFO')
		if config.has_option('General', "BLAST_EVAL_MFO"):
			try:
				float(config.get('General', 'BLAST_EVAL_MFO'))
			except ValueError:
				print >> sys.stderr,"Wrong argument for option BLAST_EVAL_MFO"
				sys.exit(1)
			configMap["BLAST_EVAL_MFO"]=config.get('General', 'BLAST_EVAL_MFO')
		if config.has_option('General', "BLAST_ITERS_MFO"):
			try:
				int(config.get('General', 'BLAST_ITERS_MFO'))
			except ValueError:
				print >> sys.stderr,"Wrong argument for option BLAST_ITERS_MFO"
				sys.exit(1)
			configMap["BLAST_ITERS_MFO"]=config.get('General', 'BLAST_ITERS_MFO')
		if config.has_option('General', "BLAST_SRC_MFO"):
			if config.get('General', 'BLAST_SRC_MFO') in ["goaexp", "goasp", "spall", "spexp", "upexp", "all","exp","expup"]:
				configMap["BLAST_SRC_MFO"] = config.get('General', 'BLAST_SRC_MFO')
			else:
				print >> sys.stderr,"Wrong argument for option BLAST_SRC_MFO"
				sys.exit(1)




		if config.has_option('General', "GROUP_A_WEIGHT_BPO"):
			try:
				configMap["GROUP_A_WEIGHT_BPO"]=float(config.get('General', 'GROUP_A_WEIGHT_BPO'))
			except ValueError:
				print >> sys.stderr,"Wrong argument for option GROUP_A_WEIGHT_BPO"
				sys.exit(1)
		if config.has_option('General', "GROUP_B_WEIGHT_BPO"):
			try:
				configMap["GROUP_B_WEIGHT_BPO"]=float(config.get('General', 'GROUP_B_WEIGHT_BPO'))
			except ValueError:
				print >> sys.stderr,"Wrong argument for option GROUP_B_WEIGHT_BPO"
				sys.exit(1)
		if config.has_option('General', "GROUP_C_WEIGHT_BPO"):
			try:
				configMap["GROUP_C_WEIGHT_BPO"]=float(config.get('General', 'GROUP_C_WEIGHT_BPO'))
			except ValueError:
				print >> sys.stderr,"Wrong argument for option GROUP_C_WEIGHT_BPO"
				sys.exit(1)
		if config.has_option('General', "INTERCEPT_BPO"):
			try:
				configMap["INTERCEPT_BPO"]=float(config.get('General', 'INTERCEPT_BPO'))
			except ValueError:
				print >> sys.stderr,"Wrong argument for option INTERCEPT_BPO"
				sys.exit(1)
				
		if config.has_option('General', "GROUP_A_THRESHOLD_BPO"):
			try:
				float(config.get('General', 'GROUP_A_THRESHOLD_BPO'))
			except ValueError:
				print >> sys.stderr,"Wrong argument for option GROUP_A_THRESHOLD_BPO"
				sys.exit(1)
			configMap["GROUP_A_THRESHOLD_BPO"]=config.get('General', 'GROUP_A_THRESHOLD_BPO')
		if config.has_option('General', "GROUP_A_K_BPO"):
			try:
				int(config.get('General', 'GROUP_A_K_BPO'))
			except ValueError:
				print >> sys.stderr,"Wrong argument for option GROUP_A_K_BPO"
				sys.exit(1)
			configMap["GROUP_A_K_BPO"]=config.get('General', 'GROUP_A_K_BPO')
		if config.has_option('General', "GROUP_B_K_BPO"):
			try:
				int(config.get('General', 'GROUP_B_K_BPO'))
			except ValueError:
				print >> sys.stderr,"Wrong argument for option GROUP_B_K_BPO"
				sys.exit(1)
			configMap["GROUP_B_K_BPO"]=config.get('General', 'GROUP_B_K_BPO')
		if config.has_option('General', "GROUP_C_SCORING_BPO"):
			try:
				int(config.get('General', 'GROUP_C_SCORING_BPO'))
			except ValueError:
				print >> sys.stderr,"Wrong argument for option GROUP_C_SCORING_BPO"
				sys.exit(1)
			configMap["GROUP_C_SCORING_BPO"]=config.get('General', 'GROUP_C_SCORING_BPO')
		if config.has_option('General', "BLAST_EVAL_BPO"):
			try:
				float(config.get('General', 'BLAST_EVAL_BPO'))
			except ValueError:
				print >> sys.stderr,"Wrong argument for option BLAST_EVAL_BPO"
				sys.exit(1)
			configMap["BLAST_EVAL_BPO"]=config.get('General', 'BLAST_EVAL_BPO')
		if config.has_option('General', "BLAST_ITERS_BPO"):
			try:
				int(config.get('General', 'BLAST_ITERS_BPO'))
			except ValueError:
				print >> sys.stderr,"Wrong argument for option BLAST_ITERS_BPO"
				sys.exit(1)
			configMap["BLAST_ITERS_BPO"]=config.get('General', 'BLAST_ITERS_BPO')
		if config.has_option('General', "BLAST_SRC_BPO"):
			if config.get('General', 'BLAST_SRC_BPO') in ["goaexp", "goasp", "spall", "spexp", "upexp", "all","exp","expup"]:
				configMap["BLAST_SRC_BPO"] = config.get('General', 'BLAST_SRC_BPO')
			else:
				print >> sys.stderr,"Wrong argument for option BLAST_SRC_BPO"
				sys.exit(1)

				


		if config.has_option('General', "GROUP_A_WEIGHT_CCO"):
			try:
				configMap["GROUP_A_WEIGHT_CCO"]=float(config.get('General', 'GROUP_A_WEIGHT_CCO'))
			except ValueError:
				print >> sys.stderr,"Wrong argument for option GROUP_A_WEIGHT_CCO"
				sys.exit(1)
		if config.has_option('General', "GROUP_B_WEIGHT_CCO"):
			try:
				configMap["GROUP_B_WEIGHT_CCO"]=float(config.get('General', 'GROUP_B_WEIGHT_CCO'))
			except ValueError:
				print >> sys.stderr,"Wrong argument for option GROUP_B_WEIGHT_CCO"
				sys.exit(1)
		if config.has_option('General', "GROUP_C_WEIGHT_CCO"):
			try:
				configMap["GROUP_C_WEIGHT_CCO"]=float(config.get('General', 'GROUP_C_WEIGHT_CCO'))
			except ValueError:
				print >> sys.stderr,"Wrong argument for option GROUP_C_WEIGHT_CCO"
				sys.exit(1)
		if config.has_option('General', "INTERCEPT_CCO"):
			try:
				configMap["INTERCEPT_CCO"]=float(config.get('General', 'INTERCEPT_CCO'))
			except ValueError:
				print >> sys.stderr,"Wrong argument for option INTERCEPT_CCO"
				sys.exit(1)
				
		if config.has_option('General', "GROUP_A_THRESHOLD_CCO"):
			try:
				float(config.get('General', 'GROUP_A_THRESHOLD_CCO'))
			except ValueError:
				print >> sys.stderr,"Wrong argument for option GROUP_A_THRESHOLD_CCO"
				sys.exit(1)
			configMap["GROUP_A_THRESHOLD_CCO"]=config.get('General', 'GROUP_A_THRESHOLD_CCO')
		if config.has_option('General', "GROUP_A_K_CCO"):
			try:
				int(config.get('General', 'GROUP_A_K_CCO'))
			except ValueError:
				print >> sys.stderr,"Wrong argument for option GROUP_A_K_CCO"
				sys.exit(1)
			configMap["GROUP_A_K_CCO"]=config.get('General', 'GROUP_A_K_CCO')
		if config.has_option('General', "GROUP_B_K_CCO"):
			try:
				int(config.get('General', 'GROUP_B_K_CCO'))
			except ValueError:
				print >> sys.stderr,"Wrong argument for option GROUP_B_K_CCO"
				sys.exit(1)
			configMap["GROUP_B_K_CCO"]=config.get('General', 'GROUP_B_K_CCO')
		if config.has_option('General', "GROUP_C_SCORING_CCO"):
			try:
				int(config.get('General', 'GROUP_C_SCORING_CCO'))
			except ValueError:
				print >> sys.stderr,"Wrong argument for option GROUP_C_SCORING_CCO"
				sys.exit(1)
			configMap["GROUP_C_SCORING_CCO"]=config.get('General', 'GROUP_C_SCORING_CCO')
		if config.has_option('General', "BLAST_EVAL_CCO"):
			try:
				float(config.get('General', 'BLAST_EVAL_CCO'))
			except ValueError:
				print >> sys.stderr,"Wrong argument for option BLAST_EVAL_CCO"
				sys.exit(1)
			configMap["BLAST_EVAL_CCO"]=config.get('General', 'BLAST_EVAL_CCO')
		if config.has_option('General', "BLAST_ITERS_CCO"):
			try:
				int(config.get('General', 'BLAST_ITERS_CCO'))
			except ValueError:
				print >> sys.stderr,"Wrong argument for option BLAST_ITERS_CCO"
				sys.exit(1)
			configMap["BLAST_ITERS_CCO"]=config.get('General', 'BLAST_ITERS_CCO')
		if config.has_option('General', "BLAST_SRC_CCO"):
			if config.get('General', 'BLAST_SRC_CCO') in ["goaexp", "goasp", "spall", "spexp", "upexp","all","exp","expup"]:
				configMap["BLAST_SRC_CCO"] = config.get('General', 'BLAST_SRC_CCO')
			else:
				print >> sys.stderr,"Wrong argument for option BLAST_SRC_CCO"
				sys.exit(1)



if __name__ == "__main__":
	currentPath = os.getcwd()
	if hasattr(sys, "frozen"):
		metastudentPath=os.path.dirname(unicode(sys.executable, sys.getfilesystemencoding( )))
	else:
		metastudentPath = os.path.dirname(os.path.abspath(__file__))
	metastudentPkgPath=getPkgPath()
	dataPkgPath=os.path.join(sys.prefix, "share", "metastudent-data")

	if commands.getstatusoutput('pp_popcon_cnt -p metastudent')[0] != 0:
		print >> sys.stderr, "The Rost Lab recommends you install the pp-popularity-contest package that provides pp_popcon_cnt:\n\nsudo apt-get install pp-popularity-contest\n"


	try:
		opts, args=getopt(sys.argv[1:], "hi:o:", ["debug", "help", "silent", "keep-temp", "temp-dir=", "version", "config=", "output-blast", "no-names", "with-images", "blast-kickstart-databases=", "ontologies=","blast-only","all-predictions"])
	except GetoptError, err:
		print >> sys.stderr, str(err)
		usage()
		sys.exit(1)
	
	debug=False
	inputFastaFilePath=""
	outputFilePath=""
	tempDirPath=""
	customConfigFilePath=""
	blastKickstartDatabasePaths=["","",""]
	outputBlast=False
	ontologies=["MFO","BPO","CCO"]
	blastOnly=False
	keepTemp=False
	allPreds = False
	noNames = False
	withImages = False

	Logger(os.path.join(metastudentPath, "metastudent.log"))
	
	optArgs = [arg[0] for arg in opts]
	if "--version" in optArgs:
		print VERSION
		sys.exit(0)
	if ("-i" not in optArgs and "--blast-kickstart-databases" not in optArgs) or "-o" not in optArgs:
		usage()
		sys.exit(1)
	
	for o, a in opts:
		if o == "-i":
			if os.path.exists(a):
				inputFastaFilePath=os.path.abspath(a)
			else:
				print >> sys.stderr,"Error: Input fasta file does not exist"
				sys.exit(1)
		elif o == "-o":
			if os.path.exists(os.path.dirname(os.path.abspath(a))):
				try:
					tmpFile=open(os.path.abspath(a), 'w')
					tmpFile.close()
					os.remove(os.path.abspath(a))
					outputFilePath=os.path.abspath(a)
				except:
					print >> sys.stderr,"Error: Cannot open output file for writing"
					sys.exit(1)
			else:
				print >> sys.stderr,"Error: Output file can not be created"
				sys.exit(1)
		elif o == "--temp-dir":
			if os.path.exists(a):
				tempDirPath=os.path.abspath(a)
			else:
				print >> sys.stderr,"Error: Temp directory does not exist."
				sys.exit(1)
		elif o == "--config":
			if os.path.exists(a):
				customConfigFilePath = a
			else:
				print >> sys.stderr,"Error: Config directory does not exist."
				sys.exit(1)
		elif o == "--blast-kickstart-databases":
			blastKickstartDatabasePathsTmp = a.split(",")
			blastKickstartDatabasePaths=[]
			for db in blastKickstartDatabasePathsTmp:
				dbAbs = db
				if db!="":
					dbAbs = os.path.abspath(db)
					if not os.path.exists(dbAbs):
						print >> sys.stderr,"Error: blast-kickstart-database does not exist."
						sys.exit(1)
				blastKickstartDatabasePaths.append(dbAbs)
		elif o == "--debug":
			debug = True;
		elif o in ("-h", "--help"):
			usage()
			sys.exit()
		elif o == "--silent":
			setSilent(True)
		elif o == "--blast-only":
			blastOnly=True
		elif o == "--output-blast":
			outputBlast = True
		elif o == "--keep-temp":
			keepTemp = True
		elif o == "--all-predictions":
			allPreds = True
		elif o == "--no-names":
			noNames = True
		elif o == "--with-images":
			withImages = True
		elif o == "--ontologies":
			ontologies = a.split(",")
			for ontology in ontologies:
				if ontology not in ["MFO", "BPO","CCO"]:
					print >> sys.stderr,"Error: unknown ontology: " % (ontology)
					sys.exit(1)
		else:
			assert False, "Error: Unhandled option: " + o
			sys.exit(1)

	if blastKickstartDatabasePaths != ["","",""]:
		if len(ontologies) != len(blastKickstartDatabasePaths):
			print >> sys.stderr,"Error: number of kickstart databases not equal to number of ontologies."
			sys.exit(1)
		else:
			p("BLAST results pre-calculted")
			for blastKickstartDatabasePath in blastKickstartDatabasePaths:
				if blastKickstartDatabasePath != "":
					p("\t" + blastKickstartDatabasePath)

	configPaths = [os.path.join(sys.prefix, "share", "metastudent", "metastudentrc.default"),
				"/etc/metastudentrc",
				os.path.expanduser("~/.metastudentrc"),
				customConfigFilePath]
	configMap={}
	if len([readConfigFile(configMap, configPath) for configPath in configPaths if os.path.exists(configPath)]) == 0:
		print >> sys.stderr,"Error: No config file found"
		sys.exit(1)
	
	if tempDirPath != "":
		tempfile.tempdir=tempDirPath
	else:
		if not os.path.exists(configMap["TMPDIR"]):
			os.mkdir(configMap["TMPDIR"])
		tempfile.tempdir=configMap["TMPDIR"]
	
	if not os.path.isdir(configMap.get('DATABASE_BASE_PATH')):
		print >> sys.stderr,"Error: DATABASE_BASE_PATH (%s) does not exist." % configMap.get('DATABASE_BASE_PATH')
		sys.exit(1);

	
	numSeqs = len([None for line in open(inputFastaFilePath).read().split("\n") if line.startswith(">")])
	if numSeqs != 1 and withImages:
		print >> sys.stderr,"Error: option --with-images can only be used with exactly one input sequence."
		sys.exit(1);
		
	
	runIt(tempfile, inputFastaFilePath, outputFilePath, outputBlast, blastKickstartDatabasePaths, ontologies, blastOnly, keepTemp, allPreds, debug, noNames, withImages)
	

"""
=pod

=head1 NAME

metastudent - predictor of gene ontology terms from protein sequence

=head1 SYNOPSIS

metastudent -i I<FASTA_FILE> -o I<RESULT_FILE_PREFIX> [--debug] [--keep-temp] [--silent] [--output-blast] [--blast-only] [--all-predictions] [--ontologies=I<MFO or BPO or CCO or MFO,BPO or ...>] [--with-images] [--blast-kickstart-databases=I<BLAST_RESULT_FILE(S)>] [--temp-dir=I<DIR>] [--config=I<CONFIG_FILE>]
!!! Make sure your fasta file contains at most 500 sequences !!!

=head1 DESCRIPTION

Metastudent predicts Gene Ontology (GO) terms from the Molecular Function Ontology (MFO), Biological Process Ontology (BPO) and Cellular Component Ontology (CCO)
for input protein sequences by homology-based inference from already annotated proteins.

Large (1 GB in total) data files necessary for the operation of metastudent are downloaded automatically on the first use of the program.  The download is restartable.  You can also make an explicit call to
       F<metastudentdata> (by default /usr/bin/metastudentdata) to download the data files.  In case the data directory (by default /usr/share/metastudent-data) is not writable and you are not root, the operation is
       reattempted with sudo(8).

=head2 Output format

For each selected ontology (see I<--ontologies>), one output file is produced (see I<-o>).
Each line in each file associates a protein with a GO term and a reliability for the association (0.0 to 1.0). The following format is used:
<PROTEIN ID><TAB><GO_TERM><TAB><RELIABILITY>

=head1 REFERENCES

=over

=item Hamp, T., Kassner, R., Seemayer, S., Vicedo, E., Schaefer, C., Achten, D., ... & Rost, B. (2013). Homology-based inference sets the bar high for protein function prediction. BMC Bioinformatics, 14(Suppl 3), S7.

=back

=head1 OPTIONS

=over

=item -i FASTA_FILE

The input fasta file. Please try to remove any special formattings (e.g. whitespaces) in the sequences before using them as input. Due to high memory usage, make sure your fasta file contains at most 500 sequences.

=item -o RESULT_FILE_PREFIX

The file name prefix of the output files. GO terms are organized in ontologies. Metatstudent treats each ontology differently and outputs one result file for each. For example, if <RESULT_FILE>=./myresult and MFO (Molecular Function Ontology) and BPO (Biological Process Ontology) ontologies are selected (see option B<--ontologies>), then metastudent creates two output files: F<./myresult.MFO.txt> and F<./myresult.BPO.txt>.

=item --debug

Print extra debugging messages.

=item --keep-temp

Whether to keep the temp directories after metastudent has finished (they can be useful when errors occur or in combination with B<--blast-kickstart-databases>).

=item --silent

No progress messages (stdout), only errors (stderr).

=item --output-blast

Whether to output the result of the BLAST runs. Useful in combination with B<--blast-kickstart-databases>. Output file name format is I<RESULT_FILE_PREFIX>.<BLAST_OPTIONS>.blast.

=item --blast-only

Whether to only output the result of the BLAST runs, and nothing else. See options I<--output-blast> and I<--blast-kickstart-databases>.

=item --all-predictions

Whether to output the prediction results of the individual predictors. File name format of the output file is <RESULT_FILE_PREFIX>.<ONTOLOGY>.<METHOD>.txt.

=item --no-names

Whether to exclude the actual GO term names as a column in the output prediction file(s).

=item --with-images

Whether to also generate an image that displays the predicted GO terms as a GO graph. This option can only be used with exactly one input sequence and only when connected to the internet.

=item --ontologies=I<MFO or BPO or CCO or MFO,BPO or ...>

A comma separated list of ontologies to create predictions for. Default is MFO,BPO,CCO. If used in combination with B<--blast-kickstart-databases>, the number and order of the ontologies must correspond to the kickstart files.

=item --blast-kickstart-databases=<BLAST_RESULT_FILES>

Since running BLAST is usually the part that takes the longest in metastudent, this option allows you to re-use the output of a previous run. This is useful to test, for example, different parameters or when you have lost a prediction. The number of kickstart files must correspond to the number of ontologies (see option B<--ontologies>). Separate the file paths with commas. For example: --blast-kickstart-databases=<RESULT_FILE_MFO>,<RESULT_FILE_BPO> (kickstart for both ontologies) or --blast-kickstart-databases=,<RESULT_FILE_BPO> (only kickstart BPO; note the comma).

=item --temp-dir=DIR

The parent temp directory to use instead of the one specified with tmpDir in the metastudent configuration file.

=item --config=FILE

The path to a custom metastudent configuration file; overrides all settings of the configuration files found in the FILES section of this man page.

=back

=head1 FILES

=over

=item F<<< <package_data_dir>/metastudentrc.default >>>

The metastudent configuration file.

=item F<<< <sysconfdir>/metastudentrc >>>

The metastudent configuration file, overrides F<<< <package_data_dir>/metastudentrc.default >>>.

=item F<<< <homedir>/.metastudentrc >>>

The metastudent configuration file, overrides F<<< <sysconfdir>/metastudentrc >>>.

=back

=head1 EXAMPLES

The example F<test.fasta> file can be found in F<<< <package_doc_dir>/examples >>> (usually F</usr/share/doc/metastudent/examples>).

=over

=item Predict the GO terms for the sequences in test.fasta for both the MFO and the BPO ontology:

 metastudent -i test.fasta -o test.result

=item Create the BLAST output to predict the MFO terms for sequences in test.fasta (not the actual predictions, yet; see next example).

 metastudent -i test.fasta -o test.result --blast-only --output-blast --ontologies=MFO

=item Predict the MFO and BPO terms for sequences in test.fasta with a precomputed MFO BLAST output (see previous example; note the comma at the end).

 metastudent -i test.fasta -o test.result --ontologies=MFO,BPO --blast-kickstart-databases=test.result_eval0.001_iters3_srcexp.mfo.blast,

=back

=head1 BUGS

Please use this link to report bugs:

L<https://rostlab.org/bugzilla3/enter_bug.cgi?product=metastudent>

=head1 AUTHOR

Tobias Hamp <hampt@rostlab.org>

=cut
"""