Commits

Chad Burrus  committed 6affdef

added script to merge Fasta files

  • Participants
  • Parent commits 491908f

Comments (0)

Files changed (1)

File FastaMerger.py

+# Merge Fasta files of individual genes into complex files.  Assumes all files
+# have the same lines with the same ids, though order shouldn't matter.
+# Probably don't want to use this on huge FASTA files as it's quick and dirty.
+#
+# @todo Figure out if this really needs to be a class or not.
+
+class FastaMerger:
+
+	def merge_files(self, files, merged_name = "merged"):
+		out = open('%s.fasta' % merged_name, 'w')
+		hash = {}
+		ids = []
+		for file in files:
+			id = None
+			in_file = open(file)
+			for line in in_file:
+				line = line.strip()
+				if line.startswith(">"):
+					id = line
+					if id not in ids:
+						ids.append(id)
+				else:
+					try:
+						hash[id] = "%s%s" % (hash[id], line)
+					except KeyError:
+						hash[id] = line
+			in_file.close()
+		for id in ids:
+			print >>out, id
+			try:
+				print >>out, hash[id]
+			except KeyError:
+				print >>out, ""
+
+		out.close()
+