2017-04-10 12 views
0

문제는 ...큰 입력 스트림의 첫 번째 줄을 한 가지 방법으로 편집하고 다른 모든 줄을 다른 방식으로 편집하는 가장 효율적인 방법은 무엇입니까?

(= 2 * 10^7 N)이로 간다 :

colName1 colName2 colName3 ... colNameN 
1  x  x  ... x 
2  x  x  ... x 
1  y  x  ... x 
2  y  x  ... x 
...  ...  ...  ... ... 
1  xx  xx  ... xx 
2  xx  xx  ... xx 

이에 :

Sample colName1 colName2 colName3 ... colNameN 
A  1  x  x  ... x 
A  2  x  x  ... x 
B  1  y  x  ... x 
B  2  y  x  ... x 
...  ...  ...  ...  ... ... 
N  1  xx  xx  ... xx 
N  2  xx  xx  ... xx 

문제 : 제가 추가해야 " 샘플 "을 첫 번째"헤더 "라인에 표시하고 각 샘플 이름을 그 이후의 모든 다른 라인에 보냅니다. 샘플 이름은 객체에 저장됩니다.

교란 문제 :

  • 데이터는 입력 스트림으로부터오고; 현재 서브 프로세스를 통해 처리됩니다. 파이프
  • 파일에 2 천만 개의 행이있는 것이 일반적 일 수 있으므로 매번 firstLine 플래그를 확인하는 것이 비용이 많이 듭니까?

입력 스트림의 첫 번째 입력란으로 처리하는 방법이 있는지 궁금합니다.

또는 ...

는 그냥 우리가 헤더 행에 샘플 이름을 추가 의미, 모든 라인에 동일한 시도 쉬울 것이다. 그런 다음 파일의 첫 번째 단어를 샘플 이름에서 "Sample \ t"로 편집합니다.

이 접근 방법의 비용은 얼마입니까? 현재 아래와 같이 firstLine 플래그가 있습니다.

fileSTREAM = subprocess.Popen(callString, stdout=subprocess.PIPE, shell=True) 

# To indicate the first line of the steam, which happens to be the column-headers. 
firstLine = True 

# Foreach to add a word to the front of each line of input. 
for line in fileSTREAM.stdout: 

    # Decode the input from btye literals to strings. 
    currLine = line.decode("utf-8") 

    # First line is different, we want to add SAMPLE, instead of the actual sample name. 
    if firstLine == True: 
     outputTARGET.write("SAMPLE \t%s" % currLine) 
     firstLine = False 

    # All other lines we want to add the sample name, instead of the word SAMPLE. 
    else: 
     outputTARGET.write(str(wildcards.samples) + "\t%s" % currLine) 

파이썬 특정 문제가 아니 겠지만 파이썬 특정 솔루션을 찾고 있습니다. @Prune 밖으로

+0

, 프로세스는 그 다음 *과 ("샘플"포함) * 파일 스트림 라인에 대한 루프 (**로 이동합니다. stdout)? – Prune

+0

참고 ** if firstLine == True **는 중복됩니다. 단지 ** ** firstLine **이 그렇게한다면. – Prune

+0

첫 번째 줄만 읽으면 첫 번째 "\ n"문자까지 읽으려면 어떤 기능을 권장합니다. readline()? EDIT 2 : 테스트 할 부분을 다시 작성하십시오. 나는 네가 의미하는 것을 얻는다. EDIT 3 : 수업을 위해 마지막으로 실행해야하며 오늘 밤 주제를 종료합니다! – TBoyarski

답변

0

큰 소리, 감사합니다 :)

가장 좋은 방법은 입력 스트림의 첫 번째 라인을 읽을 수 있었다. 파이썬에는이 문제를 처리 할 수있는 훌륭한 내장 함수가 있습니다.

이 함께가는 종료 :

단순히 파일 스트림의 첫 번째 라인을 읽을 수없는 이유
# Call the function and capture its output to modify each line. 
fileSTREAM = subprocess.Popen(callString, stdout=subprocess.PIPE, shell=True) 

# Initially read and edit just the first, adding 'SAMPLE' to header line. 
outputTARGET.write("SAMPLE \t%s" % fileSTREAM.stdout.readline().decode("utf-8")) 

# Add the sampleName to each line after the header line. 
for line in fileSTREAM.stdout: 
    # Decode the input from btye literals to strings 
    outputTARGET.write(str(wildcards.samples) + "\t%s" % line.decode("utf-8")) 
+0

아직 StackOverflow를 배우고 있습니다. 후속 질문 ...이 주제를 닫는 가장 좋은 방법은 무엇입니까? Prune의 조언을 듣고 코드를 업데이트했습니다. 나는 만족스런 결과 코드를 게시했다. 나는 이것을 내일 답으로 선택합니까? – TBoyarski