PageRenderTime 26ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 1ms

/biopython-1.57/Bio/Clustalw/__init__.py

https://github.com/lincolnn/Bioinformatics
Python | 531 lines | 480 code | 4 blank | 47 comment | 8 complexity | 35418477b4fd87669a624552b5c9063a MD5 | raw file
  1. # This code is part of the Biopython distribution and governed by its
  2. # license. Please see the LICENSE file that should have been included
  3. # as part of this package.
  4. """Code for calling ClustalW and parsing its output (DEPRECATED).
  5. This module has been superseded by the Bio.AlignIO framework for
  6. alignment parsing, and the ClustalW command line wrapper in
  7. Bio.Align.Applications for calling the tool. These are both described
  8. in the current version of the Biopython Tutorial and Cookbook.
  9. This means Bio.Clustalw is now deprecated and likely to be
  10. removed in future releases of Biopython.
  11. A set of classes to interact with the multiple alignment command
  12. line program clustalw.
  13. Clustalw is the command line version of the graphical Clustalx
  14. aligment program.
  15. This requires clustalw available from:
  16. ftp://ftp-igbmc.u-strasbg.fr/pub/ClustalW/.
  17. functions:
  18. o read
  19. o parse_file
  20. o do_alignment
  21. classes:
  22. o ClustalAlignment
  23. o MultipleAlignCL"""
  24. import Bio
  25. import warnings
  26. warnings.warn("Bio.Clustalw is deprecated. Please use the Bio.AlignIO framework for alignment parsing, and the ClustalW command line wrapper in Bio.Align.Applications for calling the tool. These are both described in the current version of the Biopython Tutorial and Cookbook.", Bio.BiopythonDeprecationWarning)
  27. # standard library
  28. import os
  29. import sys
  30. import subprocess
  31. # biopython
  32. from Bio import Alphabet
  33. from Bio.Alphabet import IUPAC
  34. from Bio.Align.Generic import Alignment
  35. from Bio.Application import _escape_filename
  36. def parse_file(file_name, alphabet = IUPAC.unambiguous_dna, debug_level = 0):
  37. """Parse the given file into a clustal aligment object (OBSOLETE).
  38. Arguments:
  39. o file_name - The name of the file to parse.
  40. o alphabet - The type of alphabet to use for the alignment sequences.
  41. This should correspond to the type of information contained in the file.
  42. Defaults to be unambiguous_dna sequence.
  43. There is a deprecated optional argument debug_level which has no effect.
  44. This function is obsolete, and any new code should call Bio.AlignIO
  45. instead. For example using Bio.Clustalw, you might have:
  46. >>> from Bio import Clustalw
  47. >>> from Bio import Alphabet
  48. >>> filename = "Clustalw/protein.aln"
  49. >>> alpha = Alphabet.Gapped(Alphabet.generic_protein)
  50. >>> align = Clustalw.parse_file(filename, alphabet=alpha)
  51. >>> print align.get_alignment_length()
  52. 411
  53. >>> clustalw_string = str(align)
  54. This becomes:
  55. >>> from Bio import AlignIO
  56. >>> from Bio import Alphabet
  57. >>> filename = "Clustalw/protein.aln"
  58. >>> alpha = Alphabet.Gapped(Alphabet.generic_protein)
  59. >>> align = AlignIO.read(open(filename), "clustal", alphabet=alpha)
  60. >>> print align.get_alignment_length()
  61. 411
  62. >>> assert clustalw_string == align.format("clustal")
  63. """
  64. import warnings
  65. warnings.warn("This function is obsolete, and any new code should call Bio.AlignIO instead.", PendingDeprecationWarning)
  66. # Avoid code duplication by calling Bio.AlignIO to do this for us.
  67. handle = open(file_name, 'r')
  68. from Bio import AlignIO
  69. generic_alignment = AlignIO.read(handle, "clustal")
  70. handle.close()
  71. #Force this generic alignment into a ClustalAlignment... nasty hack
  72. if isinstance(alphabet, Alphabet.Gapped):
  73. alpha = alphabet
  74. else:
  75. alpha = Alphabet.Gapped(alphabet)
  76. clustal_alignment = ClustalAlignment(alpha)
  77. clustal_alignment._records = generic_alignment._records
  78. for record in clustal_alignment._records:
  79. record.seq.alphabet = alpha
  80. try:
  81. clustal_alignment._version = generic_alignment._version
  82. except AttributeError:
  83. #Missing the version, could be a 3rd party tool's output
  84. pass
  85. try :
  86. clustal_alignment._star_info = generic_alignment._star_info
  87. except AttributeError:
  88. #Missing the consensus, again, this is not always present
  89. pass
  90. return clustal_alignment
  91. def do_alignment(command_line, alphabet=None):
  92. """Perform an alignment with the given command line (OBSOLETE).
  93. Arguments:
  94. o command_line - A command line object that can give out
  95. the command line we will input into clustalw.
  96. o alphabet - the alphabet to use in the created alignment. If not
  97. specified IUPAC.unambiguous_dna and IUPAC.protein will be used for
  98. dna and protein alignment respectively.
  99. Returns:
  100. o A clustal alignment object corresponding to the created alignment.
  101. If the alignment type was not a clustal object, None is returned.
  102. This function (and the associated command line object) are now obsolete.
  103. Please use the Bio.Align.Applications.ClustalwCommandline wrapper with
  104. the Python subprocess module (and Bio.AlignIO for parsing) as described
  105. in the tutorial.
  106. """
  107. import warnings
  108. warnings.warn("This function (and the associated command line object) are now obsolete. Please use the Bio.Align.Applications.ClustalwCommandline wrapper with the Python subprocess module (and Bio.AlignIO for parsing) as described in the tutorial.", PendingDeprecationWarning)
  109. #We don't need to supply any piped input, but we setup the
  110. #standard input pipe anyway as a work around for a python
  111. #bug if this is called from a Windows GUI program. For
  112. #details, see http://bugs.python.org/issue1124861
  113. child_process = subprocess.Popen(str(command_line),
  114. stdin=subprocess.PIPE,
  115. stdout=subprocess.PIPE,
  116. stderr=subprocess.PIPE,
  117. universal_newlines=True,
  118. shell=(sys.platform!="win32")
  119. )
  120. #Use .communicate as can get deadlocks with .wait(), see Bug 2804
  121. child_process.communicate() #ignore the stdout and strerr data
  122. value = child_process.returncode
  123. child_process.stdin.close()
  124. child_process.stdout.close()
  125. child_process.stderr.close()
  126. # check the return value for errors, as on 1.81 the return value
  127. # from Clustalw is actually helpful for figuring out errors
  128. # TODO - Update this for new error codes using in clustalw 2
  129. # 1 => bad command line option
  130. if value == 1:
  131. raise ValueError("Bad command line option in the command: %s"
  132. % str(command_line))
  133. # 2 => can't open sequence file
  134. elif value == 2:
  135. raise IOError("Cannot open sequence file %s"
  136. % command_line.sequence_file)
  137. # 3 => wrong format in sequence file
  138. elif value == 3:
  139. raise IOError("Sequence file %s has an invalid format."
  140. % command_line.sequence_file)
  141. # 4 => sequence file only has one sequence
  142. elif value == 4:
  143. raise IOError("Sequence file %s has only one sequence present."
  144. % command_line.sequence_file)
  145. # if an output file was specified, we need to grab it
  146. if command_line.output_file:
  147. out_file = command_line.output_file
  148. else:
  149. out_file = os.path.splitext(command_line.sequence_file)[0] + '.aln'
  150. # if we can't deal with the format, just return None
  151. if command_line.output_type and command_line.output_type != 'CLUSTAL':
  152. return None
  153. # otherwise parse it into a ClustalAlignment object
  154. else:
  155. if not alphabet:
  156. alphabet = (IUPAC.unambiguous_dna, IUPAC.protein)[
  157. command_line.type == 'PROTEIN']
  158. # check if the outfile exists before parsing
  159. if not(os.path.exists(out_file)):
  160. raise IOError("Output .aln file %s not produced, commandline: %s"
  161. % (out_file, command_line))
  162. return parse_file(out_file, alphabet)
  163. class ClustalAlignment(Alignment):
  164. """Work with the clustal aligment format (OBSOLETE).
  165. This format is the default output from clustal -- these files normally
  166. have an extension of .aln.
  167. This obsolete alignment object is a subclass of the more general alignment
  168. object used in Bio.AlignIO. The old practical difference is here str(align)
  169. would give the alignment as a string in clustal format, whereas in general
  170. you must do align.format("clustal"), which supports other formats too.
  171. """
  172. # the default version to use if one isn't set
  173. import warnings
  174. warnings.warn("This class is obsolete.", PendingDeprecationWarning)
  175. DEFAULT_VERSION = '1.81'
  176. def __init__(self, alphabet = Alphabet.Gapped(IUPAC.ambiguous_dna)):
  177. Alignment.__init__(self, alphabet)
  178. # represent all of those stars in the aln output format
  179. self._star_info = ''
  180. self._version = ''
  181. def __str__(self):
  182. """Print out the alignment so it looks pretty.
  183. The output produced from this should also be formatted in valid
  184. clustal format.
  185. """
  186. return self.format("clustal")
  187. class MultipleAlignCL:
  188. """Represent a clustalw multiple alignment command line (OBSOLETE).
  189. This command line wrapper is considerd obsolete. Please use the replacement
  190. Bio.Align.Applications.ClustalwCommandline wrapper instead, which uses the
  191. standardised Bio.Application style interface. This is described in the
  192. tutorial, with examples using ClustalW.
  193. """
  194. import warnings
  195. warnings.warn("This command line wrapper is considerd obsolete. Please use the replacement Bio.Align.Applications.ClustalwCommandline wrapper instead, which uses the standardised Bio.Application style interface. This is described in the tutorial, with examples using ClustalW.", PendingDeprecationWarning)
  196. # set the valid options for different parameters
  197. OUTPUT_TYPES = ['GCG', 'GDE', 'PHYLIP', 'PIR', 'NEXUS', 'FASTA']
  198. OUTPUT_ORDER = ['INPUT', 'ALIGNED']
  199. OUTPUT_CASE = ['LOWER', 'UPPER']
  200. OUTPUT_SEQNOS = ['OFF', 'ON']
  201. RESIDUE_TYPES = ['PROTEIN', 'DNA']
  202. PROTEIN_MATRIX = ['BLOSUM', 'PAM', 'GONNET', 'ID']
  203. DNA_MATRIX = ['IUB', 'CLUSTALW']
  204. def __init__(self, sequence_file, command = 'clustalw'):
  205. """Initialize some general parameters that can be set as attributes.
  206. Arguments:
  207. o sequence_file - The file to read the sequences for alignment from.
  208. o command - The command used to run clustalw. This defaults to
  209. just 'clustalw' (ie. assumes you have it on your path somewhere).
  210. General attributes that can be set:
  211. o is_quick - if set as 1, will use a fast algorithm to create
  212. the alignment guide tree.
  213. o allow_negative - allow negative values in the alignment matrix.
  214. Multiple alignment attributes that can be set as attributes:
  215. o gap_open_pen - Gap opening penalty
  216. o gap_ext_pen - Gap extension penalty
  217. o is_no_end_pen - A flag as to whether or not there should be a gap
  218. separation penalty for the ends.
  219. o gap_sep_range - The gap separation penalty range.
  220. o is_no_pgap - A flag to turn off residue specific gaps
  221. o is_no_hgap - A flag to turn off hydrophilic gaps
  222. o h_gap_residues - A list of residues to count a hydrophilic
  223. o max_div - A percent identity to use for delay (? - I don't undertand
  224. this!)
  225. o trans_weight - The weight to use for transitions
  226. """
  227. self.sequence_file = sequence_file
  228. self.command = command
  229. self.is_quick = None
  230. self.allow_negative = None
  231. self.gap_open_pen = None
  232. self.gap_ext_pen = None
  233. self.is_no_end_pen = None
  234. self.gap_sep_range = None
  235. self.is_no_pgap = None
  236. self.is_no_hgap = None
  237. self.h_gap_residues = []
  238. self.max_div = None
  239. self.trans_weight = None
  240. # other attributes that should be set via various functions
  241. # 1. output parameters
  242. self.output_file = None
  243. self.output_type = None
  244. self.output_order = None
  245. self.change_case = None
  246. self.add_seqnos = None
  247. # 2. a guide tree to use
  248. self.guide_tree = None
  249. self.new_tree = None
  250. # 3. matrices
  251. self.protein_matrix = None
  252. self.dna_matrix = None
  253. # 4. type of residues
  254. self.type = None
  255. def __str__(self):
  256. """Write out the command line as a string."""
  257. #On Linux with clustalw 1.83, you can do:
  258. #clustalw input.faa
  259. #clustalw /full/path/input.faa
  260. #clustalw -INFILE=input.faa
  261. #clustalw -INFILE=/full/path/input.faa
  262. #
  263. #Note these fail (using DOS style slashes):
  264. #
  265. #clustalw /INFILE=input.faa
  266. #clustalw /INFILE=/full/path/input.faa
  267. #
  268. #On Windows XP with clustalw.exe 1.83, these work at
  269. #the command prompt:
  270. #
  271. #clustalw.exe input.faa
  272. #clustalw.exe /INFILE=input.faa
  273. #clustalw.exe /INFILE="input.faa"
  274. #clustalw.exe /INFILE="with space.faa"
  275. #clustalw.exe /INFILE=C:\full\path\input.faa
  276. #clustalw.exe /INFILE="C:\full path\with spaces.faa"
  277. #
  278. #Sadly these fail:
  279. #clustalw.exe "input.faa"
  280. #clustalw.exe "with space.faa"
  281. #clustalw.exe C:\full\path\input.faa
  282. #clustalw.exe "C:\full path\with spaces.faa"
  283. #
  284. #Testing today (using a different binary of clustalw.exe 1.83),
  285. #using -INFILE as follows seems to work. However I had once noted:
  286. #These also fail but a minus/dash does seem to
  287. #work with other options (!):
  288. #clustalw.exe -INFILE=input.faa
  289. #clustalw.exe -INFILE=C:\full\path\input.faa
  290. #
  291. #Also these fail:
  292. #clustalw.exe "/INFILE=input.faa"
  293. #clustalw.exe "/INFILE=C:\full\path\input.faa"
  294. #
  295. #Thanks to Emanuel Hey for flagging this on the mailing list.
  296. #
  297. #In addition, both self.command and self.sequence_file
  298. #may contain spaces, so should be quoted. But clustalw
  299. #is fussy.
  300. cline = _escape_filename(self.command)
  301. cline += ' -INFILE=%s' % _escape_filename(self.sequence_file)
  302. # general options
  303. if self.type:
  304. cline += " -TYPE=%s" % self.type
  305. if self.is_quick == 1:
  306. #Some versions of clustalw are case sensitive,
  307. #and require -quicktree rather than -QUICKTREE
  308. cline += " -quicktree"
  309. if self.allow_negative == 1:
  310. cline += " -NEGATIVE"
  311. # output options
  312. if self.output_file:
  313. cline += " -OUTFILE=%s" % _escape_filename(self.output_file)
  314. if self.output_type:
  315. cline += " -OUTPUT=%s" % self.output_type
  316. if self.output_order:
  317. cline += " -OUTORDER=%s" % self.output_order
  318. if self.change_case:
  319. cline += " -CASE=%s" % self.change_case
  320. if self.add_seqnos:
  321. cline += " -SEQNOS=%s" % self.add_seqnos
  322. if self.new_tree:
  323. # clustal does not work if -align is written -ALIGN
  324. cline += " -NEWTREE=%s -align" % _escape_filename(self.new_tree)
  325. # multiple alignment options
  326. if self.guide_tree:
  327. cline += " -USETREE=%s" % _escape_filename(self.guide_tree)
  328. if self.protein_matrix:
  329. cline += " -MATRIX=%s" % self.protein_matrix
  330. if self.dna_matrix:
  331. cline += " -DNAMATRIX=%s" % self.dna_matrix
  332. if self.gap_open_pen:
  333. cline += " -GAPOPEN=%s" % self.gap_open_pen
  334. if self.gap_ext_pen:
  335. cline += " -GAPEXT=%s" % self.gap_ext_pen
  336. if self.is_no_end_pen == 1:
  337. cline += " -ENDGAPS"
  338. if self.gap_sep_range:
  339. cline += " -GAPDIST=%s" % self.gap_sep_range
  340. if self.is_no_pgap == 1:
  341. cline += " -NOPGAP"
  342. if self.is_no_hgap == 1:
  343. cline += " -NOHGAP"
  344. if len(self.h_gap_residues) != 0:
  345. # stick the list of residues together as one big list o' residues
  346. residue_list = ''
  347. for residue in self.h_gap_residues:
  348. residue_list = residue_list + residue
  349. cline += " -HGAPRESIDUES=%s" % residue_list
  350. if self.max_div:
  351. cline += " -MAXDIV=%s" % self.max_div
  352. if self.trans_weight:
  353. cline += " -TRANSWEIGHT=%s" % self.trans_weight
  354. return cline
  355. def set_output(self, output_file, output_type = None, output_order = None,
  356. change_case = None, add_seqnos = None):
  357. """Set the output parameters for the command line.
  358. """
  359. self.output_file = output_file
  360. if output_type:
  361. output_type = output_type.upper()
  362. if output_type not in self.OUTPUT_TYPES:
  363. raise ValueError("Invalid output type %s. Valid choices are %s"
  364. % (output_type, self.OUTPUT_TYPES))
  365. else:
  366. self.output_type = output_type
  367. if output_order:
  368. output_order = output_order.upper()
  369. if output_order not in self.OUTPUT_ORDER:
  370. raise ValueError("Invalid output order %s. Valid choices are %s"
  371. % (output_order, self.OUTPUT_ORDER))
  372. else:
  373. self.output_order = output_order
  374. if change_case:
  375. change_case = change_case.upper()
  376. if output_type != "GDE":
  377. raise ValueError("Change case only valid for GDE output.")
  378. elif change_case not in self.CHANGE_CASE:
  379. raise ValueError("Invalid change case %s. Valid choices are %s"
  380. % (change_case, self.CHANGE_CASE))
  381. else:
  382. self.change_case = change_case
  383. if add_seqnos:
  384. add_seqnos = add_seqnos.upper()
  385. if output_type:
  386. raise ValueError("Add SeqNos only valid for CLUSTAL output.")
  387. elif add_seqnos not in self.OUTPUT_SEQNOS:
  388. raise ValueError("Invalid seqnos option %s. Valid choices: %s"
  389. % (add_seqnos, self.OUTPUT_SEQNOS))
  390. else:
  391. self.add_seqnos = add_seqnos
  392. def set_guide_tree(self, tree_file):
  393. """Provide a file to use as the guide tree for alignment.
  394. Raises:
  395. o IOError - If the tree_file doesn't exist."""
  396. if not(os.path.exists(tree_file)):
  397. raise IOError("Could not find the guide tree file %s." %
  398. tree_file)
  399. else:
  400. self.guide_tree = tree_file
  401. def set_new_guide_tree(self, tree_file):
  402. """Set the name of the guide tree file generated in the alignment.
  403. """
  404. self.new_tree = tree_file
  405. def set_protein_matrix(self, protein_matrix):
  406. """Set the type of protein matrix to use.
  407. Protein matrix can be either one of the defined types (blosum, pam,
  408. gonnet or id) or a file with your own defined matrix.
  409. """
  410. if protein_matrix.upper() in self.PROTEIN_MATRIX:
  411. self.protein_matrix = protein_matrix.upper()
  412. elif os.path.exists(protein_matrix):
  413. self.protein_matrix = protein_matrix
  414. else:
  415. raise ValueError("Invalid matrix %s. Options are %s or a file." %
  416. (protein_matrix.upper(), self.PROTEIN_MATRIX))
  417. def set_dna_matrix(self, dna_matrix):
  418. """Set the type of DNA matrix to use.
  419. The dna_matrix can either be one of the defined types (iub or clustalw)
  420. or a file with the matrix to use."""
  421. if dna_matrix.upper() in self.DNA_MATRIX:
  422. self.dna_matrix = dna_matrix.upper()
  423. elif os.path.exists(dna_matrix):
  424. self.dna_matrix = dna_matrix
  425. else:
  426. raise ValueError("Invalid matrix %s. Options are %s or a file." %
  427. (dna_matrix, self.DNA_MATRIX))
  428. def set_type(self, residue_type):
  429. """Set the type of residues within the file.
  430. Clustal tries to guess whether the info is protein or DNA based on
  431. the number of GATCs, but this can be wrong if you have a messed up
  432. protein or DNA you are working with, so this allows you to set it
  433. explicitly.
  434. """
  435. residue_type = residue_type.upper()
  436. if residue_type in self.RESIDUE_TYPES:
  437. self.type = residue_type
  438. else:
  439. raise ValueError("Invalid residue type %s. Valid choices are %s"
  440. % (residue_type, self.RESIDUE_TYPES))
  441. def _test():
  442. """Run the Bio.Clustalw module's doctests (PRIVATE).
  443. This will try and locate the unit tests directory, and run the doctests
  444. from there in order that the relative paths used in the examples work.
  445. """
  446. import doctest
  447. import os
  448. if os.path.isdir(os.path.join("..","..","Tests")):
  449. print "Runing doctests..."
  450. cur_dir = os.path.abspath(os.curdir)
  451. os.chdir(os.path.join("..","..","Tests"))
  452. doctest.testmod()
  453. os.chdir(cur_dir)
  454. del cur_dir
  455. print "Done"
  456. if __name__ == "__main__":
  457. _test()